2022-01-08 05:00:36 +02:00
|
|
|
package git_commands
|
2022-01-08 04:22:29 +02:00
|
|
|
|
|
|
|
import (
|
2022-11-13 05:10:18 +02:00
|
|
|
"os"
|
|
|
|
|
2022-01-08 04:22:29 +02:00
|
|
|
"github.com/go-errors/errors"
|
2022-01-08 05:44:07 +02:00
|
|
|
gogit "github.com/jesseduffield/go-git/v5"
|
2022-01-08 04:22:29 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/config"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type commonDeps struct {
|
|
|
|
runner *oscommands.FakeCmdObjRunner
|
|
|
|
userConfig *config.UserConfig
|
|
|
|
gitConfig *git_config.FakeGitConfig
|
|
|
|
getenv func(string) string
|
|
|
|
removeFile func(string) error
|
|
|
|
dotGitDir string
|
|
|
|
common *common.Common
|
|
|
|
cmd *oscommands.CmdObjBuilder
|
|
|
|
}
|
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
func buildGitCommon(deps commonDeps) *GitCommon {
|
|
|
|
gitCommon := &GitCommon{}
|
2022-01-08 04:22:29 +02:00
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon.Common = deps.common
|
|
|
|
if gitCommon.Common == nil {
|
|
|
|
gitCommon.Common = utils.NewDummyCommonWithUserConfig(deps.userConfig)
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
runner := deps.runner
|
|
|
|
if runner == nil {
|
|
|
|
runner = oscommands.NewFakeRunner(nil)
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
cmd := deps.cmd
|
|
|
|
// gotta check deps.cmd because it's not an interface type and an interface value of nil is not considered to be nil
|
|
|
|
if cmd == nil {
|
|
|
|
cmd = oscommands.NewDummyCmdObjBuilder(runner)
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon.cmd = cmd
|
2022-01-08 04:22:29 +02:00
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon.Common.UserConfig = deps.userConfig
|
|
|
|
if gitCommon.Common.UserConfig == nil {
|
|
|
|
gitCommon.Common.UserConfig = config.GetDefaultConfig()
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
gitConfig := deps.gitConfig
|
|
|
|
if gitConfig == nil {
|
|
|
|
gitConfig = git_config.NewFakeGitConfig(nil)
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon.repo = buildRepo()
|
|
|
|
gitCommon.config = NewConfigCommands(gitCommon.Common, gitConfig, gitCommon.repo)
|
2022-01-08 04:22:29 +02:00
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
getenv := deps.getenv
|
|
|
|
if getenv == nil {
|
|
|
|
getenv = func(string) string { return "" }
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
removeFile := deps.removeFile
|
|
|
|
if removeFile == nil {
|
|
|
|
removeFile = func(string) error { return errors.New("unexpected call to removeFile") }
|
|
|
|
}
|
2022-01-08 04:22:29 +02:00
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon.os = oscommands.NewDummyOSCommandWithDeps(oscommands.OSCommandDeps{
|
|
|
|
Common: gitCommon.Common,
|
|
|
|
GetenvFn: getenv,
|
|
|
|
Cmd: cmd,
|
|
|
|
RemoveFileFn: removeFile,
|
2022-11-13 05:10:18 +02:00
|
|
|
TempDir: os.TempDir(),
|
2022-01-18 12:26:21 +02:00
|
|
|
})
|
2022-01-08 04:22:29 +02:00
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon.dotGitDir = deps.dotGitDir
|
|
|
|
if gitCommon.dotGitDir == "" {
|
|
|
|
gitCommon.dotGitDir = ".git"
|
|
|
|
}
|
2022-01-08 05:44:07 +02:00
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
return gitCommon
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
func buildRepo() *gogit.Repository {
|
|
|
|
// TODO: think of a way to actually mock this out
|
|
|
|
var repo *gogit.Repository = nil
|
|
|
|
return repo
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
2022-11-11 04:19:29 +02:00
|
|
|
func buildFileLoader(gitCommon *GitCommon) *FileLoader {
|
|
|
|
return NewFileLoader(gitCommon.Common, gitCommon.cmd, gitCommon.config)
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildSubmoduleCommands(deps commonDeps) *SubmoduleCommands {
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon := buildGitCommon(deps)
|
2022-01-08 04:22:29 +02:00
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
return NewSubmoduleCommands(gitCommon)
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildCommitCommands(deps commonDeps) *CommitCommands {
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon := buildGitCommon(deps)
|
|
|
|
return NewCommitCommands(gitCommon)
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildWorkingTreeCommands(deps commonDeps) *WorkingTreeCommands {
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon := buildGitCommon(deps)
|
2022-01-08 04:22:29 +02:00
|
|
|
submoduleCommands := buildSubmoduleCommands(deps)
|
2022-01-18 12:26:21 +02:00
|
|
|
fileLoader := buildFileLoader(gitCommon)
|
2022-01-08 04:22:29 +02:00
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
return NewWorkingTreeCommands(gitCommon, submoduleCommands, fileLoader)
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildStashCommands(deps commonDeps) *StashCommands {
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon := buildGitCommon(deps)
|
|
|
|
fileLoader := buildFileLoader(gitCommon)
|
2022-01-08 04:22:29 +02:00
|
|
|
workingTreeCommands := buildWorkingTreeCommands(deps)
|
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
return NewStashCommands(gitCommon, fileLoader, workingTreeCommands)
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildRebaseCommands(deps commonDeps) *RebaseCommands {
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon := buildGitCommon(deps)
|
2022-01-08 04:22:29 +02:00
|
|
|
workingTreeCommands := buildWorkingTreeCommands(deps)
|
|
|
|
commitCommands := buildCommitCommands(deps)
|
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
return NewRebaseCommands(gitCommon, commitCommands, workingTreeCommands)
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildSyncCommands(deps commonDeps) *SyncCommands {
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon := buildGitCommon(deps)
|
2022-01-08 04:22:29 +02:00
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
return NewSyncCommands(gitCommon)
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildFileCommands(deps commonDeps) *FileCommands {
|
2022-01-18 12:26:21 +02:00
|
|
|
gitCommon := buildGitCommon(deps)
|
|
|
|
|
|
|
|
return NewFileCommands(gitCommon)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildBranchCommands(deps commonDeps) *BranchCommands {
|
|
|
|
gitCommon := buildGitCommon(deps)
|
2022-01-08 04:22:29 +02:00
|
|
|
|
2022-01-18 12:26:21 +02:00
|
|
|
return NewBranchCommands(gitCommon)
|
2022-01-08 04:22:29 +02:00
|
|
|
}
|