Skip to content

Commit d1d2bb2

Browse files
authored
Show fixup base commits in correct order in ctrl-f error message (#5073)
If the ctrl-f command ("Find base commit for fixup") finds multiple candidates, it lists them all in an error message. Previously they were listed in random order in the error message, which can be confusing; it's nicer to see them in the same order in which they appear in the commit log. Fixes #5071.
2 parents 28cd1c2 + 26453b2 commit d1d2bb2

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

pkg/commands/git_commands/commit.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,20 +217,6 @@ func (self *CommitCommands) GetCommitMessagesFirstLine(hashes []string) (string,
217217
return self.cmd.New(cmdArgs).DontLog().RunWithOutput()
218218
}
219219

220-
// Example output:
221-
//
222-
// cd50c79ae Preserve the commit message correctly even if the description has blank lines
223-
// 3ebba5f32 Add test demonstrating a bug with preserving the commit message
224-
// 9a423c388 Remove unused function
225-
func (self *CommitCommands) GetHashesAndCommitMessagesFirstLine(hashes []string) (string, error) {
226-
cmdArgs := NewGitCmd("show").
227-
Arg("--no-patch", "--pretty=format:%h %s").
228-
Arg(hashes...).
229-
ToArgv()
230-
231-
return self.cmd.New(cmdArgs).DontLog().RunWithOutput()
232-
}
233-
234220
func (self *CommitCommands) GetCommitsOneline(hashes []string) (string, error) {
235221
cmdArgs := NewGitCmd("show").
236222
Arg("--no-patch", "--oneline").

pkg/gui/controllers/helpers/fixup_helper.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,7 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error {
114114
// If there are multiple commits that could be the base commit, list
115115
// them in the error message. But only the candidates from the current
116116
// branch, not including any that are already merged.
117-
subjects, err := self.c.Git().Commit.GetHashesAndCommitMessagesFirstLine(hashGroups[NOT_MERGED])
118-
if err != nil {
119-
return err
120-
}
117+
subjects := self.getHashesAndSubjects(commits, hashGroups[NOT_MERGED])
121118
message := lo.Ternary(hasStagedChanges,
122119
self.c.Tr.MultipleBaseCommitsFoundStaged,
123120
self.c.Tr.MultipleBaseCommitsFoundUnstaged)
@@ -146,6 +143,23 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error {
146143
})
147144
}
148145

146+
func (self *FixupHelper) getHashesAndSubjects(commits []*models.Commit, hashes []string) string {
147+
// This is called only for the NOT_MERGED commits, and we know that all of them are contained in
148+
// the commits slice.
149+
commitsSet := set.NewFromSlice(hashes)
150+
subjects := make([]string, 0, len(hashes))
151+
for _, c := range commits {
152+
if commitsSet.Includes(c.Hash()) {
153+
subjects = append(subjects, fmt.Sprintf("%s %s", c.ShortRefName(), c.Name))
154+
commitsSet.Remove(c.Hash())
155+
if commitsSet.Len() == 0 {
156+
break
157+
}
158+
}
159+
}
160+
return strings.Join(subjects, "\n")
161+
}
162+
149163
func (self *FixupHelper) getDiff() (string, bool, error) {
150164
args := []string{"-U0", "--ignore-submodules=all", "HEAD", "--"}
151165

pkg/integration/tests/commit/find_base_commit_for_fixup.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ var FindBaseCommitForFixup = NewIntegrationTest(NewIntegrationTestArgs{
3636
t.ExpectPopup().Alert().
3737
Title(Equals("Error")).
3838
Content(
39-
Contains("Multiple base commits found").
40-
Contains("2nd commit").
41-
Contains("3rd commit"),
39+
MatchesRegexp("Multiple base commits found.*\n\n" +
40+
".*3rd commit\n" +
41+
".*2nd commit"),
4242
).
4343
Confirm()
4444

pkg/integration/tests/commit/find_base_commit_for_fixup_only_added_lines.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ var FindBaseCommitForFixupOnlyAddedLines = NewIntegrationTest(NewIntegrationTest
3939
t.ExpectPopup().Alert().
4040
Title(Equals("Error")).
4141
Content(
42-
Contains("Multiple base commits found").
43-
Contains("3rd commit").
44-
Contains("4th commit"),
42+
MatchesRegexp(
43+
"Multiple base commits found.*\n\n" +
44+
".*4th commit\n" +
45+
".*3rd commit",
46+
),
4547
).
4648
Confirm()
4749

0 commit comments

Comments
 (0)