Skip to content

Commit 5149b24

Browse files
Merge pull request #2585 from stefanhaller/only-use-empty-arg-when-available
2 parents c520c5c + d607b36 commit 5149b24

File tree

9 files changed

+134
-25
lines changed

9 files changed

+134
-25
lines changed

pkg/commands/git_commands/rebase.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,18 @@ func (self *RebaseCommands) PrepareInteractiveRebaseCommand(opts PrepareInteract
181181
debug = "TRUE"
182182
}
183183

184+
emptyArg := " --empty=keep"
185+
if self.version.IsOlderThan(2, 26, 0) {
186+
emptyArg = ""
187+
}
188+
184189
rebaseMergesArg := " --rebase-merges"
185190
if self.version.IsOlderThan(2, 22, 0) {
186191
rebaseMergesArg = ""
187192
}
188-
cmdStr := fmt.Sprintf("git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash%s %s",
189-
rebaseMergesArg, opts.baseShaOrRoot)
193+
194+
cmdStr := fmt.Sprintf("git rebase --interactive --autostash --keep-empty%s --no-autosquash%s %s",
195+
emptyArg, rebaseMergesArg, opts.baseShaOrRoot)
190196
self.Log.WithField("command", cmdStr).Debug("RunCommand")
191197

192198
cmdObj := self.cmd.New(cmdStr)

pkg/commands/git_commands/rebase_test.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,60 @@ import (
1616

1717
func TestRebaseRebaseBranch(t *testing.T) {
1818
type scenario struct {
19-
testName string
20-
arg string
21-
runner *oscommands.FakeCmdObjRunner
22-
test func(error)
19+
testName string
20+
arg string
21+
gitVersion *GitVersion
22+
runner *oscommands.FakeCmdObjRunner
23+
test func(error)
2324
}
2425

2526
scenarios := []scenario{
2627
{
27-
testName: "successful rebase",
28-
arg: "master",
28+
testName: "successful rebase",
29+
arg: "master",
30+
gitVersion: &GitVersion{2, 26, 0, ""},
2931
runner: oscommands.NewFakeRunner(t).
30-
Expect(`git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash master`, "", nil),
32+
Expect(`git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash --rebase-merges master`, "", nil),
3133
test: func(err error) {
3234
assert.NoError(t, err)
3335
},
3436
},
3537
{
36-
testName: "unsuccessful rebase",
37-
arg: "master",
38+
testName: "unsuccessful rebase",
39+
arg: "master",
40+
gitVersion: &GitVersion{2, 26, 0, ""},
3841
runner: oscommands.NewFakeRunner(t).
39-
Expect(`git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash master`, "", errors.New("error")),
42+
Expect(`git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash --rebase-merges master`, "", errors.New("error")),
4043
test: func(err error) {
4144
assert.Error(t, err)
4245
},
4346
},
47+
{
48+
testName: "successful rebase (< 2.26.0)",
49+
arg: "master",
50+
gitVersion: &GitVersion{2, 25, 5, ""},
51+
runner: oscommands.NewFakeRunner(t).
52+
Expect(`git rebase --interactive --autostash --keep-empty --no-autosquash --rebase-merges master`, "", nil),
53+
test: func(err error) {
54+
assert.NoError(t, err)
55+
},
56+
},
57+
{
58+
testName: "successful rebase (< 2.22.0)",
59+
arg: "master",
60+
gitVersion: &GitVersion{2, 21, 9, ""},
61+
runner: oscommands.NewFakeRunner(t).
62+
Expect(`git rebase --interactive --autostash --keep-empty --no-autosquash master`, "", nil),
63+
test: func(err error) {
64+
assert.NoError(t, err)
65+
},
66+
},
4467
}
4568

4669
for _, s := range scenarios {
4770
s := s
4871
t.Run(s.testName, func(t *testing.T) {
49-
instance := buildRebaseCommands(commonDeps{runner: s.runner})
72+
instance := buildRebaseCommands(commonDeps{runner: s.runner, gitVersion: s.gitVersion})
5073
s.test(instance.RebaseBranch(s.arg))
5174
})
5275
}
@@ -126,7 +149,7 @@ func TestRebaseDiscardOldFileChanges(t *testing.T) {
126149
commitIndex: 0,
127150
fileName: "test999.txt",
128151
runner: oscommands.NewFakeRunner(t).
129-
Expect(`git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash abcdef`, "", nil).
152+
Expect(`git rebase --interactive --autostash --keep-empty --empty=keep --no-autosquash --rebase-merges abcdef`, "", nil).
130153
Expect(`git cat-file -e HEAD^:"test999.txt"`, "", nil).
131154
Expect(`git checkout HEAD^ -- "test999.txt"`, "", nil).
132155
Expect(`git commit --amend --no-edit --allow-empty`, "", nil).
@@ -143,8 +166,9 @@ func TestRebaseDiscardOldFileChanges(t *testing.T) {
143166
s := s
144167
t.Run(s.testName, func(t *testing.T) {
145168
instance := buildRebaseCommands(commonDeps{
146-
runner: s.runner,
147-
gitConfig: git_config.NewFakeGitConfig(s.gitConfigMockResponses),
169+
runner: s.runner,
170+
gitVersion: &GitVersion{2, 26, 0, ""},
171+
gitConfig: git_config.NewFakeGitConfig(s.gitConfigMockResponses),
148172
})
149173

150174
s.test(instance.DiscardOldFileChanges(s.commits, s.commitIndex, s.fileName))

pkg/integration/components/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type GitVersionRestriction struct {
6060
}
6161

6262
// Verifies the version is at least the given version (inclusive)
63-
func From(version string) GitVersionRestriction {
63+
func AtLeast(version string) GitVersionRestriction {
6464
return GitVersionRestriction{from: version}
6565
}
6666

pkg/integration/components/test_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,18 @@ func TestGitVersionRestriction(t *testing.T) {
9696
expectedShouldRun bool
9797
}{
9898
{
99-
testName: "From, current is newer",
100-
gitVersion: From("2.24.9"),
99+
testName: "AtLeast, current is newer",
100+
gitVersion: AtLeast("2.24.9"),
101101
expectedShouldRun: true,
102102
},
103103
{
104-
testName: "From, current is same",
105-
gitVersion: From("2.25.0"),
104+
testName: "AtLeast, current is same",
105+
gitVersion: AtLeast("2.25.0"),
106106
expectedShouldRun: true,
107107
},
108108
{
109-
testName: "From, current is older",
110-
gitVersion: From("2.26.0"),
109+
testName: "AtLeast, current is older",
110+
gitVersion: AtLeast("2.26.0"),
111111
expectedShouldRun: false,
112112
},
113113
{

pkg/integration/tests/interactive_rebase/drop_todo_commit_with_update_ref.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var DropTodoCommitWithUpdateRef = NewIntegrationTest(NewIntegrationTestArgs{
99
Description: "Drops a commit during interactive rebase when there is an update-ref in the git-rebase-todo file",
1010
ExtraCmdArgs: "",
1111
Skip: false,
12-
GitVersion: From("2.38.0"),
12+
GitVersion: AtLeast("2.38.0"),
1313
SetupConfig: func(config *config.AppConfig) {},
1414
SetupRepo: func(shell *Shell) {
1515
shell.

pkg/integration/tests/interactive_rebase/drop_todo_commit_with_update_ref_show_branch_heads.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var DropTodoCommitWithUpdateRefShowBranchHeads = NewIntegrationTest(NewIntegrati
99
Description: "Drops a commit during interactive rebase when there is an update-ref in the git-rebase-todo file (with experimentalShowBranchHeads on)",
1010
ExtraCmdArgs: "",
1111
Skip: false,
12-
GitVersion: From("2.38.0"),
12+
GitVersion: AtLeast("2.38.0"),
1313
SetupConfig: func(config *config.AppConfig) {
1414
config.UserConfig.Gui.ExperimentalShowBranchHeads = true
1515
},

pkg/integration/tests/patch_building/move_to_earlier_commit.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var MoveToEarlierCommit = NewIntegrationTest(NewIntegrationTestArgs{
99
Description: "Move a patch from a commit to an earlier commit",
1010
ExtraCmdArgs: "",
1111
Skip: false,
12+
GitVersion: AtLeast("2.26.0"),
1213
SetupConfig: func(config *config.AppConfig) {},
1314
SetupRepo: func(shell *Shell) {
1415
shell.CreateDir("dir")
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package patch_building
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var MoveToEarlierCommitNoKeepEmpty = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Move a patch from a commit to an earlier commit, for older git versions that don't keep the empty commit",
10+
ExtraCmdArgs: "",
11+
Skip: false,
12+
GitVersion: Before("2.26.0"),
13+
SetupConfig: func(config *config.AppConfig) {},
14+
SetupRepo: func(shell *Shell) {
15+
shell.CreateDir("dir")
16+
shell.CreateFileAndAdd("dir/file1", "file1 content")
17+
shell.CreateFileAndAdd("dir/file2", "file2 content")
18+
shell.Commit("first commit")
19+
20+
shell.CreateFileAndAdd("unrelated-file", "")
21+
shell.Commit("destination commit")
22+
23+
shell.UpdateFileAndAdd("dir/file1", "file1 content with old changes")
24+
shell.DeleteFileAndAdd("dir/file2")
25+
shell.CreateFileAndAdd("dir/file3", "file3 content")
26+
shell.Commit("commit to move from")
27+
},
28+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
29+
t.Views().Commits().
30+
Focus().
31+
Lines(
32+
Contains("commit to move from").IsSelected(),
33+
Contains("destination commit"),
34+
Contains("first commit"),
35+
).
36+
PressEnter()
37+
38+
t.Views().CommitFiles().
39+
IsFocused().
40+
Lines(
41+
Contains("dir").IsSelected(),
42+
Contains(" M file1"),
43+
Contains(" D file2"),
44+
Contains(" A file3"),
45+
).
46+
PressPrimaryAction().
47+
PressEscape()
48+
49+
t.Views().Information().Content(Contains("building patch"))
50+
51+
t.Views().Commits().
52+
IsFocused().
53+
SelectNextItem()
54+
55+
t.Common().SelectPatchOption(Contains("move patch to selected commit"))
56+
57+
t.Views().Commits().
58+
IsFocused().
59+
Lines(
60+
Contains("destination commit"),
61+
Contains("first commit").IsSelected(),
62+
).
63+
SelectPreviousItem().
64+
PressEnter()
65+
66+
t.Views().CommitFiles().
67+
IsFocused().
68+
Lines(
69+
Contains("dir").IsSelected(),
70+
Contains(" M file1"),
71+
Contains(" D file2"),
72+
Contains(" A file3"),
73+
Contains("A unrelated-file"),
74+
).
75+
PressEscape()
76+
},
77+
})

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ var tests = []*components.IntegrationTest{
117117
patch_building.ApplyInReverseWithConflict,
118118
patch_building.CopyPatchToClipboard,
119119
patch_building.MoveToEarlierCommit,
120+
patch_building.MoveToEarlierCommitNoKeepEmpty,
120121
patch_building.MoveToIndex,
121122
patch_building.MoveToIndexPartOfAdjacentAddedLines,
122123
patch_building.MoveToIndexPartial,

0 commit comments

Comments
 (0)