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 "" }