Files
harness-sync/internal/gitx/repo.go
T
lukaszraczyloandGitHub e01f3419ea fix(apply): propagate read errors, auto-open repo, dedupe adapter base (#6)
Three robustness fixes for the apply pipeline:

1. Silent read errors on state/target files treated 'file absent' as
   'file empty' and fast-forward-overwrote user edits. Add
   common.ReadExistingFile that returns (nil,nil) only on
   os.IsNotExist; other I/O errors propagate. Use it from apply.Run
   and from every adapter's Render (claudecode, crush, kilo,
   opencode, goose, zed).

2. apply.Run required callers to pre-construct *gitx.Repo; passing
   nil silently skipped the commit. Run now auto-opens a repo from
   Bundle.Root, gracefully no-ops when the root is not a git repo,
   and uses a new gitx.HasStagedChanges() check to skip the no-op
   'nothing to commit' that triggered the original silent-skip.

3. Each of 7 adapter packages re-implemented the same 25-line
   Option/WithHome/New constructor. Hoist into adapter.Base +
   adapter.BaseOption + NewBase. Each adapter embeds *Base and
   reduces to a 9-line constructor. Public New(WithHome(h)) API
   preserved; a.home becomes a.Home.

Tests:
  - apply: 5 new (propagates-read-error, auto-opens-repo,
    no-repo-skips-commit, skips-no-op-commit, commit-message-counts)
  - gitx: 2 new (HasStagedChanges empty/after-add)
  - adapter/common: 3 new (ReadExistingFile missing/content/perm)
  - adapter: 4 new (NewBase default/WithHome/order/typed)
  - existing adapter tests unchanged — verify a.Home still honoured.
2026-06-01 13:11:07 +01:00

94 lines
2.3 KiB
Go

// Package gitx wraps git CLI invocations the canonical repo needs.
package gitx
import (
"bytes"
"fmt"
"os/exec"
)
type Repo struct {
Dir string
}
func New(dir string) *Repo { return &Repo{Dir: dir} }
func (r *Repo) run(args ...string) ([]byte, error) {
cmd := exec.Command("git", args...)
cmd.Dir = r.Dir
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("git %v: %w: %s", args, err, stderr.String())
}
return stdout.Bytes(), nil
}
func (r *Repo) Init() error {
_, err := r.run("init", "-q", "-b", "main")
return err
}
func (r *Repo) Configure(name, email string) error {
if _, err := r.run("config", "user.name", name); err != nil {
return err
}
_, err := r.run("config", "user.email", email)
return err
}
func (r *Repo) AddAll() error {
_, err := r.run("add", "-A")
return err
}
func (r *Repo) Add(paths ...string) error {
args := append([]string{"add", "--"}, paths...)
_, err := r.run(args...)
return err
}
func (r *Repo) Commit(msg string) error {
// --no-verify: harness-sync makes internal state-tracking commits in the
// canonical repo; user-configured hooks (secret scanners, linters) must not
// block them — those hooks belong to the harness-sync source repo, not here.
_, err := r.run("commit", "-q", "--no-verify", "-m", msg)
return err
}
func (r *Repo) HeadCommit() (string, error) {
b, err := r.run("rev-parse", "HEAD")
return string(bytes.TrimSpace(b)), err
}
func (r *Repo) ShowFileAtHead(path string) ([]byte, error) {
return r.run("show", "HEAD:"+path)
}
func (r *Repo) IsRepo() bool {
_, err := r.run("rev-parse", "--git-dir")
return err == nil
}
// HasStagedChanges reports whether the index currently differs from HEAD.
// Used by callers that want to skip a no-op `git commit` (commit exits 1
// when there is nothing to commit, which is the wrong signal to propagate).
func (r *Repo) HasStagedChanges() (bool, error) {
b, err := r.run("status", "--porcelain")
if err != nil {
return false, err
}
return len(bytes.TrimSpace(b)) > 0, nil
}
// Revert undoes the last N commits with `git revert --no-edit HEAD~N..HEAD`.
// n must be >= 1.
func (r *Repo) Revert(n int) error {
if n < 1 {
return fmt.Errorf("revert: n must be >= 1, got %d", n)
}
_, err := r.run("revert", "--no-edit", fmt.Sprintf("HEAD~%d..HEAD", n))
return err
}