From c9a0cc6b30dca6ff6c520268c10afff4e99a68e9 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 8 Jan 2022 14:00:36 +1100 Subject: [PATCH] refactor --- pkg/commands/dummies.go | 28 ----- pkg/commands/git.go | 61 +++++----- .../{branches.go => git_commands/branch.go} | 2 +- .../branch_test.go} | 2 +- .../{commits.go => git_commands/commit.go} | 2 +- .../commit_test.go} | 2 +- pkg/commands/{ => git_commands}/config.go | 2 +- pkg/commands/{ => git_commands}/custom.go | 2 +- pkg/commands/{ => git_commands}/deps_test.go | 2 +- .../{files.go => git_commands/file.go} | 20 +-- .../file_test.go} | 2 +- pkg/commands/{ => git_commands}/flow.go | 2 +- .../patch.go} | 2 +- .../{rebasing.go => git_commands/rebase.go} | 2 +- .../rebase_test.go} | 2 +- .../{remotes.go => git_commands/remote.go} | 2 +- .../stash.go} | 2 +- .../stash_test.go} | 2 +- pkg/commands/{ => git_commands}/status.go | 2 +- .../submodule.go} | 2 +- pkg/commands/{ => git_commands}/sync.go | 2 +- pkg/commands/{ => git_commands}/sync_test.go | 2 +- pkg/commands/{tags.go => git_commands/tag.go} | 2 +- .../{ => git_commands}/working_tree.go | 114 +++++++++--------- .../{ => git_commands}/working_tree_test.go | 2 +- pkg/gui/branches_panel.go | 8 +- pkg/gui/dummies.go | 3 +- pkg/gui/files_panel.go | 6 +- pkg/gui/global_handlers.go | 6 +- pkg/utils/dummies.go | 5 - 30 files changed, 131 insertions(+), 162 deletions(-) delete mode 100644 pkg/commands/dummies.go rename pkg/commands/{branches.go => git_commands/branch.go} (99%) rename pkg/commands/{branches_test.go => git_commands/branch_test.go} (99%) rename pkg/commands/{commits.go => git_commands/commit.go} (99%) rename pkg/commands/{commits_test.go => git_commands/commit_test.go} (99%) rename pkg/commands/{ => git_commands}/config.go (99%) rename pkg/commands/{ => git_commands}/custom.go (97%) rename pkg/commands/{ => git_commands}/deps_test.go (99%) rename pkg/commands/{files.go => git_commands/file.go} (73%) rename pkg/commands/{files_test.go => git_commands/file_test.go} (99%) rename pkg/commands/{ => git_commands}/flow.go (98%) rename pkg/commands/{patch_rebases.go => git_commands/patch.go} (99%) rename pkg/commands/{rebasing.go => git_commands/rebase.go} (99%) rename pkg/commands/{rebasing_test.go => git_commands/rebase_test.go} (99%) rename pkg/commands/{remotes.go => git_commands/remote.go} (98%) rename pkg/commands/{stash_entries.go => git_commands/stash.go} (99%) rename pkg/commands/{stash_entries_test.go => git_commands/stash_test.go} (99%) rename pkg/commands/{ => git_commands}/status.go (98%) rename pkg/commands/{submodules.go => git_commands/submodule.go} (99%) rename pkg/commands/{ => git_commands}/sync.go (99%) rename pkg/commands/{ => git_commands}/sync_test.go (99%) rename pkg/commands/{tags.go => git_commands/tag.go} (98%) rename pkg/commands/{ => git_commands}/working_tree.go (61%) rename pkg/commands/{ => git_commands}/working_tree_test.go (99%) diff --git a/pkg/commands/dummies.go b/pkg/commands/dummies.go deleted file mode 100644 index 38982b93a..000000000 --- a/pkg/commands/dummies.go +++ /dev/null @@ -1,28 +0,0 @@ -package commands - -import ( - "github.com/jesseduffield/lazygit/pkg/commands/oscommands" - "github.com/jesseduffield/lazygit/pkg/utils" -) - -// NewDummyGitCommand creates a new dummy GitCommand for testing -func NewDummyGitCommand() *GitCommand { - return NewDummyGitCommandWithOSCommand(oscommands.NewDummyOSCommand()) -} - -// NewDummyGitCommandWithOSCommand creates a new dummy GitCommand for testing -func NewDummyGitCommandWithOSCommand(osCommand *oscommands.OSCommand) *GitCommand { - return NewGitCommandAux( - utils.NewDummyCommon(), - osCommand, - utils.NewDummyGitConfig(), - ".git", - nil, - ) -} - -func NewDummyGitCommandWithRunner(runner *oscommands.FakeCmdObjRunner) *GitCommand { - osCommand := oscommands.NewDummyOSCommandWithRunner(runner) - - return NewDummyGitCommandWithOSCommand(osCommand) -} diff --git a/pkg/commands/git.go b/pkg/commands/git.go index e0a7a8635..70aa859e9 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -9,6 +9,7 @@ import ( "github.com/go-errors/errors" gogit "github.com/jesseduffield/go-git/v5" + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/git_config" "github.com/jesseduffield/lazygit/pkg/commands/loaders" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" @@ -20,21 +21,21 @@ import ( // GitCommand is our main git interface type GitCommand struct { - Branch *BranchCommands - Commit *CommitCommands - Config *ConfigCommands - Custom *CustomCommands - File *FileCommands - Flow *FlowCommands - Patch *PatchCommands - Rebase *RebaseCommands - Remote *RemoteCommands - Stash *StashCommands - Status *StatusCommands - Submodule *SubmoduleCommands - Sync *SyncCommands - Tag *TagCommands - WorkingTree *WorkingTreeCommands + Branch *git_commands.BranchCommands + Commit *git_commands.CommitCommands + Config *git_commands.ConfigCommands + Custom *git_commands.CustomCommands + File *git_commands.FileCommands + Flow *git_commands.FlowCommands + Patch *git_commands.PatchCommands + Rebase *git_commands.RebaseCommands + Remote *git_commands.RemoteCommands + Stash *git_commands.StashCommands + Status *git_commands.StatusCommands + Submodule *git_commands.SubmoduleCommands + Sync *git_commands.SyncCommands + Tag *git_commands.TagCommands + WorkingTree *git_commands.WorkingTreeCommands Loaders Loaders } @@ -91,20 +92,20 @@ func NewGitCommandAux( // This is admittedly messy, but allows us to test each command struct in isolation, // and allows for better namespacing when compared to having every method living // on the one struct. - configCommands := NewConfigCommands(cmn, gitConfig) - statusCommands := NewStatusCommands(cmn, osCommand, repo, dotGitDir) + configCommands := git_commands.NewConfigCommands(cmn, gitConfig) + statusCommands := git_commands.NewStatusCommands(cmn, osCommand, repo, dotGitDir) fileLoader := loaders.NewFileLoader(cmn, cmd, configCommands) - flowCommands := NewFlowCommands(cmn, cmd, configCommands) - remoteCommands := NewRemoteCommands(cmn, cmd) - branchCommands := NewBranchCommands(cmn, cmd) - syncCommands := NewSyncCommands(cmn, cmd) - tagCommands := NewTagCommands(cmn, cmd) - commitCommands := NewCommitCommands(cmn, cmd) - customCommands := NewCustomCommands(cmn, cmd) - fileCommands := NewFileCommands(cmn, cmd, configCommands, osCommand) - submoduleCommands := NewSubmoduleCommands(cmn, cmd, dotGitDir) - workingTreeCommands := NewWorkingTreeCommands(cmn, cmd, submoduleCommands, osCommand, fileLoader) - rebaseCommands := NewRebaseCommands( + flowCommands := git_commands.NewFlowCommands(cmn, cmd, configCommands) + remoteCommands := git_commands.NewRemoteCommands(cmn, cmd) + branchCommands := git_commands.NewBranchCommands(cmn, cmd) + syncCommands := git_commands.NewSyncCommands(cmn, cmd) + tagCommands := git_commands.NewTagCommands(cmn, cmd) + commitCommands := git_commands.NewCommitCommands(cmn, cmd) + customCommands := git_commands.NewCustomCommands(cmn, cmd) + fileCommands := git_commands.NewFileCommands(cmn, cmd, configCommands, osCommand) + submoduleCommands := git_commands.NewSubmoduleCommands(cmn, cmd, dotGitDir) + workingTreeCommands := git_commands.NewWorkingTreeCommands(cmn, cmd, submoduleCommands, osCommand, fileLoader) + rebaseCommands := git_commands.NewRebaseCommands( cmn, cmd, osCommand, @@ -113,10 +114,10 @@ func NewGitCommandAux( configCommands, dotGitDir, ) - stashCommands := NewStashCommands(cmn, cmd, osCommand, fileLoader, workingTreeCommands) + stashCommands := git_commands.NewStashCommands(cmn, cmd, osCommand, fileLoader, workingTreeCommands) // TODO: have patch manager take workingTreeCommands in its entirety patchManager := patch.NewPatchManager(cmn.Log, workingTreeCommands.ApplyPatch, workingTreeCommands.ShowFileDiff) - patchCommands := NewPatchCommands(cmn, cmd, rebaseCommands, commitCommands, configCommands, statusCommands, patchManager) + patchCommands := git_commands.NewPatchCommands(cmn, cmd, rebaseCommands, commitCommands, configCommands, statusCommands, patchManager) return &GitCommand{ Branch: branchCommands, diff --git a/pkg/commands/branches.go b/pkg/commands/git_commands/branch.go similarity index 99% rename from pkg/commands/branches.go rename to pkg/commands/git_commands/branch.go index c9cd444ae..50dfc036b 100644 --- a/pkg/commands/branches.go +++ b/pkg/commands/git_commands/branch.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "fmt" diff --git a/pkg/commands/branches_test.go b/pkg/commands/git_commands/branch_test.go similarity index 99% rename from pkg/commands/branches_test.go rename to pkg/commands/git_commands/branch_test.go index 102e5f661..447ccaaff 100644 --- a/pkg/commands/branches_test.go +++ b/pkg/commands/git_commands/branch_test.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "testing" diff --git a/pkg/commands/commits.go b/pkg/commands/git_commands/commit.go similarity index 99% rename from pkg/commands/commits.go rename to pkg/commands/git_commands/commit.go index 7ae0fe4bd..5b36d7950 100644 --- a/pkg/commands/commits.go +++ b/pkg/commands/git_commands/commit.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "fmt" diff --git a/pkg/commands/commits_test.go b/pkg/commands/git_commands/commit_test.go similarity index 99% rename from pkg/commands/commits_test.go rename to pkg/commands/git_commands/commit_test.go index b8a5cebc3..6408f440a 100644 --- a/pkg/commands/commits_test.go +++ b/pkg/commands/git_commands/commit_test.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "testing" diff --git a/pkg/commands/config.go b/pkg/commands/git_commands/config.go similarity index 99% rename from pkg/commands/config.go rename to pkg/commands/git_commands/config.go index 5d296ddb8..1818edc2d 100644 --- a/pkg/commands/config.go +++ b/pkg/commands/git_commands/config.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "os" diff --git a/pkg/commands/custom.go b/pkg/commands/git_commands/custom.go similarity index 97% rename from pkg/commands/custom.go rename to pkg/commands/git_commands/custom.go index a9bf64bf4..27a0bf93f 100644 --- a/pkg/commands/custom.go +++ b/pkg/commands/git_commands/custom.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "github.com/jesseduffield/lazygit/pkg/commands/oscommands" diff --git a/pkg/commands/deps_test.go b/pkg/commands/git_commands/deps_test.go similarity index 99% rename from pkg/commands/deps_test.go rename to pkg/commands/git_commands/deps_test.go index 59eeb8a0e..f0d7a2a7b 100644 --- a/pkg/commands/deps_test.go +++ b/pkg/commands/git_commands/deps_test.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "github.com/go-errors/errors" diff --git a/pkg/commands/files.go b/pkg/commands/git_commands/file.go similarity index 73% rename from pkg/commands/files.go rename to pkg/commands/git_commands/file.go index 5b4f7fb1f..070a50790 100644 --- a/pkg/commands/files.go +++ b/pkg/commands/git_commands/file.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "io/ioutil" @@ -45,23 +45,23 @@ func (self *FileCommands) Cat(fileName string) (string, error) { return string(buf), nil } -func (c *FileCommands) GetEditCmdStr(filename string, lineNumber int) (string, error) { - editor := c.UserConfig.OS.EditCommand +func (self *FileCommands) GetEditCmdStr(filename string, lineNumber int) (string, error) { + editor := self.UserConfig.OS.EditCommand if editor == "" { - editor = c.config.GetCoreEditor() + editor = self.config.GetCoreEditor() } if editor == "" { - editor = c.os.Getenv("GIT_EDITOR") + editor = self.os.Getenv("GIT_EDITOR") } if editor == "" { - editor = c.os.Getenv("VISUAL") + editor = self.os.Getenv("VISUAL") } if editor == "" { - editor = c.os.Getenv("EDITOR") + editor = self.os.Getenv("EDITOR") } if editor == "" { - if err := c.cmd.New("which vi").DontLog().Run(); err == nil { + if err := self.cmd.New("which vi").DontLog().Run(); err == nil { editor = "vi" } } @@ -71,10 +71,10 @@ func (c *FileCommands) GetEditCmdStr(filename string, lineNumber int) (string, e templateValues := map[string]string{ "editor": editor, - "filename": c.cmd.Quote(filename), + "filename": self.cmd.Quote(filename), "line": strconv.Itoa(lineNumber), } - editCmdTemplate := c.UserConfig.OS.EditCommandTemplate + editCmdTemplate := self.UserConfig.OS.EditCommandTemplate return utils.ResolvePlaceholderString(editCmdTemplate, templateValues), nil } diff --git a/pkg/commands/files_test.go b/pkg/commands/git_commands/file_test.go similarity index 99% rename from pkg/commands/files_test.go rename to pkg/commands/git_commands/file_test.go index ed8a41783..a26699b3e 100644 --- a/pkg/commands/files_test.go +++ b/pkg/commands/git_commands/file_test.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "testing" diff --git a/pkg/commands/flow.go b/pkg/commands/git_commands/flow.go similarity index 98% rename from pkg/commands/flow.go rename to pkg/commands/git_commands/flow.go index 0e0186f96..6a061b537 100644 --- a/pkg/commands/flow.go +++ b/pkg/commands/git_commands/flow.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "regexp" diff --git a/pkg/commands/patch_rebases.go b/pkg/commands/git_commands/patch.go similarity index 99% rename from pkg/commands/patch_rebases.go rename to pkg/commands/git_commands/patch.go index a006f3b71..2949c6ac2 100644 --- a/pkg/commands/patch_rebases.go +++ b/pkg/commands/git_commands/patch.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "fmt" diff --git a/pkg/commands/rebasing.go b/pkg/commands/git_commands/rebase.go similarity index 99% rename from pkg/commands/rebasing.go rename to pkg/commands/git_commands/rebase.go index 4bc92238d..08c8b4cb5 100644 --- a/pkg/commands/rebasing.go +++ b/pkg/commands/git_commands/rebase.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "fmt" diff --git a/pkg/commands/rebasing_test.go b/pkg/commands/git_commands/rebase_test.go similarity index 99% rename from pkg/commands/rebasing_test.go rename to pkg/commands/git_commands/rebase_test.go index 582da91b7..e4731ad83 100644 --- a/pkg/commands/rebasing_test.go +++ b/pkg/commands/git_commands/rebase_test.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "regexp" diff --git a/pkg/commands/remotes.go b/pkg/commands/git_commands/remote.go similarity index 98% rename from pkg/commands/remotes.go rename to pkg/commands/git_commands/remote.go index 46acd49d8..fe8ab57b0 100644 --- a/pkg/commands/remotes.go +++ b/pkg/commands/git_commands/remote.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "fmt" diff --git a/pkg/commands/stash_entries.go b/pkg/commands/git_commands/stash.go similarity index 99% rename from pkg/commands/stash_entries.go rename to pkg/commands/git_commands/stash.go index 1e02ddaa5..9141c1702 100644 --- a/pkg/commands/stash_entries.go +++ b/pkg/commands/git_commands/stash.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "fmt" diff --git a/pkg/commands/stash_entries_test.go b/pkg/commands/git_commands/stash_test.go similarity index 99% rename from pkg/commands/stash_entries_test.go rename to pkg/commands/git_commands/stash_test.go index 9f51ea3f3..f9f2428e0 100644 --- a/pkg/commands/stash_entries_test.go +++ b/pkg/commands/git_commands/stash_test.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "testing" diff --git a/pkg/commands/status.go b/pkg/commands/git_commands/status.go similarity index 98% rename from pkg/commands/status.go rename to pkg/commands/git_commands/status.go index c6f038ac8..fe89646d2 100644 --- a/pkg/commands/status.go +++ b/pkg/commands/git_commands/status.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "path/filepath" diff --git a/pkg/commands/submodules.go b/pkg/commands/git_commands/submodule.go similarity index 99% rename from pkg/commands/submodules.go rename to pkg/commands/git_commands/submodule.go index 9ccf29be2..3630f531f 100644 --- a/pkg/commands/submodules.go +++ b/pkg/commands/git_commands/submodule.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "bufio" diff --git a/pkg/commands/sync.go b/pkg/commands/git_commands/sync.go similarity index 99% rename from pkg/commands/sync.go rename to pkg/commands/git_commands/sync.go index c926aef64..56060f008 100644 --- a/pkg/commands/sync.go +++ b/pkg/commands/git_commands/sync.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "fmt" diff --git a/pkg/commands/sync_test.go b/pkg/commands/git_commands/sync_test.go similarity index 99% rename from pkg/commands/sync_test.go rename to pkg/commands/git_commands/sync_test.go index af9942aa0..a1eae5dbb 100644 --- a/pkg/commands/sync_test.go +++ b/pkg/commands/git_commands/sync_test.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "testing" diff --git a/pkg/commands/tags.go b/pkg/commands/git_commands/tag.go similarity index 98% rename from pkg/commands/tags.go rename to pkg/commands/git_commands/tag.go index b802aef8c..a2dd82850 100644 --- a/pkg/commands/tags.go +++ b/pkg/commands/git_commands/tag.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "fmt" diff --git a/pkg/commands/working_tree.go b/pkg/commands/git_commands/working_tree.go similarity index 61% rename from pkg/commands/working_tree.go rename to pkg/commands/git_commands/working_tree.go index a44caceb5..084643532 100644 --- a/pkg/commands/working_tree.go +++ b/pkg/commands/git_commands/working_tree.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "fmt" @@ -87,7 +87,7 @@ func (self *WorkingTreeCommands) UnStageFile(fileNames []string, reset bool) err return nil } -func (c *WorkingTreeCommands) BeforeAndAfterFileForRename(file *models.File) (*models.File, *models.File, error) { +func (self *WorkingTreeCommands) BeforeAndAfterFileForRename(file *models.File) (*models.File, *models.File, error) { if !file.IsRename() { return nil, nil, errors.New("Expected renamed file") } @@ -96,7 +96,7 @@ func (c *WorkingTreeCommands) BeforeAndAfterFileForRename(file *models.File) (*m // all files, passing the --no-renames flag and then recursively call the function // again for the before file and after file. - filesWithoutRenames := c.fileLoader.GetStatusFiles(loaders.GetStatusFileOptions{NoRenames: true}) + filesWithoutRenames := self.fileLoader.GetStatusFiles(loaders.GetStatusFileOptions{NoRenames: true}) var beforeFile *models.File var afterFile *models.File @@ -123,43 +123,43 @@ func (c *WorkingTreeCommands) BeforeAndAfterFileForRename(file *models.File) (*m } // DiscardAllFileChanges directly -func (c *WorkingTreeCommands) DiscardAllFileChanges(file *models.File) error { +func (self *WorkingTreeCommands) DiscardAllFileChanges(file *models.File) error { if file.IsRename() { - beforeFile, afterFile, err := c.BeforeAndAfterFileForRename(file) + beforeFile, afterFile, err := self.BeforeAndAfterFileForRename(file) if err != nil { return err } - if err := c.DiscardAllFileChanges(beforeFile); err != nil { + if err := self.DiscardAllFileChanges(beforeFile); err != nil { return err } - if err := c.DiscardAllFileChanges(afterFile); err != nil { + if err := self.DiscardAllFileChanges(afterFile); err != nil { return err } return nil } - quotedFileName := c.cmd.Quote(file.Name) + quotedFileName := self.cmd.Quote(file.Name) if file.ShortStatus == "AA" { - if err := c.cmd.New("git checkout --ours -- " + quotedFileName).Run(); err != nil { + if err := self.cmd.New("git checkout --ours -- " + quotedFileName).Run(); err != nil { return err } - if err := c.cmd.New("git add -- " + quotedFileName).Run(); err != nil { + if err := self.cmd.New("git add -- " + quotedFileName).Run(); err != nil { return err } return nil } if file.ShortStatus == "DU" { - return c.cmd.New("git rm -- " + quotedFileName).Run() + return self.cmd.New("git rm -- " + quotedFileName).Run() } // if the file isn't tracked, we assume you want to delete it if file.HasStagedChanges || file.HasMergeConflicts { - if err := c.cmd.New("git reset -- " + quotedFileName).Run(); err != nil { + if err := self.cmd.New("git reset -- " + quotedFileName).Run(); err != nil { return err } } @@ -169,30 +169,30 @@ func (c *WorkingTreeCommands) DiscardAllFileChanges(file *models.File) error { } if file.Added { - return c.os.RemoveFile(file.Name) + return self.os.RemoveFile(file.Name) } - return c.DiscardUnstagedFileChanges(file) + return self.DiscardUnstagedFileChanges(file) } -func (c *WorkingTreeCommands) DiscardAllDirChanges(node *filetree.FileNode) error { +func (self *WorkingTreeCommands) DiscardAllDirChanges(node *filetree.FileNode) error { // this could be more efficient but we would need to handle all the edge cases - return node.ForEachFile(c.DiscardAllFileChanges) + return node.ForEachFile(self.DiscardAllFileChanges) } -func (c *WorkingTreeCommands) DiscardUnstagedDirChanges(node *filetree.FileNode) error { - if err := c.RemoveUntrackedDirFiles(node); err != nil { +func (self *WorkingTreeCommands) DiscardUnstagedDirChanges(node *filetree.FileNode) error { + if err := self.RemoveUntrackedDirFiles(node); err != nil { return err } - quotedPath := c.cmd.Quote(node.GetPath()) - if err := c.cmd.New("git checkout -- " + quotedPath).Run(); err != nil { + quotedPath := self.cmd.Quote(node.GetPath()) + if err := self.cmd.New("git checkout -- " + quotedPath).Run(); err != nil { return err } return nil } -func (c *WorkingTreeCommands) RemoveUntrackedDirFiles(node *filetree.FileNode) error { +func (self *WorkingTreeCommands) RemoveUntrackedDirFiles(node *filetree.FileNode) error { untrackedFilePaths := node.GetPathsMatching( func(n *filetree.FileNode) bool { return n.File != nil && !n.File.GetIsTracked() }, ) @@ -208,30 +208,30 @@ func (c *WorkingTreeCommands) RemoveUntrackedDirFiles(node *filetree.FileNode) e } // DiscardUnstagedFileChanges directly -func (c *WorkingTreeCommands) DiscardUnstagedFileChanges(file *models.File) error { - quotedFileName := c.cmd.Quote(file.Name) - return c.cmd.New("git checkout -- " + quotedFileName).Run() +func (self *WorkingTreeCommands) DiscardUnstagedFileChanges(file *models.File) error { + quotedFileName := self.cmd.Quote(file.Name) + return self.cmd.New("git checkout -- " + quotedFileName).Run() } // Ignore adds a file to the gitignore for the repo -func (c *WorkingTreeCommands) Ignore(filename string) error { - return c.os.AppendLineToFile(".gitignore", filename) +func (self *WorkingTreeCommands) Ignore(filename string) error { + return self.os.AppendLineToFile(".gitignore", filename) } // WorktreeFileDiff returns the diff of a file -func (c *WorkingTreeCommands) WorktreeFileDiff(file *models.File, plain bool, cached bool, ignoreWhitespace bool) string { +func (self *WorkingTreeCommands) WorktreeFileDiff(file *models.File, plain bool, cached bool, ignoreWhitespace bool) string { // for now we assume an error means the file was deleted - s, _ := c.WorktreeFileDiffCmdObj(file, plain, cached, ignoreWhitespace).RunWithOutput() + s, _ := self.WorktreeFileDiffCmdObj(file, plain, cached, ignoreWhitespace).RunWithOutput() return s } -func (c *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain bool, cached bool, ignoreWhitespace bool) oscommands.ICmdObj { +func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain bool, cached bool, ignoreWhitespace bool) oscommands.ICmdObj { cachedArg := "" trackedArg := "--" - colorArg := c.UserConfig.Git.Paging.ColorArg - quotedPath := c.cmd.Quote(node.GetPath()) + colorArg := self.UserConfig.Git.Paging.ColorArg + quotedPath := self.cmd.Quote(node.GetPath()) ignoreWhitespaceArg := "" - contextSize := c.UserConfig.Git.DiffContextSize + contextSize := self.UserConfig.Git.DiffContextSize if cached { cachedArg = "--cached" } @@ -247,13 +247,13 @@ func (c *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain bo cmdStr := fmt.Sprintf("git diff --submodule --no-ext-diff --unified=%d --color=%s %s %s %s %s", contextSize, colorArg, ignoreWhitespaceArg, cachedArg, trackedArg, quotedPath) - return c.cmd.New(cmdStr).DontLog() + return self.cmd.New(cmdStr).DontLog() } -func (c *WorkingTreeCommands) ApplyPatch(patch string, flags ...string) error { +func (self *WorkingTreeCommands) ApplyPatch(patch string, flags ...string) error { filepath := filepath.Join(oscommands.GetTempDir(), utils.GetCurrentRepoName(), time.Now().Format("Jan _2 15.04.05.000000000")+".patch") - c.Log.Infof("saving temporary patch to %s", filepath) - if err := c.os.CreateFileWithContent(filepath, patch); err != nil { + self.Log.Infof("saving temporary patch to %s", filepath) + if err := self.os.CreateFileWithContent(filepath, patch); err != nil { return err } @@ -262,18 +262,18 @@ func (c *WorkingTreeCommands) ApplyPatch(patch string, flags ...string) error { flagStr += " --" + flag } - return c.cmd.New(fmt.Sprintf("git apply%s %s", flagStr, c.cmd.Quote(filepath))).Run() + return self.cmd.New(fmt.Sprintf("git apply%s %s", flagStr, self.cmd.Quote(filepath))).Run() } // ShowFileDiff get the diff of specified from and to. Typically this will be used for a single commit so it'll be 123abc^..123abc // but when we're in diff mode it could be any 'from' to any 'to'. The reverse flag is also here thanks to diff mode. -func (c *WorkingTreeCommands) ShowFileDiff(from string, to string, reverse bool, fileName string, plain bool) (string, error) { - return c.ShowFileDiffCmdObj(from, to, reverse, fileName, plain).RunWithOutput() +func (self *WorkingTreeCommands) ShowFileDiff(from string, to string, reverse bool, fileName string, plain bool) (string, error) { + return self.ShowFileDiffCmdObj(from, to, reverse, fileName, plain).RunWithOutput() } -func (c *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reverse bool, fileName string, plain bool) oscommands.ICmdObj { - colorArg := c.UserConfig.Git.Paging.ColorArg - contextSize := c.UserConfig.Git.DiffContextSize +func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reverse bool, fileName string, plain bool) oscommands.ICmdObj { + colorArg := self.UserConfig.Git.Paging.ColorArg + contextSize := self.UserConfig.Git.DiffContextSize if plain { colorArg = "never" } @@ -283,53 +283,53 @@ func (c *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reverse reverseFlag = " -R " } - return c.cmd. + return self.cmd. New( fmt.Sprintf( "git diff --submodule --no-ext-diff --unified=%d --no-renames --color=%s %s %s %s -- %s", - contextSize, colorArg, from, to, reverseFlag, c.cmd.Quote(fileName)), + contextSize, colorArg, from, to, reverseFlag, self.cmd.Quote(fileName)), ). DontLog() } // CheckoutFile checks out the file for the given commit -func (c *WorkingTreeCommands) CheckoutFile(commitSha, fileName string) error { - return c.cmd.New(fmt.Sprintf("git checkout %s -- %s", commitSha, c.cmd.Quote(fileName))).Run() +func (self *WorkingTreeCommands) CheckoutFile(commitSha, fileName string) error { + return self.cmd.New(fmt.Sprintf("git checkout %s -- %s", commitSha, self.cmd.Quote(fileName))).Run() } // DiscardAnyUnstagedFileChanges discards any unstages file changes via `git checkout -- .` -func (c *WorkingTreeCommands) DiscardAnyUnstagedFileChanges() error { - return c.cmd.New("git checkout -- .").Run() +func (self *WorkingTreeCommands) DiscardAnyUnstagedFileChanges() error { + return self.cmd.New("git checkout -- .").Run() } // RemoveTrackedFiles will delete the given file(s) even if they are currently tracked -func (c *WorkingTreeCommands) RemoveTrackedFiles(name string) error { - return c.cmd.New("git rm -r --cached -- " + c.cmd.Quote(name)).Run() +func (self *WorkingTreeCommands) RemoveTrackedFiles(name string) error { + return self.cmd.New("git rm -r --cached -- " + self.cmd.Quote(name)).Run() } // RemoveUntrackedFiles runs `git clean -fd` -func (c *WorkingTreeCommands) RemoveUntrackedFiles() error { - return c.cmd.New("git clean -fd").Run() +func (self *WorkingTreeCommands) RemoveUntrackedFiles() error { + return self.cmd.New("git clean -fd").Run() } // ResetAndClean removes all unstaged changes and removes all untracked files -func (c *WorkingTreeCommands) ResetAndClean() error { - submoduleConfigs, err := c.submodule.GetConfigs() +func (self *WorkingTreeCommands) ResetAndClean() error { + submoduleConfigs, err := self.submodule.GetConfigs() if err != nil { return err } if len(submoduleConfigs) > 0 { - if err := c.submodule.ResetSubmodules(submoduleConfigs); err != nil { + if err := self.submodule.ResetSubmodules(submoduleConfigs); err != nil { return err } } - if err := c.ResetHard("HEAD"); err != nil { + if err := self.ResetHard("HEAD"); err != nil { return err } - return c.RemoveUntrackedFiles() + return self.RemoveUntrackedFiles() } // ResetHardHead runs `git reset --hard` diff --git a/pkg/commands/working_tree_test.go b/pkg/commands/git_commands/working_tree_test.go similarity index 99% rename from pkg/commands/working_tree_test.go rename to pkg/commands/git_commands/working_tree_test.go index 8e773314d..a6d2345e9 100644 --- a/pkg/commands/working_tree_test.go +++ b/pkg/commands/git_commands/working_tree_test.go @@ -1,4 +1,4 @@ -package commands +package git_commands import ( "fmt" diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 75186baca..5e0958727 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -5,7 +5,7 @@ import ( "fmt" "strings" - "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -146,7 +146,7 @@ func (gui *Gui) handleForceCheckout() error { prompt: message, handleConfirm: func() error { gui.logAction(gui.Tr.Actions.ForceCheckoutBranch) - if err := gui.GitCommand.Branch.Checkout(branch.Name, commands.CheckoutOptions{Force: true}); err != nil { + if err := gui.GitCommand.Branch.Checkout(branch.Name, git_commands.CheckoutOptions{Force: true}); err != nil { _ = gui.surfaceError(err) } return gui.refreshSidePanels(refreshOptions{mode: ASYNC}) @@ -166,7 +166,7 @@ func (gui *Gui) handleCheckoutRef(ref string, options handleCheckoutRefOptions) waitingStatus = gui.Tr.CheckingOutStatus } - cmdOptions := commands.CheckoutOptions{Force: false, EnvVars: options.EnvVars} + cmdOptions := git_commands.CheckoutOptions{Force: false, EnvVars: options.EnvVars} onSuccess := func() { gui.State.Panels.Branches.SelectedLineIdx = 0 @@ -335,7 +335,7 @@ func (gui *Gui) mergeBranchIntoCheckedOutBranch(branchName string) error { prompt: prompt, handleConfirm: func() error { gui.logAction(gui.Tr.Actions.Merge) - err := gui.GitCommand.Branch.Merge(branchName, commands.MergeOpts{}) + err := gui.GitCommand.Branch.Merge(branchName, git_commands.MergeOpts{}) return gui.handleGenericMergeCommandResult(err) }, }) diff --git a/pkg/gui/dummies.go b/pkg/gui/dummies.go index 32f032e34..587460ccd 100644 --- a/pkg/gui/dummies.go +++ b/pkg/gui/dummies.go @@ -1,6 +1,7 @@ package gui import ( + "github.com/jesseduffield/lazygit/pkg/commands/git_config" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/updates" @@ -16,6 +17,6 @@ func NewDummyUpdater() *updates.Updater { func NewDummyGui() *Gui { newAppConfig := config.NewDummyAppConfig() - dummyGui, _ := NewGui(utils.NewDummyCommon(), newAppConfig, utils.NewDummyGitConfig(), NewDummyUpdater(), "", false) + dummyGui, _ := NewGui(utils.NewDummyCommon(), newAppConfig, git_config.NewFakeGitConfig(nil), NewDummyUpdater(), "", false) return dummyGui } diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index 045970f05..fa01b6bdf 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -6,7 +6,7 @@ import ( "strings" "github.com/jesseduffield/gocui" - "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/loaders" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/config" @@ -717,7 +717,7 @@ func (gui *Gui) pullWithLock(opts PullFilesOptions) error { gui.logAction(opts.action) err := gui.GitCommand.Sync.Pull( - commands.PullOptions{ + git_commands.PullOptions{ RemoteName: opts.RemoteName, BranchName: opts.BranchName, FastForwardOnly: opts.FastForwardOnly, @@ -742,7 +742,7 @@ func (gui *Gui) push(opts pushOpts) error { } go utils.Safe(func() { gui.logAction(gui.Tr.Actions.Push) - err := gui.GitCommand.Sync.Push(commands.PushOpts{ + err := gui.GitCommand.Sync.Push(git_commands.PushOpts{ Force: opts.force, UpstreamRemote: opts.upstreamRemote, UpstreamBranch: opts.upstreamBranch, diff --git a/pkg/gui/global_handlers.go b/pkg/gui/global_handlers.go index b211baf27..fcd2d30e0 100644 --- a/pkg/gui/global_handlers.go +++ b/pkg/gui/global_handlers.go @@ -6,7 +6,7 @@ import ( "strings" "github.com/jesseduffield/gocui" - "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -215,7 +215,7 @@ func (gui *Gui) fetch() (err error) { defer gui.Mutexes.FetchMutex.Unlock() gui.logAction("Fetch") - err = gui.GitCommand.Sync.Fetch(commands.FetchOptions{}) + err = gui.GitCommand.Sync.Fetch(git_commands.FetchOptions{}) if err != nil && strings.Contains(err.Error(), "exit status 128") { _ = gui.createErrorPanel(gui.Tr.PassUnameWrong) @@ -230,7 +230,7 @@ func (gui *Gui) backgroundFetch() (err error) { gui.Mutexes.FetchMutex.Lock() defer gui.Mutexes.FetchMutex.Unlock() - err = gui.GitCommand.Sync.Fetch(commands.FetchOptions{Background: true}) + err = gui.GitCommand.Sync.Fetch(git_commands.FetchOptions{Background: true}) _ = gui.refreshSidePanels(refreshOptions{scope: []RefreshableView{BRANCHES, COMMITS, REMOTES, TAGS}, mode: ASYNC}) diff --git a/pkg/utils/dummies.go b/pkg/utils/dummies.go index eb6286257..c5fba3556 100644 --- a/pkg/utils/dummies.go +++ b/pkg/utils/dummies.go @@ -3,7 +3,6 @@ package utils import ( "io/ioutil" - "github.com/jesseduffield/lazygit/pkg/commands/git_config" "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/i18n" @@ -34,7 +33,3 @@ func NewDummyCommonWithUserConfig(userConfig *config.UserConfig) *common.Common UserConfig: userConfig, } } - -func NewDummyGitConfig() git_config.IGitConfig { - return git_config.NewFakeGitConfig(map[string]string{}) -}