| | | 1 | | package git |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "path/filepath" |
| | | 5 | | "strings" |
| | | 6 | | |
| | | 7 | | "github.com/jedi-knights/go-semantic-release/internal/domain" |
| | | 8 | | "github.com/jedi-knights/go-semantic-release/internal/ports" |
| | | 9 | | ) |
| | | 10 | | |
| | | 11 | | // Compile-time interface compliance check. |
| | | 12 | | var _ ports.ProjectImpactAnalyzer = (*PathBasedImpactAnalyzer)(nil) |
| | | 13 | | |
| | | 14 | | // PathBasedImpactAnalyzer maps changed files to projects based on path prefixes. |
| | | 15 | | type PathBasedImpactAnalyzer struct { |
| | | 16 | | propagateDeps bool |
| | | 17 | | includePaths []string |
| | | 18 | | excludePaths []string |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | // NewPathBasedImpactAnalyzer creates a new path-based impact analyzer. |
| | | 22 | | func NewPathBasedImpactAnalyzer(propagateDeps bool, includePaths, excludePaths []string) *PathBasedImpactAnalyzer { |
| | | 23 | | return &PathBasedImpactAnalyzer{ |
| | | 24 | | propagateDeps: propagateDeps, |
| | | 25 | | includePaths: includePaths, |
| | | 26 | | excludePaths: excludePaths, |
| | | 27 | | } |
| | | 28 | | } |
| | | 29 | | |
| | 8 | 30 | | func (a *PathBasedImpactAnalyzer) Analyze(projects []domain.Project, commits []domain.Commit) map[string][]domain.Commit |
| | 8 | 31 | | result := make(map[string][]domain.Commit) |
| | 8 | 32 | | |
| | 8 | 33 | | for i := range commits { |
| | 22 | 34 | | affected := a.findAffectedProjects(projects, commits[i].FilesChanged) |
| | 16 | 35 | | for _, projName := range affected { |
| | 16 | 36 | | result[projName] = append(result[projName], commits[i]) |
| | 16 | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | 1 | 40 | | if a.propagateDeps { |
| | 1 | 41 | | a.propagateDependencies(projects, result) |
| | 1 | 42 | | } |
| | | 43 | | |
| | 8 | 44 | | return result |
| | | 45 | | } |
| | | 46 | | |
| | 22 | 47 | | func (a *PathBasedImpactAnalyzer) findAffectedProjects(projects []domain.Project, files []string) []string { |
| | 22 | 48 | | seen := make(map[string]bool) |
| | 22 | 49 | | var affected []string |
| | 22 | 50 | | |
| | 22 | 51 | | filtered := a.filterFiles(files) |
| | 20 | 52 | | for _, file := range filtered { |
| | 20 | 53 | | for _, proj := range projects { |
| | 5 | 54 | | if seen[proj.Name] { |
| | 5 | 55 | | continue |
| | | 56 | | } |
| | 16 | 57 | | if proj.IsRoot() || fileInProject(file, proj.Path) { |
| | 16 | 58 | | seen[proj.Name] = true |
| | 16 | 59 | | affected = append(affected, proj.Name) |
| | 16 | 60 | | } |
| | | 61 | | } |
| | | 62 | | } |
| | 22 | 63 | | return affected |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | // filterFiles applies include/exclude glob patterns to the file list. |
| | 22 | 67 | | func (a *PathBasedImpactAnalyzer) filterFiles(files []string) []string { |
| | 7 | 68 | | if len(a.includePaths) == 0 && len(a.excludePaths) == 0 { |
| | 7 | 69 | | return files |
| | 7 | 70 | | } |
| | | 71 | | |
| | 15 | 72 | | result := make([]string, 0, len(files)) |
| | 15 | 73 | | for _, file := range files { |
| | 7 | 74 | | if len(a.includePaths) > 0 && !matchesAny(file, a.includePaths) { |
| | 7 | 75 | | continue |
| | | 76 | | } |
| | 3 | 77 | | if matchesAny(file, a.excludePaths) { |
| | 3 | 78 | | continue |
| | | 79 | | } |
| | 11 | 80 | | result = append(result, file) |
| | | 81 | | } |
| | 15 | 82 | | return result |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | // matchesAny returns true if the file matches any of the glob patterns. |
| | | 86 | | func matchesAny(file string, patterns []string) bool { |
| | | 87 | | for _, pattern := range patterns { |
| | | 88 | | if matched, _ := filepath.Match(pattern, file); matched { |
| | | 89 | | return true |
| | | 90 | | } |
| | | 91 | | // Also try matching against just the filename for simple patterns. |
| | | 92 | | if matched, _ := filepath.Match(pattern, filepath.Base(file)); matched { |
| | | 93 | | return true |
| | | 94 | | } |
| | | 95 | | // Support prefix-based patterns like "services/api/**" by checking prefix. |
| | | 96 | | if strings.HasSuffix(pattern, "/**") { |
| | | 97 | | prefix := strings.TrimSuffix(pattern, "/**") |
| | | 98 | | if strings.HasPrefix(file, prefix+"/") || file == prefix { |
| | | 99 | | return true |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | return false |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | func fileInProject(file, projectPath string) bool { |
| | | 107 | | if projectPath == "" || projectPath == "." { |
| | | 108 | | return true |
| | | 109 | | } |
| | | 110 | | prefix := projectPath + "/" |
| | | 111 | | return strings.HasPrefix(file, prefix) || file == projectPath |
| | | 112 | | } |
| | | 113 | | |
| | 1 | 114 | | func (a *PathBasedImpactAnalyzer) propagateDependencies(projects []domain.Project, result map[string][]domain.Commit) { |
| | 1 | 115 | | projectMap := make(map[string]domain.Project, len(projects)) |
| | 1 | 116 | | for _, p := range projects { |
| | 3 | 117 | | projectMap[p.Name] = p |
| | 3 | 118 | | } |
| | | 119 | | |
| | | 120 | | // Simple single-pass propagation: if a dependency has commits, mark dependents. |
| | 1 | 121 | | for _, proj := range projects { |
| | 2 | 122 | | for _, dep := range proj.Dependencies { |
| | 2 | 123 | | if commits, ok := result[dep]; ok && len(commits) > 0 { |
| | 2 | 124 | | if _, alreadyAffected := result[proj.Name]; !alreadyAffected { |
| | 2 | 125 | | // Propagate with a synthetic marker — use the dependency's commits. |
| | 2 | 126 | | result[proj.Name] = commits |
| | 2 | 127 | | } |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | } |