From 0edce80e2ceee352a632280f2d6ccaad431cd8b7 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Sun, 8 Nov 2026 18:50:46 +1100 Subject: [PATCH] Key typed worktrees on generate_types and the generator version --- internal/deploy/deploy.go | 13 +++++++++++-- internal/deploy/deploy_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index 472a577..49696de 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -262,10 +262,19 @@ func (d *Deployer) owned(s *Source, link string) string { return "" } +// worktreePrefix keys worktrees on everything besides modules that changes their content. +func (d *Deployer) worktreePrefix(sha string) string { + if d.cfg.W10k.GenerateTypes { + return sha + "-types" + gentypes.Version + "-" + } + return sha + "-" +} + func (d *Deployer) deployEnv(ctx context.Context, s *Source, name, sha string, modules bool) error { link := filepath.Join(s.Basedir, name) cur := d.owned(s, link) - if cur != "" && !modules && strings.HasPrefix(filepath.Base(cur), sha+"-") && d.complete(cur) { + prefix := d.worktreePrefix(sha) + if base := filepath.Base(cur); cur != "" && !modules && strings.HasPrefix(base, prefix) && len(base) == len(prefix)+12 && d.complete(cur) { return nil } pf, checksum, err := d.puppetfile(ctx, d.srcMirror(s), sha) @@ -280,7 +289,7 @@ func (d *Deployer) deployEnv(ctx context.Context, s *Source, name, sha string, m for _, m := range mods { _, _ = fmt.Fprintf(h, "%s %s\n", m.path, m.key) } - target := filepath.Join(d.srcWorktrees(s), sha+"-"+hex.EncodeToString(h.Sum(nil))[:12]) + target := filepath.Join(d.srcWorktrees(s), prefix+hex.EncodeToString(h.Sum(nil))[:12]) if cur == target && d.complete(cur) { return nil } diff --git a/internal/deploy/deploy_test.go b/internal/deploy/deploy_test.go index 8c9efc7..5666b6b 100644 --- a/internal/deploy/deploy_test.go +++ b/internal/deploy/deploy_test.go @@ -639,6 +639,34 @@ func TestGenerateTypesBeforeSwap(t *testing.T) { } } +func TestGenerateTypesToggleRedeploys(t *testing.T) { + f := newFixture(t) + commit(t, f.control, map[string]string{ + "environment.conf": "modulepath = site:$basemodulepath\n", + "site/local/lib/puppet/type/local.rb": "Puppet::Type.newtype(:local) do\n newparam(:name)\nend\n", + }) + f.mustDeploy(false) + plain := f.env("main") + if exists(filepath.Join(plain, ".resource_types")) { + t.Fatal("types generated while generate_types is off") + } + f.cfg.W10k.GenerateTypes = true + f.mustDeploy(false) + typed := f.env("main") + if typed == plain || !exists(filepath.Join(typed, ".resource_types", "local.pp")) { + t.Fatalf("enabling generate_types must deploy a new worktree with types: %s", typed) + } + f.mustDeploy(false) + if f.env("main") != typed { + t.Fatal("an unchanged typed env must not be redeployed") + } + f.cfg.W10k.GenerateTypes = false + f.mustDeploy(false) + if f.env("main") != plain { + t.Fatalf("disabling generate_types must return to the untyped worktree, got %s", f.env("main")) + } +} + func readFileT(t *testing.T, p string) string { t.Helper() b, err := os.ReadFile(p)