Find every stale worktree, not just the managed ones
- discover worktrees from `git worktree list` on each source checkout, not only the worktree root, so hand-made ones, stale registrations and orphaned directories are classified too - normalise each candidate to its main checkout, so a linked worktree in the source root cannot offer up the repo's real checkout - keep locked, mid-rebase and detached-with-unique-commits worktrees, whose removal would destroy state nothing else holds - name the retained branch in every unproven verdict - add --no-fetch, --json, --include-unmanaged and --include-keep
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -25,6 +26,10 @@ type Worktree struct {
|
||||
Branch string // short branch name ("" when detached or bare)
|
||||
Bare bool
|
||||
Detached bool
|
||||
Locked bool
|
||||
// Prunable is git's own reason a registration is stale (e.g. "gitdir file
|
||||
// points to non-existent location"); empty when the worktree is intact.
|
||||
Prunable string
|
||||
}
|
||||
|
||||
// runGit runs git with args, using dir as the working directory (empty = the
|
||||
@@ -114,6 +119,54 @@ func GitIsDirty(dir string) (bool, error) {
|
||||
return strings.TrimSpace(out) != "", nil
|
||||
}
|
||||
|
||||
// GitDir returns the absolute path to the git directory backing the checkout at
|
||||
// dir (per-worktree, unlike GitCommonDir).
|
||||
func GitDir(dir string) (string, error) {
|
||||
return runGit(dir, "rev-parse", "--path-format=absolute", "--git-dir")
|
||||
}
|
||||
|
||||
// inProgressMarkers maps a sentinel inside the git dir to the operation it means
|
||||
// is half-finished. Such a checkout holds state that lives nowhere else.
|
||||
var inProgressMarkers = []struct{ path, op string }{
|
||||
{"rebase-merge", "rebase"},
|
||||
{"rebase-apply", "rebase"},
|
||||
{"MERGE_HEAD", "merge"},
|
||||
{"CHERRY_PICK_HEAD", "cherry-pick"},
|
||||
{"REVERT_HEAD", "revert"},
|
||||
{"BISECT_LOG", "bisect"},
|
||||
}
|
||||
|
||||
// GitInProgressOp names the sequencer operation underway in the checkout at dir,
|
||||
// or "" when none is.
|
||||
func GitInProgressOp(dir string) (string, error) {
|
||||
gitDir, err := GitDir(dir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, m := range inProgressMarkers {
|
||||
if _, err := os.Stat(filepath.Join(gitDir, m.path)); err == nil {
|
||||
return m.op, nil
|
||||
} else if !os.IsNotExist(err) {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// GitCommitsNotOnRemotes counts commits reachable from HEAD that no
|
||||
// remote-tracking ref holds, i.e. work that exists only in this checkout.
|
||||
func GitCommitsNotOnRemotes(dir string) (int, error) {
|
||||
out, err := runGit(dir, "rev-list", "--count", "HEAD", "--not", "--remotes")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
n, err := strconv.Atoi(strings.TrimSpace(out))
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("parse rev-list count %q: %w", out, err)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// GitIsAncestor reports whether ancestor is reachable from descendant.
|
||||
func GitIsAncestor(repoDir, ancestor, descendant string) (bool, error) {
|
||||
cmd := exec.Command("git", "merge-base", "--is-ancestor", ancestor, descendant)
|
||||
@@ -277,6 +330,18 @@ func ParseWorktreeList(out string) []Worktree {
|
||||
if cur != nil {
|
||||
cur.Detached = true
|
||||
}
|
||||
case "locked":
|
||||
if cur != nil {
|
||||
cur.Locked = true
|
||||
}
|
||||
case "prunable":
|
||||
if cur != nil {
|
||||
// git omits the reason when it has none, so record the flag itself.
|
||||
cur.Prunable = val
|
||||
if cur.Prunable == "" {
|
||||
cur.Prunable = "prunable"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
flush()
|
||||
|
||||
+110
-2
@@ -42,10 +42,20 @@ branch refs/heads/benvin/foo
|
||||
worktree /home/ben/.cache/agentws/repo__detached
|
||||
HEAD 3333333333333333333333333333333333333333
|
||||
detached
|
||||
|
||||
worktree /home/ben/.cache/agentws/repo__gone
|
||||
HEAD 4444444444444444444444444444444444444444
|
||||
branch refs/heads/benvin/gone
|
||||
prunable gitdir file points to non-existent location
|
||||
|
||||
worktree /home/ben/.cache/agentws/repo__held
|
||||
HEAD 5555555555555555555555555555555555555555
|
||||
branch refs/heads/benvin/held
|
||||
locked
|
||||
`
|
||||
wts := ParseWorktreeList(out)
|
||||
if len(wts) != 3 {
|
||||
t.Fatalf("got %d worktrees, want 3: %+v", len(wts), wts)
|
||||
if len(wts) != 5 {
|
||||
t.Fatalf("got %d worktrees, want 5: %+v", len(wts), wts)
|
||||
}
|
||||
if wts[0].Branch != "main" || wts[0].Path != "/home/ben/src/prodenv/repo" {
|
||||
t.Errorf("wt[0] = %+v", wts[0])
|
||||
@@ -56,6 +66,104 @@ detached
|
||||
if !wts[2].Detached || wts[2].Branch != "" {
|
||||
t.Errorf("wt[2] = %+v, want detached with empty branch", wts[2])
|
||||
}
|
||||
if wts[3].Prunable != "gitdir file points to non-existent location" {
|
||||
t.Errorf("wt[3].Prunable = %q", wts[3].Prunable)
|
||||
}
|
||||
if !wts[4].Locked || wts[4].Prunable != "" {
|
||||
t.Errorf("wt[4] = %+v, want locked and not prunable", wts[4])
|
||||
}
|
||||
}
|
||||
|
||||
// A bare "prunable" with no reason still has to read as prunable.
|
||||
func TestParseWorktreeListPrunableWithoutReason(t *testing.T) {
|
||||
wts := ParseWorktreeList("worktree /tmp/wt\nHEAD 1111111111111111111111111111111111111111\ndetached\nprunable\n")
|
||||
if len(wts) != 1 || wts[0].Prunable == "" {
|
||||
t.Errorf("ParseWorktreeList = %+v, want one prunable worktree", wts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGitInProgressOp(t *testing.T) {
|
||||
srcDir := newTempRepos(t)
|
||||
gitDir, err := GitDir(srcDir)
|
||||
if err != nil {
|
||||
t.Fatalf("GitDir: %v", err)
|
||||
}
|
||||
if op, err := GitInProgressOp(srcDir); err != nil || op != "" {
|
||||
t.Fatalf("clean checkout: op = %q, err = %v", op, err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
marker string
|
||||
dir bool
|
||||
want string
|
||||
}{
|
||||
{"MERGE_HEAD", false, "merge"},
|
||||
{"CHERRY_PICK_HEAD", false, "cherry-pick"},
|
||||
{"REVERT_HEAD", false, "revert"},
|
||||
{"BISECT_LOG", false, "bisect"},
|
||||
{"rebase-merge", true, "rebase"},
|
||||
{"rebase-apply", true, "rebase"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
path := filepath.Join(gitDir, tt.marker)
|
||||
if tt.dir {
|
||||
if err := os.Mkdir(path, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else if err := os.WriteFile(path, []byte("x\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
op, err := GitInProgressOp(srcDir)
|
||||
if err != nil || op != tt.want {
|
||||
t.Errorf("%s: op = %q, err = %v; want %q", tt.marker, op, err, tt.want)
|
||||
}
|
||||
if err := os.RemoveAll(path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Commits that reached a remote-tracking ref are not unique local work; commits
|
||||
// made after the push are.
|
||||
func TestGitCommitsNotOnRemotes(t *testing.T) {
|
||||
srcDir := newTempRepos(t)
|
||||
if n, err := GitCommitsNotOnRemotes(srcDir); err != nil || n != 0 {
|
||||
t.Fatalf("freshly cloned main: n = %d, err = %v; want 0", n, err)
|
||||
}
|
||||
|
||||
if _, err := runGit(srcDir, "checkout", "-b", "benvin/x"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
commitFile(t, srcDir, "a.txt", "a\n", "local a")
|
||||
commitFile(t, srcDir, "b.txt", "b\n", "local b")
|
||||
if n, err := GitCommitsNotOnRemotes(srcDir); err != nil || n != 2 {
|
||||
t.Fatalf("two unpushed commits: n = %d, err = %v; want 2", n, err)
|
||||
}
|
||||
|
||||
if _, err := runGit(srcDir, "push", "origin", "benvin/x"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n, err := GitCommitsNotOnRemotes(srcDir); err != nil || n != 0 {
|
||||
t.Fatalf("after push: n = %d, err = %v; want 0", n, err)
|
||||
}
|
||||
|
||||
commitFile(t, srcDir, "c.txt", "c\n", "local c")
|
||||
if n, err := GitCommitsNotOnRemotes(srcDir); err != nil || n != 1 {
|
||||
t.Fatalf("one commit past the push: n = %d, err = %v; want 1", n, err)
|
||||
}
|
||||
}
|
||||
|
||||
func commitFile(t *testing.T, dir, name, content, msg string) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := runGit(dir, "add", "."); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := runGit(dir, "commit", "-m", msg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// gitSeed sets a repo-local identity so commits work without global config.
|
||||
|
||||
Reference in New Issue
Block a user