1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-21 00:30:00 +02:00

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.
This commit is contained in:
Stefan Haller
2024-06-18 19:32:27 +02:00
parent 05b3ae9524
commit 4b35434eba
4 changed files with 21 additions and 59 deletions

View File

@ -533,35 +533,15 @@ func (self *RebaseCommands) DiscardOldFileChanges(commits []*models.Commit, comm
// CherryPickCommits begins an interactive rebase with the given hashes being cherry picked onto HEAD // CherryPickCommits begins an interactive rebase with the given hashes being cherry picked onto HEAD
func (self *RebaseCommands) CherryPickCommits(commits []*models.Commit) error { func (self *RebaseCommands) CherryPickCommits(commits []*models.Commit) error {
commitLines := lo.Map(commits, func(commit *models.Commit, _ int) string { hasMergeCommit := lo.SomeBy(commits, func(c *models.Commit) bool { return c.IsMerge() })
return fmt.Sprintf("%s %s", utils.ShortHash(commit.Hash), commit.Name) cmdArgs := NewGitCmd("cherry-pick").
}) Arg("--allow-empty").
msg := utils.ResolvePlaceholderString( ArgIf(self.version.IsAtLeast(2, 45, 0), "--empty=keep", "--keep-redundant-commits").
self.Tr.Log.CherryPickCommits, ArgIf(hasMergeCommit, "-m1").
map[string]string{ Arg(lo.Reverse(lo.Map(commits, func(c *models.Commit, _ int) string { return c.Hash }))...).
"commitLines": strings.Join(commitLines, "\n"), ToArgv()
},
)
self.os.LogCommand(msg, false)
return self.PrepareInteractiveRebaseCommand(PrepareInteractiveRebaseCommandOpts{ return self.cmd.New(cmdArgs).Run()
baseHashOrRoot: "HEAD",
instruction: daemon.NewCherryPickCommitsInstruction(commits),
}).Run()
}
// CherryPickCommitsDuringRebase simply prepends the given commits to the existing git-rebase-todo file
func (self *RebaseCommands) CherryPickCommitsDuringRebase(commits []*models.Commit) error {
todoLines := lo.Map(commits, func(commit *models.Commit, _ int) daemon.TodoLine {
return daemon.TodoLine{
Action: "pick",
Commit: commit,
}
})
todo := daemon.TodoLinesToString(todoLines)
filePath := filepath.Join(self.repoPaths.worktreeGitDirPath, "rebase-merge/git-rebase-todo")
return utils.PrependStrToTodoFile(filePath, []byte(todo))
} }
func (self *RebaseCommands) DropMergeCommit(commits []*models.Commit, commitIndex int) error { func (self *RebaseCommands) DropMergeCommit(commits []*models.Commit, commitIndex int) error {

View File

@ -77,41 +77,23 @@ func (self *CherryPickHelper) Paste() error {
"numCommits": strconv.Itoa(len(self.getData().CherryPickedCommits)), "numCommits": strconv.Itoa(len(self.getData().CherryPickedCommits)),
}), }),
HandleConfirm: func() error { HandleConfirm: func() error {
isInRebase, err := self.c.Git().Status.IsInRebase()
if err != nil {
return err
}
if isInRebase {
if err := self.c.Git().Rebase.CherryPickCommitsDuringRebase(self.getData().CherryPickedCommits); err != nil {
return err
}
err = self.c.Refresh(types.RefreshOptions{
Mode: types.SYNC, Scope: []types.RefreshableView{types.REBASE_COMMITS},
})
if err != nil {
return err
}
return self.Reset()
}
return self.c.WithWaitingStatus(self.c.Tr.CherryPickingStatus, func(gocui.Task) error { return self.c.WithWaitingStatus(self.c.Tr.CherryPickingStatus, func(gocui.Task) error {
self.c.LogAction(self.c.Tr.Actions.CherryPick) self.c.LogAction(self.c.Tr.Actions.CherryPick)
err := self.c.Git().Rebase.CherryPickCommits(self.getData().CherryPickedCommits) result := self.c.Git().Rebase.CherryPickCommits(self.getData().CherryPickedCommits)
err = self.rebaseHelper.CheckMergeOrRebase(err) err := self.rebaseHelper.CheckMergeOrRebase(result)
if err != nil { if err != nil {
return err return result
} }
// If we're in an interactive rebase at this point, it must // If we're in the cherry-picking state at this point, it must
// be because there were conflicts. Don't clear the copied // be because there were conflicts. Don't clear the copied
// commits in this case, since we might want to abort and // commits in this case, since we might want to abort and try
// try pasting them again. // pasting them again.
isInRebase, err = self.c.Git().Status.IsInRebase() isInCherryPick, result := self.c.Git().Status.IsInCherryPick()
if err != nil { if result != nil {
return err return result
} }
if !isInRebase { if !isInCherryPick {
self.getData().DidPaste = true self.getData().DidPaste = true
self.rerender() self.rerender()
} }

View File

@ -69,7 +69,7 @@ var CherryPickConflicts = NewIntegrationTest(NewIntegrationTestArgs{
SelectNextItem(). SelectNextItem().
PressPrimaryAction() PressPrimaryAction()
t.Common().ContinueOnConflictsResolved("rebase") t.Common().ContinueOnConflictsResolved("cherry-pick")
t.Views().Files().IsEmpty() t.Views().Files().IsEmpty()

View File

@ -75,8 +75,8 @@ var CherryPickDuringRebase = NewIntegrationTest(NewIntegrationTestArgs{
}). }).
Lines( Lines(
Contains("pick CI two"), Contains("pick CI two"),
Contains("pick CI three"), Contains(" CI <-- YOU ARE HERE --- three"),
Contains(" CI <-- YOU ARE HERE --- one"), Contains(" CI one"),
Contains(" CI base"), Contains(" CI base"),
). ).
Tap(func() { Tap(func() {