mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-11-06 13:00:59 -04:00
Avoid conflicts and duplicate lines when moving a patch into a new commit before
Moving a custom patch into a new commit before the original one has the same problem as moving it to an earlier commit. After committing the patch, the rebase picks the original commit unchanged and relies on git noticing that the patch is already there. Make the rebase pick a copy of the original commit whose parent already contains the patch, as we do when moving to an earlier commit. Here, the copy's parent normally has the same tree as the new commit, so picking the copy doesn't conflict. Only a commit hook that changes the new commit's files could make it conflict. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
507b65065f
commit
16f51df6e3
@@ -419,6 +419,11 @@ func (self *PatchCommands) PullPatchIntoNewCommitBefore(
|
||||
return err
|
||||
}
|
||||
|
||||
if err := self.pickCopyWithPatchInParent(commits[commitIdx]); err != nil {
|
||||
_ = self.rebase.AbortRebase()
|
||||
return err
|
||||
}
|
||||
|
||||
if err := self.ApplyCustomPatch(false, false); err != nil {
|
||||
_ = self.rebase.AbortRebase()
|
||||
return err
|
||||
@@ -432,14 +437,15 @@ func (self *PatchCommands) PullPatchIntoNewCommitBefore(
|
||||
return self.rebase.ContinueRebase()
|
||||
}
|
||||
|
||||
// When moving the patch to an earlier commit, we apply it to that commit, and
|
||||
// the rebase then picks the commit that the patch was built from. Picking that
|
||||
// commit unchanged would rely on git noticing that the patch is already
|
||||
// applied, and git doesn't always notice this. If the patch contains only some
|
||||
// of several adjacent added lines, the pick conflicts; if git can place the
|
||||
// patch's added lines in more than one position, it may add them a second
|
||||
// time. So we make the rebase pick a copy of the commit instead, whose parent
|
||||
// already contains the patch.
|
||||
// When moving the patch to an earlier commit, or into a new commit before the
|
||||
// one it was built from, we first apply it there, and the rebase then picks
|
||||
// the commit that the patch was built from. Picking that commit unchanged
|
||||
// would rely on git noticing that the patch is already applied, and git
|
||||
// doesn't always notice this. If the patch contains only some of several
|
||||
// adjacent added lines, the pick conflicts; if git can place the patch's added
|
||||
// lines in more than one position, it may add them a second time. So we make
|
||||
// the rebase pick a copy of the commit instead, whose parent already contains
|
||||
// the patch.
|
||||
func (self *PatchCommands) pickCopyWithPatchInParent(commit *models.Commit) error {
|
||||
copyHash, err := self.copyCommitWithPatchInParent(commit)
|
||||
if err != nil {
|
||||
|
||||
-3
@@ -62,10 +62,7 @@ var MoveAmbiguouslyPlacedAddedLinesToNewCommitBefore = NewIntegrationTest(NewInt
|
||||
|
||||
t.FileSystem().FileContent(
|
||||
"file1",
|
||||
/* EXPECTED:
|
||||
Equals("first line\nextra line\n}\nadded line\n}\nlast line\n"),
|
||||
ACTUAL: */
|
||||
Equals("first line\nextra line\n}\nadded line\n}\nadded line\n}\nlast line\n"),
|
||||
)
|
||||
},
|
||||
})
|
||||
|
||||
-9
@@ -49,7 +49,6 @@ var MovePartOfAdjacentAddedLinesToNewCommitBefore = NewIntegrationTest(NewIntegr
|
||||
|
||||
t.Views().Commits().
|
||||
IsFocused().
|
||||
/* EXPECTED:
|
||||
Lines(
|
||||
Contains("commit to move from").IsSelected(),
|
||||
Contains("new commit"),
|
||||
@@ -62,13 +61,5 @@ var MovePartOfAdjacentAddedLinesToNewCommitBefore = NewIntegrationTest(NewIntegr
|
||||
Tap(func() {
|
||||
t.Views().Main().Content(Contains("+1st line").DoesNotContain("2nd line"))
|
||||
})
|
||||
ACTUAL: */
|
||||
Lines(
|
||||
Contains("─── Pending rebase todos"),
|
||||
Contains("pick").Contains("<-- CONFLICT --- commit to move from"),
|
||||
Contains("─── Commits"),
|
||||
Contains("new commit"),
|
||||
Contains("first commit"),
|
||||
)
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user