Protect containing worktrees from deleting nested checkouts

This commit is contained in:
2026-11-06 21:29:14 +00:00
parent 18eec34f56
commit aa19c82156
4 changed files with 163 additions and 3 deletions
+7 -3
View File
@@ -71,14 +71,14 @@ func discoverStorageWorktrees(ctx context.Context, roots, protected []string) ([
seenTrees[path] = true
if len(trees) >= 512 {
problems = append(problems, "Worktree discovery limit reached")
return trees, problems
return protectNestedStorageWorktrees(ctx, trees, roots, seenTrees, problems), problems
}
// The primary checkout has a .git directory; linked worktrees have a file.
gitInfo, err := os.Lstat(filepath.Join(path, ".git"))
if err != nil || !gitInfo.Mode().IsRegular() {
continue
}
tree := storageWorktree{Path: path, Repository: repository}
tree := storageWorktree{Path: path, Repository: repository, knownRoots: roots}
for _, field := range fields[1:] {
if strings.HasPrefix(field, "branch ") {
tree.Branch = strings.TrimPrefix(field, "branch refs/heads/")
@@ -95,7 +95,7 @@ func discoverStorageWorktrees(ctx context.Context, roots, protected []string) ([
trees = append(trees, tree)
}
}
return trees, problems
return protectNestedStorageWorktrees(ctx, trees, roots, seenTrees, problems), problems
}
// Eligibility is deliberately conservative: no forced removal, branch deletion,
@@ -144,6 +144,10 @@ func storageWorktreeProtection(ctx context.Context, tree storageWorktree, protec
return "Git worktree is locked"
}
if reason := storageNestedProtection(ctx, root, tree.knownRoots); reason != "" {
return reason
}
currentBranch, _ := storageGit(ctx, root, "symbolic-ref", "--quiet", "--short", "HEAD")
if currentBranch == "main" || currentBranch == "master" {
return "Main branch checkout"