Files
sam fa76f4c994
deploy / plan (push) Failing after 7s
deploy / gate (push) Skipped
deploy / archied-image (push) Skipped
deploy / agent-image (push) Skipped
deploy / dist-zip (push) Skipped
fix(worktree): sign and attribute agent commits at push; forge git credentials (#1816, #1817)
2026-11-09 00:11:54 +11:00

150 lines
4.3 KiB
Go

package worktree
import (
"context"
"errors"
"fmt"
"io"
"slices"
"time"
"github.com/go-git/go-git/v6"
"github.com/go-git/go-git/v6/plumbing"
"github.com/go-git/go-git/v6/plumbing/object"
"github.com/go-git/go-git/v6/plumbing/storer"
)
// attest re-authors and signs the branch's unpublished commits as the bot
// before a push. Commits are made inside the task container, which holds
// neither the connection's identity nor its signing key, so the daemon applies
// both here. Only the first-parent chain back to the nearest published commit
// is rewritten; trees, messages and author times are kept.
func (m *Manager) attest(ctx context.Context, r *git.Repository, ref plumbing.ReferenceName) error {
tip, err := r.Reference(ref, true)
if err != nil {
return fmt.Errorf("read %s: %w", ref, err)
}
head, err := r.CommitObject(tip.Hash())
if err != nil {
return fmt.Errorf("read %s: %w", ref, err)
}
published, err := publishedBoundary(r, head)
if err != nil || len(published) == 0 {
return err
}
var chain []*object.Commit
for c := head; !published[c.Hash]; {
chain = append(chain, c)
if c.NumParents() == 0 {
return nil
}
if c, err = c.Parent(0); err != nil {
return fmt.Errorf("walk %s: %w", ref, err)
}
}
if len(chain) == 0 || !m.needsAttest(chain) {
return nil
}
var rewritten plumbing.Hash
for _, c := range slices.Backward(chain) {
if err := ctx.Err(); err != nil {
return err
}
parents := append([]plumbing.Hash(nil), c.ParentHashes...)
if !rewritten.IsZero() {
parents[0] = rewritten
}
if rewritten, err = m.recommit(ctx, r, c, parents); err != nil {
return err
}
}
if err := r.Storer.SetReference(plumbing.NewHashReference(ref, rewritten)); err != nil {
return fmt.Errorf("update %s: %w", ref, err)
}
return nil
}
// needsAttest reports whether any commit lacks the bot identity or, with a
// signer configured, a signature.
func (m *Manager) needsAttest(chain []*object.Commit) bool {
for _, c := range chain {
if c.Author.Name != m.BotUser || c.Author.Email != m.BotEmail || c.Committer.Name != m.BotUser || c.Committer.Email != m.BotEmail {
return true
}
if m.Signer != nil && c.Signature == "" {
return true
}
}
return false
}
func (m *Manager) recommit(ctx context.Context, r *git.Repository, c *object.Commit, parents []plumbing.Hash) (plumbing.Hash, error) {
next := &object.Commit{
Author: object.Signature{Name: m.BotUser, Email: m.BotEmail, When: c.Author.When},
Committer: object.Signature{Name: m.BotUser, Email: m.BotEmail, When: time.Now()},
Message: c.Message,
TreeHash: c.TreeHash,
ParentHashes: parents,
Encoding: c.Encoding,
}
if m.Signer != nil {
unsigned := &plumbing.MemoryObject{}
if err := next.EncodeWithoutSignature(unsigned); err != nil {
return plumbing.ZeroHash, fmt.Errorf("encode commit: %w", err)
}
body, err := unsigned.Reader()
if err != nil {
return plumbing.ZeroHash, fmt.Errorf("encode commit: %w", err)
}
sig, err := m.Signer.Sign(ctx, body)
if err != nil {
return plumbing.ZeroHash, fmt.Errorf("sign commit: %w", err)
}
next.Signature = string(sig)
}
obj := r.Storer.NewEncodedObject()
if err := next.Encode(obj); err != nil {
return plumbing.ZeroHash, fmt.Errorf("encode commit: %w", err)
}
h, err := r.Storer.SetEncodedObject(obj)
if err != nil {
return plumbing.ZeroHash, fmt.Errorf("store commit: %w", err)
}
return h, nil
}
// publishedBoundary is the set of commits where head meets any remote-tracking
// ref: everything at or behind them is already on the forge.
func publishedBoundary(r *git.Repository, head *object.Commit) (map[plumbing.Hash]bool, error) {
refs, err := r.References()
if err != nil {
return nil, fmt.Errorf("list refs: %w", err)
}
out := map[plumbing.Hash]bool{}
err = refs.ForEach(func(ref *plumbing.Reference) error {
if !ref.Name().IsRemote() || ref.Type() != plumbing.HashReference {
return nil
}
remote, err := r.CommitObject(ref.Hash())
if errors.Is(err, plumbing.ErrObjectNotFound) {
return nil
}
if err != nil {
return err
}
bases, err := head.MergeBase(remote)
if err != nil {
return err
}
for _, b := range bases {
out[b.Hash] = true
}
return nil
})
if err != nil && !errors.Is(err, storer.ErrStop) && !errors.Is(err, io.EOF) {
return nil, fmt.Errorf("find published commits: %w", err)
}
return out, nil
}