Skip to content

Commit cb807af

Browse files
committed
Use "git cherry-pick" for implementing copy/paste of commits
We do this because - it's closer to what you would do on the command line - it simplifies the code a bit - it will allow us to support cherry-picking merge commits.
1 parent 361b5f8 commit cb807af

File tree

4 files changed

+21
-59
lines changed

4 files changed

+21
-59
lines changed

Diff for: pkg/commands/git_commands/rebase.go

+8-28
Original file line numberDiff line numberDiff line change
@@ -533,35 +533,15 @@ func (self *RebaseCommands) DiscardOldFileChanges(commits []*models.Commit, comm
533533

534534
// CherryPickCommits begins an interactive rebase with the given hashes being cherry picked onto HEAD
535535
func (self *RebaseCommands) CherryPickCommits(commits []*models.Commit) error {
536-
commitLines := lo.Map(commits, func(commit *models.Commit, _ int) string {
537-
return fmt.Sprintf("%s %s", utils.ShortHash(commit.Hash), commit.Name)
538-
})
539-
msg := utils.ResolvePlaceholderString(
540-
self.Tr.Log.CherryPickCommits,
541-
map[string]string{
542-
"commitLines": strings.Join(commitLines, "\n"),
543-
},
544-
)
545-
self.os.LogCommand(msg, false)
546-
547-
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{
548-
baseHashOrRoot: "HEAD",
549-
instruction: daemon.NewCherryPickCommitsInstruction(commits),
550-
}).Run()
551-
}
552-
553-
// CherryPickCommitsDuringRebase simply prepends the given commits to the existing git-rebase-todo file
554-
func (self *RebaseCommands) CherryPickCommitsDuringRebase(commits []*models.Commit) error {
555-
todoLines := lo.Map(commits, func(commit *models.Commit, _ int) daemon.TodoLine {
556-
return daemon.TodoLine{
557-
Action: "pick",
558-
Commit: commit,
559-
}
560-
})
536+
hasMergeCommit := lo.SomeBy(commits, func(c *models.Commit) bool { return c.IsMerge() })
537+
cmdArgs := NewGitCmd("cherry-pick").
538+
Arg("--allow-empty").
539+
ArgIf(self.version.IsAtLeast(2, 45, 0), "--empty=keep", "--keep-redundant-commits").
540+
ArgIf(hasMergeCommit, "-m1").
541+
Arg(lo.Reverse(lo.Map(commits, func(c *models.Commit, _ int) string { return c.Hash }))...).
542+
ToArgv()
561543

562-
todo := daemon.TodoLinesToString(todoLines)
563-
filePath := filepath.Join(self.repoPaths.worktreeGitDirPath, "rebase-merge/git-rebase-todo")
564-
return utils.PrependStrToTodoFile(filePath, []byte(todo))
544+
return self.cmd.New(cmdArgs).Run()
565545
}
566546

567547
func (self *RebaseCommands) DropMergeCommit(commits []*models.Commit, commitIndex int) error {

Diff for: pkg/gui/controllers/helpers/cherry_pick_helper.go

+10-28
Original file line numberDiff line numberDiff line change
@@ -77,41 +77,23 @@ func (self *CherryPickHelper) Paste() error {
7777
"numCommits": strconv.Itoa(len(self.getData().CherryPickedCommits)),
7878
}),
7979
HandleConfirm: func() error {
80-
isInRebase, err := self.c.Git().Status.IsInRebase()
81-
if err != nil {
82-
return err
83-
}
84-
if isInRebase {
85-
if err := self.c.Git().Rebase.CherryPickCommitsDuringRebase(self.getData().CherryPickedCommits); err != nil {
86-
return err
87-
}
88-
err = self.c.Refresh(types.RefreshOptions{
89-
Mode: types.SYNC, Scope: []types.RefreshableView{types.REBASE_COMMITS},
90-
})
91-
if err != nil {
92-
return err
93-
}
94-
95-
return self.Reset()
96-
}
97-
9880
return self.c.WithWaitingStatus(self.c.Tr.CherryPickingStatus, func(gocui.Task) error {
9981
self.c.LogAction(self.c.Tr.Actions.CherryPick)
100-
err := self.c.Git().Rebase.CherryPickCommits(self.getData().CherryPickedCommits)
101-
err = self.rebaseHelper.CheckMergeOrRebase(err)
82+
result := self.c.Git().Rebase.CherryPickCommits(self.getData().CherryPickedCommits)
83+
err := self.rebaseHelper.CheckMergeOrRebase(result)
10284
if err != nil {
103-
return err
85+
return result
10486
}
10587

106-
// If we're in an interactive rebase at this point, it must
88+
// If we're in the cherry-picking state at this point, it must
10789
// be because there were conflicts. Don't clear the copied
108-
// commits in this case, since we might want to abort and
109-
// try pasting them again.
110-
isInRebase, err = self.c.Git().Status.IsInRebase()
111-
if err != nil {
112-
return err
90+
// commits in this case, since we might want to abort and try
91+
// pasting them again.
92+
isInCherryPick, result := self.c.Git().Status.IsInCherryPick()
93+
if result != nil {
94+
return result
11395
}
114-
if !isInRebase {
96+
if !isInCherryPick {
11597
self.getData().DidPaste = true
11698
self.rerender()
11799
}

Diff for: pkg/integration/tests/cherry_pick/cherry_pick_conflicts.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var CherryPickConflicts = NewIntegrationTest(NewIntegrationTestArgs{
6969
SelectNextItem().
7070
PressPrimaryAction()
7171

72-
t.Common().ContinueOnConflictsResolved("rebase")
72+
t.Common().ContinueOnConflictsResolved("cherry-pick")
7373

7474
t.Views().Files().IsEmpty()
7575

Diff for: pkg/integration/tests/cherry_pick/cherry_pick_during_rebase.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ var CherryPickDuringRebase = NewIntegrationTest(NewIntegrationTestArgs{
7575
}).
7676
Lines(
7777
Contains("pick CI two"),
78-
Contains("pick CI three"),
79-
Contains(" CI <-- YOU ARE HERE --- one"),
78+
Contains(" CI <-- YOU ARE HERE --- three"),
79+
Contains(" CI one"),
8080
Contains(" CI base"),
8181
).
8282
Tap(func() {

0 commit comments

Comments
 (0)