Skip to content

Commit 2b3eea7

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 0f785c4 commit 2b3eea7

File tree

8 files changed

+39
-4
lines changed

8 files changed

+39
-4
lines changed

docs/Config.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ git:
309309
# e.g. 'difft --color=always'
310310
externalDiffCommand: ""
311311

312+
# 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.
313+
useExternalDiffGitConfig: false
314+
312315
# Config relating to committing
313316
commit:
314317
# 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
@@ -86,3 +86,13 @@ git:
8686
paging:
8787
externalDiffCommand: difft --color=always --display=inline --syntax-highlight=off
8888
```
89+
90+
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
91+
92+
```yaml
93+
git:
94+
paging:
95+
useExternalDiffGitConfig: true
96+
```
97+
98+
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 with external diff command",
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/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
@@ -320,6 +320,8 @@ type PagingConfig struct {
320320
UseConfig bool `yaml:"useConfig"`
321321
// e.g. 'difft --color=always'
322322
ExternalDiffCommand string `yaml:"externalDiffCommand"`
323+
// 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.
324+
UseExternalDiffGitConfig bool `yaml:"useExternalDiffGitConfig"`
323325
}
324326

325327
type CommitConfig struct {

schema/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,11 @@
16801680
"externalDiffCommand": {
16811681
"type": "string",
16821682
"description": "e.g. 'difft --color=always'"
1683+
},
1684+
"useExternalDiffGitConfig": {
1685+
"type": "boolean",
1686+
"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.",
1687+
"default": false
16831688
}
16841689
},
16851690
"additionalProperties": false,

0 commit comments

Comments
 (0)