feat(agent): mark the mounted worktree as a git safe directory
This commit is contained in:
1 parent
255add82e0
commit
62cf76ad40
5 files changed
+162
No files matched your search
@@ -79,3 +79,4 @@ bd.db
|
||||
# They would override fork protection in .git/info/exclude.
|
||||
# Config files (metadata.json, config.yaml) are tracked by git by default
|
||||
# since no pattern above ignores them.
|
||||
config.yaml
|
||||
Whitespace-only changes.
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// gitRunner runs a git invocation and returns its combined output. Injected so
|
||||
// the safe.directory configuration can be tested without a git binary.
|
||||
type gitRunner func(ctx context.Context, args ...string) ([]byte, error)
|
||||
|
||||
// runGit is the production gitRunner.
|
||||
func runGit(ctx context.Context, args ...string) ([]byte, error) {
|
||||
return exec.CommandContext(ctx, "git", args...).CombinedOutput()
|
||||
}
|
||||
|
||||
// markWorktreeSafe adds mountDir to git's global safe.directory list.
|
||||
//
|
||||
// The daemon bind-mounts the task worktree into the container from the host,
|
||||
// so the directory is owned by a UID the container user is not. Git refuses to
|
||||
// operate in a repository it does not own ("detected dubious ownership"), and
|
||||
// the Go toolchain shells out to git for VCS stamping during build/test/vet --
|
||||
// so without this every gate fails before the agent does any real work.
|
||||
//
|
||||
// Best-effort by design: git may be absent from a minimal image, and an agent
|
||||
// working a repo with no gate commands never needs it. A failure is logged and
|
||||
// the agent continues. Returns true only when git accepted the configuration.
|
||||
//
|
||||
// The mount is checked first so that running archie-agent directly on a host
|
||||
// (shared queue-group mode, no container) does not write to the operator's own
|
||||
// ~/.gitconfig for a path that was never mounted.
|
||||
func markWorktreeSafe(ctx context.Context, mountDir string, run gitRunner, log *slog.Logger) bool {
|
||||
info, err := os.Stat(mountDir)
|
||||
if err != nil || !info.IsDir() {
|
||||
log.Debug("git safe.directory skipped: no worktree mounted", "dir", mountDir)
|
||||
return false
|
||||
}
|
||||
|
||||
out, err := run(ctx, "config", "--global", "--add", "safe.directory", mountDir)
|
||||
if err != nil {
|
||||
log.Warn("git safe.directory config failed -- gates that shell out to git may fail",
|
||||
"dir", mountDir,
|
||||
"err", err,
|
||||
"output", strings.TrimSpace(string(out)),
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
log.Info("git safe.directory configured", "dir", mountDir)
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMarkWorktreeSafe(t *testing.T) {
|
||||
log := slog.New(slog.DiscardHandler)
|
||||
|
||||
present := func(t *testing.T) string { t.Helper(); return t.TempDir() }
|
||||
absent := func(t *testing.T) string { t.Helper(); return t.TempDir() + "/never-mounted" }
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mountDir func(t *testing.T) string
|
||||
runErr error
|
||||
want bool
|
||||
wantRuns int
|
||||
}{
|
||||
{
|
||||
name: "configures git when the worktree is mounted",
|
||||
mountDir: present,
|
||||
want: true,
|
||||
wantRuns: 1,
|
||||
},
|
||||
{
|
||||
name: "git failure is best-effort",
|
||||
mountDir: present,
|
||||
runErr: errors.New("exit status 1"),
|
||||
want: false,
|
||||
wantRuns: 1,
|
||||
},
|
||||
{
|
||||
name: "git missing is best-effort",
|
||||
mountDir: present,
|
||||
runErr: errors.New(`exec: "git": executable file not found in $PATH`),
|
||||
want: false,
|
||||
wantRuns: 1,
|
||||
},
|
||||
{
|
||||
name: "no mounted worktree leaves git config untouched",
|
||||
mountDir: absent,
|
||||
want: false,
|
||||
wantRuns: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := tc.mountDir(t)
|
||||
var runs [][]string
|
||||
run := func(_ context.Context, args ...string) ([]byte, error) {
|
||||
runs = append(runs, args)
|
||||
return []byte("output"), tc.runErr
|
||||
}
|
||||
|
||||
got := markWorktreeSafe(context.Background(), dir, run, log)
|
||||
|
||||
if got != tc.want {
|
||||
t.Fatalf("markWorktreeSafe() = %v, want %v", got, tc.want)
|
||||
}
|
||||
if len(runs) != tc.wantRuns {
|
||||
t.Fatalf("git invoked %d times, want %d (%v)", len(runs), tc.wantRuns, runs)
|
||||
}
|
||||
if tc.wantRuns == 0 {
|
||||
return
|
||||
}
|
||||
wantArgs := []string{"config", "--global", "--add", "safe.directory", dir}
|
||||
if !reflect.DeepEqual(runs[0], wantArgs) {
|
||||
t.Fatalf("git args = %v, want %v", runs[0], wantArgs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A mount path that is a file, not a directory, is not a worktree: git must
|
||||
// not be reconfigured for it.
|
||||
func TestMarkWorktreeSafeIgnoresNonDirectory(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
file := filepath.Join(dir, "worktree")
|
||||
if err := os.WriteFile(file, []byte("not a worktree"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
invoked := false
|
||||
run := func(_ context.Context, _ ...string) ([]byte, error) {
|
||||
invoked = true
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if got := markWorktreeSafe(context.Background(), file, run, slog.New(slog.DiscardHandler)); got {
|
||||
t.Fatal("markWorktreeSafe() = true for a non-directory mount path, want false")
|
||||
}
|
||||
if invoked {
|
||||
t.Fatal("git was invoked for a non-directory mount path")
|
||||
}
|
||||
}
|
||||
@@ -95,6 +95,10 @@ func run() int {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
// Before anything can run a gate: the worktree is a host bind mount owned
|
||||
// by another UID, and git refuses to work in it until it is marked safe.
|
||||
markWorktreeSafe(ctx, storage.WorktreeMountDir, runGit, log)
|
||||
|
||||
// One bus client owns the stream definition. archied declares the same
|
||||
// stream with the same subjects; this process previously declared its
|
||||
// own copy, free to disagree about retention, dedup window and TTLs.
|
||||
|
||||
Reference in new issue
Block a user