Skip to content

Commit

Permalink
fixup! Add command to open git difftool
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhaller committed Dec 22, 2023
1 parent 798f1e6 commit cbefecd
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 30 deletions.
38 changes: 38 additions & 0 deletions pkg/commands/git_commands/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
11 changes: 0 additions & 11 deletions pkg/commands/git_commands/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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())
}
13 changes: 0 additions & 13 deletions pkg/commands/git_commands/working_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/gui/controllers/basic_commits_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
12 changes: 10 additions & 2 deletions pkg/gui/controllers/commits_files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
12 changes: 10 additions & 2 deletions pkg/gui/controllers/files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
}),
)
}

Expand Down

0 comments on commit cbefecd

Please sign in to comment.