Files
archie/internal/worktree/worktree.go
T

1315 lines
42 KiB
Go

// Package worktree performs archied's git operations with go-git: clone,
// branch, commit, push, diff and cleanup. Each task gets its own clone, which
// borrows objects from a per-repository mirror.
package worktree
import (
"context"
"encoding/base64"
"errors"
"fmt"
"io"
"io/fs"
"log/slog"
"os"
"path/filepath"
"slices"
"strings"
"time"
"github.com/go-git/go-billy/v6"
"github.com/go-git/go-billy/v6/osfs"
"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/filemode"
fdiff "github.com/go-git/go-git/v6/plumbing/format/diff"
"github.com/go-git/go-git/v6/plumbing/object"
githttp "github.com/go-git/go-git/v6/plumbing/transport/http"
"github.com/go-git/go-git/v6/storage/memory"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
)
// preparedSentinel marks a worktree as prepared, making Prepare idempotent.
// It lives in .git so it is never committed.
const preparedSentinel = ".git/archie-prepared"
type Manager struct {
// WorkDir is the root under which task worktrees are created.
WorkDir string
// 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
AuthUser string
BotUser string
BotEmail string
// Signer signs bot commits; nil leaves them unsigned.
Signer git.Signer
// BaseURL overrides the forge host. Set from config [forge].host.
// Empty falls back to https://github.com.
BaseURL string
// Log optionally receives diagnostics and non-fatal warnings from
// worktree operations.
Log *slog.Logger
}
func (m *Manager) logger() *slog.Logger {
if m != nil && m.Log != nil {
return m.Log
}
return slog.New(slog.DiscardHandler)
}
// OrgWorkDir confines an organisation's worktrees beneath root, including
// legacy organisation IDs that contain filesystem separators.
func OrgWorkDir(root, org string) string {
return filepath.Join(root, "org-"+base64.RawURLEncoding.EncodeToString([]byte(org)))
}
// Dir is the worktree path for a task.
func (m *Manager) Dir(owner, repo string, issue int) string {
ownerKey := base64.RawURLEncoding.EncodeToString([]byte(owner))
repoKey := base64.RawURLEncoding.EncodeToString([]byte(repo))
return filepath.Join(m.WorkDir, ownerKey, repoKey, fmt.Sprintf("issue-%d", issue))
}
// ValidCoordinates reports whether owner, repo, and issue can safely identify
// a task worktree without being interpreted as filesystem navigation.
func ValidCoordinates(owner, repo string, issue int) bool {
return owner != "" && repo != "" && issue > 0 &&
filepath.Base(owner) == owner && filepath.Base(repo) == repo &&
owner != "." && owner != ".." && repo != "." && repo != ".."
}
// auth returns the HTTP credential, or nil for anonymous access.
func (m *Manager) auth() []gitclient.Option {
if m.Token == "" {
return nil
}
username := m.AuthUser
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}),
}
}
// 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()}
}
// Target is the branch a worktree is positioned on. Empty (Fresh) resets onto
// origin/<base>; a branch name resumes onto origin/<branch>.
type Target string
// Fresh is the fresh-run target: reset onto origin/<base>.
const Fresh Target = ""
// Keep leaves a prepared worktree as it stands, uncommitted changes included,
// so a run resumes where the last one stopped. A missing worktree is cloned
// fresh.
const Keep Target = "\x00keep"
// Prepare clones the task's worktree if missing and positions it on target,
// returning its directory and branch. A resume fails if the branch is not on
// the remote.
func (m *Manager) Prepare(
ctx context.Context,
owner, repo, base string,
issue int,
title, body, labels string,
target Target,
) (dir, branch string, err error) {
if !ValidCoordinates(owner, repo, issue) {
return "", "", fmt.Errorf("invalid worktree coordinates")
}
dir = m.Dir(owner, repo, issue)
branch = archieBranch(issue, title, labels)
if target != Fresh && target != Keep {
branch = string(target)
}
prepared := false
if _, statErr := os.Stat(filepath.Join(dir, preparedSentinel)); statErr == nil {
prepared = true
} else {
migrated, migrateErr := m.migrateLegacy(owner, repo, issue, dir)
if migrateErr != nil {
return "", "", migrateErr
}
prepared = migrated
}
if prepared && !repositoryUsesURL(dir, m.cloneURL(owner, repo)) {
return "", "", fmt.Errorf("the worktree belongs to a different repository connection; archive it before starting again")
}
if !prepared {
if err := m.createClone(ctx, owner, repo, base, dir); err != nil {
return "", "", err
}
}
if target == Keep && prepared {
return dir, branch, nil
}
if target == Keep {
target = Fresh
}
if err := m.sync(ctx, dir, base, branch, target); err != nil {
return "", "", err
}
return dir, branch, nil
}
// createClone removes whatever sits at dir, clones the task's repository there,
// records the bot identity and marks the worktree prepared. Prepare calls it
// only for a worktree it could not reuse.
func (m *Manager) createClone(ctx context.Context, owner, repo, base, dir string) error {
if err := os.RemoveAll(dir); err != nil {
return fmt.Errorf("clear stale worktree: %w", err)
}
if err := os.MkdirAll(filepath.Dir(dir), 0o755); err != nil {
return fmt.Errorf("create worktree parent: %w", err)
}
// The lock spans the clone too, so PruneMirrors cannot remove the mirror
// between its refresh and the clone that borrows from it.
unlock, err := lockMirror(m.mirrorDir(owner, repo), true)
if err != nil {
return err
}
defer unlock()
mirror, err := m.ensureMirror(ctx, owner, repo)
if err != nil {
return err
}
r, err := m.cloneFromMirror(mirror, owner, repo, base, dir)
if err != nil {
return fmt.Errorf("clone %s/%s: %w", owner, repo, err)
}
if err := m.setIdentity(r); err != nil {
return err
}
return writeSentinel(dir)
}
// mirrorDir is the bare repository whose objects every task clone of
// owner/repo shares.
func (m *Manager) mirrorDir(owner, repo string) string {
return filepath.Join(filepath.Dir(m.Dir(owner, repo, 1)), ".mirror")
}
// ensureMirror brings the repository's shared mirror up to date, creating it
// on first use and recreating it when it tracks a different URL. The caller
// holds the mirror lock.
func (m *Manager) ensureMirror(ctx context.Context, owner, repo string) (string, error) {
dir := m.mirrorDir(owner, repo)
url := m.cloneURL(owner, repo)
if !repositoryUsesURL(dir, url) {
if err := m.createMirror(ctx, dir, url); err != nil {
return "", fmt.Errorf("mirror %s/%s: %w", owner, repo, err)
}
return dir, nil
}
if err := m.fetchMirror(ctx, dir); err != nil {
if ctx.Err() != nil {
return "", fmt.Errorf("refresh mirror %s/%s: %w", owner, repo, err)
}
// A fetch cut short (full disk) can leave a corrupt ref that fails every
// later fetch. The mirror is a cache and createMirror swaps it in only
// after a complete fetch, so rebuild it.
if cerr := m.createMirror(ctx, dir, url); cerr != nil {
return "", fmt.Errorf("refresh mirror %s/%s: %w; rebuild: %w", owner, repo, err, cerr)
}
}
return dir, nil
}
// rebuildMirror recreates an existing mirror from the URL it tracks.
func (m *Manager) rebuildMirror(ctx context.Context, dir string) error {
r, err := openRepo(dir)
if err != nil {
return fmt.Errorf("open: %w", err)
}
cfg, err := r.Config()
if err != nil {
return err
}
remote, ok := cfg.Remotes[git.DefaultRemoteName]
if !ok || len(remote.URLs) == 0 {
return errors.New("mirror has no remote")
}
unlock, err := lockMirror(dir, true)
if err != nil {
return err
}
defer unlock()
return m.createMirror(ctx, dir, remote.URLs[0])
}
func (m *Manager) fetchMirror(ctx context.Context, dir string) error {
r, err := openRepo(dir)
if err != nil {
return fmt.Errorf("open: %w", err)
}
// Force: the mirror copies the forge, so a moved tag or rewritten branch
// replaces the local ref instead of failing every later fetch.
if err := r.FetchContext(ctx, &git.FetchOptions{ClientOptions: m.auth(), Tags: plumbing.AllTags, Force: true}); err != nil &&
!errors.Is(err, git.NoErrAlreadyUpToDate) {
return fmt.Errorf("fetch: %w", err)
}
return nil
}
// createMirror builds a bare repository tracking every branch of url. It is
// assembled beside dir and renamed into place so a failed fetch leaves nothing
// that looks usable.
func (m *Manager) createMirror(ctx context.Context, dir, url string) error {
tmp := dir + ".tmp"
if err := os.RemoveAll(tmp); err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(dir), 0o755); err != nil {
return err
}
r, err := git.PlainInit(tmp, true)
if err != nil {
return err
}
if _, err := r.CreateRemote(&gitconfig.RemoteConfig{
Name: git.DefaultRemoteName,
URLs: []string{url},
Fetch: []gitconfig.RefSpec{"+refs/heads/*:refs/heads/*"},
}); err != nil {
return err
}
if err := r.FetchContext(ctx, &git.FetchOptions{ClientOptions: m.auth(), Tags: plumbing.AllTags}); err != nil {
_ = os.RemoveAll(tmp)
return err
}
if err := os.RemoveAll(dir); err != nil {
return err
}
return os.Rename(tmp, dir)
}
// cloneFromMirror creates a clone at dir that borrows the mirror's objects via
// alternates and copies its branches in as origin/*, so nothing is transferred.
// Origin is the real forge, so fetch and push behave as for a direct clone.
// (go-git's own Shared clone still downloads a full pack from the mirror.)
func (m *Manager) cloneFromMirror(mirror, owner, repo, base, dir string) (*git.Repository, error) {
r, err := initSharedRepo(mirror, dir)
if err != nil {
return nil, err
}
if _, err := r.CreateRemote(&gitconfig.RemoteConfig{
Name: git.DefaultRemoteName,
URLs: []string{m.cloneURL(owner, repo)},
Fetch: []gitconfig.RefSpec{gitconfig.RefSpec(fmt.Sprintf(gitconfig.DefaultFetchRefSpec, git.DefaultRemoteName))},
}); err != nil {
return nil, err
}
tip, err := importBranches(mirror, r, base)
if err != nil {
return nil, err
}
branch := plumbing.NewBranchReferenceName(base)
if err := r.Storer.SetReference(plumbing.NewHashReference(branch, tip)); err != nil {
return nil, err
}
if err := r.Storer.SetReference(plumbing.NewSymbolicReference(plumbing.HEAD, branch)); err != nil {
return nil, err
}
if err := r.CreateBranch(&gitconfig.Branch{Name: base, Remote: git.DefaultRemoteName, Merge: branch}); err != nil {
return nil, err
}
wt, err := r.Worktree()
if err != nil {
return nil, err
}
if err := wt.Reset(&git.ResetOptions{Mode: git.HardReset, Commit: tip}); err != nil {
return nil, err
}
return r, nil
}
// initSharedRepo creates an empty repository at dir whose object store falls
// back to the mirror's.
func initSharedRepo(mirror, dir string) (*git.Repository, error) {
r, err := git.PlainInit(dir, false)
if err != nil {
return nil, err
}
alt, ok := r.Storer.(interface{ AddAlternate(string) error })
if !ok {
return nil, errors.New("object storage does not support alternates")
}
if err := alt.AddAlternate(mirror); err != nil {
return nil, err
}
// Reopen so the object store sees the alternates file.
return openRepo(dir)
}
// importBranches records each mirror branch as origin/<name> in r and returns
// the tip of base.
func importBranches(mirror string, r *git.Repository, base string) (plumbing.Hash, error) {
src, err := openRepo(mirror)
if err != nil {
return plumbing.ZeroHash, err
}
refs, err := src.Branches()
if err != nil {
return plumbing.ZeroHash, err
}
defer refs.Close()
var tip plumbing.Hash
if err := refs.ForEach(func(ref *plumbing.Reference) error {
if ref.Name() == plumbing.NewBranchReferenceName(base) {
tip = ref.Hash()
}
return r.Storer.SetReference(plumbing.NewHashReference(
plumbing.NewRemoteReferenceName(git.DefaultRemoteName, ref.Name().Short()), ref.Hash()))
}); err != nil {
return plumbing.ZeroHash, err
}
if tip.IsZero() {
return plumbing.ZeroHash, fmt.Errorf("%w: %s", ErrBaseBranchNotFound, base)
}
return tip, nil
}
// sync positions an already-prepared worktree: Fresh resets it onto
// origin/<base>; a resume resets it onto origin/<branch>. Both fetch first and
// clear the worktree against the target tree before committing to it.
func (m *Manager) sync(ctx context.Context, dir, base, branch string, target Target) error {
if target == Fresh {
return m.refresh(ctx, dir, base, branch)
}
return m.resume(ctx, dir, branch)
}
func (m *Manager) legacyDir(owner, repo string, issue int) string {
return filepath.Join(m.WorkDir, owner+"-"+repo, fmt.Sprintf("issue-%d", issue))
}
// migrateLegacy preserves worktrees created before repository coordinates
// were encoded as separate path components. The old flattened key was
// ambiguous, so migration is allowed only when the clone's origin exactly
// matches the requested repository; an uncertain directory is left untouched.
func (m *Manager) migrateLegacy(owner, repo string, issue int, dir string) (bool, error) {
legacy := m.legacyDir(owner, repo, issue)
if _, err := os.Stat(filepath.Join(legacy, preparedSentinel)); err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, fmt.Errorf("inspect legacy worktree: %w", err)
}
if !repositoryUsesURL(legacy, m.cloneURL(owner, repo)) {
return false, nil
}
if err := os.MkdirAll(filepath.Dir(dir), 0o755); err != nil {
return false, fmt.Errorf("create migrated worktree parent: %w", err)
}
if err := os.Rename(legacy, dir); err != nil {
return false, fmt.Errorf("migrate legacy worktree: %w", err)
}
_ = os.Remove(filepath.Dir(legacy))
return true, nil
}
func repositoryUsesURL(dir, want string) bool {
r, err := openRepo(dir)
if err != nil {
return false
}
cfg, err := r.Config()
if err != nil {
return false
}
remote, ok := cfg.Remotes[git.DefaultRemoteName]
if !ok {
return false
}
return slices.Contains(remote.URLs, want)
}
// 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 := openRepo(dir)
if err != nil {
return fmt.Errorf("open prepared worktree: %w", err)
}
if err := r.FetchContext(ctx, &git.FetchOptions{
RemoteName: git.DefaultRemoteName,
ClientOptions: m.auth(),
}); err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return fmt.Errorf("fetch origin: %w", err)
}
// Reset onto the freshly fetched base. This is a prepared clone, so a
// missing base is configuration or repository drift and must fail closed
// -- and it is resolved before the first mutation, so a failure here
// leaves the worktree as it was rather than half-refreshed.
baseHash, err := resolveBase(r, base)
if err != nil {
return err
}
return resetOnto(r, dir, branch, baseHash, remoteBase(base))
}
// resume resets an existing worktree onto origin/<branch>.
func (m *Manager) resume(ctx context.Context, dir, branch string) error {
r, err := openRepo(dir)
if err != nil {
return fmt.Errorf("open prepared worktree: %w", err)
}
if err := r.FetchContext(ctx, &git.FetchOptions{
RemoteName: git.DefaultRemoteName,
ClientOptions: m.auth(),
}); err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return fmt.Errorf("fetch origin: %w", err)
}
tip, err := r.ResolveRevision(plumbing.Revision(remoteBase(branch)))
if err != nil {
return fmt.Errorf("resolve remote tip of %s: %w", branch, err)
}
return resetOnto(r, dir, branch, *tip, remoteBase(branch))
}
// resetOnto removes everything the target tree does not contain, then points
// branch at commit with a clean tree.
func resetOnto(r *git.Repository, dir, branch string, commit plumbing.Hash, label string) error {
target, err := commitTree(r, commit)
if err != nil {
return err
}
if err := cleanUntracked(dir, target); err != nil {
return fmt.Errorf("clear abandoned worktree files: %w", err)
}
wt, err := r.Worktree()
if err != nil {
return fmt.Errorf("open worktree: %w", err)
}
if err := checkOutBranch(r, wt, branch, commit); err != nil {
return err
}
if err := wt.Reset(&git.ResetOptions{Commit: commit, Mode: git.HardReset}); err != nil {
return fmt.Errorf("reset to %s: %w", label, err)
}
return nil
}
// checkOutBranch checks out branch, creating it at commit if it does not
// exist.
func checkOutBranch(r *git.Repository, wt *git.Worktree, branch string, commit plumbing.Hash) error {
ref := plumbing.NewBranchReferenceName(branch)
opts := &git.CheckoutOptions{Branch: ref, Force: true}
switch _, err := r.Reference(ref, false); {
case err == nil:
case errors.Is(err, plumbing.ErrReferenceNotFound):
opts.Create = true
opts.Hash = commit
default:
return fmt.Errorf("resolve branch %s: %w", branch, err)
}
if err := wt.Checkout(opts); err != nil {
return fmt.Errorf("checkout branch %s: %w", branch, err)
}
return nil
}
// commitTree loads the tree of one commit, for the caller that has to know
// which paths the worktree is about to be reset onto.
func commitTree(r *git.Repository, commit plumbing.Hash) (*object.Tree, error) {
c, err := r.CommitObject(commit)
if err != nil {
return nil, fmt.Errorf("load commit %s: %w", commit, err)
}
tree, err := c.Tree()
if err != nil {
return nil, fmt.Errorf("load tree of %s: %w", commit, err)
}
return tree, nil
}
// cleanUntracked removes every path under dir that tree does not contain.
func cleanUntracked(dir string, tree *object.Tree) error {
files := make(map[string]struct{})
dirs := make(map[string]struct{})
opaqueDirs := make(map[string]struct{})
if err := collectTrackedPaths(tree, "", files, dirs, opaqueDirs); err != nil {
return err
}
// The agent writes this tree, so every operation goes through a root that
// refuses to follow a symlink out of it, even one swapped in mid-walk.
root, err := os.OpenRoot(dir)
if err != nil {
return err
}
defer func() { _ = root.Close() }()
return fs.WalkDir(root.FS(), ".", func(rel string, entry fs.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if rel == "." {
return nil
}
if rel == ".git" {
// Leave a .git file or directory alone.
if entry.IsDir() {
return filepath.SkipDir
}
return nil
}
if entry.IsDir() {
if _, ok := opaqueDirs[rel]; ok {
return filepath.SkipDir
}
if _, ok := dirs[rel]; ok {
return nil
}
// A directory the tree does not declare as one -- including a
// directory sitting on a path the tree declares as a FILE -- is
// output, not tracked content. It has to go before the reset can
// write the file this path is supposed to hold.
if err := root.RemoveAll(rel); err != nil {
return err
}
return filepath.SkipDir
}
if _, ok := files[rel]; ok {
return nil
}
return root.Remove(rel)
})
}
func collectTrackedPaths(tree *object.Tree, prefix string, files, dirs, opaqueDirs map[string]struct{}) error {
for _, entry := range tree.Entries {
path := filepath.ToSlash(filepath.Join(prefix, entry.Name))
switch entry.Mode {
case filemode.Dir:
dirs[path] = struct{}{}
child, err := tree.Tree(entry.Name)
if err != nil {
return fmt.Errorf("load tracked directory %s: %w", path, err)
}
if err := collectTrackedPaths(child, path, files, dirs, opaqueDirs); err != nil {
return err
}
case filemode.Submodule:
opaqueDirs[path] = struct{}{}
default:
files[path] = struct{}{}
}
}
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
// go-git signs through Signer; a user-level gpgsign must not apply.
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 { //nolint:gosec // G306: the task container reads the sentinel as another user.
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(ctx context.Context, dir, message string) (bool, error) {
if err := ctx.Err(); err != nil {
return false, err
}
r, err := openRepo(dir)
if err != nil {
return false, fmt.Errorf("open worktree: %w", err)
}
if err := ctx.Err(); err != nil {
return false, err
}
wt, err := r.Worktree()
if err != nil {
return false, fmt.Errorf("open worktree: %w", err)
}
if err := ctx.Err(); err != nil {
return false, err
}
if err := wt.AddWithOptions(&git.AddOptions{All: true}); err != nil {
return false, fmt.Errorf("stage changes: %w", err)
}
if err := ctx.Err(); err != nil {
return false, err
}
status, err := wt.Status()
if err != nil {
return false, fmt.Errorf("read status: %w", err)
}
if status.IsClean() {
return false, nil
}
if err := ctx.Err(); err != nil {
return false, err
}
sig := m.signature()
if _, err := wt.Commit(message, &git.CommitOptions{Author: sig, Committer: sig, Signer: m.Signer}); 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 {
return m.push(ctx, dir, branch, "")
}
// PushRepository pins the destination to the authorised connection rather
// than trusting origin in a container-writable Git configuration.
func (m *Manager) PushRepository(ctx context.Context, owner, repo string, issue int, branch string) error {
return m.push(ctx, m.Dir(owner, repo, issue), branch, m.cloneURL(owner, repo))
}
func (m *Manager) push(ctx context.Context, dir, branch, remoteURL string) error {
r, err := openRepo(dir)
if err != nil {
return fmt.Errorf("open worktree: %w", err)
}
if m.Token == "" && remoteUsesHTTP(r) {
return fmt.Errorf("push %s: no forge credential configured; "+
"check the repository connection's credential", branch)
}
ref := plumbing.NewBranchReferenceName(branch)
if err := m.attest(ctx, r, ref); err != nil {
return fmt.Errorf("attest %s: %w", branch, err)
}
spec := gitconfig.RefSpec(fmt.Sprintf("%s:%s", ref, ref))
publish := func() error {
return r.PushContext(ctx, &git.PushOptions{
RemoteName: git.DefaultRemoteName,
RemoteURL: remoteURL,
RefSpecs: []gitconfig.RefSpec{spec},
ClientOptions: m.auth(),
})
}
err = publish()
if errors.Is(err, plumbing.ErrObjectNotFound) {
// The clone borrows objects from the shared mirror; a damaged mirror
// loses them. The forge still has every base object, so rebuild it.
m.logger().WarnContext(ctx, "push hit a missing object, rebuilding the mirror", "branch", branch, "err", err)
if rerr := m.rebuildMirror(ctx, filepath.Join(filepath.Dir(dir), mirrorName)); rerr != nil {
return fmt.Errorf("push %s: %w; rebuild mirror: %w", branch, err, rerr)
}
err = publish()
}
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return fmt.Errorf("push %s: %w", branch, err)
}
// The tracking ref marks what is published, so the next attest leaves
// these commits alone.
if pushed, err := r.Reference(ref, true); err == nil {
tracking := plumbing.NewRemoteReferenceName(git.DefaultRemoteName, branch)
if err := r.Storer.SetReference(plumbing.NewHashReference(tracking, pushed.Hash())); err != nil {
m.logger().WarnContext(ctx, "record pushed branch", "branch", branch, "err", err)
}
}
// Record the upstream so the branch tracks origin, matching what
// `git push -u` left behind previously. A post-publication metadata
// failure must not cause push to report an error.
if err := m.recordUpstream(r, branch, ref); err != nil {
m.logger().WarnContext(ctx, "record branch upstream", "branch", branch, "err", err)
}
return nil
}
func (m *Manager) recordUpstream(r *git.Repository, branch string, ref plumbing.ReferenceName) error {
cfg, err := r.Config()
if err != nil {
return fmt.Errorf("read repo config: %w", err)
}
// SetConfig may succeed for root even when the file is marked read-only.
// Respect the repository's mode bits so callers see the same failure that
// an unprivileged process would get.
if fsStorer, ok := r.Storer.(interface{ Filesystem() billy.Filesystem }); ok {
if info, statErr := fsStorer.Filesystem().Stat("config"); statErr == nil && info.Mode().Perm()&0o222 == 0 {
return fmt.Errorf("write repo config: config is read-only")
}
}
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("write repo config: %w", err)
}
return nil
}
// remoteUsesHTTP reports whether the repository's origin remote is
// configured over HTTP(S), the only transport that requires a token.
func remoteUsesHTTP(r *git.Repository) bool {
cfg, err := r.Config()
if err != nil {
return false
}
remote, ok := cfg.Remotes[git.DefaultRemoteName]
if !ok {
return false
}
for _, u := range remote.URLs {
if strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://") {
return true
}
}
return false
}
// 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) {
p, _, err := m.patchFromMergeBase(ctx, dir, base)
return p, err
}
// patchFromMergeBase returns the patch and the merge-base commit it was taken
// against.
func (m *Manager) patchFromMergeBase(ctx context.Context, dir, base string) (*object.Patch, plumbing.Hash, error) {
r, err := openRepo(dir)
if err != nil {
return nil, plumbing.ZeroHash, fmt.Errorf("open worktree: %w", err)
}
baseHash, err := resolveBase(r, base)
if err != nil {
return nil, plumbing.ZeroHash, err
}
head, err := r.Head()
if err != nil {
return nil, plumbing.ZeroHash, fmt.Errorf("resolve HEAD: %w", err)
}
baseCommit, err := r.CommitObject(baseHash)
if err != nil {
return nil, plumbing.ZeroHash, fmt.Errorf("load base commit: %w", err)
}
headCommit, err := r.CommitObject(head.Hash())
if err != nil {
return nil, plumbing.ZeroHash, fmt.Errorf("load head commit: %w", err)
}
bases, err := baseCommit.MergeBase(headCommit)
if err != nil {
return nil, plumbing.ZeroHash, fmt.Errorf("find merge base for %s...HEAD: %w", remoteBase(base), err)
}
if len(bases) == 0 {
return nil, plumbing.ZeroHash, fmt.Errorf("find merge base for %s...HEAD: no common ancestor", remoteBase(base))
}
p, err := bases[0].PatchContext(ctx, headCommit)
if err != nil {
return nil, plumbing.ZeroHash, fmt.Errorf("diff %s...HEAD: %w", remoteBase(base), err)
}
return p, bases[0].Hash, nil
}
// HasUncommittedChanges reports whether the worktree has staged, unstaged or
// untracked changes, ignoring ignored files.
func (m *Manager) HasUncommittedChanges(ctx context.Context, dir string) (bool, error) {
if err := ctx.Err(); err != nil {
return false, err
}
r, err := openRepo(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 := ctx.Err(); err != nil {
return false, err
}
status, err := wt.Status()
if err != nil {
return false, fmt.Errorf("read status: %w", err)
}
return !status.IsClean(), 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 (
files []string
seen = map[string]struct{}{}
)
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
}
if _, ok := seen[name]; ok {
continue
}
seen[name] = struct{}{}
files = append(files, name)
}
return files, nil
}
// fileTypeMask is the file-type subset of a tree entry's mode: the bits that
// distinguish a regular file from a symlink or a gitlink. A mode change inside
// one type (the executable bit) is a modification, exactly as git reports it;
// a change across types is a typechange.
const fileTypeMask = 0o170000
// ChangedFileStats reports HEAD's per-file changes against the merge base
// with base.
func (m *Manager) ChangedFileStats(ctx context.Context, dir, base string) (task.ChangeStats, error) {
// The diffed base is the merge base patchFromMergeBase diffed against, not
// base's tip: the recorded pair has to describe the file list beside it.
p, diffedBase, err := m.patchFromMergeBase(ctx, dir, base)
if err != nil {
return task.ChangeStats{}, err
}
r, err := openRepo(dir)
if err != nil {
return task.ChangeStats{}, fmt.Errorf("open worktree: %w", err)
}
head, err := r.Head()
if err != nil {
return task.ChangeStats{}, fmt.Errorf("resolve HEAD: %w", err)
}
patches := p.FilePatches()
// Stats has no entry for binary or content-identical files.
stats := p.Stats()
next := 0
files := make([]task.FileChange, 0, len(patches))
totals := task.ChangeTotals{Files: len(patches)}
for _, fp := range patches {
change := fileChange(fp)
if change.Binary || next >= len(stats) {
files = append(files, change)
continue
}
change.Additions = stats[next].Addition
change.Deletions = stats[next].Deletion
totals.Additions += stats[next].Addition
totals.Deletions += stats[next].Deletion
next++
files = append(files, change)
}
return task.ChangeStats{
BaseSHA: diffedBase.String(),
HeadSHA: head.Hash().String(),
Files: files,
Totals: totals,
}, nil
}
// fileChange classifies one file patch: whether the file was added, deleted,
// renamed, retyped or modified, and whether it produced any textual hunks.
func fileChange(fp fdiff.FilePatch) task.FileChange {
from, to := fp.Files()
change := task.FileChange{
// No textual hunks. This is what go-git can tell us, not a claim about
// the file on disk: a binary file reports it, and so does a rename whose
// content did not change.
Binary: len(fp.Chunks()) == 0,
}
// At least one side is always present: a change with both entries empty is
// rejected upstream (object.Change.Action), so there is no third state to
// report here.
switch {
case from == nil:
change.Path = to.Path()
change.Status = task.ChangeAdded
case to == nil:
change.Path = from.Path()
change.Status = task.ChangeDeleted
case from.Path() != to.Path():
change.Path = to.Path()
change.OldPath = from.Path()
change.Status = task.ChangeRenamed
case from.Mode()&fileTypeMask != to.Mode()&fileTypeMask:
change.Path = to.Path()
change.Status = task.ChangeTypeChanged
default:
change.Path = to.Path()
change.Status = task.ChangeModified
}
return change
}
// Snapshot exports HEAD's tracked files into destDir, without .git.
func (m *Manager) Snapshot(ctx context.Context, dir, destDir string) error {
r, err := openRepo(dir)
if err != nil {
return fmt.Errorf("open worktree: %w", err)
}
head, err := r.Head()
if err != nil {
return fmt.Errorf("resolve HEAD: %w", err)
}
commit, err := r.CommitObject(head.Hash())
if err != nil {
return fmt.Errorf("load HEAD commit: %w", err)
}
tree, err := commit.Tree()
if err != nil {
return fmt.Errorf("load HEAD tree: %w", err)
}
if err := os.MkdirAll(destDir, 0o755); err != nil {
return fmt.Errorf("create snapshot directory: %w", err)
}
files := tree.Files()
defer files.Close()
for {
if err := ctx.Err(); err != nil {
return err
}
file, iterErr := files.Next()
if errors.Is(iterErr, io.EOF) {
break
}
if iterErr != nil {
return fmt.Errorf("walk HEAD tree: %w", iterErr)
}
if err := writeSnapshotFile(destDir, file); err != nil {
return err
}
}
return nil
}
// writeSnapshotFile writes one tracked file's blob content to destDir,
// preserving its relative path and creating parent directories as needed.
func writeSnapshotFile(destDir string, file *object.File) error {
target := filepath.Join(destDir, filepath.FromSlash(file.Name))
if rel, relErr := filepath.Rel(destDir, target); relErr != nil || strings.HasPrefix(rel, "..") {
return fmt.Errorf("snapshot: tracked path %q escapes destination", file.Name)
}
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
return fmt.Errorf("create snapshot subdirectory for %s: %w", file.Name, err)
}
contents, err := file.Contents()
if err != nil {
return fmt.Errorf("read tracked file %s: %w", file.Name, err)
}
if err := os.WriteFile(target, []byte(contents), 0o644); err != nil { //nolint:gosec // G306: the task container works on these files as another user.
return fmt.Errorf("write snapshot file %s: %w", file.Name, err)
}
return 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 {
if !ValidCoordinates(owner, repo, issue) {
return fmt.Errorf("invalid worktree coordinates")
}
if err := os.RemoveAll(m.Dir(owner, repo, issue)); err != nil {
return err
}
legacy := m.legacyDir(owner, repo, issue)
if repositoryUsesURL(legacy, m.cloneURL(owner, repo)) {
if err := os.RemoveAll(legacy); err != nil {
return err
}
_ = os.Remove(filepath.Dir(legacy))
}
return nil
}
func remoteBase(base string) string {
return "refs/remotes/" + git.DefaultRemoteName + "/" + base
}
// CheckoutPR clones a pull request's head into a fresh directory. The head
// must be in the same repository. The cleanup removes the clone.
func (m *Manager) CheckoutPR(ctx context.Context, owner, repo, headRef, baseRef string) (dir string, cleanup func(), err error) {
if m.WorkDir != "" {
if err := os.MkdirAll(m.WorkDir, 0o755); err != nil {
return "", nil, fmt.Errorf("create worktree root directory: %w", err)
}
}
dir, err = os.MkdirTemp(m.WorkDir, "pr-review-*")
if err != nil {
return "", nil, fmt.Errorf("create PR review directory: %w", err)
}
cleanup = func() { _ = os.RemoveAll(dir) }
r, err := git.PlainCloneContext(ctx, dir, &git.CloneOptions{
URL: m.cloneURL(owner, repo),
ClientOptions: m.auth(),
})
if err != nil {
cleanup()
return "", nil, fmt.Errorf("clone %s/%s for review: %w", owner, repo, err)
}
headHash, err := r.ResolveRevision(plumbing.Revision(remoteBase(headRef)))
if err != nil {
cleanup()
return "", nil, fmt.Errorf("resolve head branch %q: %w (the PR head must already be pushed to this repository)", headRef, err)
}
wt, err := r.Worktree()
if err != nil {
cleanup()
return "", nil, fmt.Errorf("open review worktree: %w", err)
}
if err := wt.Checkout(&git.CheckoutOptions{Hash: *headHash}); err != nil {
cleanup()
return "", nil, fmt.Errorf("checkout head %q: %w", headRef, err)
}
// Diff resolves origin/<baseRef>; a full clone fetches every branch, so
// it exists. Fail closed now rather than mid-diff with a confusing error.
if _, err := resolveBase(r, baseRef); err != nil {
cleanup()
return "", nil, fmt.Errorf("resolve base branch %q: %w", baseRef, err)
}
return dir, cleanup, nil
}
// resolveBase resolves the base commit from refs/remotes/origin/<base>, then
// origin/<base>.
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
// commit prefix (feat/fix/chore/etc.) from the issue title when present,
// falls back to labels, then defaults to "feat".
func archieBranch(issue int, title, labels string) string {
prefix := branchPrefix(title, labels)
_, rest := parseTitlePrefix(title)
slug := branchSlug(rest)
if slug == "" {
return fmt.Sprintf("%s/%d", prefix, issue)
}
return fmt.Sprintf("%s/%d-%s", prefix, issue, slug)
}
// branchPrefix derives a conventional-commit prefix from the issue title
// when present, falls back to labels, and defaults to "feat".
// Supported label mappings: bug→fix, feature/enhancement→feat, docs→docs,
// chore→chore, test→test, refactor→refactor.
func branchPrefix(title, labels string) string {
if prefix, _ := parseTitlePrefix(title); prefix != "" {
return prefix
}
return labelPrefix(labels)
}
func parseTitlePrefix(title string) (prefix, rest string) {
before, after, ok := strings.Cut(title, ":")
if !ok {
return "", title
}
lead := strings.TrimSpace(strings.ToLower(before))
if open := strings.Index(lead, "("); open != -1 && strings.HasSuffix(lead, ")") {
lead = lead[:open]
}
switch lead {
case "fix", "feat", "chore", "docs", "test", "refactor", "perf", "build", "ci", "style", "revert":
return lead, strings.TrimSpace(after)
case "bug":
return "fix", strings.TrimSpace(after)
case "feature", "enhancement":
return "feat", strings.TrimSpace(after)
}
return "", title
}
func labelPrefix(labels string) string {
for l := range strings.SplitSeq(labels, ",") {
switch strings.TrimSpace(strings.ToLower(l)) {
case "bug":
return "fix"
case "feature", "enhancement":
return "feat"
case "docs":
return "docs"
case "chore":
return "chore"
case "test":
return "test"
case "refactor":
return "refactor"
}
}
return "feat"
}
// branchSlug converts a title to a kebab-case slug suitable for a git
// branch. Only keeps lowercase alphanumerics and hyphens, max 60 chars.
func branchSlug(title string) string {
var b strings.Builder
for _, r := range strings.ToLower(title) {
if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' {
b.WriteRune(r)
} else if r == ' ' || r == '-' || r == '_' {
b.WriteRune('-')
}
if b.Len() >= 60 {
break
}
}
slug := strings.Trim(b.String(), "-")
// Collapse multiple hyphens.
for strings.Contains(slug, "--") {
slug = strings.ReplaceAll(slug, "--", "-")
}
return slug
}
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://github.com/%s/%s.git", owner, repo)
}
var ErrBaseBranchNotFound = errors.New("base branch not found")
// CheckAccess checks the same authenticated Git endpoint as Prepare, without
// creating a clone, branch, or commit.
func (m *Manager) CheckAccess(ctx context.Context, owner, repo, base string) error {
remote := git.NewRemote(memory.NewStorage(), &gitconfig.RemoteConfig{Name: "origin", URLs: []string{m.cloneURL(owner, repo)}})
refs, err := remote.ListContext(ctx, &git.ListOptions{ClientOptions: m.auth()})
if err != nil {
return err
}
for _, ref := range refs {
if ref.Name() == plumbing.NewBranchReferenceName(base) {
return nil
}
}
return fmt.Errorf("%w: %s", ErrBaseBranchNotFound, base)
}
// openRepo opens a repository whose objects may live in the mirror it borrows
// from, which sits outside the repository's own directory.
func openRepo(dir string) (*git.Repository, error) {
return git.PlainOpenWithOptions(dir, &git.PlainOpenOptions{AlternatesFS: osfs.New(string(filepath.Separator))})
}
// HoldsUnsharedWork reports whether the clone at dir has uncommitted changes
// or a local branch whose tip its remote does not have. An unreadable clone
// counts as holding work, so it is never deleted on a guess.
func (m *Manager) HoldsUnsharedWork(ctx context.Context, dir string) bool {
if dirty, err := m.HasUncommittedChanges(ctx, dir); err != nil || dirty {
return true
}
r, err := git.PlainOpen(dir)
if err != nil {
return true
}
branches, err := r.Branches()
if err != nil {
return true
}
unshared := false
_ = branches.ForEach(func(ref *plumbing.Reference) error {
remote, err := r.Reference(plumbing.NewRemoteReferenceName(git.DefaultRemoteName, ref.Name().Short()), true)
if err != nil || remote.Hash() != ref.Hash() {
if _, err := r.CommitObject(ref.Hash()); err == nil && !m.reachableFromRemotes(r, ref.Hash()) {
unshared = true
}
}
return nil
})
return unshared
}
// reachableFromRemotes reports whether any remote-tracking ref contains hash.
func (m *Manager) reachableFromRemotes(r *git.Repository, hash plumbing.Hash) bool {
refs, err := r.References()
if err != nil {
return false
}
found := false
_ = refs.ForEach(func(ref *plumbing.Reference) error {
if found || !ref.Name().IsRemote() {
return nil
}
tip, err := r.CommitObject(ref.Hash())
if err != nil {
return nil //nolint:nilerr // an unreadable ref is skipped
}
target, err := r.CommitObject(hash)
if err != nil {
return nil //nolint:nilerr // an unreadable commit is skipped
}
if ok, err := target.IsAncestor(tip); err == nil && ok {
found = true
}
return nil
})
return found
}