Initial commit

This commit is contained in:
Shaun Kruger
2026-10-11 21:36:20 -06:00
commit 48b6c0d6cb
13 changed files with 1717 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
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
}