From f5c1fe690bf6cb669e946f322b2a34b9be55aa01 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 15 Nov 2026 09:39:01 +0530 Subject: [PATCH 1/4] fix(promote): refuse to run on a dirty tree and never strand HEAD on promote round-trips through a checkout of . go-git's Checkout only refuses unstaged edits, so staged edits and staged new files were reset away with exit 0, and when it did refuse it had already moved HEAD, leaving the user on with their work in the tree. promote now checks for staged or modified tracked files up front and stops with "commit or stash first" (untracked files are fine; the checkout leaves them alone). If the checkout of still fails, HEAD is restored to the branch or commit the user started on. Fixes #14 Fixes #15 --- cmd/promptctl/main.go | 41 ++++++++- cmd/promptctl/promote_dirty_test.go | 132 ++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 cmd/promptctl/promote_dirty_test.go diff --git a/cmd/promptctl/main.go b/cmd/promptctl/main.go index f321f8d..cd32c83 100644 --- a/cmd/promptctl/main.go +++ b/cmd/promptctl/main.go @@ -206,7 +206,9 @@ func logCmd(args []string) { // commits it there — the dev → staging → prod version promotion. A no-op (the // prompt already matches on `to`) reports "already up to date" instead of failing // on an empty commit. The branch (or commit) checked out before is restored after. -// ponytail: assumes both branches exist and the tree is clean; go-git errors surface as-is. +// It refuses to run with staged or modified tracked files: the checkout round +// trip through `to` would silently reset them. +// ponytail: assumes both branches exist; go-git errors surface as-is. func promote(args []string) { if len(args) != 3 { log.Fatal("usage: promptctl promote ") @@ -227,11 +229,17 @@ func promote(args []string) { } wt := worktree(r) + if dirty := trackedChanges(wt); len(dirty) > 0 { + log.Fatalf("promote needs a clean working tree; commit or stash first: %s", strings.Join(dirty, ", ")) + } head, err := r.Head() if err != nil { log.Fatal(err) } if err := wt.Checkout(&git.CheckoutOptions{Branch: plumbing.NewBranchReferenceName(to)}); err != nil { + // go-git can move HEAD before it fails; put it back so the user is not + // left on `to` with their own files in the tree. + restoreHead(r, head) log.Fatalf("checkout %s: %v", to, err) } h, err := commitFile(r, wt, path, content, fmt.Sprintf("promote %s: %s -> %s", path, from, to)) @@ -251,6 +259,37 @@ func promote(args []string) { fmt.Printf("promoted %s onto %s (%s)\n", path, to, h.String()[:12]) } +// trackedChanges lists files that are staged or modified. Untracked files are +// left alone by the checkout round trip, so they don't block promote. +func trackedChanges(wt *git.Worktree) []string { + st, err := wt.Status() + if err != nil { + log.Fatal(err) + } + var dirty []string + for f, fs := range st { + if fs.Staging == git.Untracked && fs.Worktree == git.Untracked { + continue + } + if fs.Staging != git.Unmodified || fs.Worktree != git.Unmodified { + dirty = append(dirty, f) + } + } + sort.Strings(dirty) + return dirty +} + +// restoreHead points HEAD back at the branch (or detached commit) it was on. +func restoreHead(r *git.Repository, head *plumbing.Reference) { + ref := plumbing.NewHashReference(plumbing.HEAD, head.Hash()) + if head.Name().IsBranch() { + ref = plumbing.NewSymbolicReference(plumbing.HEAD, head.Name()) + } + if err := r.Storer.SetReference(ref); err != nil { + log.Printf("could not restore HEAD to %s: %v", head.Name().Short(), err) + } +} + // commitFile writes content to path in the checked-out worktree and commits it. func commitFile(r *git.Repository, wt *git.Worktree, path, content, msg string) (plumbing.Hash, error) { if err := os.WriteFile(filepath.FromSlash(path), []byte(content), 0o644); err != nil { diff --git a/cmd/promptctl/promote_dirty_test.go b/cmd/promptctl/promote_dirty_test.go new file mode 100644 index 0000000..00267c5 --- /dev/null +++ b/cmd/promptctl/promote_dirty_test.go @@ -0,0 +1,132 @@ +package main + +import ( + "os" + "os/exec" + "strings" + "testing" + + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" +) + +// promoteRepo makes a repo on master whose acme/a.prompt differs on dev, with +// acme/b.prompt committed everywhere, and chdirs into it. +func promoteRepo(t *testing.T) *git.Repository { + t.Helper() + dir := t.TempDir() + t.Chdir(dir) + r, err := git.PlainInit(dir, false) + if err != nil { + t.Fatal(err) + } + wt, _ := r.Worktree() + sig := &object.Signature{Name: "t", Email: "t@t"} + write := func(path, content string) { + t.Helper() + if err := os.MkdirAll("acme", 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { + t.Fatal(err) + } + } + write("acme/a.prompt", "A v1 {x}\n") + write("acme/b.prompt", "B v1 {y}\n") + wt.Add("acme") + if _, err := wt.Commit("init", &git.CommitOptions{Author: sig}); err != nil { + t.Fatal(err) + } + head, _ := r.Head() + for _, b := range []string{"dev", "prod"} { + r.Storer.SetReference(plumbing.NewHashReference(plumbing.NewBranchReferenceName(b), head.Hash())) + } + wt.Checkout(&git.CheckoutOptions{Branch: plumbing.NewBranchReferenceName("dev")}) + write("acme/a.prompt", "A v2 {x}\n") + wt.Add("acme/a.prompt") + if _, err := wt.Commit("dev edit", &git.CommitOptions{Author: sig}); err != nil { + t.Fatal(err) + } + if err := wt.Checkout(&git.CheckoutOptions{Branch: plumbing.NewBranchReferenceName("master")}); err != nil { + t.Fatal(err) + } + return r +} + +// runPromote runs promote in a child process (it exits via log.Fatal) and +// returns its combined output and whether it failed. +func runPromote(t *testing.T, args ...string) (string, bool) { + t.Helper() + cmd := exec.Command(os.Args[0], "-test.run=^TestPromoteHelperProcess$") + cmd.Env = append(os.Environ(), "PROMPTCTL_PROMOTE_ARGS="+strings.Join(args, ",")) + out, err := cmd.CombinedOutput() + return string(out), err != nil +} + +func TestPromoteHelperProcess(t *testing.T) { + args := os.Getenv("PROMPTCTL_PROMOTE_ARGS") + if args == "" { + t.Skip("helper for runPromote") + } + promote(strings.Split(args, ",")) + os.Exit(0) +} + +func TestPromoteRefusesStagedChanges(t *testing.T) { + r := promoteRepo(t) + wt, _ := r.Worktree() + os.WriteFile("acme/b.prompt", []byte("B staged {y}\n"), 0o644) + os.WriteFile("acme/n.prompt", []byte("N new {n}\n"), 0o644) + wt.Add("acme") + + out, failed := runPromote(t, "acme/a.prompt", "dev", "prod") + if !failed || !strings.Contains(out, "clean working tree") { + t.Fatalf("promote with staged changes: failed=%v out=%q, want refusal", failed, out) + } + if got := diskContent("acme/b.prompt"); got != "B staged {y}\n" { + t.Errorf("staged edit lost: b.prompt = %q", got) + } + if got := diskContent("acme/n.prompt"); got != "N new {n}\n" { + t.Errorf("staged new file lost: n.prompt = %q", got) + } + if st, _ := wt.Status(); st.File("acme/n.prompt").Staging != git.Added { + t.Errorf("n.prompt no longer staged: %q", st.File("acme/n.prompt").Staging) + } + if got := blobContent(treeAt(r, "prod"), "acme/a.prompt"); got != "A v1 {x}\n" { + t.Errorf("prod changed to %q although promote refused", got) + } +} + +func TestPromoteRefusesUnstagedChangesAndStaysOnBranch(t *testing.T) { + r := promoteRepo(t) + os.WriteFile("acme/b.prompt", []byte("B wip {y}\n"), 0o644) + + out, failed := runPromote(t, "acme/a.prompt", "dev", "prod") + if !failed || !strings.Contains(out, "clean working tree") { + t.Fatalf("promote with unstaged changes: failed=%v out=%q, want refusal", failed, out) + } + if h, _ := r.Head(); h.Name().Short() != "master" { + t.Errorf("HEAD is %s after refused promote, want master", h.Name().Short()) + } + if got := diskContent("acme/b.prompt"); got != "B wip {y}\n" { + t.Errorf("unstaged edit lost: b.prompt = %q", got) + } +} + +func TestPromoteAllowsUntrackedFiles(t *testing.T) { + r := promoteRepo(t) + os.WriteFile("notes.txt", []byte("scratch"), 0o644) + + promote([]string{"acme/a.prompt", "dev", "prod"}) + + if got := blobContent(treeAt(r, "prod"), "acme/a.prompt"); got != "A v2 {x}\n" { + t.Errorf("prod has %q, want promoted content", got) + } + if b, err := os.ReadFile("notes.txt"); err != nil || string(b) != "scratch" { + t.Errorf("untracked file lost: %q %v", b, err) + } + if h, _ := r.Head(); h.Name().Short() != "master" { + t.Errorf("HEAD is %s, want master", h.Name().Short()) + } +} From fb815fb729feba9b2c9b64df3b06df9a6755c1c3 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 15 Nov 2026 09:49:20 +0530 Subject: [PATCH 2/4] fix(promote): don't treat CRLF-only checkouts as uncommitted changes go-git ignores core.autocrlf when it has to hash a file (a racy index entry right after a checkout, or a touched file), so a clean CRLF checkout of an LF blob shows as modified and the new clean-tree check would block promote on Windows. A worktree-modified file now only counts when it differs from HEAD by more than line endings (changedLines). --- cmd/promptctl/main.go | 18 +++++++++++++++--- cmd/promptctl/promote_dirty_test.go | 13 +++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/cmd/promptctl/main.go b/cmd/promptctl/main.go index cd32c83..d716f3e 100644 --- a/cmd/promptctl/main.go +++ b/cmd/promptctl/main.go @@ -229,7 +229,7 @@ func promote(args []string) { } wt := worktree(r) - if dirty := trackedChanges(wt); len(dirty) > 0 { + if dirty := trackedChanges(r, wt); len(dirty) > 0 { log.Fatalf("promote needs a clean working tree; commit or stash first: %s", strings.Join(dirty, ", ")) } head, err := r.Head() @@ -260,17 +260,29 @@ func promote(args []string) { } // trackedChanges lists files that are staged or modified. Untracked files are -// left alone by the checkout round trip, so they don't block promote. -func trackedChanges(wt *git.Worktree) []string { +// left alone by the checkout round trip, so they don't block promote. go-git +// ignores core.autocrlf whenever it has to hash a file (a racy index entry, a +// touched file), so a CRLF checkout of an LF blob shows as modified on +// Windows; such a file only counts when it differs by more than line endings. +func trackedChanges(r *git.Repository, wt *git.Worktree) []string { st, err := wt.Status() if err != nil { log.Fatal(err) } + var head *object.Tree var dirty []string for f, fs := range st { if fs.Staging == git.Untracked && fs.Worktree == git.Untracked { continue } + if fs.Staging == git.Unmodified && fs.Worktree == git.Modified { + if head == nil { + head = treeAt(r, "HEAD") + } + if _, _, changed := changedLines(blobContent(head, f), diskContent(f)); !changed { + continue + } + } if fs.Staging != git.Unmodified || fs.Worktree != git.Unmodified { dirty = append(dirty, f) } diff --git a/cmd/promptctl/promote_dirty_test.go b/cmd/promptctl/promote_dirty_test.go index 00267c5..3449456 100644 --- a/cmd/promptctl/promote_dirty_test.go +++ b/cmd/promptctl/promote_dirty_test.go @@ -114,6 +114,19 @@ func TestPromoteRefusesUnstagedChangesAndStaysOnBranch(t *testing.T) { } } +// A CRLF checkout of an LF blob (core.autocrlf on Windows) is not a change, +// even when go-git's status reports it as modified. +func TestPromoteAllowsLineEndingOnlyDifferences(t *testing.T) { + r := promoteRepo(t) + os.WriteFile("acme/b.prompt", []byte("B v1 {y}\r\n"), 0o644) + + promote([]string{"acme/a.prompt", "dev", "prod"}) + + if got := blobContent(treeAt(r, "prod"), "acme/a.prompt"); got != "A v2 {x}\n" { + t.Errorf("prod has %q, want promoted content", got) + } +} + func TestPromoteAllowsUntrackedFiles(t *testing.T) { r := promoteRepo(t) os.WriteFile("notes.txt", []byte("scratch"), 0o644) From 3c320c0b48955de5a8a20def1de4adc5d1047a11 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 15 Nov 2026 09:51:02 +0530 Subject: [PATCH 3/4] fix(promote): rewrite CRLF-only files instead of letting go-git's checkout refuse The previous commit let a CRLF-only file through promote's own check, but go-git's Checkout hits the same false positive and refuses with "worktree contains unstaged changes" (TestPromoteAllowsLineEndingOnlyDifferences failed). Such files are now rewritten with their committed content before the round trip; only their line endings change, as for every file go-git checks out. Checkout Force was not an option: it deletes untracked files. --- cmd/promptctl/main.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/promptctl/main.go b/cmd/promptctl/main.go index d716f3e..25e08bb 100644 --- a/cmd/promptctl/main.go +++ b/cmd/promptctl/main.go @@ -260,10 +260,13 @@ func promote(args []string) { } // trackedChanges lists files that are staged or modified. Untracked files are -// left alone by the checkout round trip, so they don't block promote. go-git -// ignores core.autocrlf whenever it has to hash a file (a racy index entry, a -// touched file), so a CRLF checkout of an LF blob shows as modified on -// Windows; such a file only counts when it differs by more than line endings. +// left alone by the checkout round trip, so they don't block promote. +// +// go-git ignores core.autocrlf whenever it has to hash a file (a racy index +// entry, a touched file), so a CRLF checkout of an LF blob shows as modified on +// Windows, and go-git's own Checkout then refuses to run. Such a file is not a +// change: it is rewritten with its committed content (only its line endings +// change, as they do for every file go-git checks out) and not reported. func trackedChanges(r *git.Repository, wt *git.Worktree) []string { st, err := wt.Status() if err != nil { @@ -279,7 +282,11 @@ func trackedChanges(r *git.Repository, wt *git.Worktree) []string { if head == nil { head = treeAt(r, "HEAD") } - if _, _, changed := changedLines(blobContent(head, f), diskContent(f)); !changed { + committed := blobContent(head, f) + if _, _, changed := changedLines(committed, diskContent(f)); !changed { + if err := os.WriteFile(filepath.FromSlash(f), []byte(committed), 0o644); err != nil { + log.Fatal(err) + } continue } } From 06a3c9142252598b5b3033dafc5ada768d9e23a2 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 15 Nov 2026 10:39:53 +0530 Subject: [PATCH 4/4] refactor(promote): compare line endings with a local helper The changedLines helper this branch used came from #12, which was reverted in #33. Use a small sameIgnoringLineEndings helper instead so promote does not depend on that change. --- cmd/promptctl/main.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cmd/promptctl/main.go b/cmd/promptctl/main.go index 25e08bb..3b8fcda 100644 --- a/cmd/promptctl/main.go +++ b/cmd/promptctl/main.go @@ -283,7 +283,7 @@ func trackedChanges(r *git.Repository, wt *git.Worktree) []string { head = treeAt(r, "HEAD") } committed := blobContent(head, f) - if _, _, changed := changedLines(committed, diskContent(f)); !changed { + if sameIgnoringLineEndings(committed, diskContent(f)) { if err := os.WriteFile(filepath.FromSlash(f), []byte(committed), 0o644); err != nil { log.Fatal(err) } @@ -607,3 +607,9 @@ func firstLine(s string) string { func splitLines(s string) []string { return strings.Split(strings.ReplaceAll(s, "\r\n", "\n"), "\n") } + +// sameIgnoringLineEndings reports whether two versions differ only in CRLF vs +// LF — a core.autocrlf checkout on Windows is not a change. +func sameIgnoringLineEndings(a, b string) bool { + return strings.ReplaceAll(a, "\r\n", "\n") == strings.ReplaceAll(b, "\r\n", "\n") +}