Skip to content

Commit 40e9894

Browse files
committed
Add a user config for using git's external diff command for paging
This is similar to using lazygit's Git.Paging.ExternalDiffCommand config, except that the command is configured in git. This can be done either with git's `diff.external` config, or through .gitattributes, so it gives a bit more flexibility.
1 parent 86934ce commit 40e9894

File tree

10 files changed

+52
-5
lines changed

10 files changed

+52
-5
lines changed

docs/Config.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ git:
306306
# e.g. 'difft --color=always'
307307
externalDiffCommand: ""
308308

309+
# If true, Lazygit will use git's `diff.external` config for paging. The advantage over `externalDiffCommand` is that this can be configured per file type in .gitattributes; see https://git-scm.com/docs/gitattributes#_defining_an_external_diff_driver.
310+
useExternalDiffGitConfig: false
311+
309312
# Config relating to committing
310313
commit:
311314
# If true, pass '--signoff' flag when committing

docs/Custom_Pagers.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,13 @@ git:
7474
paging:
7575
externalDiffCommand: difft --color=always --display=inline --syntax-highlight=off
7676
```
77+
78+
Instead of setting this command in lazygit's `externalDiffCommand` config, you can also tell lazygit to use the external diff command that is configured in git itself (`diff.external`), by using
79+
80+
```yaml
81+
git:
82+
paging:
83+
useExternalDiffGitConfig: true
84+
```
85+
86+
This can be useful if you also want to use it for diffs on the command line, and it also has the advantage that you can configure it per file type in `.gitattributes`; see https://git-scm.com/docs/gitattributes#_defining_an_external_diff_driver.

pkg/commands/git_commands/commit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,11 @@ func (self *CommitCommands) ShowCmdObj(hash string, filterPaths []string) *oscom
257257
contextSize := self.UserConfig().Git.DiffContextSize
258258

259259
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand
260+
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig
260261
cmdArgs := NewGitCmd("show").
261262
Config("diff.noprefix=false").
262263
ConfigIf(extDiffCmd != "", "diff.external="+extDiffCmd).
263-
ArgIfElse(extDiffCmd != "", "--ext-diff", "--no-ext-diff").
264+
ArgIfElse(extDiffCmd != "" || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff").
264265
Arg("--submodule").
265266
Arg("--color="+self.UserConfig().Git.Paging.ColorArg).
266267
Arg(fmt.Sprintf("--unified=%d", contextSize)).

pkg/commands/git_commands/commit_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ func TestCommitShowCmdObj(t *testing.T) {
256256
similarityThreshold int
257257
ignoreWhitespace bool
258258
extDiffCmd string
259+
useExtDiffGitConfig bool
259260
expected []string
260261
}
261262

@@ -314,6 +315,15 @@ func TestCommitShowCmdObj(t *testing.T) {
314315
extDiffCmd: "difft --color=always",
315316
expected: []string{"-C", "/path/to/worktree", "-c", "diff.external=difft --color=always", "-c", "diff.noprefix=false", "show", "--ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890", "--find-renames=50%", "--"},
316317
},
318+
{
319+
testName: "Show diff using git's external diff config",
320+
filterPaths: []string{},
321+
contextSize: 3,
322+
similarityThreshold: 50,
323+
ignoreWhitespace: false,
324+
useExtDiffGitConfig: true,
325+
expected: []string{"-C", "/path/to/worktree", "-c", "diff.noprefix=false", "show", "--ext-diff", "--submodule", "--color=always", "--unified=3", "--stat", "--decorate", "-p", "1234567890", "--find-renames=50%", "--"},
326+
},
317327
}
318328

319329
for _, s := range scenarios {
@@ -323,6 +333,7 @@ func TestCommitShowCmdObj(t *testing.T) {
323333
userConfig.Git.IgnoreWhitespaceInDiffView = s.ignoreWhitespace
324334
userConfig.Git.DiffContextSize = s.contextSize
325335
userConfig.Git.RenameSimilarityThreshold = s.similarityThreshold
336+
userConfig.Git.Paging.UseExternalDiffGitConfig = s.useExtDiffGitConfig
326337

327338
runner := oscommands.NewFakeRunner(t).ExpectGitArgs(s.expected, "", nil)
328339
repoPaths := RepoPaths{

pkg/commands/git_commands/diff.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ func NewDiffCommands(gitCommon *GitCommon) *DiffCommands {
2121
func (self *DiffCommands) DiffCmdObj(diffArgs []string) *oscommands.CmdObj {
2222
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand
2323
useExtDiff := extDiffCmd != ""
24+
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig
2425
ignoreWhitespace := self.UserConfig().Git.IgnoreWhitespaceInDiffView
2526

2627
return self.cmd.New(
2728
NewGitCmd("diff").
2829
Config("diff.noprefix=false").
2930
ConfigIf(useExtDiff, "diff.external="+extDiffCmd).
30-
ArgIfElse(useExtDiff, "--ext-diff", "--no-ext-diff").
31+
ArgIfElse(useExtDiff || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff").
3132
Arg("--submodule").
3233
Arg(fmt.Sprintf("--color=%s", self.UserConfig().Git.Paging.ColorArg)).
3334
ArgIf(ignoreWhitespace, "--ignore-all-space").

pkg/commands/git_commands/stash.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,15 @@ func (self *StashCommands) Hash(index int) (string, error) {
8282

8383
func (self *StashCommands) ShowStashEntryCmdObj(index int) *oscommands.CmdObj {
8484
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand
85+
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig
8586

8687
// "-u" is the same as "--include-untracked", but the latter fails in older git versions for some reason
8788
cmdArgs := NewGitCmd("stash").Arg("show").
8889
Arg("-p").
8990
Arg("--stat").
9091
Arg("-u").
9192
ConfigIf(extDiffCmd != "", "diff.external="+extDiffCmd).
92-
ArgIfElse(extDiffCmd != "", "--ext-diff", "--no-ext-diff").
93+
ArgIfElse(extDiffCmd != "" || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff").
9394
Arg(fmt.Sprintf("--color=%s", self.UserConfig().Git.Paging.ColorArg)).
9495
Arg(fmt.Sprintf("--unified=%d", self.UserConfig().Git.DiffContextSize)).
9596
ArgIf(self.UserConfig().Git.IgnoreWhitespaceInDiffView, "--ignore-all-space").

pkg/commands/git_commands/stash_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func TestStashStashEntryCmdObj(t *testing.T) {
104104
similarityThreshold int
105105
ignoreWhitespace bool
106106
extDiffCmd string
107+
useExtDiffGitConfig bool
107108
expected []string
108109
}
109110

@@ -141,6 +142,15 @@ func TestStashStashEntryCmdObj(t *testing.T) {
141142
extDiffCmd: "difft --color=always",
142143
expected: []string{"git", "-C", "/path/to/worktree", "-c", "diff.external=difft --color=always", "stash", "show", "-p", "--stat", "-u", "--ext-diff", "--color=always", "--unified=3", "--find-renames=50%", "refs/stash@{5}"},
143144
},
145+
{
146+
testName: "Show diff using git's external diff config",
147+
index: 5,
148+
contextSize: 3,
149+
similarityThreshold: 50,
150+
ignoreWhitespace: false,
151+
useExtDiffGitConfig: true,
152+
expected: []string{"git", "-C", "/path/to/worktree", "stash", "show", "-p", "--stat", "-u", "--ext-diff", "--color=always", "--unified=3", "--find-renames=50%", "refs/stash@{5}"},
153+
},
144154
{
145155
testName: "Default case",
146156
index: 5,
@@ -158,6 +168,7 @@ func TestStashStashEntryCmdObj(t *testing.T) {
158168
userConfig.Git.DiffContextSize = s.contextSize
159169
userConfig.Git.RenameSimilarityThreshold = s.similarityThreshold
160170
userConfig.Git.Paging.ExternalDiffCommand = s.extDiffCmd
171+
userConfig.Git.Paging.UseExternalDiffGitConfig = s.useExtDiffGitConfig
161172
repoPaths := RepoPaths{
162173
worktreePath: "/path/to/worktree",
163174
}

pkg/commands/git_commands/working_tree.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,11 @@ func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain
267267
noIndex := !node.GetIsTracked() && !node.GetHasStagedChanges() && !cached && node.GetIsFile()
268268
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand
269269
useExtDiff := extDiffCmd != "" && !plain
270+
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig
270271

271272
cmdArgs := NewGitCmd("diff").
272273
ConfigIf(useExtDiff, "diff.external="+extDiffCmd).
273-
ArgIfElse(useExtDiff, "--ext-diff", "--no-ext-diff").
274+
ArgIfElse(useExtDiff || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff").
274275
Arg("--submodule").
275276
Arg(fmt.Sprintf("--unified=%d", contextSize)).
276277
Arg(fmt.Sprintf("--color=%s", colorArg)).
@@ -304,11 +305,12 @@ func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reve
304305

305306
extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand
306307
useExtDiff := extDiffCmd != "" && !plain
308+
useExtDiffGitConfig := self.UserConfig().Git.Paging.UseExternalDiffGitConfig
307309

308310
cmdArgs := NewGitCmd("diff").
309311
Config("diff.noprefix=false").
310312
ConfigIf(useExtDiff, "diff.external="+extDiffCmd).
311-
ArgIfElse(useExtDiff, "--ext-diff", "--no-ext-diff").
313+
ArgIfElse(useExtDiff || useExtDiffGitConfig, "--ext-diff", "--no-ext-diff").
312314
Arg("--submodule").
313315
Arg(fmt.Sprintf("--unified=%d", contextSize)).
314316
Arg("--no-renames").

pkg/config/user_config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ type PagingConfig struct {
318318
Pager PagerType `yaml:"pager"`
319319
// e.g. 'difft --color=always'
320320
ExternalDiffCommand string `yaml:"externalDiffCommand"`
321+
// If true, Lazygit will use git's `diff.external` config for paging. The advantage over `externalDiffCommand` is that this can be configured per file type in .gitattributes; see https://git-scm.com/docs/gitattributes#_defining_an_external_diff_driver.
322+
UseExternalDiffGitConfig bool `yaml:"useExternalDiffGitConfig"`
321323
}
322324

323325
type CommitConfig struct {

schema/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,11 @@
16751675
"externalDiffCommand": {
16761676
"type": "string",
16771677
"description": "e.g. 'difft --color=always'"
1678+
},
1679+
"useExternalDiffGitConfig": {
1680+
"type": "boolean",
1681+
"description": "If true, Lazygit will use git's `diff.external` config for paging. The advantage over `externalDiffCommand` is that this can be configured per file type in .gitattributes; see https://git-scm.com/docs/gitattributes#_defining_an_external_diff_driver.",
1682+
"default": false
16781683
}
16791684
},
16801685
"additionalProperties": false,

0 commit comments

Comments
 (0)