feat(worktree): Resume re-syncs a worktree onto its branch tip
Add Manager.Resume, the complement of refresh: refresh resets to origin/<base> for a fresh run; Resume fetches and hard-resets to origin/<branch> so the already-pushed PR work survives. This is the worktree primitive the remediate workflow (archie-core-8li9 step 3) needs to continue work on an existing PR branch without discarding its commits. Tests pin the discriminating property: Resume lands on the branch tip (the committed work), not base, and cleans abandoned untracked files.
This commit is contained in:
1 parent
b5113ee5d5
commit
83177089cb
2 files changed
+121
No files matched your search
@@ -0,0 +1,82 @@
|
||||
package worktree
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/go-git/go-git/v6"
|
||||
)
|
||||
|
||||
// TestResumeResetsToBranchTipNotBase pins the remediate workflow's worktree
|
||||
// semantics: Resume must land the worktree on the PR branch's remote tip (the
|
||||
// committed work), not reset to base the way refresh does for a fresh run.
|
||||
func TestResumeResetsToBranchTipNotBase(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
host := newLocalRemote(t, "acme", "todo")
|
||||
m := newManager(t, host)
|
||||
|
||||
dir, branch, err := m.Prepare(ctx, "acme", "todo", testBase, 7, "feat: add widget", "", "feature")
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare() error = %v", err)
|
||||
}
|
||||
|
||||
// Commit + push work on the branch, so origin/<branch> has widget.go
|
||||
// while base (main) has only README.
|
||||
r, err := git.PlainOpen(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("PlainOpen: %v", err)
|
||||
}
|
||||
writeSeedCommit(t, r, dir, "widget.go", "package widget\n", "add widget")
|
||||
if err := m.Push(ctx, dir, branch); err != nil {
|
||||
t.Fatalf("Push() error = %v", err)
|
||||
}
|
||||
|
||||
if err := m.Resume(ctx, dir, branch); err != nil {
|
||||
t.Fatalf("Resume() error = %v", err)
|
||||
}
|
||||
content, err := os.ReadFile(filepath.Join(dir, "widget.go"))
|
||||
if err != nil {
|
||||
t.Fatalf("widget.go missing after Resume: %v (resume reset to base and discarded the branch work)", err)
|
||||
}
|
||||
if string(content) != "package widget\n" {
|
||||
t.Fatalf("widget.go = %q, want %q", content, "package widget\n")
|
||||
}
|
||||
}
|
||||
|
||||
// TestResumeCleansAbandonedFiles ensures Resume clears untracked residue the
|
||||
// way refresh does, so a prior interrupted remediation cannot leak files into
|
||||
// the next run.
|
||||
func TestResumeCleansAbandonedFiles(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
host := newLocalRemote(t, "acme", "todo")
|
||||
m := newManager(t, host)
|
||||
|
||||
dir, branch, err := m.Prepare(ctx, "acme", "todo", testBase, 8, "fix: thing", "", "bug")
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare() error = %v", err)
|
||||
}
|
||||
|
||||
// The branch must be pushed for Resume to resolve origin/<branch>.
|
||||
r, err := git.PlainOpen(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("PlainOpen: %v", err)
|
||||
}
|
||||
writeSeedCommit(t, r, dir, "change.go", "package change\n", "a change")
|
||||
if err := m.Push(ctx, dir, branch); err != nil {
|
||||
t.Fatalf("Push() error = %v", err)
|
||||
}
|
||||
|
||||
// Untracked residue that a clean run must remove.
|
||||
if err := os.WriteFile(filepath.Join(dir, "scratch.txt"), []byte("junk\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := m.Resume(ctx, dir, branch); err != nil {
|
||||
t.Fatalf("Resume() error = %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "scratch.txt")); !os.IsNotExist(err) {
|
||||
t.Errorf("scratch.txt still exists after Resume; untracked residue was not cleaned")
|
||||
}
|
||||
}
|
||||
@@ -278,6 +278,45 @@ func (m *Manager) refresh(ctx context.Context, dir, base, branch string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Resume re-syncs an already-prepared worktree onto its branch's remote tip
|
||||
// without resetting to base, so the remediate workflow can continue work on
|
||||
// the PR branch the implement run already pushed. It is refresh's complement:
|
||||
// refresh resets to origin/<base> for a fresh run; Resume resets to
|
||||
// origin/<branch> so the committed PR work survives.
|
||||
func (m *Manager) Resume(ctx context.Context, dir, branch string) error {
|
||||
r, err := git.PlainOpen(dir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open prepared worktree: %w", err)
|
||||
}
|
||||
if err := r.FetchContext(ctx, &git.FetchOptions{
|
||||
RemoteName: git.DefaultRemoteName,
|
||||
ClientOptions: m.auth(),
|
||||
}); err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
|
||||
return fmt.Errorf("fetch origin: %w", err)
|
||||
}
|
||||
|
||||
wt, err := r.Worktree()
|
||||
if err != nil {
|
||||
return fmt.Errorf("open worktree: %w", err)
|
||||
}
|
||||
ref := plumbing.NewBranchReferenceName(branch)
|
||||
if err := wt.Checkout(&git.CheckoutOptions{Branch: ref, Force: true}); err != nil {
|
||||
return fmt.Errorf("checkout branch %s: %w", branch, err)
|
||||
}
|
||||
|
||||
tip, err := r.ResolveRevision(plumbing.Revision(remoteBase(branch)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve remote tip of %s: %w", branch, err)
|
||||
}
|
||||
if err := wt.Reset(&git.ResetOptions{Commit: *tip, Mode: git.HardReset}); err != nil {
|
||||
return fmt.Errorf("reset to %s: %w", remoteBase(branch), err)
|
||||
}
|
||||
if err := cleanUntracked(r, dir); err != nil {
|
||||
return fmt.Errorf("clean abandoned worktree files: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func cleanUntracked(r *git.Repository, dir string) error {
|
||||
head, err := r.Head()
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user