Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "bufio" |
Patrick Georgi | 62a2738 | 2018-11-12 18:48:40 +0100 | [diff] [blame] | 7 | "flag" |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 8 | "fmt" |
| 9 | "log" |
| 10 | "os" |
| 11 | "os/exec" |
Patrick Georgi | 0cadafa | 2018-11-12 17:31:48 +0100 | [diff] [blame] | 12 | "regexp" |
Patrick Georgi | 89bd489 | 2018-11-12 17:52:24 +0100 | [diff] [blame] | 13 | "strings" |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | type subsystem struct { |
| 17 | name string |
| 18 | maintainer []string |
Patrick Georgi | 0cadafa | 2018-11-12 17:31:48 +0100 | [diff] [blame] | 19 | paths []string |
| 20 | globs []*regexp.Regexp |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | var subsystems []subsystem |
| 24 | |
| 25 | func get_git_files() ([]string, error) { |
| 26 | var files []string |
| 27 | |
| 28 | /* Read in list of all files in the git repository */ |
| 29 | cmd := exec.Command("git", "ls-files") |
| 30 | out, err := cmd.StdoutPipe() |
| 31 | if err != nil { |
| 32 | log.Fatalf("git ls-files failed: %v", err) |
| 33 | return files, err |
| 34 | } |
| 35 | if err := cmd.Start(); err != nil { |
| 36 | log.Fatalf("Could not start %v: %v", cmd, err) |
| 37 | return files, err |
| 38 | } |
| 39 | |
| 40 | r := bufio.NewScanner(out) |
| 41 | for r.Scan() { |
| 42 | /* Cut out leading tab */ |
| 43 | files = append(files, r.Text()) |
| 44 | } |
| 45 | |
| 46 | cmd.Wait() |
| 47 | |
| 48 | return files, nil |
| 49 | } |
| 50 | |
| 51 | func get_maintainers() ([]string, error) { |
| 52 | var maintainers []string |
| 53 | |
| 54 | /* Read in all maintainers */ |
| 55 | file, err := os.Open("MAINTAINERS") |
| 56 | if err != nil { |
| 57 | log.Fatalf("Can't open MAINTAINERS file: %v", err) |
| 58 | log.Fatalf("Are you running from the top-level directory?") |
| 59 | return maintainers, err |
| 60 | } |
| 61 | defer file.Close() |
| 62 | |
| 63 | keep := false |
| 64 | s := bufio.NewScanner(file) |
| 65 | for s.Scan() { |
| 66 | /* Are we in the "data" section and have a non-empty line? */ |
| 67 | if keep && s.Text() != "" { |
| 68 | maintainers = append(maintainers, s.Text()) |
| 69 | } |
| 70 | /* Skip everything before the delimiter */ |
| 71 | if s.Text() == "\t\t-----------------------------------" { |
| 72 | keep = true |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return maintainers, nil |
| 77 | } |
| 78 | |
Patrick Georgi | 0cadafa | 2018-11-12 17:31:48 +0100 | [diff] [blame] | 79 | func path_to_regexstr(path string) string { |
Michael Niewöhner | a0c7f34 | 2021-04-13 00:44:21 +0200 | [diff] [blame] | 80 | /* Add missing trailing slash if path is a directory */ |
| 81 | if path[len(path)-1] != '/' { |
| 82 | fileInfo, err := os.Stat(path) |
| 83 | if err == nil && fileInfo.IsDir() { |
| 84 | path += "/" |
| 85 | } |
| 86 | } |
| 87 | |
Michael Niewöhner | 158fed9 | 2021-04-13 00:17:39 +0200 | [diff] [blame] | 88 | regexstr := glob_to_regex(path) |
| 89 | |
| 90 | /* Handle path with trailing '/' as prefix */ |
| 91 | if regexstr[len(regexstr)-2:] == "/$" { |
| 92 | regexstr = regexstr[:len(regexstr)-1] + ".*$" |
Patrick Georgi | 0cadafa | 2018-11-12 17:31:48 +0100 | [diff] [blame] | 93 | } |
Michael Niewöhner | 158fed9 | 2021-04-13 00:17:39 +0200 | [diff] [blame] | 94 | |
| 95 | return regexstr; |
Patrick Georgi | 0cadafa | 2018-11-12 17:31:48 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | func path_to_regex(path string) *regexp.Regexp { |
| 99 | regexstr := path_to_regexstr(path) |
| 100 | return regexp.MustCompile(regexstr) |
| 101 | } |
| 102 | |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 103 | func build_maintainers(maintainers []string) { |
| 104 | var current *subsystem |
| 105 | for _, line := range maintainers { |
| 106 | if line[1] != ':' { |
| 107 | /* Create new subsystem entry */ |
| 108 | var tmp subsystem |
| 109 | subsystems = append(subsystems, tmp) |
| 110 | current = &subsystems[len(subsystems)-1] |
| 111 | current.name = line |
| 112 | } else { |
| 113 | switch line[0] { |
Patrick Georgi | 9233263 | 2018-11-12 17:09:40 +0100 | [diff] [blame] | 114 | case 'R', 'M': |
| 115 | /* Add subsystem maintainer */ |
| 116 | current.maintainer = append(current.maintainer, line[3:len(line)]) |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 117 | case 'F': |
Patrick Georgi | 9233263 | 2018-11-12 17:09:40 +0100 | [diff] [blame] | 118 | // add files |
Patrick Georgi | 0cadafa | 2018-11-12 17:31:48 +0100 | [diff] [blame] | 119 | current.paths = append(current.paths, line[3:len(line)]) |
| 120 | current.globs = append(current.globs, path_to_regex(line[3:len(line)])) |
| 121 | break |
Patrick Georgi | 9233263 | 2018-11-12 17:09:40 +0100 | [diff] [blame] | 122 | case 'L', 'S', 'T', 'W': // ignore |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 123 | default: |
Patrick Georgi | 9233263 | 2018-11-12 17:09:40 +0100 | [diff] [blame] | 124 | fmt.Println("No such specifier: ", line) |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func print_maintainers() { |
| 131 | for _, subsystem := range subsystems { |
| 132 | fmt.Println(subsystem.name) |
| 133 | fmt.Println(" ", subsystem.maintainer) |
Patrick Georgi | 0cadafa | 2018-11-12 17:31:48 +0100 | [diff] [blame] | 134 | fmt.Println(" ", subsystem.paths) |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Patrick Georgi | 0cadafa | 2018-11-12 17:31:48 +0100 | [diff] [blame] | 138 | func match_file(fname string, component subsystem) bool { |
| 139 | for _, glob := range component.globs { |
| 140 | if glob.Match([]byte(fname)) { |
| 141 | return true |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
Patrick Georgi | 0cadafa | 2018-11-12 17:31:48 +0100 | [diff] [blame] | 144 | return false |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | func find_maintainer(fname string) { |
Patrick Georgi | 2e5d6a8 | 2018-11-12 17:29:49 +0100 | [diff] [blame] | 148 | var success bool |
| 149 | |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 150 | for _, subsystem := range subsystems { |
Patrick Georgi | 0cadafa | 2018-11-12 17:31:48 +0100 | [diff] [blame] | 151 | matched := match_file(fname, subsystem) |
Patrick Georgi | 9651058 | 2018-11-16 12:41:19 +0100 | [diff] [blame] | 152 | if matched { |
Patrick Georgi | 2e5d6a8 | 2018-11-12 17:29:49 +0100 | [diff] [blame] | 153 | success = true |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 154 | fmt.Println(fname, "is in subsystem", |
| 155 | subsystem.name) |
Patrick Georgi | 89bd489 | 2018-11-12 17:52:24 +0100 | [diff] [blame] | 156 | fmt.Println("Maintainers: ", strings.Join(subsystem.maintainer, ", ")) |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
Patrick Georgi | 2e5d6a8 | 2018-11-12 17:29:49 +0100 | [diff] [blame] | 159 | if !success { |
| 160 | fmt.Println(fname, "has no subsystem defined in MAINTAINERS") |
| 161 | } |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | func find_unmaintained(fname string) { |
Patrick Georgi | 2e5d6a8 | 2018-11-12 17:29:49 +0100 | [diff] [blame] | 165 | var success bool |
| 166 | |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 167 | for _, subsystem := range subsystems { |
Patrick Georgi | 0cadafa | 2018-11-12 17:31:48 +0100 | [diff] [blame] | 168 | matched := match_file(fname, subsystem) |
Patrick Georgi | 9651058 | 2018-11-16 12:41:19 +0100 | [diff] [blame] | 169 | if matched { |
Patrick Georgi | 2e5d6a8 | 2018-11-12 17:29:49 +0100 | [diff] [blame] | 170 | success = true |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 171 | fmt.Println(fname, "is in subsystem", |
| 172 | subsystem.name) |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
Patrick Georgi | 2e5d6a8 | 2018-11-12 17:29:49 +0100 | [diff] [blame] | 175 | if !success { |
| 176 | fmt.Println(fname, "has no subsystem defined in MAINTAINERS") |
| 177 | } |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Patrick Georgi | 0cadafa | 2018-11-12 17:31:48 +0100 | [diff] [blame] | 180 | // taken from https://github.com/zyedidia/glob/blob/master/glob.go which is |
| 181 | // Copyright (c) 2016: Zachary Yedidia. |
| 182 | // and was published under the MIT "Expat" license. |
| 183 | // |
| 184 | // only change: return the string, instead of a compiled golang regex |
| 185 | func glob_to_regex(glob string) string { |
| 186 | regex := "" |
| 187 | inGroup := 0 |
| 188 | inClass := 0 |
| 189 | firstIndexInClass := -1 |
| 190 | arr := []byte(glob) |
| 191 | |
| 192 | for i := 0; i < len(arr); i++ { |
| 193 | ch := arr[i] |
| 194 | |
| 195 | switch ch { |
| 196 | case '\\': |
| 197 | i++ |
| 198 | if i >= len(arr) { |
| 199 | regex += "\\" |
| 200 | } else { |
| 201 | next := arr[i] |
| 202 | switch next { |
| 203 | case ',': |
| 204 | // Nothing |
| 205 | case 'Q', 'E': |
| 206 | regex += "\\\\" |
| 207 | default: |
| 208 | regex += "\\" |
| 209 | } |
| 210 | regex += string(next) |
| 211 | } |
| 212 | case '*': |
| 213 | if inClass == 0 { |
Michael Niewöhner | 158fed9 | 2021-04-13 00:17:39 +0200 | [diff] [blame] | 214 | regex += "[^/]*" |
Patrick Georgi | 0cadafa | 2018-11-12 17:31:48 +0100 | [diff] [blame] | 215 | } else { |
| 216 | regex += "*" |
| 217 | } |
| 218 | case '?': |
| 219 | if inClass == 0 { |
| 220 | regex += "." |
| 221 | } else { |
| 222 | regex += "?" |
| 223 | } |
| 224 | case '[': |
| 225 | inClass++ |
| 226 | firstIndexInClass = i + 1 |
| 227 | regex += "[" |
| 228 | case ']': |
| 229 | inClass-- |
| 230 | regex += "]" |
| 231 | case '.', '(', ')', '+', '|', '^', '$', '@', '%': |
| 232 | if inClass == 0 || (firstIndexInClass == i && ch == '^') { |
| 233 | regex += "\\" |
| 234 | } |
| 235 | regex += string(ch) |
| 236 | case '!': |
| 237 | if firstIndexInClass == i { |
| 238 | regex += "^" |
| 239 | } else { |
| 240 | regex += "!" |
| 241 | } |
| 242 | case '{': |
| 243 | inGroup++ |
| 244 | regex += "(" |
| 245 | case '}': |
| 246 | inGroup-- |
| 247 | regex += ")" |
| 248 | case ',': |
| 249 | if inGroup > 0 { |
| 250 | regex += "|" |
| 251 | } else { |
| 252 | regex += "," |
| 253 | } |
| 254 | default: |
| 255 | regex += string(ch) |
| 256 | } |
| 257 | } |
| 258 | return "^" + regex + "$" |
| 259 | } |
| 260 | |
Patrick Georgi | e874df9 | 2018-11-12 18:49:09 +0100 | [diff] [blame] | 261 | var is_email *regexp.Regexp |
| 262 | |
| 263 | func extract_maintainer(maintainer string) string { |
| 264 | if is_email == nil { |
| 265 | is_email = regexp.MustCompile("<[^>]*>") |
| 266 | } |
| 267 | |
| 268 | if match := is_email.FindStringSubmatch(maintainer); match != nil { |
| 269 | return match[0][1 : len(match[0])-1] |
| 270 | } |
| 271 | return maintainer |
| 272 | } |
| 273 | |
| 274 | func do_print_gerrit_rules() { |
| 275 | for _, subsystem := range subsystems { |
| 276 | if len(subsystem.paths) == 0 || len(subsystem.maintainer) == 0 { |
| 277 | continue |
| 278 | } |
| 279 | fmt.Println("#", subsystem.name) |
| 280 | for _, path := range subsystem.paths { |
Patrick Georgi | 571477b | 2018-11-21 22:07:38 +0100 | [diff] [blame] | 281 | fmt.Println("[filter \"file:\\\"" + path_to_regexstr(path) + "\\\"\"]") |
Patrick Georgi | e874df9 | 2018-11-12 18:49:09 +0100 | [diff] [blame] | 282 | for _, maint := range subsystem.maintainer { |
| 283 | fmt.Println(" reviewer =", extract_maintainer(maint)) |
| 284 | } |
| 285 | } |
| 286 | fmt.Println() |
| 287 | } |
| 288 | } |
| 289 | |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 290 | func main() { |
Patrick Georgi | 62a2738 | 2018-11-12 18:48:40 +0100 | [diff] [blame] | 291 | var ( |
Patrick Georgi | e874df9 | 2018-11-12 18:49:09 +0100 | [diff] [blame] | 292 | files []string |
| 293 | err error |
| 294 | print_gerrit_rules = flag.Bool("print-gerrit-rules", false, "emit the MAINTAINERS rules in a format suitable for Gerrit's reviewers plugin") |
| 295 | debug = flag.Bool("debug", false, "emit additional debug output") |
Patrick Georgi | 62a2738 | 2018-11-12 18:48:40 +0100 | [diff] [blame] | 296 | ) |
| 297 | flag.Parse() |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 298 | |
Patrick Georgi | 62a2738 | 2018-11-12 18:48:40 +0100 | [diff] [blame] | 299 | /* get and build subsystem database */ |
| 300 | maintainers, err := get_maintainers() |
| 301 | if err != nil { |
| 302 | log.Fatalf("Oops.") |
| 303 | return |
| 304 | } |
| 305 | build_maintainers(maintainers) |
| 306 | |
| 307 | if *debug { |
| 308 | print_maintainers() |
| 309 | } |
| 310 | |
Patrick Georgi | e874df9 | 2018-11-12 18:49:09 +0100 | [diff] [blame] | 311 | if *print_gerrit_rules { |
| 312 | do_print_gerrit_rules() |
| 313 | return |
| 314 | } |
| 315 | |
Patrick Georgi | 62a2738 | 2018-11-12 18:48:40 +0100 | [diff] [blame] | 316 | args := flag.Args() |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 317 | if len(args) == 0 { |
| 318 | /* get the filenames */ |
| 319 | files, err = get_git_files() |
| 320 | if err != nil { |
| 321 | log.Fatalf("Oops.") |
| 322 | return |
| 323 | } |
Patrick Georgi | 62a2738 | 2018-11-12 18:48:40 +0100 | [diff] [blame] | 324 | for _, file := range files { |
| 325 | find_unmaintained(file) |
| 326 | } |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 327 | } else { |
| 328 | files = args |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 329 | |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 330 | /* Find maintainers for each file */ |
| 331 | for _, file := range files { |
| 332 | find_maintainer(file) |
| 333 | } |
Stefan Reinauer | 1ecf252 | 2015-10-21 13:12:51 -0700 | [diff] [blame] | 334 | } |
| 335 | } |