diff --git a/pkg/commands/files.go b/pkg/commands/files.go index 9e077a462..648c5f511 100644 --- a/pkg/commands/files.go +++ b/pkg/commands/files.go @@ -189,17 +189,18 @@ func (c *GitCommand) Ignore(filename string) error { } // WorktreeFileDiff returns the diff of a file -func (c *GitCommand) WorktreeFileDiff(file *models.File, plain bool, cached bool) string { +func (c *GitCommand) WorktreeFileDiff(file *models.File, plain bool, cached bool, ignoreWhitespace bool) string { // for now we assume an error means the file was deleted - s, _ := c.OSCommand.RunCommandWithOutput(c.WorktreeFileDiffCmdStr(file, plain, cached)) + s, _ := c.OSCommand.RunCommandWithOutput(c.WorktreeFileDiffCmdStr(file, plain, cached, ignoreWhitespace)) return s } -func (c *GitCommand) WorktreeFileDiffCmdStr(node models.IFile, plain bool, cached bool) string { +func (c *GitCommand) WorktreeFileDiffCmdStr(node models.IFile, plain bool, cached bool, ignoreWhitespace bool) string { cachedArg := "" trackedArg := "--" colorArg := c.colorArg() path := c.OSCommand.Quote(node.GetPath()) + ignoreWhitespaceArg := "" if cached { cachedArg = "--cached" } @@ -209,8 +210,11 @@ func (c *GitCommand) WorktreeFileDiffCmdStr(node models.IFile, plain bool, cache if plain { colorArg = "never" } + if ignoreWhitespace { + ignoreWhitespaceArg = "-w" + } - return fmt.Sprintf("git diff --submodule --no-ext-diff --color=%s %s %s %s", colorArg, cachedArg, trackedArg, path) + return fmt.Sprintf("git diff --submodule --no-ext-diff --color=%s %s %s %s %s", colorArg, ignoreWhitespaceArg, cachedArg, trackedArg, path) } func (c *GitCommand) ApplyPatch(patch string, flags ...string) error { diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index e7231a55b..f87567ac0 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -165,6 +165,7 @@ type KeybindingUniversalConfig struct { SubmitEditorText string `yaml:"submitEditorText"` AppendNewline string `yaml:"appendNewline"` ExtrasMenu string `yaml:"extrasMenu"` + ToggleWhitespaceInDiffView string `yaml:"toggleWhitespaceInDiffView"` } type KeybindingStatusConfig struct { @@ -404,6 +405,7 @@ func GetDefaultConfig() *UserConfig { SubmitEditorText: "", AppendNewline: "", ExtrasMenu: "@", + ToggleWhitespaceInDiffView: "", }, Status: KeybindingStatusConfig{ CheckForUpdate: "u", diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index 4d225d945..48c0621cb 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -71,7 +71,7 @@ func (gui *Gui) selectFile(alreadySelected bool) error { return gui.refreshMergePanelWithLock() } - cmdStr := gui.GitCommand.WorktreeFileDiffCmdStr(node, false, !node.GetHasUnstagedChanges() && node.GetHasStagedChanges()) + cmdStr := gui.GitCommand.WorktreeFileDiffCmdStr(node, false, !node.GetHasUnstagedChanges() && node.GetHasStagedChanges(), gui.State.IgnoreWhitespaceInDiffView) cmd := gui.OSCommand.ExecutableFromString(cmdStr) refreshOpts := refreshMainOpts{main: &viewUpdateOpts{ @@ -81,7 +81,7 @@ func (gui *Gui) selectFile(alreadySelected bool) error { if node.GetHasUnstagedChanges() { if node.GetHasStagedChanges() { - cmdStr := gui.GitCommand.WorktreeFileDiffCmdStr(node, false, true) + cmdStr := gui.GitCommand.WorktreeFileDiffCmdStr(node, false, true, gui.State.IgnoreWhitespaceInDiffView) cmd := gui.OSCommand.ExecutableFromString(cmdStr) refreshOpts.secondary = &viewUpdateOpts{ diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 990654587..71b9702f2 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -339,6 +339,9 @@ type guiState struct { // do this whenever we switch back and forth between repos to get the views // back in sync with the repo state ViewsSetup bool + + // flag as to whether or not the diff view should ignore whitespace + IgnoreWhitespaceInDiffView bool } // reuseState determines if we pull the repo state from our repo state map or diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 169a1be24..dbcb0b8fe 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -1719,6 +1719,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Description: gui.Tr.LcViewBulkSubmoduleOptions, OpensMenu: true, }, + { + ViewName: "files", + Contexts: []string{string(FILES_CONTEXT_KEY)}, + Key: gui.getKey(config.Universal.ToggleWhitespaceInDiffView), + Handler: gui.toggleWhitespaceInDiffView, + Description: gui.Tr.ToggleWhitespaceInDiffView, + }, { ViewName: "extras", Key: gocui.MouseWheelUp, diff --git a/pkg/gui/quitting.go b/pkg/gui/quitting.go index 24b92efc2..8abe2e121 100644 --- a/pkg/gui/quitting.go +++ b/pkg/gui/quitting.go @@ -29,6 +29,11 @@ func (gui *Gui) handleQuitWithoutChangingDirectory() error { return gui.quit() } +func (gui *Gui) toggleWhitespaceInDiffView() error { + gui.State.IgnoreWhitespaceInDiffView = !gui.State.IgnoreWhitespaceInDiffView + return gui.refreshFilesAndSubmodules() +} + func (gui *Gui) handleQuit() error { gui.State.RetainOriginalDir = false return gui.quit() diff --git a/pkg/gui/staging_panel.go b/pkg/gui/staging_panel.go index c016586da..916361fc1 100644 --- a/pkg/gui/staging_panel.go +++ b/pkg/gui/staging_panel.go @@ -34,8 +34,8 @@ func (gui *Gui) refreshStagingPanel(forceSecondaryFocused bool, selectedLineIdx } // note for custom diffs, we'll need to send a flag here saying not to use the custom diff - diff := gui.GitCommand.WorktreeFileDiff(file, true, secondaryFocused) - secondaryDiff := gui.GitCommand.WorktreeFileDiff(file, true, !secondaryFocused) + diff := gui.GitCommand.WorktreeFileDiff(file, true, secondaryFocused, gui.State.IgnoreWhitespaceInDiffView) + secondaryDiff := gui.GitCommand.WorktreeFileDiff(file, true, !secondaryFocused, gui.State.IgnoreWhitespaceInDiffView) // if we have e.g. a deleted file with nothing else to the diff will have only // 4-5 lines in which case we'll swap panels diff --git a/pkg/gui/submodules_panel.go b/pkg/gui/submodules_panel.go index 8a1d8d753..cec3d3397 100644 --- a/pkg/gui/submodules_panel.go +++ b/pkg/gui/submodules_panel.go @@ -37,7 +37,7 @@ func (gui *Gui) handleSubmoduleSelect() error { if file == nil { task = NewRenderStringTask(prefix) } else { - cmdStr := gui.GitCommand.WorktreeFileDiffCmdStr(file, false, !file.HasUnstagedChanges && file.HasStagedChanges) + cmdStr := gui.GitCommand.WorktreeFileDiffCmdStr(file, false, !file.HasUnstagedChanges && file.HasStagedChanges, gui.State.IgnoreWhitespaceInDiffView) cmd := gui.OSCommand.ExecutableFromString(cmdStr) task = NewRunCommandTaskWithPrefix(cmd, prefix) } diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index fc7d59ebb..755f7c1be 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -451,6 +451,7 @@ type TranslationSet struct { CommandLogHeader string RandomTip string SelectParentCommitForMerge string + ToggleWhitespaceInDiffView string Spans Spans } @@ -995,6 +996,7 @@ func englishTranslationSet() TranslationSet { CommandLogHeader: "You can hide/focus this panel by pressing '%s' or hide it permanently in your config with `gui.showCommandLog: false`\n", RandomTip: "Random Tip", SelectParentCommitForMerge: "Select parent commit for merge", + ToggleWhitespaceInDiffView: "Toggle whether or not whitespace changes are shown in the diff view", Spans: Spans{ // TODO: combine this with the original keybinding descriptions (those are all in lowercase atm) CheckoutCommit: "Checkout commit",