mirror of
https://github.com/lukaszraczylo/harness-sync.git
synced 2026-10-14 00:57:49 +00:00
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.
This commit is contained in:
@@ -71,6 +71,17 @@ func (r *Repo) IsRepo() bool {
|
||||
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 {
|
||||
|
||||
@@ -41,3 +41,33 @@ func TestShowFileAtHead(t *testing.T) {
|
||||
_, err = r.ShowFileAtHead("nope.txt")
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestHasStagedChangesEmptyAfterInit(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
r := New(dir)
|
||||
require.NoError(t, r.Init())
|
||||
require.NoError(t, r.Configure("nobody", "[email protected]"))
|
||||
// No commits yet, working tree clean.
|
||||
dirty, err := r.HasStagedChanges()
|
||||
require.NoError(t, err)
|
||||
assert.False(t, dirty)
|
||||
}
|
||||
|
||||
func TestHasStagedChangesAfterAdd(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
r := New(dir)
|
||||
require.NoError(t, r.Init())
|
||||
require.NoError(t, r.Configure("nobody", "[email protected]"))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(dir, "a.txt"), []byte("x"), 0o600))
|
||||
require.NoError(t, r.AddAll())
|
||||
|
||||
dirty, err := r.HasStagedChanges()
|
||||
require.NoError(t, err)
|
||||
assert.True(t, dirty)
|
||||
|
||||
// After commit, index matches HEAD again → no staged changes.
|
||||
require.NoError(t, r.Commit("c"))
|
||||
dirty, err = r.HasStagedChanges()
|
||||
require.NoError(t, err)
|
||||
assert.False(t, dirty)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user