internal/worktree no longer executes the git binary. Clone, fetch,
checkout, commit, push and diff all run in-process through go-git v6,
which also removes the package's 9 noctx findings since the Context
variants are first-class.
Security: the forge token no longer leaves the process. The old
implementation wrote a GIT_ASKPASS shell script to disk and exported the
token into the environment of every git child process. Authentication is
now an in-process credential passed via client.WithHTTPAuth. cloneURL
also stops embedding the bot user in the GitHub URL.
Dropped the per-repo object cache (PreparePersistent, prepareCache,
CleanupExpiredCaches). go-git can clone with shared alternates but has no
equivalent of --dissociate, so a cached bare repo would remain a live
dependency of every worktree built from it, and expiring one would
corrupt running tasks rather than fail cleanly. Every task now gets an
independent full clone. repo.PersistentStorage still governs the
container volume; only the git object cache is gone.
Two real defects surfaced while porting, both caught by the new tests:
- go-git honours the host's global git config and cannot sign commits
(no gpg fallback, only an ObjectSigner plugin). On any machine with
commit.gpgSign=true set globally, every bot commit would have failed
with "cannot auto-sign commit". setIdentity now pins gpgsign=false in
the clone's own config so daemon behaviour does not depend on the
ambient config of whatever host it runs on.
- go-git's Add(All) does NOT honour .git/info/exclude, which is how the
.archie-prepared sentinel was kept untracked. It was being committed
and would have been pushed onto every task branch. The sentinel now
lives at .git/archie-prepared, where nothing can ever track it, and a
regression test asserts it never appears in a commit.
Diff is now merge-base relative via Commit.MergeBase, matching the three
dot semantics of `git diff origin/base...HEAD`; diffing against the remote
tip would attribute commits landed on base since the branch started to
this task and could trip the diff-size cap. Base resolution tries the
remote-tracking ref first and falls back to the short "origin/<base>"
revision, keeping git's more permissive lookup.
The test suite was rebuilt on go-git fixtures and no longer needs a git
binary installed to run.
480 lines
15 KiB
Go
480 lines
15 KiB
Go
package worktree
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/go-git/go-git/v6"
|
|
gitconfig "github.com/go-git/go-git/v6/config"
|
|
"github.com/go-git/go-git/v6/plumbing"
|
|
"github.com/go-git/go-git/v6/plumbing/object"
|
|
)
|
|
|
|
const testBase = "main"
|
|
|
|
func testSignature() *object.Signature {
|
|
return &object.Signature{
|
|
Name: "seeder",
|
|
Email: "seed@example.com",
|
|
When: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
|
|
}
|
|
}
|
|
|
|
// newLocalRemote creates a bare repo with one commit on main and returns
|
|
// the directory that serves as the fake forge host: the bare repo lives
|
|
// at <host>/<owner>/<repo>.git so cloneURL resolves it.
|
|
//
|
|
// Built with go-git rather than by shelling out, so the suite no longer
|
|
// requires a git binary on the machine running it.
|
|
func newLocalRemote(t *testing.T, owner, repo string) string {
|
|
t.Helper()
|
|
host := t.TempDir()
|
|
bare := filepath.Join(host, owner, repo+".git")
|
|
|
|
if _, err := git.PlainInit(bare, true, git.WithDefaultBranch(
|
|
plumbing.NewBranchReferenceName(testBase),
|
|
)); err != nil {
|
|
t.Fatalf("init bare remote: %v", err)
|
|
}
|
|
|
|
seed := filepath.Join(t.TempDir(), "seed")
|
|
sr, err := git.PlainInit(seed, false, git.WithDefaultBranch(
|
|
plumbing.NewBranchReferenceName(testBase),
|
|
))
|
|
if err != nil {
|
|
t.Fatalf("init seed repo: %v", err)
|
|
}
|
|
writeSeedCommit(t, sr, seed, "README.md", "seed\n", "seed")
|
|
|
|
if _, err := sr.CreateRemote(&gitconfig.RemoteConfig{
|
|
Name: git.DefaultRemoteName,
|
|
URLs: []string{bare},
|
|
}); err != nil {
|
|
t.Fatalf("add remote: %v", err)
|
|
}
|
|
ref := plumbing.NewBranchReferenceName(testBase)
|
|
if err := sr.Push(&git.PushOptions{
|
|
RemoteName: git.DefaultRemoteName,
|
|
RefSpecs: []gitconfig.RefSpec{gitconfig.RefSpec(ref + ":" + ref)},
|
|
}); err != nil {
|
|
t.Fatalf("seed push: %v", err)
|
|
}
|
|
return host
|
|
}
|
|
|
|
func writeSeedCommit(t *testing.T, r *git.Repository, dir, name, content, message string) plumbing.Hash {
|
|
t.Helper()
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o600); err != nil {
|
|
t.Fatalf("write %s: %v", name, err)
|
|
}
|
|
disableSigning(t, r)
|
|
wt, err := r.Worktree()
|
|
if err != nil {
|
|
t.Fatalf("worktree: %v", err)
|
|
}
|
|
if _, err := wt.Add(name); err != nil {
|
|
t.Fatalf("add %s: %v", name, err)
|
|
}
|
|
sig := testSignature()
|
|
hash, err := wt.Commit(message, &git.CommitOptions{Author: sig, Committer: sig})
|
|
if err != nil {
|
|
t.Fatalf("commit: %v", err)
|
|
}
|
|
return hash
|
|
}
|
|
|
|
// disableSigning mirrors what Manager.setIdentity does for real clones,
|
|
// so fixtures do not inherit commit.gpgSign from the host's global config.
|
|
func disableSigning(t *testing.T, r *git.Repository) {
|
|
t.Helper()
|
|
cfg, err := r.Config()
|
|
if err != nil {
|
|
t.Fatalf("read fixture config: %v", err)
|
|
}
|
|
cfg.Raw.Section("commit").SetOption("gpgsign", "false")
|
|
if err := r.SetConfig(cfg); err != nil {
|
|
t.Fatalf("write fixture config: %v", err)
|
|
}
|
|
}
|
|
|
|
func newManager(t *testing.T, host string) *Manager {
|
|
t.Helper()
|
|
return &Manager{
|
|
WorkDir: t.TempDir(),
|
|
BotUser: "archie-bot",
|
|
BotEmail: "archie@example.com",
|
|
BaseURL: host,
|
|
}
|
|
}
|
|
|
|
func TestPrepareCommitPushRoundTrip(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)
|
|
}
|
|
if !strings.HasPrefix(branch, "feat/7-") {
|
|
t.Errorf("branch = %q, want prefix feat/7-", branch)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, preparedSentinel)); err != nil {
|
|
t.Errorf("prepared sentinel missing: %v", err)
|
|
}
|
|
|
|
// A clean tree must report "nothing committed" rather than erroring.
|
|
committed, err := m.CommitAll(ctx, dir, "no-op")
|
|
if err != nil {
|
|
t.Fatalf("CommitAll() on clean tree error = %v", err)
|
|
}
|
|
if committed {
|
|
t.Error("CommitAll() = true on a clean tree, want false")
|
|
}
|
|
|
|
if err := os.WriteFile(filepath.Join(dir, "widget.txt"), []byte("one\ntwo\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
committed, err = m.CommitAll(ctx, dir, "feat: add widget")
|
|
if err != nil {
|
|
t.Fatalf("CommitAll() error = %v", err)
|
|
}
|
|
if !committed {
|
|
t.Fatal("CommitAll() = false, want true after adding a file")
|
|
}
|
|
|
|
files, err := m.ChangedFiles(ctx, dir, testBase)
|
|
if err != nil {
|
|
t.Fatalf("ChangedFiles() error = %v", err)
|
|
}
|
|
if len(files) != 1 || files[0] != "widget.txt" {
|
|
t.Errorf("ChangedFiles() = %v, want [widget.txt]", files)
|
|
}
|
|
|
|
lines, err := m.ChangedLines(ctx, dir, testBase)
|
|
if err != nil {
|
|
t.Fatalf("ChangedLines() error = %v", err)
|
|
}
|
|
if lines != 2 {
|
|
t.Errorf("ChangedLines() = %d, want 2", lines)
|
|
}
|
|
|
|
diff, err := m.Diff(ctx, dir, testBase)
|
|
if err != nil {
|
|
t.Fatalf("Diff() error = %v", err)
|
|
}
|
|
if !strings.Contains(diff, "widget.txt") {
|
|
t.Errorf("Diff() does not mention the changed file:\n%s", diff)
|
|
}
|
|
|
|
if err := m.Push(ctx, dir, branch); err != nil {
|
|
t.Fatalf("Push() error = %v", err)
|
|
}
|
|
// The branch must actually exist on the remote afterwards.
|
|
bare := filepath.Join(host, "acme", "todo.git")
|
|
remote, err := git.PlainOpen(bare)
|
|
if err != nil {
|
|
t.Fatalf("open bare remote: %v", err)
|
|
}
|
|
if _, err := remote.Reference(plumbing.NewBranchReferenceName(branch), true); err != nil {
|
|
t.Errorf("branch %q not present on remote after Push: %v", branch, err)
|
|
}
|
|
}
|
|
|
|
func TestPrepareIsIdempotent(t *testing.T) {
|
|
ctx := context.Background()
|
|
host := newLocalRemote(t, "acme", "todo")
|
|
m := newManager(t, host)
|
|
|
|
dir1, branch1, err := m.Prepare(ctx, "acme", "todo", testBase, 3, "fix: thing", "", "bug")
|
|
if err != nil {
|
|
t.Fatalf("first Prepare() error = %v", err)
|
|
}
|
|
// A marker file proves the second call reused the clone instead of
|
|
// wiping and re-cloning it.
|
|
marker := filepath.Join(dir1, "reuse-marker")
|
|
if err := os.WriteFile(marker, nil, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
dir2, branch2, err := m.Prepare(ctx, "acme", "todo", testBase, 3, "fix: thing", "", "bug")
|
|
if err != nil {
|
|
t.Fatalf("second Prepare() error = %v", err)
|
|
}
|
|
if dir1 != dir2 || branch1 != branch2 {
|
|
t.Errorf("Prepare() not stable: (%q,%q) then (%q,%q)", dir1, branch1, dir2, branch2)
|
|
}
|
|
if _, err := os.Stat(marker); err != nil {
|
|
t.Error("second Prepare() re-cloned instead of reusing the prepared worktree")
|
|
}
|
|
}
|
|
|
|
// An interrupted clone leaves a directory with no sentinel. Prepare must
|
|
// discard it rather than trying to reuse a half-built worktree.
|
|
func TestPrepareDiscardsUnsentinelledDirectory(t *testing.T) {
|
|
ctx := context.Background()
|
|
host := newLocalRemote(t, "acme", "todo")
|
|
m := newManager(t, host)
|
|
|
|
dir := m.Dir("acme", "todo", 11)
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
junk := filepath.Join(dir, "half-written")
|
|
if err := os.WriteFile(junk, []byte("partial"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, _, err := m.Prepare(ctx, "acme", "todo", testBase, 11, "chore: retry", "", "chore"); err != nil {
|
|
t.Fatalf("Prepare() error = %v", err)
|
|
}
|
|
if _, err := os.Stat(junk); !os.IsNotExist(err) {
|
|
t.Error("Prepare() reused an interrupted clone instead of discarding it")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, preparedSentinel)); err != nil {
|
|
t.Errorf("prepared sentinel missing after recovery: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPrepareErrors(t *testing.T) {
|
|
host := newLocalRemote(t, "acme", "todo")
|
|
|
|
tests := []struct {
|
|
name string
|
|
owner string
|
|
repo string
|
|
base string
|
|
host string
|
|
}{
|
|
{name: "unknown base branch", owner: "acme", repo: "todo", base: "no-such-branch", host: host},
|
|
{name: "unknown repository", owner: "acme", repo: "missing", base: testBase, host: host},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
m := newManager(t, tc.host)
|
|
_, _, err := m.Prepare(context.Background(), tc.owner, tc.repo, tc.base, 1, "t", "", "")
|
|
if err == nil {
|
|
t.Fatal("Prepare() error = nil, want a failure")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Every read-only operation must report a clear error on a directory that
|
|
// is not a repository, rather than panicking or returning a zero value
|
|
// that reads as "no changes".
|
|
func TestOperationsOnNonRepository(t *testing.T) {
|
|
ctx := context.Background()
|
|
m := &Manager{WorkDir: t.TempDir()}
|
|
dir := t.TempDir()
|
|
|
|
t.Run("CommitAll", func(t *testing.T) {
|
|
if _, err := m.CommitAll(ctx, dir, "msg"); err == nil {
|
|
t.Error("CommitAll() error = nil, want a failure")
|
|
}
|
|
})
|
|
t.Run("ChangedLines", func(t *testing.T) {
|
|
if _, err := m.ChangedLines(ctx, dir, testBase); err == nil {
|
|
t.Error("ChangedLines() error = nil, want a failure")
|
|
}
|
|
})
|
|
t.Run("ChangedFiles", func(t *testing.T) {
|
|
if _, err := m.ChangedFiles(ctx, dir, testBase); err == nil {
|
|
t.Error("ChangedFiles() error = nil, want a failure")
|
|
}
|
|
})
|
|
t.Run("Diff", func(t *testing.T) {
|
|
if _, err := m.Diff(ctx, dir, testBase); err == nil {
|
|
t.Error("Diff() error = nil, want a failure")
|
|
}
|
|
})
|
|
t.Run("Push", func(t *testing.T) {
|
|
if err := m.Push(ctx, dir, "any"); err == nil {
|
|
t.Error("Push() error = nil, want a failure")
|
|
}
|
|
})
|
|
}
|
|
|
|
// The diff must be measured from the merge base, not from the remote tip.
|
|
// Otherwise commits landed on the base branch after the task started are
|
|
// attributed to the task and can trip the diff-size cap.
|
|
func TestDiffUsesMergeBaseNotRemoteTip(t *testing.T) {
|
|
ctx := context.Background()
|
|
host := newLocalRemote(t, "acme", "todo")
|
|
m := newManager(t, host)
|
|
|
|
dir, _, err := m.Prepare(ctx, "acme", "todo", testBase, 5, "feat: mine", "", "feature")
|
|
if err != nil {
|
|
t.Fatalf("Prepare() error = %v", err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "mine.txt"), []byte("mine\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := m.CommitAll(ctx, dir, "feat: mine"); err != nil {
|
|
t.Fatalf("CommitAll() error = %v", err)
|
|
}
|
|
|
|
// Land an unrelated commit on the remote base and fetch it, so
|
|
// origin/main moves ahead of where this branch diverged.
|
|
seed := filepath.Join(t.TempDir(), "other")
|
|
sr, err := git.PlainClone(seed, &git.CloneOptions{URL: filepath.Join(host, "acme", "todo.git")})
|
|
if err != nil {
|
|
t.Fatalf("clone for second author: %v", err)
|
|
}
|
|
writeSeedCommit(t, sr, seed, "theirs.txt", "theirs\n", "chore: theirs")
|
|
ref := plumbing.NewBranchReferenceName(testBase)
|
|
if err := sr.Push(&git.PushOptions{
|
|
RemoteName: git.DefaultRemoteName,
|
|
RefSpecs: []gitconfig.RefSpec{gitconfig.RefSpec(ref + ":" + ref)},
|
|
}); err != nil {
|
|
t.Fatalf("second author push: %v", err)
|
|
}
|
|
|
|
r, err := git.PlainOpen(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := r.Fetch(&git.FetchOptions{RemoteName: git.DefaultRemoteName}); err != nil {
|
|
t.Fatalf("fetch: %v", err)
|
|
}
|
|
|
|
files, err := m.ChangedFiles(ctx, dir, testBase)
|
|
if err != nil {
|
|
t.Fatalf("ChangedFiles() error = %v", err)
|
|
}
|
|
for _, f := range files {
|
|
if f == "theirs.txt" {
|
|
t.Errorf("ChangedFiles() = %v, includes another author's file; diff is not merge-base relative", files)
|
|
}
|
|
}
|
|
if len(files) != 1 || files[0] != "mine.txt" {
|
|
t.Errorf("ChangedFiles() = %v, want [mine.txt]", files)
|
|
}
|
|
}
|
|
|
|
func TestBranchNaming(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
issue int
|
|
title string
|
|
labels string
|
|
want string
|
|
}{
|
|
{name: "bug label maps to fix", issue: 4, title: "crash on save", labels: "bug", want: "fix/4-crash-on-save"},
|
|
{name: "feature label maps to feat", issue: 9, title: "add export", labels: "feature", want: "feat/9-add-export"},
|
|
{name: "no label defaults to feat", issue: 1, title: "tidy", labels: "", want: "feat/1-tidy"},
|
|
{name: "conventional prefix is not duplicated", issue: 2, title: "fix: broken link", labels: "bug", want: "fix/2-broken-link"},
|
|
{name: "empty title falls back to prefix and issue", issue: 8, title: "", labels: "chore", want: "chore/8"},
|
|
{name: "punctuation collapses to single hyphens", issue: 3, title: "a -- b", labels: "docs", want: "docs/3-a-b"},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := archieBranch(tc.issue, tc.title, "", tc.labels); got != tc.want {
|
|
t.Errorf("archieBranch() = %q, want %q", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCloneURL(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
baseURL string
|
|
want string
|
|
}{
|
|
{
|
|
name: "explicit forge host",
|
|
baseURL: "https://git.example.test",
|
|
want: "https://git.example.test/acme/todo.git",
|
|
},
|
|
{
|
|
// The token must never be embedded in the URL: it travels as
|
|
// an in-process credential instead.
|
|
name: "default github host carries no credentials",
|
|
baseURL: "",
|
|
want: "https://github.com/acme/todo.git",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
m := &Manager{BaseURL: tc.baseURL, BotUser: "archie-bot", Token: "s3cret"}
|
|
got := m.cloneURL("acme", "todo")
|
|
if got != tc.want {
|
|
t.Errorf("cloneURL() = %q, want %q", got, tc.want)
|
|
}
|
|
if strings.Contains(got, "s3cret") {
|
|
t.Errorf("cloneURL() leaks the token: %q", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAuthOmittedWithoutToken(t *testing.T) {
|
|
if opts := (&Manager{BotUser: "archie-bot"}).auth(); opts != nil {
|
|
t.Errorf("auth() = %v with no token, want nil for an anonymous request", opts)
|
|
}
|
|
if opts := (&Manager{BotUser: "archie-bot", Token: "t"}).auth(); len(opts) == 0 {
|
|
t.Error("auth() returned no options despite a configured token")
|
|
}
|
|
}
|
|
|
|
func TestCleanupRemovesWorktree(t *testing.T) {
|
|
m := &Manager{WorkDir: t.TempDir()}
|
|
dir := m.Dir("acme", "todo", 42)
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := m.Cleanup("acme", "todo", 42); err != nil {
|
|
t.Fatalf("Cleanup() error = %v", err)
|
|
}
|
|
if _, err := os.Stat(dir); !os.IsNotExist(err) {
|
|
t.Error("Cleanup() left the worktree behind")
|
|
}
|
|
// Cleanup of an absent worktree is not an error.
|
|
if err := m.Cleanup("acme", "todo", 42); err != nil {
|
|
t.Errorf("Cleanup() on a missing worktree error = %v, want nil", err)
|
|
}
|
|
}
|
|
|
|
// The prepared sentinel must never reach a commit. It previously lived in
|
|
// the working tree, hidden by .git/info/exclude -- which git honours and
|
|
// go-git does not, so it was committed and pushed onto every task branch.
|
|
func TestSentinelIsNeverCommitted(t *testing.T) {
|
|
ctx := context.Background()
|
|
host := newLocalRemote(t, "acme", "todo")
|
|
m := newManager(t, host)
|
|
|
|
dir, _, err := m.Prepare(ctx, "acme", "todo", testBase, 21, "feat: thing", "", "feature")
|
|
if err != nil {
|
|
t.Fatalf("Prepare() error = %v", err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "real.txt"), []byte("x\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := m.CommitAll(ctx, dir, "feat: thing"); err != nil {
|
|
t.Fatalf("CommitAll() error = %v", err)
|
|
}
|
|
|
|
files, err := m.ChangedFiles(ctx, dir, testBase)
|
|
if err != nil {
|
|
t.Fatalf("ChangedFiles() error = %v", err)
|
|
}
|
|
for _, f := range files {
|
|
if strings.Contains(f, "archie-prepared") {
|
|
t.Errorf("the prepared sentinel was committed: ChangedFiles() = %v", files)
|
|
}
|
|
}
|
|
|
|
// It must still exist on disk, or Prepare stops being idempotent.
|
|
if _, err := os.Stat(filepath.Join(dir, preparedSentinel)); err != nil {
|
|
t.Errorf("sentinel missing after commit: %v", err)
|
|
}
|
|
}
|