From cbefecd25fc5c3c62ca5d5e08e4c22e6c4ca78d8 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 22 Dec 2023 13:22:53 +0100 Subject: [PATCH] fixup! Add command to open git difftool --- pkg/commands/git_commands/diff.go | 38 +++++++++++++++++++ pkg/commands/git_commands/file.go | 11 ------ pkg/commands/git_commands/working_tree.go | 13 ------- .../controllers/basic_commits_controller.go | 12 +++++- .../controllers/commits_files_controller.go | 12 +++++- pkg/gui/controllers/files_controller.go | 12 +++++- 6 files changed, 68 insertions(+), 30 deletions(-) diff --git a/pkg/commands/git_commands/diff.go b/pkg/commands/git_commands/diff.go index 1e5f9824402a..b623864fbd1e 100644 --- a/pkg/commands/git_commands/diff.go +++ b/pkg/commands/git_commands/diff.go @@ -40,3 +40,41 @@ func (self *DiffCommands) GetAllDiff(staged bool) (string, error) { ToArgv(), ).RunWithOutput() } + +type DiffToolCmdOptions struct { + // The path to show a diff for. Pass "." for the entire repo. + Filepath string + + // The commit against which to show the diff. Leave empty to show a diff of + // the working copy. + FromCommit string + + // The commit to diff against FromCommit. Leave empty to diff the working + // copy against FromCommit. Leave both FromCommit and ToCommit empty to show + // the diff of the unstaged working copy changes against the index if Staged + // is false, or the staged changes against HEAD if Staged is true. + ToCommit string + + // Whether to reverse the left and right sides of the diff. + Reverse bool + + // Whether the given Filepath is a directory. We'll pass --dir-diff to + // git-difftool in that case. + IsDirectory bool + + // Whether to show the staged or the unstaged changes. Has no effect if both + // FromCommit and ToCommit are non-empty. + Staged bool +} + +func (self *DiffCommands) OpenDiffToolCmdObj(opts DiffToolCmdOptions) oscommands.ICmdObj { + return self.cmd.New(NewGitCmd("difftool"). + Arg("--no-prompt"). + ArgIf(opts.IsDirectory, "--dir-diff"). + ArgIf(opts.Staged, "--cached"). + ArgIf(len(opts.FromCommit) > 0, opts.FromCommit). + ArgIf(len(opts.ToCommit) > 0, opts.ToCommit). + ArgIf(opts.Reverse, "-R"). + Arg("--", opts.Filepath). + ToArgv()) +} diff --git a/pkg/commands/git_commands/file.go b/pkg/commands/git_commands/file.go index 568737319404..173e342f754d 100644 --- a/pkg/commands/git_commands/file.go +++ b/pkg/commands/git_commands/file.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/go-errors/errors" - "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -165,13 +164,3 @@ func (self *FileCommands) guessDefaultEditor() string { return editor } - -func (self *FileCommands) OpenDiffToolCmdObj(fromCommit string, toCommit string, reverse bool, filename string, isDirectory bool) oscommands.ICmdObj { - return self.cmd.New(NewGitCmd("difftool"). - Arg("--no-prompt"). - ArgIf(isDirectory, "--dir-diff"). - Arg(fromCommit, toCommit). - ArgIf(reverse, "-R"). - Arg("--", filename). - ToArgv()) -} diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go index 85fc46326aaf..9630e2870eec 100644 --- a/pkg/commands/git_commands/working_tree.go +++ b/pkg/commands/git_commands/working_tree.go @@ -27,19 +27,6 @@ func NewWorkingTreeCommands( } } -func (self *WorkingTreeCommands) OpenDiffToolCmdObj( - fromCommit string, reverse bool, filename string, isDirectory bool, staged bool, -) oscommands.ICmdObj { - return self.cmd.New(NewGitCmd("difftool"). - Arg("--no-prompt"). - ArgIf(isDirectory, "--dir-diff"). - ArgIf(staged, "--cached"). - ArgIf(len(fromCommit) > 0, fromCommit). - ArgIf(reverse, "-R"). - Arg("--", filename). - ToArgv()) -} - func (self *WorkingTreeCommands) OpenMergeToolCmdObj() oscommands.ICmdObj { return self.cmd.New(NewGitCmd("mergetool").ToArgv()) } diff --git a/pkg/gui/controllers/basic_commits_controller.go b/pkg/gui/controllers/basic_commits_controller.go index 13ecd43d0369..70c346580cb1 100644 --- a/pkg/gui/controllers/basic_commits_controller.go +++ b/pkg/gui/controllers/basic_commits_controller.go @@ -3,6 +3,7 @@ package controllers import ( "fmt" + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -260,7 +261,14 @@ func (self *BasicCommitsController) copyRange(*models.Commit) error { func (self *BasicCommitsController) openDiffTool(commit *models.Commit) error { to := commit.RefName() from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(commit.ParentRefName()) - _, err := self.c.RunSubprocess(self.c.Git().File.OpenDiffToolCmdObj( - from, to, reverse, ".", true)) + _, err := self.c.RunSubprocess(self.c.Git().Diff.OpenDiffToolCmdObj( + git_commands.DiffToolCmdOptions{ + Filepath: ".", + FromCommit: from, + ToCommit: to, + Reverse: reverse, + IsDirectory: true, + Staged: false, + })) return err } diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index 87c23984c944..5b3097363d91 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -2,6 +2,7 @@ package controllers import ( "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/gui/context" @@ -210,8 +211,15 @@ func (self *CommitFilesController) openDiffTool(node *filetree.CommitFileNode) e ref := self.context().GetRef() to := ref.RefName() from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName()) - _, err := self.c.RunSubprocess(self.c.Git().File.OpenDiffToolCmdObj( - from, to, reverse, node.GetPath(), !node.IsFile())) + _, err := self.c.RunSubprocess(self.c.Git().Diff.OpenDiffToolCmdObj( + git_commands.DiffToolCmdOptions{ + Filepath: node.GetPath(), + FromCommit: from, + ToCommit: to, + Reverse: reverse, + IsDirectory: !node.IsFile(), + Staged: false, + })) return err } diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index 473eb3a1e26f..0db509ca3b0f 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -4,6 +4,7 @@ import ( "strings" "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/filetree" @@ -697,8 +698,15 @@ func (self *FilesController) openDiffTool(node *filetree.FileNode) error { reverse = self.c.Modes().Diffing.Reverse } return self.c.RunSubprocessAndRefresh( - self.c.Git().WorkingTree.OpenDiffToolCmdObj( - fromCommit, reverse, node.Path, !node.IsFile(), !node.GetHasUnstagedChanges()), + self.c.Git().Diff.OpenDiffToolCmdObj( + git_commands.DiffToolCmdOptions{ + Filepath: node.Path, + FromCommit: fromCommit, + ToCommit: "", + Reverse: reverse, + IsDirectory: !node.IsFile(), + Staged: !node.GetHasUnstagedChanges(), + }), ) }