| 97 | } |
| 98 | |
| 99 | func runDeduplicate(cmd *cobra.Command, args []string) error { |
| 100 | if err := ValidateFormat(dedupFormat, []string{"text", "json"}); err != nil { |
| 101 | return err |
| 102 | } |
| 103 | |
| 104 | flags := GetGlobalFlags() |
| 105 | scanDir := ResolveScanDir(args) |
| 106 | |
| 107 | taskScanner := scanner.NewScanner(scanDir, flags.Verbose, flags.IgnoreDirs) |
| 108 | scanResult, err := taskScanner.Scan() |
| 109 | if err != nil { |
| 110 | return fmt.Errorf("scan failed: %w", err) |
| 111 | } |
| 112 | |
| 113 | reassignments, err := planReassignments(scanResult.Tasks) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | |
| 118 | idMap := buildIDMap(scanResult.Tasks) |
| 119 | ambRefs := findAmbiguousRefs(reassignments, scanResult.Tasks, idMap) |
| 120 | |
| 121 | interactive := !dedupNoInteractive && !dedupDryRun && dedupIsTTY() |
| 122 | |
| 123 | var refOverrides map[string]string |
| 124 | if len(ambRefs) > 0 && interactive { |
| 125 | refOverrides, err = promptDisambiguation(ambRefs, reassignments) |
| 126 | if err != nil { |
| 127 | return err |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if !dedupDryRun { |
| 132 | if err := applyReassignments(reassignments, scanResult.Tasks, refOverrides); err != nil { |
| 133 | return err |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | result := deduplicateResult{ |
| 138 | DryRun: dedupDryRun, |
| 139 | Duplicates: len(reassignments), |
| 140 | Reassignments: reassignments, |
| 141 | AmbiguousRefs: ambRefs, |
| 142 | } |
| 143 | |
| 144 | return outputDeduplicateResult(result, flags.Quiet) |
| 145 | } |
| 146 | |
| 147 | // findAmbiguousRefs finds references to duplicate IDs from tasks outside the collision group. |
| 148 | func findAmbiguousRefs(reassignments []reassignment, allTasks []*model.Task, idMap map[string][]*model.Task) []ambiguousRef { |