package main import ( "path/filepath" "sort" ) type patternItem struct { Delete string Keep string } // PlanPatternCleanup finds deletions that mirror the user's choice in one group. // For every duplicate group with a copy directly in the preferred file's directory, // copies directly in the directories of the marked files that have the same file // name as that kept copy are scheduled for deletion. Marked files are always included // (kept copy: the preferred file). Groups whose kept copy is in pendingMarks are skipped. func PlanPatternCleanup(groups []DupGroup, preferred string, marked []string, pendingMarks map[string]bool) []patternItem { prefDir := filepath.Dir(preferred) delDirs := map[string]bool{} for _, m := range marked { if d := filepath.Dir(m); d != prefDir { delDirs[d] = true } } var items []patternItem seen := map[string]bool{} add := func(del, keep string) { if !seen[del] { seen[del] = true items = append(items, patternItem{del, keep}) } } for _, m := range marked { add(m, preferred) } for _, g := range groups { for _, keep := range g.Files { if filepath.Dir(keep.FilePath) != prefDir || pendingMarks[keep.FilePath] { continue } for _, f := range g.Files { if delDirs[filepath.Dir(f.FilePath)] && f.FileName == keep.FileName { add(f.FilePath, keep.FilePath) } } } } sort.SliceStable(items, func(i, j int) bool { return items[i].Delete < items[j].Delete }) return items }