|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package helpers |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "io/fs" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + "path/filepath" |
| 25 | + "strings" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + errFoundConflict = errors.New("found-conflict") |
| 30 | + errGoConflict = errors.New("go-conflict") |
| 31 | +) |
| 32 | + |
| 33 | +type ConflictSummary struct { |
| 34 | + Makefile bool // Makefile or makefile conflicted |
| 35 | + API bool // anything under api/ or apis/ conflicted |
| 36 | + AnyGo bool // any *.go file anywhere conflicted |
| 37 | +} |
| 38 | + |
| 39 | +func DetectConflicts() ConflictSummary { |
| 40 | + return ConflictSummary{ |
| 41 | + Makefile: hasConflict("Makefile", "makefile"), |
| 42 | + API: hasConflict("api", "apis"), |
| 43 | + AnyGo: hasGoConflicts(), // checks all *.go in repo (index fast path + FS scan) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// hasConflict: file/dir conflicts via index fast path + marker scan. |
| 48 | +func hasConflict(paths ...string) bool { |
| 49 | + if len(paths) == 0 { |
| 50 | + return false |
| 51 | + } |
| 52 | + // Fast path: any unmerged entry under these pathspecs? |
| 53 | + args := append([]string{"ls-files", "-u", "--"}, paths...) |
| 54 | + out, err := exec.Command("git", args...).Output() |
| 55 | + if err == nil && len(strings.TrimSpace(string(out))) > 0 { |
| 56 | + return true |
| 57 | + } |
| 58 | + |
| 59 | + // Fallback: scan for conflict markers. |
| 60 | + hasMarkers := func(p string) bool { |
| 61 | + // Best-effort, skip large likely-binaries. |
| 62 | + if fi, err := os.Stat(p); err == nil && fi.Size() > 1<<20 { |
| 63 | + return false |
| 64 | + } |
| 65 | + b, err := os.ReadFile(p) |
| 66 | + if err != nil { |
| 67 | + return false |
| 68 | + } |
| 69 | + s := string(b) |
| 70 | + return strings.Contains(s, "<<<<<<<") && |
| 71 | + strings.Contains(s, "=======") && |
| 72 | + strings.Contains(s, ">>>>>>>") |
| 73 | + } |
| 74 | + |
| 75 | + for _, root := range paths { |
| 76 | + info, err := os.Stat(root) |
| 77 | + if err != nil { |
| 78 | + continue |
| 79 | + } |
| 80 | + if !info.IsDir() { |
| 81 | + if hasMarkers(root) { |
| 82 | + return true |
| 83 | + } |
| 84 | + continue |
| 85 | + } |
| 86 | + |
| 87 | + werr := filepath.WalkDir(root, func(p string, d fs.DirEntry, walkErr error) error { |
| 88 | + if walkErr != nil || d.IsDir() { |
| 89 | + return nil |
| 90 | + } |
| 91 | + // Skip obvious noise dirs. |
| 92 | + if d.Name() == ".git" || strings.Contains(p, string(filepath.Separator)+".git"+string(filepath.Separator)) { |
| 93 | + return nil |
| 94 | + } |
| 95 | + if hasMarkers(p) { |
| 96 | + return errFoundConflict |
| 97 | + } |
| 98 | + return nil |
| 99 | + }) |
| 100 | + if errors.Is(werr, errFoundConflict) { |
| 101 | + return true |
| 102 | + } |
| 103 | + } |
| 104 | + return false |
| 105 | +} |
| 106 | + |
| 107 | +// hasGoConflicts: any *.go file conflicted (repo-wide). |
| 108 | +func hasGoConflicts(roots ...string) bool { |
| 109 | + // Fast path: any unmerged *.go anywhere? |
| 110 | + if out, err := exec.Command("git", "ls-files", "-u", "--", "*.go").Output(); err == nil { |
| 111 | + if len(strings.TrimSpace(string(out))) > 0 { |
| 112 | + return true |
| 113 | + } |
| 114 | + } |
| 115 | + // Fallback: filesystem scan (repo-wide or limited to roots if provided). |
| 116 | + if len(roots) == 0 { |
| 117 | + roots = []string{"."} |
| 118 | + } |
| 119 | + for _, root := range roots { |
| 120 | + werr := filepath.WalkDir(root, func(p string, d fs.DirEntry, walkErr error) error { |
| 121 | + if walkErr != nil || d.IsDir() || !strings.HasSuffix(p, ".go") { |
| 122 | + return nil |
| 123 | + } |
| 124 | + // Skip .git and large files. |
| 125 | + if strings.Contains(p, string(filepath.Separator)+".git"+string(filepath.Separator)) { |
| 126 | + return nil |
| 127 | + } |
| 128 | + if fi, err := os.Stat(p); err == nil && fi.Size() > 1<<20 { |
| 129 | + return nil |
| 130 | + } |
| 131 | + b, err := os.ReadFile(p) |
| 132 | + if err != nil { |
| 133 | + return nil |
| 134 | + } |
| 135 | + s := string(b) |
| 136 | + if strings.Contains(s, "<<<<<<<") && |
| 137 | + strings.Contains(s, "=======") && |
| 138 | + strings.Contains(s, ">>>>>>>") { |
| 139 | + return errGoConflict |
| 140 | + } |
| 141 | + return nil |
| 142 | + }) |
| 143 | + if errors.Is(werr, errGoConflict) { |
| 144 | + return true |
| 145 | + } |
| 146 | + } |
| 147 | + return false |
| 148 | +} |
| 149 | + |
| 150 | +// DecideMakeTargets applies simple policy over the summary. |
| 151 | +func DecideMakeTargets(cs ConflictSummary) []string { |
| 152 | + all := []string{"manifests", "generate", "fmt", "vet", "lint-fix"} |
| 153 | + if cs.Makefile { |
| 154 | + return nil |
| 155 | + } |
| 156 | + keep := make([]string, 0, len(all)) |
| 157 | + for _, t := range all { |
| 158 | + if cs.API && (t == "manifests" || t == "generate") { |
| 159 | + continue |
| 160 | + } |
| 161 | + if cs.AnyGo && (t == "fmt" || t == "vet" || t == "lint-fix") { |
| 162 | + continue |
| 163 | + } |
| 164 | + keep = append(keep, t) |
| 165 | + } |
| 166 | + return keep |
| 167 | +} |
0 commit comments