AnalyzeImpactFromAnalyses computes impact using a pre-computed ast-grep scan, so callers that already hold the analyses can avoid a redundant full-repo ScanForDeps. AnalyzeImpact is the convenience wrapper that performs the scan.
(changedFiles []FileInfo, analyses []FileAnalysis)
| 179 | // so callers that already hold the analyses can avoid a redundant full-repo |
| 180 | // ScanForDeps. AnalyzeImpact is the convenience wrapper that performs the scan. |
| 181 | func AnalyzeImpactFromAnalyses(changedFiles []FileInfo, analyses []FileAnalysis) []ImpactInfo { |
| 182 | if len(changedFiles) == 0 { |
| 183 | return nil |
| 184 | } |
| 185 | |
| 186 | // Build set of changed file base names and directories |
| 187 | changedBases := make(map[string]string) // base name -> full path |
| 188 | changedDirs := make(map[string]string) // dir name -> representative file |
| 189 | for _, f := range changedFiles { |
| 190 | base := strings.TrimSuffix(filepath.Base(f.Path), filepath.Ext(f.Path)) |
| 191 | changedBases[base] = f.Path |
| 192 | |
| 193 | // Also track directories for Go-style package imports |
| 194 | dir := filepath.Dir(f.Path) |
| 195 | if dir != "." && dir != "" { |
| 196 | dirBase := filepath.Base(dir) |
| 197 | if _, exists := changedDirs[dirBase]; !exists { |
| 198 | changedDirs[dirBase] = f.Path |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | usageCounts := make(map[string]int) |
| 204 | for _, analysis := range analyses { |
| 205 | // Check each import to see if it references a changed file |
| 206 | for _, imp := range analysis.Imports { |
| 207 | // Extract the last component of the import path |
| 208 | impBase := filepath.Base(imp) |
| 209 | impBase = strings.TrimSuffix(impBase, filepath.Ext(impBase)) |
| 210 | |
| 211 | // Check if this import matches a changed file (by filename) |
| 212 | if changedPath, ok := changedBases[impBase]; ok { |
| 213 | if analysis.Path != changedPath { |
| 214 | usageCounts[changedPath]++ |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Also check if import matches a changed directory (Go packages) |
| 219 | if changedPath, ok := changedDirs[impBase]; ok { |
| 220 | changedDir := filepath.Dir(changedPath) |
| 221 | if filepath.Dir(analysis.Path) != changedDir { |
| 222 | usageCounts[changedDir+"/"]++ |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // Build impact info |
| 229 | var impacts []ImpactInfo |
| 230 | for file, count := range usageCounts { |
| 231 | if count > 0 { |
| 232 | impacts = append(impacts, ImpactInfo{ |
| 233 | File: filepath.Base(file), |
| 234 | UsedBy: count, |
| 235 | }) |
| 236 | } |
| 237 | } |
| 238 |
no outgoing calls