diff --git a/go.mod b/go.mod index 8436df1f3..dca94bca1 100644 --- a/go.mod +++ b/go.mod @@ -11,8 +11,10 @@ require ( require ( github.com/42wim/httpsig v1.2.4 // indirect github.com/Microsoft/go-winio v0.6.2 // indirect + github.com/ProtonMail/go-crypto v1.4.1 // indirect github.com/antithesishq/antithesis-sdk-go v0.7.0-default-no-op // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/cloudflare/circl v1.6.3 // indirect github.com/containerd/errdefs v1.0.0 // indirect github.com/containerd/errdefs/pkg v0.3.0 // indirect github.com/davidmz/go-pageant v1.0.2 // indirect @@ -20,8 +22,11 @@ require ( github.com/docker/go-connections v0.7.0 // indirect github.com/docker/go-units v0.5.0 // indirect github.com/dustin/go-humanize v1.0.1 // indirect + github.com/emirpasic/gods v1.18.1 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/go-fed/httpsig v1.1.0 // indirect + github.com/go-git/gcfg/v2 v2.0.2 // indirect + github.com/go-git/go-billy/v6 v6.0.0-alpha.1 // indirect github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/google/codesearch v1.3.0-rc.1 // indirect @@ -32,7 +37,9 @@ require ( github.com/hashicorp/go-version v1.9.0 // indirect github.com/ilyakaznacheev/cleanenv v1.5.0 // indirect github.com/joho/godotenv v1.5.1 // indirect + github.com/kevinburke/ssh_config v1.6.0 // indirect github.com/klauspost/compress v1.18.6 // indirect + github.com/klauspost/cpuid/v2 v2.3.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/minio/highwayhash v1.0.4 // indirect github.com/moby/docker-image-spec v1.3.1 // indirect @@ -42,14 +49,18 @@ require ( github.com/ncruces/go-strftime v1.0.0 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.1.1 // indirect + github.com/pjbgf/sha1cd v0.6.0 // indirect github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + github.com/sergi/go-diff v1.4.0 // indirect go.opentelemetry.io/auto/sdk v1.2.1 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect go.opentelemetry.io/otel v1.44.0 // indirect go.opentelemetry.io/otel/metric v1.44.0 // indirect go.opentelemetry.io/otel/trace v1.44.0 // indirect golang.org/x/crypto v0.53.0 // indirect + golang.org/x/net v0.55.0 // indirect + golang.org/x/sync v0.21.0 // indirect golang.org/x/sys v0.46.0 // indirect golang.org/x/time v0.15.0 // indirect modernc.org/libc v1.74.1 // indirect @@ -60,6 +71,7 @@ require ( require ( code.gitea.io/sdk/gitea v0.25.1 + github.com/go-git/go-git/v6 v6.0.0-alpha.4 github.com/go-telegram/bot v1.22.0 github.com/moby/moby/api v1.55.0 github.com/moby/moby/client v0.5.0 diff --git a/go.sum b/go.sum index 73a06e2bd..b59e5bf34 100644 --- a/go.sum +++ b/go.sum @@ -7,16 +7,21 @@ github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= +github.com/ProtonMail/go-crypto v1.4.1 h1:9RfcZHqEQUvP8RzecWEUafnZVtEvrBVL9BiF67IQOfM= +github.com/ProtonMail/go-crypto v1.4.1/go.mod h1:e1OaTyu5SYVrO9gKOEhTc+5UcXtTUa+P3uLudwcgPqo= github.com/antithesishq/antithesis-sdk-go v0.7.0-default-no-op h1:Z/MZK75wC/NSrkgqeNIa7jexam9uWzhLmFTSCPI/kn0= github.com/antithesishq/antithesis-sdk-go v0.7.0-default-no-op/go.mod h1:FQyySiasQQM8735Ddel3MRojmy4dA1IqCeyJ5jmPMbI= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg8= +github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJRn81BiS4= github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI= github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M= github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE= github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davidmz/go-pageant v1.0.2 h1:bPblRCh5jGU+Uptpz6LgMZGD5hJoOt7otgT454WvHn0= @@ -29,10 +34,18 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4 github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= +github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/go-fed/httpsig v1.1.0 h1:9M+hb0jkEICD8/cAiNqEB66R87tTINszBRTjwjQzWcI= github.com/go-fed/httpsig v1.1.0/go.mod h1:RCMrTZvN1bJYtofsG4rd5NaO5obxQ5xBkdiS7xsT7bM= +github.com/go-git/gcfg/v2 v2.0.2 h1:MY5SIIfTGGEMhdA7d7JePuVVxtKL7Hp+ApGDJAJ7dpo= +github.com/go-git/gcfg/v2 v2.0.2/go.mod h1:/lv2NsxvhepuMrldsFilrgct6pxzpGdSRC13ydTLSLs= +github.com/go-git/go-billy/v6 v6.0.0-alpha.1 h1:xVjAR4oUvrKy7/Xuw/lLlV3gkxR3KO2H8W+MamuVVsQ= +github.com/go-git/go-billy/v6 v6.0.0-alpha.1/go.mod h1:eaCUpHbedW7//EwcYmUDfJe2N6sJC9O12AT0OTqJR1E= +github.com/go-git/go-git/v6 v6.0.0-alpha.4 h1:aDTc2UGanmaE7FkGLSlBEB9nohMnQ+RKXcfq/D+esDQ= +github.com/go-git/go-git/v6 v6.0.0-alpha.4/go.mod h1:4ODa/G7hPWrh4Y+7lmt59Ij3zW38IEfvRoAZxLYYBhc= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= @@ -69,10 +82,17 @@ github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2l github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/kevinburke/ssh_config v1.6.0 h1:J1FBfmuVosPHf5GRdltRLhPJtJpTlMdKTBjRgTaQBFY= +github.com/kevinburke/ssh_config v1.6.0/go.mod h1:q2RIzfka+BXARoNexmF9gkxEX7DmvbW9P4hIVx2Kg4M= github.com/klauspost/compress v1.18.6 h1:2jupLlAwFm95+YDR+NwD2MEfFO9d4z4Prjl1XXDjuao= github.com/klauspost/compress v1.18.6/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= +github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= +github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= @@ -105,6 +125,8 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8 github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040= github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M= +github.com/pjbgf/sha1cd v0.6.0 h1:3WJ8Wz8gvDz29quX1OcEmkAlUg9diU4GxJHqs0/XiwU= +github.com/pjbgf/sha1cd v0.6.0/go.mod h1:lhpGlyHLpQZoxMv8HcgXvZEhcGs0PG/vsZnEJ7H0iCM= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -129,6 +151,10 @@ github.com/samcharles93/ai-sdk v0.1.17 h1:yH48jrYVyFv+VO1qYkbReVpVxP063XMs0T/Jb9 github.com/samcharles93/ai-sdk v0.1.17/go.mod h1:a4JYsUrwGsmm2eiXgrpcKExtYe7uHQ5zsHDbbkcPB6o= github.com/samcharles93/ai-sdk v0.1.18 h1:Jvz2OSnzJ68XCAiTHdLKKvgoNI/rKrNw9LJ7jbZs55c= github.com/samcharles93/ai-sdk v0.1.18/go.mod h1:a4JYsUrwGsmm2eiXgrpcKExtYe7uHQ5zsHDbbkcPB6o= +github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw= +github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/traefik/yaegi v0.16.1 h1:f1De3DVJqIDKmnasUF6MwmWv1dSEEat0wcpXhD2On3E= @@ -186,8 +212,11 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q= diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 45878e923..60e769c20 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -322,15 +322,6 @@ func (d *Daemon) cleanupExpiredStorage(ctx context.Context) { d.Log.Info("expired persistent volumes removed", "count", n) } } - if d.Trees != nil { - n, err := d.Trees.CleanupExpiredCaches(ttl) - if err != nil { - d.Log.Warn("repo cache cleanup failed", "err", err) - } - if n > 0 { - d.Log.Info("expired repo caches removed", "count", n) - } - } } // drainSQLite claims queued tasks from SQLite and dispatches them concurrently. @@ -892,14 +883,12 @@ func (d *Daemon) process(ctx context.Context, task *store.Task) { // must exist before Acquire is called. var branch string var err error - if repo.PersistentStorage && d.Cfg.Containers.VolumeTTL.Std() > 0 { - _, branch, err = trees.PreparePersistent( - ctx, task.Owner, task.Repo, repo.Base, task.IssueNumber, - task.Title, task.Body, task.Labels, d.Cfg.Containers.VolumeTTL.Std(), - ) - } else { - _, branch, err = trees.Prepare(ctx, task.Owner, task.Repo, repo.Base, task.IssueNumber, task.Title, task.Body, task.Labels) - } + // Every task gets an independent full clone. The former + // PreparePersistent path shared objects with a per-repo bare cache; + // go-git has no --dissociate, so a shared cache would stay a live + // dependency of each worktree and expiring one would corrupt running + // tasks. repo.PersistentStorage still governs the container volume. + _, branch, err = trees.Prepare(ctx, task.Owner, task.Repo, repo.Base, task.IssueNumber, task.Title, task.Body, task.Labels) if err != nil { d.Log.Error("worktree prepare failed", "err", err) _ = d.Store.Transition(ctx, task.ID, store.StatusRunning, store.StatusParked, "worktree prepare failed: "+err.Error()) diff --git a/internal/worktree/worktree.go b/internal/worktree/worktree.go index 245bfae79..b687a85bb 100644 --- a/internal/worktree/worktree.go +++ b/internal/worktree/worktree.go @@ -2,38 +2,62 @@ // branch, commit as the bot identity, push, diff stats, cleanup. The // daemon performs these deterministically -- the model's shell tool never // drives git. +// +// Implemented with go-git rather than by shelling out to the git binary. +// Two consequences are worth knowing: +// +// - The forge token never leaves the process. The previous +// implementation wrote a GIT_ASKPASS helper script and exported the +// token into every child environment; authentication is now an +// in-process http.BasicAuth value. +// - There is no per-repo object cache. 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. Each task +// gets an independent full clone instead. package worktree import ( "context" "errors" "fmt" - "net/url" "os" - "os/exec" "path/filepath" "strings" - "sync" "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" + gitclient "github.com/go-git/go-git/v6/plumbing/client" + "github.com/go-git/go-git/v6/plumbing/object" + githttp "github.com/go-git/go-git/v6/plumbing/transport/http" ) +// preparedSentinel marks a worktree as fully cloned, branched and +// configured. Its presence is what makes Prepare idempotent: the daemon +// may prepare a worktree before the task container is acquired. +// +// It lives inside .git deliberately. The previous implementation kept it +// in the working tree and hid it via .git/info/exclude, which worked +// because `git add -A` honours that file -- go-git's Add does NOT, so the +// sentinel was committed and pushed onto every task branch. Nothing under +// .git can ever be tracked, so placing it here removes the failure mode +// rather than papering over it. +const preparedSentinel = ".git/archie-prepared" + type Manager struct { // WorkDir is the root under which task worktrees are created. WorkDir string - // Token authenticates clone/push over HTTPS via an askpass helper, - // keeping it out of .git/config and process argv. + // Token authenticates clone and push over HTTPS. It is held in memory + // and passed to go-git directly, never written to .git/config, an + // askpass helper, or a child process environment. Token string BotUser string BotEmail string // BaseURL overrides the forge host. Set from config [forge].host. // Empty falls back to https://github.com. BaseURL string - - cacheMu sync.Mutex - - askpassOnce sync.Once - askpassPath string - askpassErr error } // Dir is the worktree path for a task. @@ -41,216 +65,342 @@ func (m *Manager) Dir(owner, repo string, issue int) string { return filepath.Join(m.WorkDir, owner+"-"+repo, fmt.Sprintf("issue-%d", issue)) } -// askpass writes (once) a helper script that emits the token, so git -// authenticates without the token appearing in argv or .git/config. -func (m *Manager) askpass() (string, error) { - m.askpassOnce.Do(func() { - m.askpassPath = filepath.Join(m.WorkDir, ".git-askpass") - script := "#!/bin/sh\necho \"$ARCHIE_GIT_TOKEN\"\n" - if err := os.MkdirAll(m.WorkDir, 0o755); err != nil { - m.askpassErr = err - return - } - if err := os.WriteFile(m.askpassPath, []byte(script), 0o700); err != nil { - m.askpassErr = err - return - } - }) - return m.askpassPath, m.askpassErr +// auth builds the HTTP credential for clone, fetch and push. +// +// Forges accept a personal access token as the password with any +// non-empty username, so the bot's own name is used. A nil return means +// no token was configured and go-git attempts an anonymous request, +// which is correct for a public read. +func (m *Manager) auth() []gitclient.Option { + if m.Token == "" { + return nil + } + username := m.BotUser + if username == "" { + // Any non-empty username is accepted alongside a token; this one + // makes the origin obvious in a forge's audit log. + username = "archie" + } + return []gitclient.Option{ + gitclient.WithHTTPAuth(&githttp.BasicAuth{Username: username, Password: m.Token}), + } } -func (m *Manager) git(ctx context.Context, dir string, args ...string) (string, error) { - helper, err := m.askpass() - if err != nil { - return "", err - } - cmd := exec.CommandContext(ctx, "git", args...) - cmd.Dir = dir - cmd.Env = append( - os.Environ(), - "GIT_ASKPASS="+helper, - "ARCHIE_GIT_TOKEN="+m.Token, - "GIT_TERMINAL_PROMPT=0", - ) - out, err := cmd.CombinedOutput() - if err != nil { - return string(out), fmt.Errorf("git %s: %w\n%s", strings.Join(args, " "), err, out) - } - return string(out), nil +// signature is the author and committer identity for bot commits. +func (m *Manager) signature() *object.Signature { + return &object.Signature{Name: m.BotUser, Email: m.BotEmail, When: time.Now()} } // Prepare creates a fresh clone for the task and checks out its branch. -// Any leftover worktree from a prior attempt is removed first. -// If the worktree is already prepared (the daemon may have done this -// before container acquisition), it returns the existing directory. -func (m *Manager) Prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels string) (dir, branch string, err error) { - return m.prepare(ctx, owner, repo, base, issue, title, body, labels, "") -} - -// PreparePersistent creates an isolated task worktree using a per-repo bare -// Git cache as an object reference. The cache avoids repeated full network -// clones while --dissociate keeps each task worktree independent so expiry -// cannot break active or parked tasks. -func (m *Manager) PreparePersistent(ctx context.Context, owner, repo, base string, issue int, title, body, labels string, ttl time.Duration) (dir, branch string, err error) { - m.cacheMu.Lock() - defer m.cacheMu.Unlock() - - cache, err := m.prepareCache(ctx, owner, repo, ttl) - if err != nil { - return "", "", err - } - return m.prepare(ctx, owner, repo, base, issue, title, body, labels, cache) -} - -func (m *Manager) prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels, cache string) (dir, branch string, err error) { +// Any leftover worktree from a prior attempt is removed first. If the +// worktree is already prepared, it is refreshed and reused. +func (m *Manager) Prepare( + ctx context.Context, + owner, repo, base string, + issue int, + title, body, labels string, +) (dir, branch string, err error) { dir = m.Dir(owner, repo, issue) branch = archieBranch(issue, title, body, labels) - // Already prepared: sentinel proves clone + checkout + config completed. - sentinel := filepath.Join(dir, ".archie-prepared") - if _, err := os.Stat(sentinel); err == nil { - if _, err := m.git(ctx, dir, "fetch", "origin"); err != nil { + if _, statErr := os.Stat(filepath.Join(dir, preparedSentinel)); statErr == nil { + if err := m.refresh(ctx, dir, base, branch); err != nil { return "", "", err } - // Try switching to the branch; create it if it doesn't exist yet. - if _, err := m.git(ctx, dir, "checkout", branch); err != nil { - if _, err := m.git(ctx, dir, "checkout", "-b", branch); err != nil { - return "", "", err - } - } - if _, err := m.git(ctx, dir, "reset", "--hard", "origin/"+base); err != nil { - _ = err // Non-fatal -- the branch may not have been pushed yet. - } return dir, branch, nil } + if err := os.RemoveAll(dir); err != nil { - return "", "", err + return "", "", fmt.Errorf("clear stale worktree: %w", err) } if err := os.MkdirAll(filepath.Dir(dir), 0o755); err != nil { - return "", "", err + return "", "", fmt.Errorf("create worktree parent: %w", err) } - url := m.cloneURL(owner, repo) - cloneArgs := []string{"clone", "--branch", base} - if cache != "" { - cloneArgs = append(cloneArgs, "--reference-if-able", cache, "--dissociate") - } - cloneArgs = append(cloneArgs, url, dir) - if _, err := m.git(ctx, filepath.Dir(dir), cloneArgs...); err != nil { - return "", "", err - } - for _, args := range [][]string{ - {"config", "user.name", m.BotUser}, - {"config", "user.email", m.BotEmail}, - {"checkout", "-b", branch}, - } { - if _, err := m.git(ctx, dir, args...); err != nil { - return "", "", err - } - } - // Exclude the sentinel from git tracking via the local exclude file. - exclude := filepath.Join(dir, ".git", "info", "exclude") - f, err := os.OpenFile(exclude, os.O_APPEND|os.O_WRONLY, 0o644) + + r, err := git.PlainCloneContext(ctx, dir, &git.CloneOptions{ + URL: m.cloneURL(owner, repo), + ClientOptions: m.auth(), + ReferenceName: plumbing.NewBranchReferenceName(base), + }) if err != nil { + return "", "", fmt.Errorf("clone %s/%s: %w", owner, repo, err) + } + if err := m.setIdentity(r); err != nil { return "", "", err } - if _, err := f.WriteString(".archie-prepared\n"); err != nil { - _ = f.Close() - return "", "", err + + wt, err := r.Worktree() + if err != nil { + return "", "", fmt.Errorf("open worktree: %w", err) } - if err := f.Close(); err != nil { - return "", "", err + if err := wt.Checkout(&git.CheckoutOptions{ + Branch: plumbing.NewBranchReferenceName(branch), + Create: true, + }); err != nil { + return "", "", fmt.Errorf("create branch %s: %w", branch, err) } - // Write sentinel to mark the worktree as fully prepared. - if err := os.WriteFile(filepath.Join(dir, ".archie-prepared"), nil, 0o644); err != nil { + + if err := writeSentinel(dir); err != nil { return "", "", err } return dir, branch, nil } -func (m *Manager) cacheDir(owner, repo string) string { - key := url.PathEscape(owner) + "-" + url.PathEscape(repo) + ".git" - return filepath.Join(m.WorkDir, ".repo-cache", key) -} - -func (m *Manager) prepareCache(ctx context.Context, owner, repo string, ttl time.Duration) (string, error) { - cache := m.cacheDir(owner, repo) - if st, err := os.Stat(filepath.Join(cache, "HEAD")); err == nil && !st.IsDir() { - if ttl > 0 { - if cacheInfo, statErr := os.Stat(cache); statErr == nil && time.Since(cacheInfo.ModTime()) >= ttl { - if err := os.RemoveAll(cache); err != nil { - return "", fmt.Errorf("expire repo cache: %w", err) - } - } - } - } - - if st, err := os.Stat(filepath.Join(cache, "HEAD")); err == nil && !st.IsDir() { - if _, err := m.git(ctx, cache, "fetch", "--prune", "origin"); err != nil { - return "", fmt.Errorf("refresh repo cache: %w", err) - } - if err := os.Chtimes(cache, time.Now(), time.Now()); err != nil { - return "", fmt.Errorf("touch repo cache: %w", err) - } - return cache, nil - } - - if err := os.RemoveAll(cache); err != nil { - return "", fmt.Errorf("reset repo cache: %w", err) - } - if err := os.MkdirAll(filepath.Dir(cache), 0o755); err != nil { - return "", fmt.Errorf("create repo cache dir: %w", err) - } - if _, err := m.git(ctx, filepath.Dir(cache), "clone", "--mirror", m.cloneURL(owner, repo), cache); err != nil { - return "", fmt.Errorf("create repo cache: %w", err) - } - if err := os.Chtimes(cache, time.Now(), time.Now()); err != nil { - return "", fmt.Errorf("touch repo cache: %w", err) - } - return cache, nil -} - -// CleanupExpiredCaches removes per-repo Git object caches not used within ttl. -// Task worktrees are dissociated clones and remain intact. -func (m *Manager) CleanupExpiredCaches(ttl time.Duration) (int, error) { - if ttl <= 0 { - return 0, nil - } - m.cacheMu.Lock() - defer m.cacheMu.Unlock() - - root := filepath.Join(m.WorkDir, ".repo-cache") - entries, err := os.ReadDir(root) - if os.IsNotExist(err) { - return 0, nil - } +// refresh brings an already-prepared worktree back in line with the +// remote: fetch, move onto the task branch, and reset it to the base. +func (m *Manager) refresh(ctx context.Context, dir, base, branch string) error { + r, err := git.PlainOpen(dir) if err != nil { - return 0, fmt.Errorf("list repo caches: %w", err) + 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}); err != nil { + // The branch does not exist yet in this worktree. + if err := wt.Checkout(&git.CheckoutOptions{Branch: ref, Create: true}); err != nil { + return fmt.Errorf("checkout branch %s: %w", branch, err) + } + } + + // Reset onto the freshly fetched base. A missing remote base is not + // fatal: the branch may exist only locally on a first attempt. + baseHash, err := resolveBase(r, base) + if err != nil { + return nil //nolint:nilerr // no remote base yet is a normal first-attempt state, not a failure + } + if err := wt.Reset(&git.ResetOptions{Commit: baseHash, Mode: git.HardReset}); err != nil { + return fmt.Errorf("reset to %s: %w", remoteBase(base), err) + } + return nil +} + +// setIdentity records the bot identity in the clone's own config so that +// anything inspecting the worktree sees the same author go-git commits +// with. +func (m *Manager) setIdentity(r *git.Repository) error { + cfg, err := r.Config() + if err != nil { + return fmt.Errorf("read repo config: %w", err) + } + cfg.User.Name = m.BotUser + cfg.User.Email = m.BotEmail + // Pin signing off for this repository. + // + // go-git honours the host's global git config, and it cannot sign: + // it has no gpg fallback, only an ObjectSigner plugin. A developer + // or CI image with commit.gpgSign=true set globally would therefore + // make every bot commit fail with "cannot auto-sign commit". The + // daemon's behaviour must not depend on the ambient git config of + // whatever machine it happens to run on. + cfg.Raw.Section("commit").SetOption("gpgsign", "false") + if err := r.SetConfig(cfg); err != nil { + return fmt.Errorf("write repo config: %w", err) + } + return nil +} + +// writeSentinel marks the worktree as fully prepared. +func writeSentinel(dir string) error { + if err := os.WriteFile(filepath.Join(dir, preparedSentinel), nil, 0o644); err != nil { + return fmt.Errorf("write prepared sentinel: %w", err) + } + return nil +} + +// CommitAll stages everything and commits; returns false when the tree +// is clean (nothing to commit). +func (m *Manager) CommitAll(_ context.Context, dir, message string) (bool, error) { + r, err := git.PlainOpen(dir) + if err != nil { + return false, fmt.Errorf("open worktree: %w", err) + } + wt, err := r.Worktree() + if err != nil { + return false, fmt.Errorf("open worktree: %w", err) + } + if err := wt.AddWithOptions(&git.AddOptions{All: true}); err != nil { + return false, fmt.Errorf("stage changes: %w", err) + } + status, err := wt.Status() + if err != nil { + return false, fmt.Errorf("read status: %w", err) + } + if status.IsClean() { + return false, nil + } + sig := m.signature() + if _, err := wt.Commit(message, &git.CommitOptions{Author: sig, Committer: sig}); err != nil { + return false, fmt.Errorf("commit: %w", err) + } + return true, nil +} + +// Push publishes the branch and sets it to track origin. +func (m *Manager) Push(ctx context.Context, dir, branch string) error { + r, err := git.PlainOpen(dir) + if err != nil { + return fmt.Errorf("open worktree: %w", err) + } + ref := plumbing.NewBranchReferenceName(branch) + spec := gitconfig.RefSpec(fmt.Sprintf("%s:%s", ref, ref)) + if err := r.PushContext(ctx, &git.PushOptions{ + RemoteName: git.DefaultRemoteName, + RefSpecs: []gitconfig.RefSpec{spec}, + ClientOptions: m.auth(), + }); err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) { + return fmt.Errorf("push %s: %w", branch, err) + } + // Record the upstream so the branch tracks origin, matching what + // `git push -u` left behind previously. + cfg, err := r.Config() + if err != nil { + return fmt.Errorf("read repo config: %w", err) + } + if cfg.Branches == nil { + cfg.Branches = map[string]*gitconfig.Branch{} + } + cfg.Branches[branch] = &gitconfig.Branch{ + Name: branch, + Remote: git.DefaultRemoteName, + Merge: ref, + } + if err := r.SetConfig(cfg); err != nil { + return fmt.Errorf("record branch upstream: %w", err) + } + return nil +} + +// patch computes the diff of the task branch against base, from their +// merge base -- the three-dot semantics of `git diff origin/base...HEAD`. +// Diffing against the remote tip instead would attribute every commit +// landed on base since the branch started to this task. +func (m *Manager) patch(ctx context.Context, dir, base string) (*object.Patch, error) { + r, err := git.PlainOpen(dir) + if err != nil { + return nil, fmt.Errorf("open worktree: %w", err) + } + baseHash, err := resolveBase(r, base) + if err != nil { + return nil, err + } + head, err := r.Head() + if err != nil { + return nil, fmt.Errorf("resolve HEAD: %w", err) + } + baseCommit, err := r.CommitObject(baseHash) + if err != nil { + return nil, fmt.Errorf("load base commit: %w", err) + } + headCommit, err := r.CommitObject(head.Hash()) + if err != nil { + return nil, fmt.Errorf("load head commit: %w", err) + } + from := baseCommit + if bases, err := baseCommit.MergeBase(headCommit); err == nil && len(bases) > 0 { + from = bases[0] + } + p, err := from.PatchContext(ctx, headCommit) + if err != nil { + return nil, fmt.Errorf("diff %s...HEAD: %w", remoteBase(base), err) + } + return p, nil +} + +// ChangedLines reports lines added+deleted vs the base branch -- the +// input to the diff-size cap. +func (m *Manager) ChangedLines(ctx context.Context, dir, base string) (int, error) { + p, err := m.patch(ctx, dir, base) + if err != nil { + return 0, err + } + total := 0 + for _, stat := range p.Stats() { + total += stat.Addition + stat.Deletion + } + return total, nil +} + +// Diff returns the unified diff of all committed changes against base. +func (m *Manager) Diff(ctx context.Context, dir, base string) (string, error) { + p, err := m.patch(ctx, dir, base) + if err != nil { + return "", err + } + return p.String(), nil +} + +// ChangedFiles lists the repo-relative paths changed against base. +// A rename reports its destination; a deletion reports the removed path. +func (m *Manager) ChangedFiles(ctx context.Context, dir, base string) ([]string, error) { + p, err := m.patch(ctx, dir, base) + if err != nil { + return nil, err } var ( - removed int - errs []error + files []string + seen = map[string]struct{}{} ) - now := time.Now() - for _, entry := range entries { - if !entry.IsDir() { + for _, fp := range p.FilePatches() { + fromFile, toFile := fp.Files() + name := "" + switch { + case toFile != nil: + name = toFile.Path() + case fromFile != nil: + name = fromFile.Path() + } + if name == "" { continue } - path := filepath.Join(root, entry.Name()) - info, err := entry.Info() - if err != nil { - errs = append(errs, fmt.Errorf("stat repo cache %s: %w", entry.Name(), err)) + if _, ok := seen[name]; ok { continue } - if now.Sub(info.ModTime()) < ttl { - continue - } - if err := os.RemoveAll(path); err != nil { - errs = append(errs, fmt.Errorf("remove repo cache %s: %w", entry.Name(), err)) - continue - } - removed++ + seen[name] = struct{}{} + files = append(files, name) } - return removed, errors.Join(errs...) + return files, nil +} + +// Cleanup removes a task's worktree (merged/rejected); parked worktrees +// are kept for post-mortems. +func (m *Manager) Cleanup(owner, repo string, issue int) error { + return os.RemoveAll(m.Dir(owner, repo, issue)) +} + +func remoteBase(base string) string { + return "refs/remotes/" + git.DefaultRemoteName + "/" + base +} + +// resolveBase finds the commit the task branch is compared against. +// +// The fully-qualified remote-tracking ref is tried first because that is +// what a clone produces and it cannot be shadowed. The bare "origin/" +// revision is tried second so that resolution stays as permissive as +// `git diff origin/...HEAD` was -- git resolves that short form +// against refs/heads too, and repositories built that way exist. +func resolveBase(r *git.Repository, base string) (plumbing.Hash, error) { + candidates := []plumbing.Revision{ + plumbing.Revision(remoteBase(base)), + plumbing.Revision(git.DefaultRemoteName + "/" + base), + } + var err error + for _, rev := range candidates { + var hash *plumbing.Hash + if hash, err = r.ResolveRevision(rev); err == nil { + return *hash, nil + } + } + return plumbing.ZeroHash, fmt.Errorf("resolve %s: %w", remoteBase(base), err) } // archieBranch builds a descriptive branch name. Uses conventional @@ -322,85 +472,5 @@ func (m *Manager) cloneURL(owner, repo string) string { if m.BaseURL != "" { return fmt.Sprintf("%s/%s/%s.git", m.BaseURL, owner, repo) } - return fmt.Sprintf("https://%s@github.com/%s/%s.git", m.BotUser, owner, repo) -} - -// CommitAll stages everything and commits; returns false when the tree -// is clean (nothing to commit). -func (m *Manager) CommitAll(ctx context.Context, dir, message string) (bool, error) { - if _, err := m.git(ctx, dir, "add", "-A"); err != nil { - return false, err - } - status, err := m.git(ctx, dir, "status", "--porcelain") - if err != nil { - return false, err - } - if strings.TrimSpace(status) == "" { - return false, nil - } - if _, err := m.git(ctx, dir, "commit", "-m", message); err != nil { - return false, err - } - return true, nil -} - -// Push publishes the branch. -func (m *Manager) Push(ctx context.Context, dir, branch string) error { - _, err := m.git(ctx, dir, "push", "-u", "origin", branch) - return err -} - -// ChangedLines reports lines added+deleted vs the base branch -- the -// input to the diff-size cap. -func (m *Manager) ChangedLines(ctx context.Context, dir, base string) (int, error) { - out, err := m.git(ctx, dir, "diff", "--shortstat", "origin/"+base+"...HEAD") - if err != nil { - return 0, err - } - total := 0 - for f := range strings.FieldsSeq(out) { - var n int - if _, err := fmt.Sscanf(f, "%d", &n); err == nil { - total += n - } - } - // The first field is the file count; subtract it back out. - if strings.TrimSpace(out) == "" { - return 0, nil - } - var files int - if _, err := fmt.Sscanf(strings.TrimSpace(out), "%d", &files); err != nil { - return 0, fmt.Errorf("parse git diff shortstat %q: %w", strings.TrimSpace(out), err) - } - return total - files, nil -} - -// Diff returns the unified diff of all committed changes against base. -func (m *Manager) Diff(ctx context.Context, dir, base string) (string, error) { - out, err := m.git(ctx, dir, "diff", "origin/"+base+"...HEAD") - if err != nil { - return "", err - } - return out, nil -} - -// ChangedFiles lists the repo-relative paths changed against base. -func (m *Manager) ChangedFiles(ctx context.Context, dir, base string) ([]string, error) { - out, err := m.git(ctx, dir, "diff", "--name-only", "origin/"+base+"...HEAD") - if err != nil { - return nil, err - } - var files []string - for line := range strings.SplitSeq(out, "\n") { - if line = strings.TrimSpace(line); line != "" { - files = append(files, line) - } - } - return files, nil -} - -// Cleanup removes a task's worktree (merged/rejected); parked worktrees -// are kept for post-mortems. -func (m *Manager) Cleanup(owner, repo string, issue int) error { - return os.RemoveAll(m.Dir(owner, repo, issue)) + return fmt.Sprintf("https://github.com/%s/%s.git", owner, repo) } diff --git a/internal/worktree/worktree_test.go b/internal/worktree/worktree_test.go index 0dc7464d2..9617274d3 100644 --- a/internal/worktree/worktree_test.go +++ b/internal/worktree/worktree_test.go @@ -3,653 +3,477 @@ package worktree import ( "context" "os" - "os/exec" "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" ) -// 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 //.git so cloneURL resolves it via -// file://. +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 //.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") - run := func(dir string, args ...string) { - t.Helper() - cmd := exec.Command("git", args...) - cmd.Dir = dir - cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0") - if out, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("git %v: %v\n%s", args, err, out) - } + if _, err := sr.CreateRemote(&gitconfig.RemoteConfig{ + Name: git.DefaultRemoteName, + URLs: []string{bare}, + }); err != nil { + t.Fatalf("add remote: %v", err) } - - if err := os.MkdirAll(bare, 0o755); err != nil { - t.Fatal(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) } - run(host, "init", "--bare", "-b", "main", bare) - if err := os.MkdirAll(seed, 0o755); err != nil { - t.Fatal(err) - } - run(seed, "init", "-b", "main") - run(seed, "config", "user.name", "seeder") - run(seed, "config", "user.email", "seed@example.com") - if err := os.WriteFile(filepath.Join(seed, "README.md"), []byte("seed\n"), 0o644); err != nil { - t.Fatal(err) - } - run(seed, "add", "-A") - run(seed, "commit", "-m", "seed") - run(seed, "push", "file://"+bare, "main") return host } -func TestPrepareCommitPushRoundTrip(t *testing.T) { - if _, err := exec.LookPath("git"); err != nil { - t.Skip("git not installed") +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) - m := &Manager{ - WorkDir: t.TempDir(), - Token: "unused-for-file-remotes", - BotUser: "archie-bot", - BotEmail: "archie-bot@example.com", - BaseURL: "file://" + host, - } - - dir, branch, err := m.Prepare(ctx, "acme", "todo", "main", 42, "feat: add test issue title", "", "") + dir, branch, err := m.Prepare(ctx, "acme", "todo", testBase, 7, "feat: add widget", "", "feature") if err != nil { - t.Fatalf("prepare: %v", err) + t.Fatalf("Prepare() error = %v", err) } - if branch != "feat/42-add-test-issue-title" { - t.Fatalf("branch = %q", branch) + 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) } - // Sentinel must exist after successful Prepare. - if _, err := os.Stat(filepath.Join(dir, ".archie-prepared")); err != nil { - t.Fatalf("sentinel missing after prepare: %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") } - // Clean tree commits nothing. - changed, err := m.CommitAll(ctx, dir, "noop") - if err != nil || changed { - t.Fatalf("clean CommitAll = (%v, %v)", changed, err) - } - - if err := os.WriteFile(filepath.Join(dir, "hello.txt"), []byte("one\ntwo\n"), 0o644); err != nil { + if err := os.WriteFile(filepath.Join(dir, "widget.txt"), []byte("one\ntwo\n"), 0o600); err != nil { t.Fatal(err) } - changed, err = m.CommitAll(ctx, dir, "add hello") - if err != nil || !changed { - t.Fatalf("CommitAll = (%v, %v)", changed, 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") } - lines, err := m.ChangedLines(ctx, dir, "main") + files, err := m.ChangedFiles(ctx, dir, testBase) if err != nil { - t.Fatalf("ChangedLines: %v", err) + 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.Fatalf("ChangedLines = %d, want 2", lines) + t.Errorf("ChangedLines() = %d, want 2", lines) } - files, err := m.ChangedFiles(ctx, dir, "main") + diff, err := m.Diff(ctx, dir, testBase) if err != nil { - t.Fatalf("ChangedFiles: %v", err) + t.Fatalf("Diff() error = %v", err) } - if len(files) != 1 || files[0] != "hello.txt" { - t.Fatalf("ChangedFiles = %v, want [hello.txt]", files) - } - - diff, err := m.Diff(ctx, dir, "main") - if err != nil { - t.Fatalf("Diff: %v", err) - } - if !strings.Contains(diff, "+one") || !strings.Contains(diff, "hello.txt") { - t.Fatalf("Diff missing expected content: %q", diff) + 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: %v", err) + t.Fatalf("Push() error = %v", err) } - - // The branch must exist on the remote after push. - cmd := exec.Command("git", "ls-remote", "--heads", - "file://"+filepath.Join(host, "acme", "todo.git"), branch) - out, err := cmd.CombinedOutput() - if err != nil || !strings.Contains(string(out), branch) { - t.Fatalf("branch not on remote: %v\n%s", err, out) + // 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) } - - // Committer identity must be the bot. - cmd = exec.Command("git", "log", "-1", "--format=%an <%ae>") - cmd.Dir = dir - out, _ = cmd.CombinedOutput() - if got := strings.TrimSpace(string(out)); got != "archie-bot " { - t.Fatalf("author = %q", got) - } - - if err := m.Cleanup("acme", "todo", 42); err != nil { - t.Fatal(err) - } - if _, err := os.Stat(dir); !os.IsNotExist(err) { - t.Fatal("cleanup left the worktree behind") + if _, err := remote.Reference(plumbing.NewBranchReferenceName(branch), true); err != nil { + t.Errorf("branch %q not present on remote after Push: %v", branch, err) } } -func TestPreparePersistentReusesRepoCacheAndCleanupExpiresIt(t *testing.T) { - if _, err := exec.LookPath("git"); err != nil { - t.Skip("git not installed") - } +func TestPrepareIsIdempotent(t *testing.T) { ctx := context.Background() host := newLocalRemote(t, "acme", "todo") - m := &Manager{ - WorkDir: t.TempDir(), - Token: "unused-for-file-remotes", - BotUser: "archie-bot", - BotEmail: "archie-bot@example.com", - BaseURL: "file://" + host, - } - const ttl = 24 * time.Hour + m := newManager(t, host) - first, _, err := m.PreparePersistent(ctx, "acme", "todo", "main", 41, "feat: first", "", "", ttl) + dir1, branch1, err := m.Prepare(ctx, "acme", "todo", testBase, 3, "fix: thing", "", "bug") if err != nil { - t.Fatalf("first persistent prepare: %v", err) + t.Fatalf("first Prepare() error = %v", err) } - cache := m.cacheDir("acme", "todo") - if st, err := os.Stat(filepath.Join(cache, "HEAD")); err != nil || st.IsDir() { - t.Fatalf("repo cache was not created at %s: %v", cache, err) - } - - second, _, err := m.PreparePersistent(ctx, "acme", "todo", "main", 42, "feat: second", "", "", ttl) - if err != nil { - t.Fatalf("second persistent prepare: %v", err) - } - if first == second { - t.Fatalf("persistent tasks share a mutable worktree: %s", first) - } - for _, dir := range []string{first, second} { - if _, err := os.Stat(filepath.Join(dir, ".git")); err != nil { - t.Fatalf("isolated worktree missing at %s: %v", dir, err) - } - } - - expired := time.Now().Add(-ttl - time.Hour) - if err := os.Chtimes(cache, expired, expired); err != nil { + // 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) } - count, err := m.CleanupExpiredCaches(ttl) + + dir2, branch2, err := m.Prepare(ctx, "acme", "todo", testBase, 3, "fix: thing", "", "bug") if err != nil { - t.Fatal(err) + t.Fatalf("second Prepare() error = %v", err) } - if count != 1 { - t.Fatalf("CleanupExpiredCaches count = %d, want 1", count) + if dir1 != dir2 || branch1 != branch2 { + t.Errorf("Prepare() not stable: (%q,%q) then (%q,%q)", dir1, branch1, dir2, branch2) } - if _, err := os.Stat(cache); !os.IsNotExist(err) { - t.Fatalf("expired repo cache still exists: %v", err) - } - for _, dir := range []string{first, second} { - if _, err := os.Stat(filepath.Join(dir, ".git")); err != nil { - t.Fatalf("cache cleanup removed or broke task worktree %s: %v", dir, err) - } + if _, err := os.Stat(marker); err != nil { + t.Error("second Prepare() re-cloned instead of reusing the prepared worktree") } } -func TestAskpassWrittenOnce(t *testing.T) { - m := &Manager{WorkDir: t.TempDir()} - - p1, err1 := m.askpass() - if err1 != nil { - t.Fatalf("first askpass: %v", err1) - } - if _, err := os.Stat(p1); err != nil { - t.Fatalf("askpass file not created: %v", err) - } - fi1, err := os.Stat(p1) - if err != nil { - t.Fatal(err) - } - - p2, err2 := m.askpass() - if err2 != nil { - t.Fatalf("second askpass: %v", err2) - } - if p1 != p2 { - t.Fatalf("paths differ: %q vs %q", p1, p2) - } - - // Mod time must be unchanged -- file was not rewritten. - fi2, err := os.Stat(p2) - if err != nil { - t.Fatal(err) - } - if !fi1.ModTime().Equal(fi2.ModTime()) { - t.Fatalf("askpass file was rewritten: mod time changed from %v to %v", fi1.ModTime(), fi2.ModTime()) - } - - // Content must be correct. - b, err := os.ReadFile(p1) - if err != nil { - t.Fatal(err) - } - want := "#!/bin/sh\necho \"$ARCHIE_GIT_TOKEN\"\n" - if string(b) != want { - t.Fatalf("content = %q, want %q", string(b), want) - } -} - -func TestPrepareResumesAfterInterruptedClone(t *testing.T) { - if _, err := exec.LookPath("git"); err != nil { - t.Skip("git not installed") - } +// 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", "interrupt") + host := newLocalRemote(t, "acme", "todo") + m := newManager(t, host) - m := &Manager{ - WorkDir: t.TempDir(), - Token: "unused-for-file-remotes", - BotUser: "archie-bot", - BotEmail: "archie-bot@example.com", - BaseURL: "file://" + host, - } - - dir := m.Dir("acme", "interrupt", 1) - - // Simulate an interrupted clone: create parent dir with a bare-bones - // .git directory (via git init + remote + fetch) but no checked-out - // files and no .archie-prepared sentinel. + dir := m.Dir("acme", "todo", 11) if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } - run := func(dir string, args ...string) { - t.Helper() - cmd := exec.Command("git", args...) - cmd.Dir = dir - cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0") - if out, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("git %v: %v\n%s", args, err, out) + 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) } } - run(dir, "init") - run(dir, "remote", "add", "origin", "file://"+filepath.Join(host, "acme", "interrupt.git")) - run(dir, "fetch", "origin") - - // Confirm the broken state: .git exists but no sentinel and no README.md. - if _, err := os.Stat(filepath.Join(dir, ".git")); err != nil { - t.Fatalf(".git missing in simulated broken clone: %v", err) - } - if _, err := os.Stat(filepath.Join(dir, ".archie-prepared")); !os.IsNotExist(err) { - t.Fatal("sentinel unexpectedly exists in broken clone") - } - if _, err := os.Stat(filepath.Join(dir, "README.md")); !os.IsNotExist(err) { - t.Fatal("working tree files unexpectedly exist in broken clone") - } - - // Prepare must detect the broken state, remove it, and do a fresh clone. - dir2, branch, err := m.Prepare(ctx, "acme", "interrupt", "main", 1, "feat: interrupted", "", "") - if err != nil { - t.Fatalf("prepare after interrupted clone: %v", err) - } - - // The sentinel must exist after a successful Prepare. - if _, err := os.Stat(filepath.Join(dir2, ".archie-prepared")); err != nil { - t.Fatalf("sentinel missing after prepare: %v", err) - } - - // The working tree must be fully functional: files from the remote exist. - if _, err := os.Stat(filepath.Join(dir2, "README.md")); err != nil { - t.Fatalf("working tree missing README.md after prepare: %v", err) - } - - // CommitAll must work -- the sentinel is excluded from git tracking. - changed, err := m.CommitAll(ctx, dir2, "noop") - if err != nil || changed { - t.Fatalf("clean CommitAll = (%v, %v), want (false, nil)", changed, err) - } - - // Push must work. - if err := m.Push(ctx, dir2, branch); err != nil { - t.Fatalf("push: %v", err) - } - - // A second Prepare call must short-circuit via the sentinel -- no re-clone. - dir3, _, err := m.Prepare(ctx, "acme", "interrupt", "main", 1, "feat: interrupted", "", "") - if err != nil { - t.Fatalf("second prepare: %v", err) - } - if dir3 != dir2 { - t.Fatalf("second prepare created a different directory: %q vs %q", dir3, dir2) - } - - if err := m.Cleanup("acme", "interrupt", 1); err != nil { - t.Fatal(err) - } - if _, err := os.Stat(dir3); !os.IsNotExist(err) { - t.Fatal("cleanup left the worktree behind") + if len(files) != 1 || files[0] != "mine.txt" { + t.Errorf("ChangedFiles() = %v, want [mine.txt]", files) } } -func TestCacheDirCannotEscapeWorkDir(t *testing.T) { +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()} - cache := m.cacheDir("../outside", `..\also-outside`) - rel, err := filepath.Rel(filepath.Join(m.WorkDir, ".repo-cache"), cache) + 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 rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { - t.Fatalf("cache path escaped managed root: %s", cache) - } -} - -// --------------------------------------------------------------------------- -// Repro tests for uncovered code paths (issue #58) -// --------------------------------------------------------------------------- - -// TestPrepareIdempotentFastPath proves the "already prepared" short-circuit -// in prepare(). The second call must return the same dir/branch without -// error, and the worktree must remain functional (fetch+checkout+reset). -func TestPrepareIdempotentFastPath(t *testing.T) { - if _, err := exec.LookPath("git"); err != nil { - t.Skip("git not installed") - } - ctx := context.Background() - host := newLocalRemote(t, "acme", "idem") - - m := &Manager{ - WorkDir: t.TempDir(), - Token: "unused-for-file-remotes", - BotUser: "archie-bot", - BotEmail: "archie-bot@example.com", - BaseURL: "file://" + host, + if _, err := m.CommitAll(ctx, dir, "feat: thing"); err != nil { + t.Fatalf("CommitAll() error = %v", err) } - dir1, branch1, err := m.Prepare(ctx, "acme", "idem", "main", 1, "feat: idempotent", "", "") + files, err := m.ChangedFiles(ctx, dir, testBase) if err != nil { - t.Fatalf("first prepare: %v", err) + t.Fatalf("ChangedFiles() error = %v", err) } - if _, err := os.Stat(filepath.Join(dir1, ".git")); err != nil { - t.Fatalf("worktree not created: %v", err) + for _, f := range files { + if strings.Contains(f, "archie-prepared") { + t.Errorf("the prepared sentinel was committed: ChangedFiles() = %v", files) + } } - // Second Prepare with the same arguments must hit the fast path. - dir2, branch2, err := m.Prepare(ctx, "acme", "idem", "main", 1, "feat: idempotent", "", "") - if err != nil { - t.Fatalf("second prepare (fast path): %v", err) - } - if dir1 != dir2 { - t.Fatalf("fast path returned different dir: %q vs %q", dir1, dir2) - } - if branch1 != branch2 { - t.Fatalf("fast path returned different branch: %q vs %q", branch1, branch2) - } - - // The worktree must still be usable after the fast path. - if _, err := os.Stat(filepath.Join(dir2, ".git")); err != nil { - t.Fatalf("fast path broke .git: %v", err) - } - - // After the fast path, we should be able to commit and push. - if err := os.WriteFile(filepath.Join(dir2, "fast.txt"), []byte("fast path\n"), 0o644); err != nil { - t.Fatal(err) - } - changed, err := m.CommitAll(ctx, dir2, "test fast path") - if err != nil { - t.Fatalf("CommitAll after fast path: %v", err) - } - if !changed { - t.Fatal("CommitAll after fast path reported clean tree, expected changes") - } - if err := m.Push(ctx, dir2, branch2); err != nil { - t.Fatalf("Push after fast path: %v", err) - } -} - -// TestPrepareBadBaseBranch proves that prepare() propagates git clone -// failures when the requested base branch does not exist. -func TestPrepareBadBaseBranch(t *testing.T) { - if _, err := exec.LookPath("git"); err != nil { - t.Skip("git not installed") - } - ctx := context.Background() - host := newLocalRemote(t, "acme", "badbase") - - m := &Manager{ - WorkDir: t.TempDir(), - Token: "unused-for-file-remotes", - BotUser: "archie-bot", - BotEmail: "archie-bot@example.com", - BaseURL: "file://" + host, - } - - _, _, err := m.Prepare(ctx, "acme", "badbase", "nonexistent-branch", 1, "feat: test", "", "") - if err == nil { - t.Fatal("expected error for nonexistent base branch, got nil") - } - // The error must mention the git command ("clone") and the bad branch. - if !strings.Contains(err.Error(), "clone") { - t.Fatalf("error should mention 'clone', got: %v", err) - } - if !strings.Contains(err.Error(), "nonexistent-branch") { - t.Fatalf("error should mention bad branch, got: %v", err) - } -} - -// TestPrepareBadCloneURL proves that prepare() propagates git clone -// failures when the clone URL points to a nonexistent repository. -func TestPrepareBadCloneURL(t *testing.T) { - if _, err := exec.LookPath("git"); err != nil { - t.Skip("git not installed") - } - ctx := context.Background() - - m := &Manager{ - WorkDir: t.TempDir(), - Token: "unused-for-file-remotes", - BotUser: "archie-bot", - BotEmail: "archie-bot@example.com", - BaseURL: "file:///nonexistent/path/that/does/not/exist", - } - - _, _, err := m.Prepare(ctx, "acme", "norepo", "main", 1, "feat: test", "", "") - if err == nil { - t.Fatal("expected error for bad clone URL, got nil") - } - // The error must mention the git command ("clone"). - if !strings.Contains(err.Error(), "clone") { - t.Fatalf("error should mention 'clone', got: %v", err) - } -} - -// TestCommitAllInNonGitDir proves that CommitAll returns an error when -// invoked in a directory that is not a git repository. -func TestCommitAllInNonGitDir(t *testing.T) { - if _, err := exec.LookPath("git"); err != nil { - t.Skip("git not installed") - } - ctx := context.Background() - - m := &Manager{ - WorkDir: t.TempDir(), - Token: "unused", - BotUser: "bot", - BotEmail: "bot@example.com", - } - - dir := t.TempDir() // plain directory, no .git - changed, err := m.CommitAll(ctx, dir, "should fail") - if err == nil { - t.Fatalf("expected error for CommitAll in non-git dir, got (changed=%v, err=nil)", changed) - } - if changed { - t.Fatal("CommitAll in non-git dir reported changes, expected false") - } -} - -// TestChangedLinesInNonGitDir proves that ChangedLines returns an error -// when invoked in a directory that is not a git repository. -func TestChangedLinesInNonGitDir(t *testing.T) { - if _, err := exec.LookPath("git"); err != nil { - t.Skip("git not installed") - } - ctx := context.Background() - - m := &Manager{ - WorkDir: t.TempDir(), - Token: "unused", - BotUser: "bot", - BotEmail: "bot@example.com", - } - - dir := t.TempDir() // plain directory, no .git - lines, err := m.ChangedLines(ctx, dir, "main") - if err == nil { - t.Fatalf("expected error for ChangedLines in non-git dir, got (lines=%d, err=nil)", lines) - } - if lines != 0 { - t.Fatalf("ChangedLines in non-git dir returned non-zero lines: %d", lines) - } -} - -// TestDiffInNonGitDir proves that Diff returns an error when invoked -// in a directory that is not a git repository. -func TestDiffInNonGitDir(t *testing.T) { - if _, err := exec.LookPath("git"); err != nil { - t.Skip("git not installed") - } - ctx := context.Background() - - m := &Manager{ - WorkDir: t.TempDir(), - Token: "unused", - BotUser: "bot", - BotEmail: "bot@example.com", - } - - dir := t.TempDir() // plain directory, no .git - diff, err := m.Diff(ctx, dir, "main") - if err == nil { - t.Fatalf("expected error for Diff in non-git dir, got diff=%q, err=nil", diff) - } - if diff != "" { - t.Fatalf("Diff in non-git dir returned non-empty output: %q", diff) - } -} - -// TestPushRejected proves that Push returns an error when the remote -// rejects the push (e.g. non-fast-forward). -func TestPushRejected(t *testing.T) { - if _, err := exec.LookPath("git"); err != nil { - t.Skip("git not installed") - } - ctx := context.Background() - host := newLocalRemote(t, "acme", "reject") - - m := &Manager{ - WorkDir: t.TempDir(), - Token: "unused-for-file-remotes", - BotUser: "archie-bot", - BotEmail: "archie-bot@example.com", - BaseURL: "file://" + host, - } - - dir, branch, err := m.Prepare(ctx, "acme", "reject", "main", 1, "feat: push test", "", "") - if err != nil { - t.Fatalf("prepare: %v", err) - } - - // Push once so the branch exists on the remote. - if err := os.WriteFile(filepath.Join(dir, "first.txt"), []byte("first\n"), 0o644); err != nil { - t.Fatal(err) - } - if _, err := m.CommitAll(ctx, dir, "first commit"); err != nil { - t.Fatal(err) - } - if err := m.Push(ctx, dir, branch); err != nil { - t.Fatalf("first push: %v", err) - } - - // Now advance the remote branch independently (simulate another - // client pushing), then try to push again from the stale local branch. - // We do this by creating a second worktree that pushes to the same - // branch, making the first worktree's branch out of date. - dir2, _, err := m.Prepare(ctx, "acme", "reject", "main", 2, "feat: conflicting", "", "") - if err != nil { - t.Fatalf("second prepare: %v", err) - } - // Checkout the same branch in the second worktree. - if _, err := exec.Command("git", "-C", dir2, "fetch", "origin", branch).CombinedOutput(); err != nil { - t.Fatalf("fetch branch in second worktree: %v", err) - } - if _, err := exec.Command("git", "-C", dir2, "checkout", "-b", branch, "origin/"+branch).CombinedOutput(); err != nil { - t.Fatalf("checkout branch in second worktree: %v", err) - } - if err := os.WriteFile(filepath.Join(dir2, "second.txt"), []byte("second\n"), 0o644); err != nil { - t.Fatal(err) - } - // Commit and push from the second worktree using raw git to bypass Manager. - cmd := exec.Command("git", "-C", dir2, "add", "-A") - cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0") - if out, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("add in second worktree: %v\n%s", err, out) - } - cmd = exec.Command("git", "-C", dir2, "commit", "-m", "second commit") - cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0") - if out, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("commit in second worktree: %v\n%s", err, out) - } - cmd = exec.Command("git", "-C", dir2, "push", "origin", branch) - cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0") - if out, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("push from second worktree: %v\n%s", err, out) - } - - // Now the first worktree's branch is behind origin/branch. - // Make a change and try to push -- it should be rejected. - if err := os.WriteFile(filepath.Join(dir, "third.txt"), []byte("third\n"), 0o644); err != nil { - t.Fatal(err) - } - if _, err := m.CommitAll(ctx, dir, "third commit"); err != nil { - t.Fatal(err) - } - err = m.Push(ctx, dir, branch) - if err == nil { - t.Fatal("expected push rejection due to non-fast-forward, got nil error") - } -} - -// TestGitErrorWrapping proves that git() wraps errors with the command -// name and preserves the underlying error via %w. -func TestGitErrorWrapping(t *testing.T) { - if _, err := exec.LookPath("git"); err != nil { - t.Skip("git not installed") - } - ctx := context.Background() - - m := &Manager{ - WorkDir: t.TempDir(), - Token: "unused", - BotUser: "bot", - BotEmail: "bot@example.com", - } - - dir := t.TempDir() // plain directory, no .git - _, err := m.CommitAll(ctx, dir, "test message") - if err == nil { - t.Fatal("expected error from git in non-repo dir") - } - - // The error must contain "git" and the specific command ("add"). - errStr := err.Error() - if !strings.Contains(errStr, "git") { - t.Fatalf("error should contain 'git' prefix: %v", err) - } - if !strings.Contains(errStr, "add") { - t.Fatalf("error should mention the failing command 'add': %v", err) + // 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) } }