Protect containing worktrees from deleting nested checkouts
This commit is contained in:
@@ -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