From 8d7bfd131ef1b951dd0b69a03787b0eab77196dd Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 7 Jul 2025 14:57:12 +0200 Subject: [PATCH] Move IgnoreWhitespaceInDiffView to user config When toggling the value in the UI we simply overwrite the value in UserConfig; this would be bad if there was ever a chance that we want to write the user config back to disk, but it is very unlikely that we can do that, because currently we have no way to tell which parts of the config come from the global config file and which ones come from a repo-local one. --- docs/Config.md | 3 +++ pkg/commands/git_commands/commit.go | 2 +- pkg/commands/git_commands/commit_test.go | 2 +- pkg/commands/git_commands/diff.go | 2 +- pkg/commands/git_commands/stash.go | 2 +- pkg/commands/git_commands/stash_test.go | 2 +- pkg/commands/git_commands/working_tree.go | 4 ++-- pkg/commands/git_commands/working_tree_test.go | 4 ++-- pkg/config/app_config.go | 11 +++++------ pkg/config/user_config.go | 3 +++ pkg/gui/controllers/helpers/diff_helper.go | 2 +- pkg/gui/controllers/toggle_whitespace_action.go | 3 +-- schema/config.json | 5 +++++ 13 files changed, 27 insertions(+), 18 deletions(-) diff --git a/docs/Config.md b/docs/Config.md index 925ceb1b3..3742c4090 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -366,6 +366,9 @@ git: allBranchesLogCmds: - git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium + # If true, git diffs are rendered with the `--ignore-all-space` flag, which ignores whitespace changes. Can be toggled from within Lazygit with ``. + ignoreWhitespaceInDiffView: false + # If true, do not spawn a separate process when using GPG overrideGpg: false diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go index 3568dbb33..2aed0f06d 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -268,7 +268,7 @@ func (self *CommitCommands) ShowCmdObj(hash string, filterPath string) *oscomman Arg("--decorate"). Arg("-p"). Arg(hash). - ArgIf(self.AppState.IgnoreWhitespaceInDiffView, "--ignore-all-space"). + ArgIf(self.UserConfig().Git.IgnoreWhitespaceInDiffView, "--ignore-all-space"). Arg(fmt.Sprintf("--find-renames=%d%%", self.AppState.RenameSimilarityThreshold)). ArgIf(filterPath != "", "--", filterPath). Dir(self.repoPaths.worktreePath). diff --git a/pkg/commands/git_commands/commit_test.go b/pkg/commands/git_commands/commit_test.go index a00a4ff23..3ce414a83 100644 --- a/pkg/commands/git_commands/commit_test.go +++ b/pkg/commands/git_commands/commit_test.go @@ -320,8 +320,8 @@ func TestCommitShowCmdObj(t *testing.T) { t.Run(s.testName, func(t *testing.T) { userConfig := config.GetDefaultConfig() userConfig.Git.Paging.ExternalDiffCommand = s.extDiffCmd + userConfig.Git.IgnoreWhitespaceInDiffView = s.ignoreWhitespace appState := &config.AppState{} - appState.IgnoreWhitespaceInDiffView = s.ignoreWhitespace appState.DiffContextSize = s.contextSize appState.RenameSimilarityThreshold = s.similarityThreshold diff --git a/pkg/commands/git_commands/diff.go b/pkg/commands/git_commands/diff.go index 16bb76275..8da1b103d 100644 --- a/pkg/commands/git_commands/diff.go +++ b/pkg/commands/git_commands/diff.go @@ -21,7 +21,7 @@ func NewDiffCommands(gitCommon *GitCommon) *DiffCommands { func (self *DiffCommands) DiffCmdObj(diffArgs []string) *oscommands.CmdObj { extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand useExtDiff := extDiffCmd != "" - ignoreWhitespace := self.AppState.IgnoreWhitespaceInDiffView + ignoreWhitespace := self.UserConfig().Git.IgnoreWhitespaceInDiffView return self.cmd.New( NewGitCmd("diff"). diff --git a/pkg/commands/git_commands/stash.go b/pkg/commands/git_commands/stash.go index a0097e87e..585b1f277 100644 --- a/pkg/commands/git_commands/stash.go +++ b/pkg/commands/git_commands/stash.go @@ -88,7 +88,7 @@ func (self *StashCommands) ShowStashEntryCmdObj(index int) *oscommands.CmdObj { Arg("-u"). Arg(fmt.Sprintf("--color=%s", self.UserConfig().Git.Paging.ColorArg)). Arg(fmt.Sprintf("--unified=%d", self.AppState.DiffContextSize)). - ArgIf(self.AppState.IgnoreWhitespaceInDiffView, "--ignore-all-space"). + ArgIf(self.UserConfig().Git.IgnoreWhitespaceInDiffView, "--ignore-all-space"). Arg(fmt.Sprintf("--find-renames=%d%%", self.AppState.RenameSimilarityThreshold)). Arg(fmt.Sprintf("refs/stash@{%d}", index)). Dir(self.repoPaths.worktreePath). diff --git a/pkg/commands/git_commands/stash_test.go b/pkg/commands/git_commands/stash_test.go index 4d54ea521..fc34e2c93 100644 --- a/pkg/commands/git_commands/stash_test.go +++ b/pkg/commands/git_commands/stash_test.go @@ -144,8 +144,8 @@ func TestStashStashEntryCmdObj(t *testing.T) { for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { userConfig := config.GetDefaultConfig() + userConfig.Git.IgnoreWhitespaceInDiffView = s.ignoreWhitespace appState := &config.AppState{} - appState.IgnoreWhitespaceInDiffView = s.ignoreWhitespace appState.DiffContextSize = s.contextSize appState.RenameSimilarityThreshold = s.similarityThreshold repoPaths := RepoPaths{ diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go index a5faa801b..e521b38ca 100644 --- a/pkg/commands/git_commands/working_tree.go +++ b/pkg/commands/git_commands/working_tree.go @@ -272,7 +272,7 @@ func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain Arg("--submodule"). Arg(fmt.Sprintf("--unified=%d", contextSize)). Arg(fmt.Sprintf("--color=%s", colorArg)). - ArgIf(!plain && self.AppState.IgnoreWhitespaceInDiffView, "--ignore-all-space"). + ArgIf(!plain && self.UserConfig().Git.IgnoreWhitespaceInDiffView, "--ignore-all-space"). Arg(fmt.Sprintf("--find-renames=%d%%", self.AppState.RenameSimilarityThreshold)). ArgIf(cached, "--cached"). ArgIf(noIndex, "--no-index"). @@ -314,7 +314,7 @@ func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reve Arg(from). Arg(to). ArgIf(reverse, "-R"). - ArgIf(!plain && self.AppState.IgnoreWhitespaceInDiffView, "--ignore-all-space"). + ArgIf(!plain && self.UserConfig().Git.IgnoreWhitespaceInDiffView, "--ignore-all-space"). Arg("--"). Arg(fileName). Dir(self.repoPaths.worktreePath). diff --git a/pkg/commands/git_commands/working_tree_test.go b/pkg/commands/git_commands/working_tree_test.go index bbe3d7720..1191e5a0c 100644 --- a/pkg/commands/git_commands/working_tree_test.go +++ b/pkg/commands/git_commands/working_tree_test.go @@ -327,8 +327,8 @@ func TestWorkingTreeDiff(t *testing.T) { for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { userConfig := config.GetDefaultConfig() + userConfig.Git.IgnoreWhitespaceInDiffView = s.ignoreWhitespace appState := &config.AppState{} - appState.IgnoreWhitespaceInDiffView = s.ignoreWhitespace appState.DiffContextSize = s.contextSize appState.RenameSimilarityThreshold = s.similarityThreshold repoPaths := RepoPaths{ @@ -396,8 +396,8 @@ func TestWorkingTreeShowFileDiff(t *testing.T) { for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { userConfig := config.GetDefaultConfig() + userConfig.Git.IgnoreWhitespaceInDiffView = s.ignoreWhitespace appState := &config.AppState{} - appState.IgnoreWhitespaceInDiffView = s.ignoreWhitespace appState.DiffContextSize = s.contextSize repoPaths := RepoPaths{ worktreePath: "/path/to/worktree", diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 90c6c1013..521cad07d 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -676,12 +676,11 @@ type AppState struct { // For backwards compatibility we keep the old name in yaml files. ShellCommandsHistory []string `yaml:"customcommandshistory"` - HideCommandLog bool - IgnoreWhitespaceInDiffView bool - DiffContextSize uint64 - RenameSimilarityThreshold int - LocalBranchSortOrder string - RemoteBranchSortOrder string + HideCommandLog bool + DiffContextSize uint64 + RenameSimilarityThreshold int + LocalBranchSortOrder string + RemoteBranchSortOrder string // One of: 'date-order' | 'author-date-order' | 'topo-order' | 'default' // 'topo-order' makes it easier to read the git log graph, but commits may not diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 179410aa9..8f6ed2187 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -264,6 +264,8 @@ type GitConfig struct { BranchLogCmd string `yaml:"branchLogCmd"` // Commands used to display git log of all branches in the main window, they will be cycled in order of appearance (array of strings) AllBranchesLogCmds []string `yaml:"allBranchesLogCmds"` + // If true, git diffs are rendered with the `--ignore-all-space` flag, which ignores whitespace changes. Can be toggled from within Lazygit with ``. + IgnoreWhitespaceInDiffView bool `yaml:"ignoreWhitespaceInDiffView"` // If true, do not spawn a separate process when using GPG OverrideGpg bool `yaml:"overrideGpg"` // If true, do not allow force pushes @@ -808,6 +810,7 @@ func GetDefaultConfig() *UserConfig { AutoStageResolvedConflicts: true, BranchLogCmd: "git log --graph --color=always --abbrev-commit --decorate --date=relative --pretty=medium {{branchName}} --", AllBranchesLogCmds: []string{"git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium"}, + IgnoreWhitespaceInDiffView: false, DisableForcePushing: false, CommitPrefixes: map[string][]CommitPrefixConfig(nil), BranchPrefix: "", diff --git a/pkg/gui/controllers/helpers/diff_helper.go b/pkg/gui/controllers/helpers/diff_helper.go index 95c820d37..9a08f15ec 100644 --- a/pkg/gui/controllers/helpers/diff_helper.go +++ b/pkg/gui/controllers/helpers/diff_helper.go @@ -144,7 +144,7 @@ func (self *DiffHelper) WithDiffModeCheck(f func()) { } func (self *DiffHelper) IgnoringWhitespaceSubTitle() string { - if self.c.GetAppState().IgnoreWhitespaceInDiffView { + if self.c.UserConfig().Git.IgnoreWhitespaceInDiffView { return self.c.Tr.IgnoreWhitespaceDiffViewSubTitle } diff --git a/pkg/gui/controllers/toggle_whitespace_action.go b/pkg/gui/controllers/toggle_whitespace_action.go index 4d491e79b..a1ac0c8da 100644 --- a/pkg/gui/controllers/toggle_whitespace_action.go +++ b/pkg/gui/controllers/toggle_whitespace_action.go @@ -25,8 +25,7 @@ func (self *ToggleWhitespaceAction) Call() error { return errors.New(self.c.Tr.IgnoreWhitespaceNotSupportedHere) } - self.c.GetAppState().IgnoreWhitespaceInDiffView = !self.c.GetAppState().IgnoreWhitespaceInDiffView - self.c.SaveAppStateAndLogError() + self.c.UserConfig().Git.IgnoreWhitespaceInDiffView = !self.c.UserConfig().Git.IgnoreWhitespaceInDiffView self.c.Context().CurrentSide().HandleFocus(types.OnFocusOpts{}) return nil diff --git a/schema/config.json b/schema/config.json index e21b7bb66..194cb8e9c 100644 --- a/schema/config.json +++ b/schema/config.json @@ -359,6 +359,11 @@ "git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium" ] }, + "ignoreWhitespaceInDiffView": { + "type": "boolean", + "description": "If true, git diffs are rendered with the `--ignore-all-space` flag, which ignores whitespace changes. Can be toggled from within Lazygit with `\u003cc-w\u003e`.", + "default": false + }, "overrideGpg": { "type": "boolean", "description": "If true, do not spawn a separate process when using GPG",