refactor(worktree): replace git shell-out with go-git v6
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.
This commit is contained in:
1 parent
529b25eada
commit
f800e1de36
5 files changed
+777
-853
No files matched your search
@@ -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
|
||||
|
||||
@@ -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=
|
||||
|
||||
@@ -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())
|
||||
|
||||
+337
-267
@@ -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/<base>"
|
||||
// revision is tried second so that resolution stays as permissive as
|
||||
// `git diff origin/<base>...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)
|
||||
}
|
||||
+393
-569
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user