diff --git a/docs/Config.md b/docs/Config.md index 3742c4090..25f9fcbd3 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -369,6 +369,12 @@ git: # 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 + # The number of lines of context to show around each diff hunk. Can be changed from within Lazygit with the `{` and `}` keys. + diffContextSize: 3 + + # The threshold for considering a file to be renamed, in percent. Can be changed from within Lazygit with the `(` and `)` keys. + renameSimilarityThreshold: 50 + # 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 2aed0f06d..f021a90bf 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -254,7 +254,7 @@ func (self *CommitCommands) AmendHeadCmdObj() *oscommands.CmdObj { } func (self *CommitCommands) ShowCmdObj(hash string, filterPath string) *oscommands.CmdObj { - contextSize := self.AppState.DiffContextSize + contextSize := self.UserConfig().Git.DiffContextSize extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand cmdArgs := NewGitCmd("show"). @@ -269,7 +269,7 @@ func (self *CommitCommands) ShowCmdObj(hash string, filterPath string) *oscomman Arg("-p"). Arg(hash). ArgIf(self.UserConfig().Git.IgnoreWhitespaceInDiffView, "--ignore-all-space"). - Arg(fmt.Sprintf("--find-renames=%d%%", self.AppState.RenameSimilarityThreshold)). + Arg(fmt.Sprintf("--find-renames=%d%%", self.UserConfig().Git.RenameSimilarityThreshold)). ArgIf(filterPath != "", "--", filterPath). Dir(self.repoPaths.worktreePath). ToArgv() diff --git a/pkg/commands/git_commands/commit_test.go b/pkg/commands/git_commands/commit_test.go index 3ce414a83..112cf6b77 100644 --- a/pkg/commands/git_commands/commit_test.go +++ b/pkg/commands/git_commands/commit_test.go @@ -321,15 +321,14 @@ func TestCommitShowCmdObj(t *testing.T) { userConfig := config.GetDefaultConfig() userConfig.Git.Paging.ExternalDiffCommand = s.extDiffCmd userConfig.Git.IgnoreWhitespaceInDiffView = s.ignoreWhitespace - appState := &config.AppState{} - appState.DiffContextSize = s.contextSize - appState.RenameSimilarityThreshold = s.similarityThreshold + userConfig.Git.DiffContextSize = s.contextSize + userConfig.Git.RenameSimilarityThreshold = s.similarityThreshold runner := oscommands.NewFakeRunner(t).ExpectGitArgs(s.expected, "", nil) repoPaths := RepoPaths{ worktreePath: "/path/to/worktree", } - instance := buildCommitCommands(commonDeps{userConfig: userConfig, appState: appState, runner: runner, repoPaths: &repoPaths}) + instance := buildCommitCommands(commonDeps{userConfig: userConfig, appState: &config.AppState{}, runner: runner, repoPaths: &repoPaths}) assert.NoError(t, instance.ShowCmdObj("1234567890", s.filterPath).Run()) runner.CheckForMissingCalls() diff --git a/pkg/commands/git_commands/diff.go b/pkg/commands/git_commands/diff.go index 8da1b103d..5f1db045f 100644 --- a/pkg/commands/git_commands/diff.go +++ b/pkg/commands/git_commands/diff.go @@ -31,7 +31,7 @@ func (self *DiffCommands) DiffCmdObj(diffArgs []string) *oscommands.CmdObj { Arg("--submodule"). Arg(fmt.Sprintf("--color=%s", self.UserConfig().Git.Paging.ColorArg)). ArgIf(ignoreWhitespace, "--ignore-all-space"). - Arg(fmt.Sprintf("--unified=%d", self.AppState.DiffContextSize)). + Arg(fmt.Sprintf("--unified=%d", self.UserConfig().Git.DiffContextSize)). Arg(diffArgs...). Dir(self.repoPaths.worktreePath). ToArgv(), diff --git a/pkg/commands/git_commands/file_loader.go b/pkg/commands/git_commands/file_loader.go index 92a011f63..19c07ece1 100644 --- a/pkg/commands/git_commands/file_loader.go +++ b/pkg/commands/git_commands/file_loader.go @@ -175,7 +175,7 @@ func (self *FileLoader) gitStatus(opts GitStatusOptions) ([]FileStatus, error) { ArgIfElse( opts.NoRenames, "--no-renames", - fmt.Sprintf("--find-renames=%d%%", self.AppState.RenameSimilarityThreshold), + fmt.Sprintf("--find-renames=%d%%", self.UserConfig().Git.RenameSimilarityThreshold), ). ToArgv() diff --git a/pkg/commands/git_commands/file_loader_test.go b/pkg/commands/git_commands/file_loader_test.go index 233bfb855..80373eef1 100644 --- a/pkg/commands/git_commands/file_loader_test.go +++ b/pkg/commands/git_commands/file_loader_test.go @@ -198,17 +198,12 @@ func TestFileGetStatusFiles(t *testing.T) { t.Run(s.testName, func(t *testing.T) { cmd := oscommands.NewDummyCmdObjBuilder(s.runner) - appState := &config.AppState{} - appState.RenameSimilarityThreshold = s.similarityThreshold - - userConfig := &config.UserConfig{ - Gui: config.GuiConfig{ - ShowNumstatInFilesView: s.showNumstatInFilesView, - }, - } + userConfig := &config.UserConfig{} + userConfig.Gui.ShowNumstatInFilesView = s.showNumstatInFilesView + userConfig.Git.RenameSimilarityThreshold = s.similarityThreshold loader := &FileLoader{ - GitCommon: buildGitCommon(commonDeps{appState: appState, userConfig: userConfig}), + GitCommon: buildGitCommon(commonDeps{appState: &config.AppState{}, userConfig: userConfig}), cmd: cmd, config: &FakeFileLoaderConfig{showUntrackedFiles: "yes"}, getFileType: func(string) string { return "file" }, diff --git a/pkg/commands/git_commands/stash.go b/pkg/commands/git_commands/stash.go index 585b1f277..86dccff24 100644 --- a/pkg/commands/git_commands/stash.go +++ b/pkg/commands/git_commands/stash.go @@ -87,9 +87,9 @@ func (self *StashCommands) ShowStashEntryCmdObj(index int) *oscommands.CmdObj { Arg("--stat"). Arg("-u"). Arg(fmt.Sprintf("--color=%s", self.UserConfig().Git.Paging.ColorArg)). - Arg(fmt.Sprintf("--unified=%d", self.AppState.DiffContextSize)). + Arg(fmt.Sprintf("--unified=%d", self.UserConfig().Git.DiffContextSize)). ArgIf(self.UserConfig().Git.IgnoreWhitespaceInDiffView, "--ignore-all-space"). - Arg(fmt.Sprintf("--find-renames=%d%%", self.AppState.RenameSimilarityThreshold)). + Arg(fmt.Sprintf("--find-renames=%d%%", self.UserConfig().Git.RenameSimilarityThreshold)). Arg(fmt.Sprintf("refs/stash@{%d}", index)). Dir(self.repoPaths.worktreePath). ToArgv() diff --git a/pkg/commands/git_commands/stash_test.go b/pkg/commands/git_commands/stash_test.go index fc34e2c93..0311f791d 100644 --- a/pkg/commands/git_commands/stash_test.go +++ b/pkg/commands/git_commands/stash_test.go @@ -145,13 +145,12 @@ func TestStashStashEntryCmdObj(t *testing.T) { t.Run(s.testName, func(t *testing.T) { userConfig := config.GetDefaultConfig() userConfig.Git.IgnoreWhitespaceInDiffView = s.ignoreWhitespace - appState := &config.AppState{} - appState.DiffContextSize = s.contextSize - appState.RenameSimilarityThreshold = s.similarityThreshold + userConfig.Git.DiffContextSize = s.contextSize + userConfig.Git.RenameSimilarityThreshold = s.similarityThreshold repoPaths := RepoPaths{ worktreePath: "/path/to/worktree", } - instance := buildStashCommands(commonDeps{userConfig: userConfig, appState: appState, repoPaths: &repoPaths}) + instance := buildStashCommands(commonDeps{userConfig: userConfig, appState: &config.AppState{}, repoPaths: &repoPaths}) cmdStr := instance.ShowStashEntryCmdObj(s.index).Args() assert.Equal(t, s.expected, cmdStr) diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go index e521b38ca..deb9e6a59 100644 --- a/pkg/commands/git_commands/working_tree.go +++ b/pkg/commands/git_commands/working_tree.go @@ -260,7 +260,7 @@ func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain colorArg = "never" } - contextSize := self.AppState.DiffContextSize + contextSize := self.UserConfig().Git.DiffContextSize prevPath := node.GetPreviousPath() noIndex := !node.GetIsTracked() && !node.GetHasStagedChanges() && !cached && node.GetIsFile() extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand @@ -273,7 +273,7 @@ func (self *WorkingTreeCommands) WorktreeFileDiffCmdObj(node models.IFile, plain Arg(fmt.Sprintf("--unified=%d", contextSize)). Arg(fmt.Sprintf("--color=%s", colorArg)). ArgIf(!plain && self.UserConfig().Git.IgnoreWhitespaceInDiffView, "--ignore-all-space"). - Arg(fmt.Sprintf("--find-renames=%d%%", self.AppState.RenameSimilarityThreshold)). + Arg(fmt.Sprintf("--find-renames=%d%%", self.UserConfig().Git.RenameSimilarityThreshold)). ArgIf(cached, "--cached"). ArgIf(noIndex, "--no-index"). Arg("--"). @@ -293,7 +293,7 @@ func (self *WorkingTreeCommands) ShowFileDiff(from string, to string, reverse bo } func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reverse bool, fileName string, plain bool) *oscommands.CmdObj { - contextSize := self.AppState.DiffContextSize + contextSize := self.UserConfig().Git.DiffContextSize colorArg := self.UserConfig().Git.Paging.ColorArg if plain { diff --git a/pkg/commands/git_commands/working_tree_test.go b/pkg/commands/git_commands/working_tree_test.go index 1191e5a0c..759eb1bbe 100644 --- a/pkg/commands/git_commands/working_tree_test.go +++ b/pkg/commands/git_commands/working_tree_test.go @@ -328,14 +328,13 @@ func TestWorkingTreeDiff(t *testing.T) { t.Run(s.testName, func(t *testing.T) { userConfig := config.GetDefaultConfig() userConfig.Git.IgnoreWhitespaceInDiffView = s.ignoreWhitespace - appState := &config.AppState{} - appState.DiffContextSize = s.contextSize - appState.RenameSimilarityThreshold = s.similarityThreshold + userConfig.Git.DiffContextSize = s.contextSize + userConfig.Git.RenameSimilarityThreshold = s.similarityThreshold repoPaths := RepoPaths{ worktreePath: "/path/to/worktree", } - instance := buildWorkingTreeCommands(commonDeps{runner: s.runner, userConfig: userConfig, appState: appState, repoPaths: &repoPaths}) + instance := buildWorkingTreeCommands(commonDeps{runner: s.runner, userConfig: userConfig, appState: &config.AppState{}, repoPaths: &repoPaths}) result := instance.WorktreeFileDiff(s.file, s.plain, s.cached) assert.Equal(t, expectedResult, result) s.runner.CheckForMissingCalls() @@ -397,13 +396,12 @@ func TestWorkingTreeShowFileDiff(t *testing.T) { t.Run(s.testName, func(t *testing.T) { userConfig := config.GetDefaultConfig() userConfig.Git.IgnoreWhitespaceInDiffView = s.ignoreWhitespace - appState := &config.AppState{} - appState.DiffContextSize = s.contextSize + userConfig.Git.DiffContextSize = s.contextSize repoPaths := RepoPaths{ worktreePath: "/path/to/worktree", } - instance := buildWorkingTreeCommands(commonDeps{runner: s.runner, userConfig: userConfig, appState: appState, repoPaths: &repoPaths}) + instance := buildWorkingTreeCommands(commonDeps{runner: s.runner, userConfig: userConfig, appState: &config.AppState{}, repoPaths: &repoPaths}) result, err := instance.ShowFileDiff(s.from, s.to, s.reverse, "test.txt", s.plain) assert.NoError(t, err) diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 521cad07d..aaaf7adb3 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -676,11 +676,9 @@ type AppState struct { // For backwards compatibility we keep the old name in yaml files. ShellCommandsHistory []string `yaml:"customcommandshistory"` - HideCommandLog bool - DiffContextSize uint64 - RenameSimilarityThreshold int - LocalBranchSortOrder string - RemoteBranchSortOrder string + HideCommandLog bool + 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 @@ -694,16 +692,14 @@ type AppState struct { func getDefaultAppState() *AppState { return &AppState{ - LastUpdateCheck: 0, - RecentRepos: []string{}, - StartupPopupVersion: 0, - LastVersion: "", - DiffContextSize: 3, - RenameSimilarityThreshold: 50, - LocalBranchSortOrder: "recency", - RemoteBranchSortOrder: "alphabetical", - GitLogOrder: "", // should be "topo-order" eventually - GitLogShowGraph: "", // should be "always" eventually + LastUpdateCheck: 0, + RecentRepos: []string{}, + StartupPopupVersion: 0, + LastVersion: "", + LocalBranchSortOrder: "recency", + RemoteBranchSortOrder: "alphabetical", + GitLogOrder: "", // should be "topo-order" eventually + GitLogShowGraph: "", // should be "always" eventually } } diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 8f6ed2187..39c67bc4b 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -266,6 +266,10 @@ type GitConfig struct { 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"` + // The number of lines of context to show around each diff hunk. Can be changed from within Lazygit with the `{` and `}` keys. + DiffContextSize uint64 `yaml:"diffContextSize"` + // The threshold for considering a file to be renamed, in percent. Can be changed from within Lazygit with the `(` and `)` keys. + RenameSimilarityThreshold int `yaml:"renameSimilarityThreshold" jsonschema:"minimum=0,maximum=100"` // If true, do not spawn a separate process when using GPG OverrideGpg bool `yaml:"overrideGpg"` // If true, do not allow force pushes @@ -811,6 +815,8 @@ func GetDefaultConfig() *UserConfig { 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, + DiffContextSize: 3, + RenameSimilarityThreshold: 50, DisableForcePushing: false, CommitPrefixes: map[string][]CommitPrefixConfig(nil), BranchPrefix: "", diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index 42e515b08..02cc67eae 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -378,7 +378,7 @@ func (self *CommitFilesController) openDiffTool(node *filetree.CommitFileNode) e } func (self *CommitFilesController) toggleForPatch(selectedNodes []*filetree.CommitFileNode) error { - if self.c.AppState.DiffContextSize == 0 { + if self.c.UserConfig().Git.DiffContextSize == 0 { return fmt.Errorf(self.c.Tr.Actions.NotEnoughContextForCustomPatch, keybindings.Label(self.c.UserConfig().Keybinding.Universal.IncreaseContextInDiffView)) } @@ -472,7 +472,7 @@ func (self *CommitFilesController) enterCommitFile(node *filetree.CommitFileNode return self.handleToggleCommitFileDirCollapsed(node) } - if self.c.AppState.DiffContextSize == 0 { + if self.c.UserConfig().Git.DiffContextSize == 0 { return fmt.Errorf(self.c.Tr.Actions.NotEnoughContextForCustomPatch, keybindings.Label(self.c.UserConfig().Keybinding.Universal.IncreaseContextInDiffView)) } diff --git a/pkg/gui/controllers/context_lines_controller.go b/pkg/gui/controllers/context_lines_controller.go index b26a8e632..5e0f9d606 100644 --- a/pkg/gui/controllers/context_lines_controller.go +++ b/pkg/gui/controllers/context_lines_controller.go @@ -71,8 +71,8 @@ func (self *ContextLinesController) Increase() error { return err } - if self.c.AppState.DiffContextSize < math.MaxUint64 { - self.c.AppState.DiffContextSize++ + if self.c.UserConfig().Git.DiffContextSize < math.MaxUint64 { + self.c.UserConfig().Git.DiffContextSize++ } return self.applyChange() } @@ -86,8 +86,8 @@ func (self *ContextLinesController) Decrease() error { return err } - if self.c.AppState.DiffContextSize > 0 { - self.c.AppState.DiffContextSize-- + if self.c.UserConfig().Git.DiffContextSize > 0 { + self.c.UserConfig().Git.DiffContextSize-- } return self.applyChange() } @@ -96,8 +96,7 @@ func (self *ContextLinesController) Decrease() error { } func (self *ContextLinesController) applyChange() error { - self.c.Toast(fmt.Sprintf(self.c.Tr.DiffContextSizeChanged, self.c.AppState.DiffContextSize)) - self.c.SaveAppStateAndLogError() + self.c.Toast(fmt.Sprintf(self.c.Tr.DiffContextSizeChanged, self.c.UserConfig().Git.DiffContextSize)) currentContext := self.currentSidePanel() switch currentContext.GetKey() { diff --git a/pkg/gui/controllers/rename_similarity_threshold_controller.go b/pkg/gui/controllers/rename_similarity_threshold_controller.go index cae0fb80f..98df26127 100644 --- a/pkg/gui/controllers/rename_similarity_threshold_controller.go +++ b/pkg/gui/controllers/rename_similarity_threshold_controller.go @@ -59,10 +59,10 @@ func (self *RenameSimilarityThresholdController) Context() types.Context { } func (self *RenameSimilarityThresholdController) Increase() error { - old_size := self.c.AppState.RenameSimilarityThreshold + old_size := self.c.UserConfig().Git.RenameSimilarityThreshold if self.isShowingRenames() && old_size < 100 { - self.c.AppState.RenameSimilarityThreshold = min(100, old_size+5) + self.c.UserConfig().Git.RenameSimilarityThreshold = min(100, old_size+5) return self.applyChange() } @@ -70,10 +70,10 @@ func (self *RenameSimilarityThresholdController) Increase() error { } func (self *RenameSimilarityThresholdController) Decrease() error { - old_size := self.c.AppState.RenameSimilarityThreshold + old_size := self.c.UserConfig().Git.RenameSimilarityThreshold if self.isShowingRenames() && old_size > 5 { - self.c.AppState.RenameSimilarityThreshold = max(5, old_size-5) + self.c.UserConfig().Git.RenameSimilarityThreshold = max(5, old_size-5) return self.applyChange() } @@ -81,8 +81,7 @@ func (self *RenameSimilarityThresholdController) Decrease() error { } func (self *RenameSimilarityThresholdController) applyChange() error { - self.c.Toast(fmt.Sprintf(self.c.Tr.RenameSimilarityThresholdChanged, self.c.AppState.RenameSimilarityThreshold)) - self.c.SaveAppStateAndLogError() + self.c.Toast(fmt.Sprintf(self.c.Tr.RenameSimilarityThresholdChanged, self.c.UserConfig().Git.RenameSimilarityThreshold)) currentContext := self.currentSidePanel() switch currentContext.GetKey() { diff --git a/pkg/gui/controllers/staging_controller.go b/pkg/gui/controllers/staging_controller.go index d7198bdcb..51c6fafec 100644 --- a/pkg/gui/controllers/staging_controller.go +++ b/pkg/gui/controllers/staging_controller.go @@ -187,7 +187,7 @@ func (self *StagingController) TogglePanel() error { } func (self *StagingController) ToggleStaged() error { - if self.c.AppState.DiffContextSize == 0 { + if self.c.UserConfig().Git.DiffContextSize == 0 { return fmt.Errorf(self.c.Tr.Actions.NotEnoughContextToStage, keybindings.Label(self.c.UserConfig().Keybinding.Universal.IncreaseContextInDiffView)) } @@ -196,7 +196,7 @@ func (self *StagingController) ToggleStaged() error { } func (self *StagingController) DiscardSelection() error { - if self.c.AppState.DiffContextSize == 0 { + if self.c.UserConfig().Git.DiffContextSize == 0 { return fmt.Errorf(self.c.Tr.Actions.NotEnoughContextToDiscard, keybindings.Label(self.c.UserConfig().Keybinding.Universal.IncreaseContextInDiffView)) } diff --git a/schema/config.json b/schema/config.json index 194cb8e9c..9da832b4e 100644 --- a/schema/config.json +++ b/schema/config.json @@ -364,6 +364,18 @@ "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 }, + "diffContextSize": { + "type": "integer", + "description": "The number of lines of context to show around each diff hunk. Can be changed from within Lazygit with the `{` and `}` keys.", + "default": 3 + }, + "renameSimilarityThreshold": { + "type": "integer", + "maximum": 100, + "minimum": 0, + "description": "The threshold for considering a file to be renamed, in percent. Can be changed from within Lazygit with the `(` and `)` keys.", + "default": 50 + }, "overrideGpg": { "type": "boolean", "description": "If true, do not spawn a separate process when using GPG",