Files
Stefan HallerandClaude Opus 4.8 f84ada4941 Show renamed files in the custom patch builder
When loading the files of a commit we passed --no-renames, so a rename
showed up as a separate delete and add rather than a single R entry.
That made it impossible to work with a rename that also modifies the
file: the modifications were spread across a full deletion and a full
addition instead of appearing as the handful of lines that actually
changed. The staging view already shows renames and lets you stage
their hunks, so there was no good reason for the patch builder to
differ; the flag was only there because the commit-file parser couldn't
cope with the rename record format.

Switch the commit-file loader and the per-file diff to --find-renames,
teach the parser about the rename record (a status followed by two
paths), and carry the previous path through the patch builder so the
diff for a rename is loaded with both paths, which is what makes git
emit the rename in the first place.

A whole-file selection keeps the rename in the header, so the rename
moves or is discarded together with the file's contents. A partial
selection instead strips the rename metadata and points the header at
the new path, so applying the patch only changes the contents and
leaves the rename in place; the blob index line is kept so that a 3-way
apply can still fall back to a blob merge.

Discarding a renamed file from a commit now discards both the new and
the old path, so the new file is removed and the old one is restored.

Changing the rename similarity threshold refreshes the commit files
panel too, not just the files panel, so that a rename can turn into a
delete and add or back. It is disabled while building a patch, however,
because the patch builder caches each file's diff by path and would
desync if a rename changed into a delete and add underneath it.

Finally, copying a file's diff from the commit files panel now passes
both paths for a rename, so the copied diff shows the rename instead of
a new-file add.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-04 13:05:09 +02:00

74 lines
2.1 KiB
Go

package patch_building
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var RenamedFilePartial = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Select part of a renamed file's changes into a custom patch and remove it from the commit, keeping the rename in place",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.CreateFileAndAdd("original", "line1\nline2\nline3\nline4\nline5\n")
shell.Commit("first commit")
shell.RenameFileInGit("original", "renamed")
shell.UpdateFileAndAdd("renamed", "line1\nline2 changed\nline3\nline4\nline5\n")
shell.Commit("rename with modification")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Commits().
Focus().
Lines(
Contains("rename with modification").IsSelected(),
Contains("first commit"),
).
PressEnter()
t.Views().CommitFiles().
IsFocused().
Lines(
Contains("original → renamed").IsSelected(),
).
PressEnter()
// The main view shows the rename together with its content change.
t.Views().PatchBuilding().
IsFocused().
Content(Contains("rename from original").Contains("rename to renamed")).
ContainsLines(
Contains(" line1"),
Contains("-line2"),
Contains("+line2 changed"),
Contains(" line3"),
).
// Add the hunk (a line selection, as opposed to adding the whole
// file), so this is a partial patch.
PressPrimaryAction()
t.Views().Information().Content(Contains("Building patch"))
t.Common().SelectPatchOption(Contains("Remove patch from original commit"))
// The rename is preserved; only the content change is gone, so the file
// is still shown as a rename but now has no content change.
t.Views().CommitFiles().
IsFocused().
Lines(
Contains("original → renamed").IsSelected(),
)
t.Views().Main().
Content(DoesNotContain("line2 changed"))
t.Views().Commits().
Focus().
Lines(
Contains("rename with modification").IsSelected(),
Contains("first commit"),
)
},
})