Skip to content

Commit eaaf123

Browse files
committed
Combine GetPathDiff and GetAllDiff into one command (GetDiff)
This makes it more reusable for other purposes.
1 parent 1c5fe8f commit eaaf123

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

pkg/commands/git_commands/diff.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ func NewDiffCommands(gitCommon *GitCommon) *DiffCommands {
1616
}
1717
}
1818

19+
// This is for generating diffs to be shown in the UI (e.g. rendering a range
20+
// diff to the main view). It uses a custom pager if one is configured.
1921
func (self *DiffCommands) DiffCmdObj(diffArgs []string) oscommands.ICmdObj {
2022
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand
2123
useExtDiff := extDiffCmd != ""
@@ -36,27 +38,21 @@ func (self *DiffCommands) DiffCmdObj(diffArgs []string) oscommands.ICmdObj {
3638
)
3739
}
3840

39-
func (self *DiffCommands) internalDiffCmdObj(diffArgs ...string) *GitCommandBuilder {
40-
return NewGitCmd("diff").
41-
Config("diff.noprefix=false").
42-
Arg("--no-ext-diff", "--no-color").
43-
Arg(diffArgs...).
44-
Dir(self.repoPaths.worktreePath)
45-
}
46-
47-
func (self *DiffCommands) GetPathDiff(path string, staged bool) (string, error) {
41+
// This is a basic generic diff command that can be used for any diff operation
42+
// (e.g. copying a diff to the clipboard). It will not use a custom pager, and
43+
// does not use user configs such as ignore whitespace.
44+
// If you want to diff specific refs (one or two), you need to add them yourself
45+
// in additionalArgs; it is recommended to also pass `--` after that. If you
46+
// want to restrict the diff to specific paths, pass them in additionalArgs
47+
// after the `--`.
48+
func (self *DiffCommands) GetDiff(staged bool, additionalArgs ...string) (string, error) {
4849
return self.cmd.New(
49-
self.internalDiffCmdObj().
50-
ArgIf(staged, "--staged").
51-
Arg(path).
52-
ToArgv(),
53-
).RunWithOutput()
54-
}
55-
56-
func (self *DiffCommands) GetAllDiff(staged bool) (string, error) {
57-
return self.cmd.New(
58-
self.internalDiffCmdObj().
50+
NewGitCmd("diff").
51+
Config("diff.noprefix=false").
52+
Arg("--no-ext-diff", "--no-color").
5953
ArgIf(staged, "--staged").
54+
Dir(self.repoPaths.worktreePath).
55+
Arg(additionalArgs...).
6056
ToArgv(),
6157
).RunWithOutput()
6258
}

pkg/gui/controllers/files_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ func (self *FilesController) openCopyMenu() error {
869869
OnPress: func() error {
870870
path := self.context().GetSelectedPath()
871871
hasStaged := self.hasPathStagedChanges(node)
872-
diff, err := self.c.Git().Diff.GetPathDiff(path, hasStaged)
872+
diff, err := self.c.Git().Diff.GetDiff(hasStaged, "--", path)
873873
if err != nil {
874874
return err
875875
}
@@ -894,7 +894,7 @@ func (self *FilesController) openCopyMenu() error {
894894
Tooltip: self.c.Tr.CopyFileDiffTooltip,
895895
OnPress: func() error {
896896
hasStaged := self.c.Helpers().WorkingTree.AnyStagedFiles()
897-
diff, err := self.c.Git().Diff.GetAllDiff(hasStaged)
897+
diff, err := self.c.Git().Diff.GetDiff(hasStaged, "--")
898898
if err != nil {
899899
return err
900900
}

0 commit comments

Comments
 (0)