Make add idempotent: skip already-tracked files instead of erroring.

Enables glob workflows like `sgard add ~/.config/mcp/services/*` to
pick up new files without failing on ones already tracked.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-30 09:52:37 -07:00
parent de5759ac77
commit 7713d071c2
4 changed files with 20 additions and 8 deletions

View File

@@ -239,7 +239,7 @@ func (g *Garden) Add(paths []string, opts ...AddOptions) error {
// Track the directory itself as a structural entry.
tilded := toTildePath(abs)
if g.findEntry(tilded) != nil {
return fmt.Errorf("already tracking %s", tilded)
continue
}
entry := manifest.Entry{
Path: tilded,
@@ -277,7 +277,7 @@ func (g *Garden) Add(paths []string, opts ...AddOptions) error {
}
}
} else {
if err := g.addEntry(abs, info, now, false, o); err != nil {
if err := g.addEntry(abs, info, now, true, o); err != nil {
return err
}
}

View File

@@ -200,7 +200,7 @@ func TestAddSymlink(t *testing.T) {
}
}
func TestAddDuplicateRejected(t *testing.T) {
func TestAddDuplicateIsIdempotent(t *testing.T) {
root := t.TempDir()
repoDir := filepath.Join(root, "repo")
@@ -218,8 +218,19 @@ func TestAddDuplicateRejected(t *testing.T) {
t.Fatalf("first Add: %v", err)
}
if err := g.Add([]string{testFile}); err == nil {
t.Fatal("second Add of same path should fail")
if err := g.Add([]string{testFile}); err != nil {
t.Fatalf("second Add of same path should be idempotent: %v", err)
}
entries := g.List()
count := 0
for _, e := range entries {
if e.Path == toTildePath(testFile) {
count++
}
}
if count != 1 {
t.Fatalf("expected 1 entry, got %d", count)
}
}