Protect containing worktrees from deleting nested checkouts
This commit is contained in:
@@ -31,6 +31,7 @@ type storageWorktree struct {
|
||||
ModifiedAt time.Time `json:"modified_at"`
|
||||
Eligible bool `json:"eligible"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
knownRoots []string
|
||||
locked bool
|
||||
}
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// A clean ignored directory may itself be another checkout. Removing its
|
||||
// parent would erase that independent tree, so retain every containing tree.
|
||||
// Primary checkouts and recorded workspaces matter as well as linked leaves.
|
||||
func protectNestedStorageWorktrees(ctx context.Context, trees []storageWorktree, roots []string, registered map[string]bool, problems []string) []storageWorktree {
|
||||
known := append([]string{}, roots...)
|
||||
for path := range registered {
|
||||
known = append(known, path)
|
||||
}
|
||||
incomplete := len(problems) > 0 || len(roots) > 256
|
||||
canonical := []string{}
|
||||
for _, path := range known {
|
||||
if ctx.Err() != nil {
|
||||
incomplete = true
|
||||
break
|
||||
}
|
||||
resolved, err := storageCanonicalPath(path)
|
||||
if err != nil {
|
||||
incomplete = true
|
||||
continue
|
||||
}
|
||||
canonical = append(canonical, resolved)
|
||||
}
|
||||
for index := range trees {
|
||||
if incomplete {
|
||||
trees[index].Eligible = false
|
||||
trees[index].Reason = "Workspace discovery incomplete"
|
||||
continue
|
||||
}
|
||||
for _, path := range canonical {
|
||||
if pathWithin(trees[index].Path, path) {
|
||||
trees[index].Eligible = false
|
||||
trees[index].Reason = "Contains another registered worktree or workspace"
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return trees
|
||||
}
|
||||
|
||||
// Refresh the same-repository registry at the destructive boundary. A nested
|
||||
// tree created after preview must not be removed even when Git ignores it.
|
||||
func storageNestedProtection(ctx context.Context, root string, known []string) string {
|
||||
listing, err := storageGit(ctx, root, "worktree", "list", "--porcelain", "-z")
|
||||
if err != nil {
|
||||
return "Worktree registry unavailable"
|
||||
}
|
||||
paths := append([]string{}, known...)
|
||||
for _, record := range strings.Split(listing, "\x00\x00") {
|
||||
fields := strings.Split(record, "\x00")
|
||||
if len(fields) > 0 && strings.HasPrefix(fields[0], "worktree ") {
|
||||
paths = append(paths, strings.TrimPrefix(fields[0], "worktree "))
|
||||
}
|
||||
}
|
||||
if len(known) > 256 {
|
||||
return "Workspace discovery incomplete"
|
||||
}
|
||||
for _, path := range paths {
|
||||
if ctx.Err() != nil {
|
||||
return "Workspace discovery incomplete"
|
||||
}
|
||||
canonical, err := storageCanonicalPath(path)
|
||||
if err != nil {
|
||||
return "Workspace path cannot be verified"
|
||||
}
|
||||
if pathWithin(root, canonical) {
|
||||
return "Contains another registered worktree or workspace"
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -160,3 +160,81 @@ func TestStorageWorktreesRequireMergedCleanInactiveAndUnlocked(t *testing.T) {
|
||||
t.Fatal("main checkout removed")
|
||||
}
|
||||
}
|
||||
|
||||
func storageTestIgnoredParent(t *testing.T) (string, string, storageWorktree) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
main, parent := storageTestRepo(t)
|
||||
storageTestFile(t, filepath.Join(parent, ".gitignore"), "nested/\n")
|
||||
storageTestGit(t, parent, "add", ".gitignore")
|
||||
storageTestGit(t, parent, "commit", "-m", "ignore nested workspaces")
|
||||
storageTestGit(t, main, "merge", "--ff-only", "item")
|
||||
trees, problems := discoverStorageWorktrees(ctx, []string{main}, nil)
|
||||
if len(problems) > 0 || len(trees) != 1 || !trees[0].Eligible {
|
||||
t.Fatalf("parent initially unavailable: %+v %+v", trees, problems)
|
||||
}
|
||||
return main, parent, trees[0]
|
||||
}
|
||||
|
||||
func TestStorageNestedIgnoredRegisteredWorktreeProtectsParent(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
main, parent, before := storageTestIgnoredParent(t)
|
||||
child := filepath.Join(parent, "nested")
|
||||
storageTestGit(t, main, "worktree", "add", "-b", "nested-child", child)
|
||||
if status := storageTestGit(t, parent, "status", "--porcelain", "--untracked-files=all"); status != "" {
|
||||
t.Fatalf("fixture parent dirty: %q", status)
|
||||
}
|
||||
trees, problems := discoverStorageWorktrees(ctx, []string{main}, nil)
|
||||
if len(problems) > 0 || len(trees) != 2 {
|
||||
t.Fatalf("trees %+v problems %+v", trees, problems)
|
||||
}
|
||||
found := false
|
||||
for _, tree := range trees {
|
||||
if tree.Path == before.Path {
|
||||
found = true
|
||||
if tree.Eligible || !strings.Contains(tree.Reason, "Contains another") {
|
||||
t.Fatalf("parent not protected: %+v", tree)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("parent missing from inventory")
|
||||
}
|
||||
// Before has no knowledge of the child. Removal must read Git's live registry.
|
||||
if err := removeStorageWorktree(ctx, before, nil); err == nil {
|
||||
t.Fatal("stale parent candidate removed ignored child")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(child, "tracked")); err != nil {
|
||||
t.Fatal("child checkout was damaged")
|
||||
}
|
||||
storageTestGit(t, main, "show-ref", "--verify", "refs/heads/nested-child")
|
||||
}
|
||||
|
||||
func TestStorageNestedForeignPrimaryWorkspaceProtectsParent(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
main, parent, before := storageTestIgnoredParent(t)
|
||||
child := filepath.Join(parent, "nested")
|
||||
if err := os.Mkdir(child, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
storageTestGit(t, child, "init", "-b", "main")
|
||||
storageTestGit(t, child, "config", "user.email", "fixture@example.invalid")
|
||||
storageTestGit(t, child, "config", "user.name", "Fixture")
|
||||
storageTestFile(t, filepath.Join(child, "independent"), "preserve foreign primary")
|
||||
storageTestGit(t, child, "add", "independent")
|
||||
storageTestGit(t, child, "commit", "-m", "foreign primary")
|
||||
trees, problems := discoverStorageWorktrees(ctx, []string{main, child}, nil)
|
||||
if len(problems) > 0 || len(trees) != 1 || trees[0].Eligible {
|
||||
t.Fatalf("foreign nested workspace not protected: %+v %+v", trees, problems)
|
||||
}
|
||||
// A manager passes the fresh full inventory object to destructive revalidation.
|
||||
if err := removeStorageWorktree(ctx, trees[0], nil); err == nil {
|
||||
t.Fatal("parent removed foreign recorded workspace")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(child, "independent")); err != nil {
|
||||
t.Fatal("foreign primary damaged")
|
||||
}
|
||||
if trees[0].Path != before.Path {
|
||||
t.Fatal("unexpected parent")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user