Skip to content

Commit 9547e8c

Browse files
committed
Add cherry-pick abort/quit/in-progress git primitives
Introduce IsCherryPickInProgress() (detects .git/CHERRY_PICK_HEAD) and split the existing cherry-pick reset into two distinct operations: - CherryPickQuit() runs `git cherry-pick --quit`, clearing the sequencer state without touching the index (used to clear stale state before starting a fresh cherry-pick). - CherryPickAbort() now runs `git cherry-pick --abort`, which fully restores the working tree and index to the pre-cherry-pick state. The previous CherryPickAbort() ran --quit, which leaves an unmerged index and therefore cannot recover a conflicted fold-down. Integration tests cover the in-progress detection, the full abort restore, and the --quit-leaves-index behavior.
1 parent 45472f0 commit 9547e8c

4 files changed

Lines changed: 204 additions & 55 deletions

File tree

‎internal/git/git.go‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,23 @@ func CherryPick(commits []string) error {
399399
return ops.CherryPick(commits)
400400
}
401401

402-
// CherryPickAbort clears any in-progress cherry-pick state.
403-
// Errors are silently ignored (no-op if no cherry-pick is in progress).
404-
func CherryPickAbort() {
405-
_ = ops.CherryPickAbort()
402+
// CherryPickQuit clears any in-progress cherry-pick sequencer state without
403+
// touching the working tree or index. Errors are silently ignored (no-op if no
404+
// cherry-pick is in progress).
405+
func CherryPickQuit() {
406+
_ = ops.CherryPickQuit()
407+
}
408+
409+
// CherryPickAbort cancels an in-progress cherry-pick, restoring the working
410+
// tree and index to the state before the cherry-pick began. Callers should
411+
// gate this with IsCherryPickInProgress, as it errors when none is in progress.
412+
func CherryPickAbort() error {
413+
return ops.CherryPickAbort()
414+
}
415+
416+
// IsCherryPickInProgress reports whether a cherry-pick is currently in progress.
417+
func IsCherryPickInProgress() bool {
418+
return ops.IsCherryPickInProgress()
406419
}
407420

408421
// CherryPickContinue continues an in-progress cherry-pick after conflicts are resolved.

‎internal/git/gitops.go‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ type Ops interface {
6969
ValidateRefName(name string) error
7070
RenameBranch(oldName, newName string) error
7171
CherryPick(commits []string) error
72+
CherryPickQuit() error
7273
CherryPickAbort() error
7374
CherryPickContinue() error
75+
IsCherryPickInProgress() bool
7476
HasUncommittedChanges() (bool, error)
7577
LogMerges(base, head string) ([]CommitInfo, error)
7678
}
@@ -612,16 +614,40 @@ func (d *defaultOps) CherryPick(commits []string) error {
612614
return runSilent(args...)
613615
}
614616

615-
func (d *defaultOps) CherryPickAbort() error {
617+
// CherryPickQuit clears the in-progress cherry-pick sequencer state without
618+
// touching the working tree or index (git cherry-pick --quit). Used to clear
619+
// any stale sequencer state before starting a fresh cherry-pick.
620+
func (d *defaultOps) CherryPickQuit() error {
616621
return runSilent("cherry-pick", "--quit")
617622
}
618623

624+
// CherryPickAbort cancels an in-progress cherry-pick and restores the working
625+
// tree and index to the state before the cherry-pick began
626+
// (git cherry-pick --abort). Errors if no cherry-pick is in progress, so
627+
// callers should gate this with IsCherryPickInProgress.
628+
func (d *defaultOps) CherryPickAbort() error {
629+
return runSilent("cherry-pick", "--abort")
630+
}
631+
619632
func (d *defaultOps) CherryPickContinue() error {
620633
cmd := exec.Command("git", "cherry-pick", "--continue")
621634
cmd.Env = append(os.Environ(), "GIT_EDITOR=true")
622635
return cmd.Run()
623636
}
624637

638+
// IsCherryPickInProgress reports whether a cherry-pick is currently in progress
639+
// by checking for the CHERRY_PICK_HEAD marker in the git directory.
640+
func (d *defaultOps) IsCherryPickInProgress() bool {
641+
gitDir, err := GitDir()
642+
if err != nil {
643+
return false
644+
}
645+
if _, err := os.Stat(filepath.Join(gitDir, "CHERRY_PICK_HEAD")); err == nil {
646+
return true
647+
}
648+
return false
649+
}
650+
625651
func (d *defaultOps) HasUncommittedChanges() (bool, error) {
626652
out, err := run("status", "--porcelain")
627653
if err != nil {

‎internal/git/gitops_test.go‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,89 @@ func TestIntegration_SaveAndGetRemote(t *testing.T) {
581581
_, err = GetSavedRemote()
582582
require.Error(t, err)
583583
}
584+
585+
// ---------------------------------------------------------------------------
586+
// Integration tests for cherry-pick in-progress detection and abort
587+
// ---------------------------------------------------------------------------
588+
589+
// A conflicting cherry-pick must be detected as in-progress, and CherryPickAbort
590+
// must fully restore the working tree/index so branch checkouts succeed again.
591+
// This underpins modify's --abort recovery for fold-down (cherry-pick) conflicts.
592+
func TestIntegration_CherryPickInProgressAndAbort(t *testing.T) {
593+
_, cloneDir := setupBareAndClone(t)
594+
restore := withGitDir(t, cloneDir)
595+
defer restore()
596+
597+
// Ensure runSilent-based git commands have a committer identity.
598+
gitExec(t, cloneDir, "config", "user.name", "Test")
599+
gitExec(t, cloneDir, "config", "user.email", "test@test.com")
600+
601+
// feature edits conflict.txt one way; main edits it another way.
602+
gitExec(t, cloneDir, "checkout", "-b", "feature")
603+
writeFile(t, cloneDir, "conflict.txt", "feature change\n")
604+
gitExec(t, cloneDir, "add", ".")
605+
gitExec(t, cloneDir, "commit", "-m", "feature edit")
606+
featureSHA := gitExec(t, cloneDir, "rev-parse", "feature")
607+
608+
gitExec(t, cloneDir, "checkout", "main")
609+
writeFile(t, cloneDir, "conflict.txt", "main change\n")
610+
gitExec(t, cloneDir, "add", ".")
611+
gitExec(t, cloneDir, "commit", "-m", "main edit")
612+
613+
// No cherry-pick in progress before we start.
614+
assert.False(t, IsCherryPickInProgress(), "no cherry-pick should be in progress initially")
615+
616+
// Cherry-picking feature onto main conflicts.
617+
err := CherryPick([]string{featureSHA})
618+
require.Error(t, err, "cherry-pick should conflict")
619+
assert.True(t, IsCherryPickInProgress(), "cherry-pick should be in progress after a conflict")
620+
621+
// While mid-conflict, a plain checkout must fail (unmerged index).
622+
_, coErr := gitExecMayFail(t, cloneDir, "checkout", "feature")
623+
require.Error(t, coErr, "checkout should fail while cherry-pick index is unmerged")
624+
625+
// Aborting must fully restore: no longer in progress, clean tree, checkout works.
626+
require.NoError(t, CherryPickAbort())
627+
assert.False(t, IsCherryPickInProgress(), "cherry-pick should not be in progress after abort")
628+
629+
status, err := gitExecMayFail(t, cloneDir, "status", "--porcelain")
630+
require.NoError(t, err)
631+
assert.Empty(t, status, "working tree should be clean after abort")
632+
633+
_, coErr = gitExecMayFail(t, cloneDir, "checkout", "feature")
634+
require.NoError(t, coErr, "checkout should succeed after abort restores a clean index")
635+
}
636+
637+
// CherryPickQuit clears the sequencer state but intentionally leaves the index
638+
// as-is, so a plain checkout still fails. This documents why Unwind uses the
639+
// full --abort rather than --quit.
640+
func TestIntegration_CherryPickQuitLeavesIndexUnmerged(t *testing.T) {
641+
_, cloneDir := setupBareAndClone(t)
642+
restore := withGitDir(t, cloneDir)
643+
defer restore()
644+
645+
gitExec(t, cloneDir, "config", "user.name", "Test")
646+
gitExec(t, cloneDir, "config", "user.email", "test@test.com")
647+
648+
gitExec(t, cloneDir, "checkout", "-b", "feature")
649+
writeFile(t, cloneDir, "conflict.txt", "feature change\n")
650+
gitExec(t, cloneDir, "add", ".")
651+
gitExec(t, cloneDir, "commit", "-m", "feature edit")
652+
featureSHA := gitExec(t, cloneDir, "rev-parse", "feature")
653+
654+
gitExec(t, cloneDir, "checkout", "main")
655+
writeFile(t, cloneDir, "conflict.txt", "main change\n")
656+
gitExec(t, cloneDir, "add", ".")
657+
gitExec(t, cloneDir, "commit", "-m", "main edit")
658+
659+
require.Error(t, CherryPick([]string{featureSHA}))
660+
require.True(t, IsCherryPickInProgress())
661+
662+
// --quit clears sequencer state (no longer "in progress") ...
663+
CherryPickQuit()
664+
assert.False(t, IsCherryPickInProgress(), "quit should clear cherry-pick sequencer state")
665+
666+
// ... but leaves the unmerged index behind, so checkout still fails.
667+
_, coErr := gitExecMayFail(t, cloneDir, "checkout", "feature")
668+
require.Error(t, coErr, "checkout should still fail after --quit because the index is unmerged")
669+
}

‎internal/git/mock_ops.go‎

Lines changed: 74 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,60 @@ import "fmt"
66
// Each field is an optional function that, when set, handles the corresponding
77
// Ops method call. When nil, a reasonable default is returned.
88
type MockOps struct {
9-
GitDirFn func() (string, error)
10-
RootDirFn func() (string, error)
11-
CurrentBranchFn func() (string, error)
12-
BranchExistsFn func(string) bool
13-
CheckoutBranchFn func(string) error
14-
FetchFn func(string) error
15-
FetchBranchesFn func(string, []string) error
16-
DefaultBranchFn func() (string, error)
17-
CreateBranchFn func(string, string) error
18-
PushFn func(string, []string, bool, bool) error
19-
ResolveRemoteFn func(string) (string, error)
20-
RebaseFn func(string, RebaseOpts) error
21-
EnableRerereFn func() error
22-
IsRerereEnabledFn func() (bool, error)
23-
IsRerereDeclinedFn func() (bool, error)
24-
SaveRerereDeclinedFn func() error
25-
GetSavedRemoteFn func() (string, error)
26-
SaveRemoteFn func(string) error
27-
ClearRemoteFn func() error
28-
RebaseOntoFn func(string, string, string, RebaseOpts) error
29-
RebaseContinueFn func(RebaseOpts) error
30-
RebaseAbortFn func() error
31-
IsRebaseInProgressFn func() bool
32-
ConflictedFilesFn func() ([]string, error)
33-
FindConflictMarkersFn func(string) (*ConflictMarkerInfo, error)
34-
IsAncestorFn func(string, string) (bool, error)
35-
RevParseFn func(string) (string, error)
36-
RevParseMultiFn func([]string) ([]string, error)
37-
MergeBaseFn func(string, string) (string, error)
38-
LogFn func(string, int) ([]CommitInfo, error)
39-
LogRangeFn func(string, string) ([]CommitInfo, error)
40-
DiffStatRangeFn func(string, string) (int, int, error)
41-
DiffStatFilesFn func(string, string) ([]FileDiffStat, error)
42-
DeleteBranchFn func(string, bool) error
43-
DeleteRemoteBranchFn func(string, string) error
44-
DeleteTrackingRefFn func(string, string) error
45-
ResetHardFn func(string) error
46-
SetUpstreamTrackingFn func(string, string) error
47-
MergeFFFn func(string) error
48-
UpdateBranchRefFn func(string, string) error
49-
StageAllFn func() error
50-
StageTrackedFn func() error
51-
HasStagedChangesFn func() bool
52-
CommitFn func(string) (string, error)
53-
CommitInteractiveFn func() (string, error)
54-
ValidateRefNameFn func(string) error
55-
RenameBranchFn func(string, string) error
56-
CherryPickFn func([]string) error
57-
HasUncommittedChangesFn func() (bool, error)
58-
LogMergesFn func(string, string) ([]CommitInfo, error)
9+
GitDirFn func() (string, error)
10+
RootDirFn func() (string, error)
11+
CurrentBranchFn func() (string, error)
12+
BranchExistsFn func(string) bool
13+
CheckoutBranchFn func(string) error
14+
FetchFn func(string) error
15+
FetchBranchesFn func(string, []string) error
16+
DefaultBranchFn func() (string, error)
17+
CreateBranchFn func(string, string) error
18+
PushFn func(string, []string, bool, bool) error
19+
ResolveRemoteFn func(string) (string, error)
20+
RebaseFn func(string, RebaseOpts) error
21+
EnableRerereFn func() error
22+
IsRerereEnabledFn func() (bool, error)
23+
IsRerereDeclinedFn func() (bool, error)
24+
SaveRerereDeclinedFn func() error
25+
GetSavedRemoteFn func() (string, error)
26+
SaveRemoteFn func(string) error
27+
ClearRemoteFn func() error
28+
RebaseOntoFn func(string, string, string, RebaseOpts) error
29+
RebaseContinueFn func(RebaseOpts) error
30+
RebaseAbortFn func() error
31+
IsRebaseInProgressFn func() bool
32+
ConflictedFilesFn func() ([]string, error)
33+
FindConflictMarkersFn func(string) (*ConflictMarkerInfo, error)
34+
IsAncestorFn func(string, string) (bool, error)
35+
RevParseFn func(string) (string, error)
36+
RevParseMultiFn func([]string) ([]string, error)
37+
MergeBaseFn func(string, string) (string, error)
38+
LogFn func(string, int) ([]CommitInfo, error)
39+
LogRangeFn func(string, string) ([]CommitInfo, error)
40+
DiffStatRangeFn func(string, string) (int, int, error)
41+
DiffStatFilesFn func(string, string) ([]FileDiffStat, error)
42+
DeleteBranchFn func(string, bool) error
43+
DeleteRemoteBranchFn func(string, string) error
44+
DeleteTrackingRefFn func(string, string) error
45+
ResetHardFn func(string) error
46+
SetUpstreamTrackingFn func(string, string) error
47+
MergeFFFn func(string) error
48+
UpdateBranchRefFn func(string, string) error
49+
StageAllFn func() error
50+
StageTrackedFn func() error
51+
HasStagedChangesFn func() bool
52+
CommitFn func(string) (string, error)
53+
CommitInteractiveFn func() (string, error)
54+
ValidateRefNameFn func(string) error
55+
RenameBranchFn func(string, string) error
56+
CherryPickFn func([]string) error
57+
CherryPickQuitFn func() error
58+
CherryPickAbortFn func() error
59+
CherryPickContinueFn func() error
60+
IsCherryPickInProgressFn func() bool
61+
HasUncommittedChangesFn func() (bool, error)
62+
LogMergesFn func(string, string) ([]CommitInfo, error)
5963
}
6064

6165
var _ Ops = (*MockOps)(nil)
@@ -405,11 +409,31 @@ func (m *MockOps) CherryPick(commits []string) error {
405409
return nil
406410
}
407411

412+
func (m *MockOps) CherryPickQuit() error {
413+
if m.CherryPickQuitFn != nil {
414+
return m.CherryPickQuitFn()
415+
}
416+
return nil
417+
}
418+
408419
func (m *MockOps) CherryPickAbort() error {
420+
if m.CherryPickAbortFn != nil {
421+
return m.CherryPickAbortFn()
422+
}
409423
return nil
410424
}
411425

426+
func (m *MockOps) IsCherryPickInProgress() bool {
427+
if m.IsCherryPickInProgressFn != nil {
428+
return m.IsCherryPickInProgressFn()
429+
}
430+
return false
431+
}
432+
412433
func (m *MockOps) CherryPickContinue() error {
434+
if m.CherryPickContinueFn != nil {
435+
return m.CherryPickContinueFn()
436+
}
413437
return nil
414438
}
415439

0 commit comments

Comments
 (0)