From 1c192c94096b61178aacb8355f7c91ccb71d2ab6 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Sun, 8 Nov 2026 20:39:22 +1100 Subject: [PATCH 1/2] Keep targeted deploys targeted across sources and drop unverifiable GC marks --- internal/deploy/deploy.go | 14 +++---- internal/deploy/deploy_test.go | 68 ++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index c79851b..133b647 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -204,14 +204,17 @@ func (d *Deployer) Deploy(ctx context.Context, only []string, modules bool) erro for _, e := range only { want[e] = true } + found := map[string]bool{} var errs []error for _, s := range d.Sources() { - if err := d.deploySource(ctx, s, want, modules); err != nil { + if err := d.deploySource(ctx, s, want, found, modules); err != nil { errs = append(errs, fmt.Errorf("source %s: %w", s.Name, err)) } } for e := range want { - errs = append(errs, fmt.Errorf("environment %s not found", e)) + if !found[e] { + errs = append(errs, fmt.Errorf("environment %s not found", e)) + } } if err := d.GC(ctx); err != nil { errs = append(errs, err) @@ -252,7 +255,7 @@ func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]stri return envs, errs } -func (d *Deployer) deploySource(ctx context.Context, s *Source, want map[string]bool, modules bool) error { +func (d *Deployer) deploySource(ctx context.Context, s *Source, want, found map[string]bool, modules bool) error { if err := d.syncMirror(ctx, d.srcMirror(s), s.Remote, nil, envRefspecs); err != nil { return err } @@ -270,7 +273,7 @@ func (d *Deployer) deploySource(ctx context.Context, s *Source, want map[string] if !full && !want[name] { continue } - delete(want, name) + found[name] = true wg.Add(1) go func() { defer wg.Done() @@ -729,9 +732,6 @@ func (d *Deployer) GC(ctx context.Context) error { liveMod, err := d.moduleRefs(liveEnv) if err != nil { g.errs = append(g.errs, err) - for p := range g.marks { - g.seen[p] = true - } } else { keys, _ := os.ReadDir(d.path("modules")) for _, k := range keys { diff --git a/internal/deploy/deploy_test.go b/internal/deploy/deploy_test.go index 6466652..18f12fd 100644 --- a/internal/deploy/deploy_test.go +++ b/internal/deploy/deploy_test.go @@ -829,3 +829,71 @@ func TestConcurrentWorktreeAddsOneMirror(t *testing.T) { t.Fatal(err) } } + +func TestGCRefsErrorDropsMarkOfLiveWorktree(t *testing.T) { + root := t.TempDir() + s := &Source{Name: "s", Basedir: filepath.Join(root, "envs")} + cfg := &Config{Cachedir: filepath.Join(root, "cache"), Sources: map[string]*Source{"s": s}} + clock := time.Unix(1_700_000_000, 0) + gc := func() error { + d := New(cfg, 1, 10*time.Minute) + d.now = func() time.Time { return clock } + return d.GC(context.Background()) + } + d := New(cfg, 1, 0) + old, cur, broken := filepath.Join(d.srcWorktrees(s), "old"), filepath.Join(d.srcWorktrees(s), "cur"), filepath.Join(d.srcWorktrees(s), "broken") + mkdirs(t, old, cur, broken, s.Basedir, filepath.Dir(d.doneFile(old))) + for _, p := range []string{old, cur} { + if err := os.WriteFile(d.doneFile(p), []byte(`{"modules":[]}`), 0o644); err != nil { + t.Fatal(err) + } + } + link := filepath.Join(s.Basedir, "develop") + if err := Swap(cur, link); err != nil { + t.Fatal(err) + } + if err := gc(); err != nil { + t.Fatal(err) + } + clock = clock.Add(time.Hour) + if err := Swap(old, link); err != nil { + t.Fatal(err) + } + if err := Swap(broken, filepath.Join(s.Basedir, "other")); err != nil { + t.Fatal(err) + } + if err := gc(); err == nil { + t.Fatal("want moduleRefs error from live worktree without done file") + } + if err := os.WriteFile(d.doneFile(broken), []byte(`{"modules":[]}`), 0o644); err != nil { + t.Fatal(err) + } + if err := Swap(cur, link); err != nil { + t.Fatal(err) + } + _ = gc() + if !exists(old) { + t.Fatal("worktree unreferenced this run was deleted in the same run via a stale mark kept while it was live") + } +} + +func TestDeployOnlyLeavesLaterSourceAlone(t *testing.T) { + f := newFixture(t) + commit(t, f.control, map[string]string{"manifests/site.pp": "node default {}\n"}) + gitT(t, f.control, "branch", "production") + other := filepath.Join(f.root, "other") + newRepo(t, other) + commit(t, other, map[string]string{"manifests/site.pp": "node default {}\n"}) + otherBase := filepath.Join(f.root, "other-envs") + cfg, err := ParseConfig(fmt.Appendf(nil, "cachedir: %s\nsources:\n aaa:\n remote: %s\n basedir: %s\n zzz:\n remote: %s\n basedir: %s\n", f.cfg.Cachedir, f.control, f.basedir, other, otherBase)) + if err != nil { + t.Fatal(err) + } + f.cfg = cfg + if err := f.deploy(false, "production"); err != nil { + t.Fatal(err) + } + if exists(filepath.Join(otherBase, "main")) { + t.Fatal("deploy production also deployed every env of the next source") + } +} From d7ab79f8744c9e10eb3a558511fb06d3b70d32f3 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Sun, 8 Nov 2026 20:40:10 +1100 Subject: [PATCH 2/2] Write the branch as the deploy marker name, as g10k does --- internal/deploy/deploy.go | 25 ++++++++++++++----------- internal/deploy/deploy_test.go | 13 +++++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index 133b647..0c1868f 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -222,7 +222,9 @@ func (d *Deployer) Deploy(ctx context.Context, only []string, modules bool) erro return errors.Join(errs...) } -func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]string, []error) { +type envRef struct{ branch, sha string } + +func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]envRef, []error) { all, err := d.branches(ctx, d.srcMirror(s)) if err != nil { return nil, []error{err} @@ -232,7 +234,7 @@ func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]stri names = append(names, b) } sort.Strings(names) - envs := map[string]string{} + envs := map[string]envRef{} var errs []error for _, b := range names { if s.Ignored(b) { @@ -250,7 +252,7 @@ func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]stri errs = append(errs, fmt.Errorf("branch %s collides with environment %s, skipped", b, name)) continue } - envs[name] = all[b] + envs[name] = envRef{b, all[b]} } return envs, errs } @@ -269,7 +271,7 @@ func (d *Deployer) deploySource(ctx context.Context, s *Source, want, found map[ } var mu sync.Mutex var wg sync.WaitGroup - for name, sha := range envs { + for name, ref := range envs { if !full && !want[name] { continue } @@ -279,7 +281,7 @@ func (d *Deployer) deploySource(ctx context.Context, s *Source, want, found map[ defer wg.Done() err := func() (err error) { defer recoverErr(&err, "environment "+name) - return d.deployEnv(ctx, s, name, sha, modules) + return d.deployEnv(ctx, s, name, ref, modules) }() if err != nil { mu.Lock() @@ -313,7 +315,8 @@ func (d *Deployer) owned(s *Source, link string) string { return "" } -func (d *Deployer) deployEnv(ctx context.Context, s *Source, name, sha string, modules bool) error { +func (d *Deployer) deployEnv(ctx context.Context, s *Source, name string, ref envRef, modules bool) error { + sha := ref.sha link := filepath.Join(s.Basedir, name) cur := d.owned(s, link) if cur != "" && !modules && strings.HasPrefix(filepath.Base(cur), sha+"-") && d.complete(cur) { @@ -343,7 +346,7 @@ func (d *Deployer) deployEnv(ctx context.Context, s *Source, name, sha string, m if err := d.checkout(ctx, s, sha, target, mods); err != nil { return "", err } - return "", d.prepare(s, name, sha, checksum, target, mods, started) + return "", d.prepare(s, ref, checksum, target, mods, started) }); err != nil { return err } @@ -518,10 +521,10 @@ func linkModule(root *os.Root, rel, dir string) error { } // prepare runs last before a worktree can be swapped in; its out-of-tree done file marks the worktree complete. -func (d *Deployer) prepare(s *Source, name, sha, checksum, target string, mods []resolved, started time.Time) error { +func (d *Deployer) prepare(s *Source, ref envRef, checksum, target string, mods []resolved, started time.Time) error { r := deployResult{ - Name: name, - Signature: sha, + Name: ref.branch, + Signature: ref.sha, StartedAt: started, FinishedAt: d.now().UTC(), DeploySuccess: true, @@ -597,7 +600,7 @@ func Swap(target, link string) error { return os.Rename(tmp, link) } -func (d *Deployer) purge(s *Source, envs map[string]string) error { +func (d *Deployer) purge(s *Source, envs map[string]envRef) error { entries, err := os.ReadDir(s.Basedir) if err != nil { return err diff --git a/internal/deploy/deploy_test.go b/internal/deploy/deploy_test.go index 18f12fd..62474c7 100644 --- a/internal/deploy/deploy_test.go +++ b/internal/deploy/deploy_test.go @@ -897,3 +897,16 @@ func TestDeployOnlyLeavesLaterSourceAlone(t *testing.T) { t.Fatal("deploy production also deployed every env of the next source") } } + +func TestDeployMarkerNameIsBranch(t *testing.T) { + f := newFixture(t) + commit(t, f.control, map[string]string{"manifests/site.pp": "node default {}\n"}) + gitT(t, f.control, "checkout", "-q", "-b", "feat/x") + commit(t, f.control, map[string]string{"manifests/site.pp": "node x {}\n"}) + if err := f.deploy(false, "feat_x"); err != nil { + t.Fatal(err) + } + if m := readMarker(t, f.env("feat_x")); m.Name != "feat/x" { + t.Fatalf("marker name %q, want branch feat/x", m.Name) + } +}