From 1243d3d5a8db6d2685633252789478722285abff Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Wed, 7 Oct 2026 13:48:57 +0200 Subject: [PATCH] Extract the diff.noprefix override into a GitCommandBuilder method Seven diff commands pass -c diff.noprefix=false so that the paths in their output carry a prefix. The next commits use the override for more commands and add more config to it, so give it a single home first. Co-Authored-By: Claude Opus 5.5 (1M context) --- pkg/commands/git_commands/commit.go | 2 +- pkg/commands/git_commands/commit_file_loader.go | 2 +- pkg/commands/git_commands/diff.go | 6 +++--- pkg/commands/git_commands/git_command_builder.go | 8 ++++++++ pkg/commands/git_commands/patch.go | 2 +- pkg/commands/git_commands/working_tree.go | 2 +- 6 files changed, 15 insertions(+), 7 deletions(-) diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go index 387a1fdc7..e46fecd84 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -242,7 +242,7 @@ func (self *CommitCommands) AmendHeadCmdObj() *oscommands.CmdObj { func (self *CommitCommands) ShowCmdObj(hash string, filterPaths []string, mode DiffMode) *oscommands.CmdObj { cmdArgs := NewGitCmd("show"). - Config("diff.noprefix=false"). + ParseableDiffPrefixes(). AddCommonDiffArgs(self.diffRendererConfigManager, self.UserConfig(), mode). Arg("--submodule"). Arg("--color=" + mode.colorArg(self.diffRendererConfigManager)). diff --git a/pkg/commands/git_commands/commit_file_loader.go b/pkg/commands/git_commands/commit_file_loader.go index 9500dabfd..2a817b93e 100644 --- a/pkg/commands/git_commands/commit_file_loader.go +++ b/pkg/commands/git_commands/commit_file_loader.go @@ -24,7 +24,7 @@ func NewCommitFileLoader(common *common.Common, cmd oscommands.ICmdObjBuilder) * // GetFilesInDiff get the specified commit files func (self *CommitFileLoader) GetFilesInDiff(from string, to string, reverse bool) ([]*models.CommitFile, error) { cmdArgs := NewGitCmd("diff"). - Config("diff.noprefix=false"). + ParseableDiffPrefixes(). Arg("--submodule"). Arg("--no-ext-diff"). Arg("--name-status"). diff --git a/pkg/commands/git_commands/diff.go b/pkg/commands/git_commands/diff.go index 01bb670c2..ef70d0422 100644 --- a/pkg/commands/git_commands/diff.go +++ b/pkg/commands/git_commands/diff.go @@ -146,7 +146,7 @@ func NewDiffCommands(gitCommon *GitCommon) *DiffCommands { func (self *DiffCommands) DiffCmdObj(diffArgs []string, mode DiffMode) *oscommands.CmdObj { return self.cmd.New( NewGitCmd("diff"). - Config("diff.noprefix=false"). + ParseableDiffPrefixes(). AddCommonDiffArgs(self.diffRendererConfigManager, self.UserConfig(), mode). Arg("--submodule"). Arg(fmt.Sprintf("--color=%s", mode.colorArg(self.diffRendererConfigManager))). @@ -188,7 +188,7 @@ func (self *DiffCommands) CustomPatchDiffCmdObj(dir string, mode DiffMode) *osco func (self *DiffCommands) GetDiff(staged bool, additionalArgs ...string) (string, error) { return self.cmd.New( NewGitCmd("diff"). - Config("diff.noprefix=false"). + ParseableDiffPrefixes(). Arg("--no-ext-diff", "--no-color"). ArgIf(staged, "--staged"). Dir(self.repoPaths.worktreePath). @@ -238,7 +238,7 @@ func (self *DiffCommands) OpenDiffToolCmdObj(opts DiffToolCmdOptions) *oscommand func (self *DiffCommands) DiffIndexCmdObj(diffArgs ...string) *oscommands.CmdObj { return self.cmd.New( NewGitCmd("diff-index"). - Config("diff.noprefix=false"). + ParseableDiffPrefixes(). Arg("--submodule", "--no-ext-diff", "--no-color", "--patch"). Arg(diffArgs...).ToArgv(), ) diff --git a/pkg/commands/git_commands/git_command_builder.go b/pkg/commands/git_commands/git_command_builder.go index a4bd7340e..40af4af08 100644 --- a/pkg/commands/git_commands/git_command_builder.go +++ b/pkg/commands/git_commands/git_command_builder.go @@ -144,6 +144,14 @@ func (self *GitCommandBuilder) NoLineEndingConversion() *GitCommandBuilder { Config("core.safecrlf=false") } +// ParseableDiffPrefixes makes git put a prefix in front of the paths of a diff +// even if the user has set diff.noprefix. We read the paths of the diffs we show +// out of their headers, and we build patches from them for `git apply`. Both +// rely on there being a prefix to strip. +func (self *GitCommandBuilder) ParseableDiffPrefixes() *GitCommandBuilder { + return self.Config("diff.noprefix=false") +} + func (self *GitCommandBuilder) AddCommonDiffArgs(diffRendererConfigManager *config.DiffRendererConfigManager, userConfig *config.UserConfig, mode DiffMode) *GitCommandBuilder { contextSize := userConfig.Git.DiffContextSize useExtDiff := mode == DiffModeRendered && diffRendererConfigManager.GetDiffRendererType() == config.DiffRendererType_ExtDiff diff --git a/pkg/commands/git_commands/patch.go b/pkg/commands/git_commands/patch.go index 823206c90..39f60478a 100644 --- a/pkg/commands/git_commands/patch.go +++ b/pkg/commands/git_commands/patch.go @@ -434,7 +434,7 @@ func (self *PatchCommands) PullPatchIntoNewCommitBefore( // get the diff of HEAD and the original commit and then apply that. func (self *PatchCommands) diffHeadAgainstCommit(commit *models.Commit) (string, error) { cmdArgs := NewGitCmd("diff"). - Config("diff.noprefix=false"). + ParseableDiffPrefixes(). Arg("--no-ext-diff", "--no-color"). Arg("HEAD.." + commit.Hash()). ToArgv() diff --git a/pkg/commands/git_commands/working_tree.go b/pkg/commands/git_commands/working_tree.go index 4f4a19a89..43960e252 100644 --- a/pkg/commands/git_commands/working_tree.go +++ b/pkg/commands/git_commands/working_tree.go @@ -425,7 +425,7 @@ func (self *WorkingTreeCommands) ShowFileDiff(from string, to string, reverse bo func (self *WorkingTreeCommands) ShowFileDiffCmdObj(from string, to string, reverse bool, fileNames []string, mode DiffMode) *oscommands.CmdObj { cmdArgs := NewGitCmd("diff"). - Config("diff.noprefix=false"). + ParseableDiffPrefixes(). AddCommonDiffArgs(self.diffRendererConfigManager, self.UserConfig(), mode). Arg("--submodule"). Arg(fmt.Sprintf("--color=%s", mode.colorArg(self.diffRendererConfigManager))).