mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-11-21 04:46:29 +03:00
When moving a custom patch to an earlier commit, we apply the patch to the earlier commit and then let the rebase pick the commit that the patch came from, unchanged. This relies on git noticing during the pick that the patch's changes are already there. If the patch contains only some of several adjacent added lines, git doesn't notice, and the pick stops with a conflict. If git can place the patch's added lines in more than one position, it may place them differently for the pick than for the patch. It then adds them a second time, without any conflict. Make the rebase pick a copy of the source commit instead. The copy has the same tree, author and message, but its parent is the original parent with the patch applied. Picking it only replays the source commit's changes minus the patch, so git no longer has to recognize the patch as already applied. Create the copy with plumbing commands and a temporary index; this doesn't touch the working tree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
77 lines
2.4 KiB
Go
77 lines
2.4 KiB
Go
package git_commands
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCommitObjectWithHeader(t *testing.T) {
|
|
scenarios := []struct {
|
|
name string
|
|
commitObject string
|
|
headerName string
|
|
value string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "replace parent",
|
|
commitObject: "tree 1111\nparent 2222\n" +
|
|
"author A U Thor <author@example.com> 1700000000 +0100\n" +
|
|
"committer C O Mitter <committer@example.com> 1700000001 +0100\n" +
|
|
"\nsubject\n\nbody\n",
|
|
headerName: "parent",
|
|
value: "3333",
|
|
expected: "tree 1111\nparent 3333\n" +
|
|
"author A U Thor <author@example.com> 1700000000 +0100\n" +
|
|
"committer C O Mitter <committer@example.com> 1700000001 +0100\n" +
|
|
"\nsubject\n\nbody\n",
|
|
},
|
|
{
|
|
name: "replace tree, keeping other headers",
|
|
commitObject: "tree 1111\nparent 2222\n" +
|
|
"author A U Thor <author@example.com> 1700000000 +0100\n" +
|
|
"committer C O Mitter <committer@example.com> 1700000001 +0100\n" +
|
|
"encoding ISO-8859-1\n" +
|
|
"\nsubject\n",
|
|
headerName: "tree",
|
|
value: "3333",
|
|
expected: "tree 3333\nparent 2222\n" +
|
|
"author A U Thor <author@example.com> 1700000000 +0100\n" +
|
|
"committer C O Mitter <committer@example.com> 1700000001 +0100\n" +
|
|
"encoding ISO-8859-1\n" +
|
|
"\nsubject\n",
|
|
},
|
|
{
|
|
name: "drop signature",
|
|
commitObject: "tree 1111\nparent 2222\n" +
|
|
"author A U Thor <author@example.com> 1700000000 +0100\n" +
|
|
"committer C O Mitter <committer@example.com> 1700000001 +0100\n" +
|
|
"gpgsig -----BEGIN PGP SIGNATURE-----\n \n iQEz\n -----END PGP SIGNATURE-----\n" +
|
|
"gpgsig-sha256 -----BEGIN SSH SIGNATURE-----\n U1NI\n -----END SSH SIGNATURE-----\n" +
|
|
"some-other-header value\n" +
|
|
"\nsubject\n",
|
|
headerName: "parent",
|
|
value: "3333",
|
|
expected: "tree 1111\nparent 3333\n" +
|
|
"author A U Thor <author@example.com> 1700000000 +0100\n" +
|
|
"committer C O Mitter <committer@example.com> 1700000001 +0100\n" +
|
|
"some-other-header value\n" +
|
|
"\nsubject\n",
|
|
},
|
|
{
|
|
name: "empty message",
|
|
commitObject: "tree 1111\nparent 2222\n\n",
|
|
headerName: "parent",
|
|
value: "3333",
|
|
expected: "tree 1111\nparent 3333\n\n",
|
|
},
|
|
}
|
|
|
|
for _, s := range scenarios {
|
|
t.Run(s.name, func(t *testing.T) {
|
|
assert.Equal(t, s.expected, commitObjectWithHeader(s.commitObject, s.headerName, s.value))
|
|
})
|
|
}
|
|
}
|