cmdWorkdirScan walks a root (or ~/code and ~/work by default) up to 3 levels deep, finds .git directories, and either prints candidates or upserts them with --add.
(args []string)
| 277 | // levels deep, finds .git directories, and either prints candidates or |
| 278 | // upserts them with --add. |
| 279 | func cmdWorkdirScan(args []string) int { |
| 280 | // --add may appear anywhere; extract it manually. |
| 281 | add, rest := extractWorkdirBoolFlag(args, "--add") |
| 282 | fs := flagSet("workdir scan") |
| 283 | fs.Bool("add", false, "register discovered candidates") // for -h only |
| 284 | if err := fs.Parse(rest); err != nil { |
| 285 | return 2 |
| 286 | } |
| 287 | |
| 288 | var roots []string |
| 289 | if fs.NArg() == 1 { |
| 290 | abs, err := filepath.Abs(fs.Arg(0)) |
| 291 | if err != nil { |
| 292 | fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 293 | return 1 |
| 294 | } |
| 295 | roots = []string{abs} |
| 296 | } else if fs.NArg() > 1 { |
| 297 | fmt.Fprintln(os.Stderr, "error: workdir scan takes at most one root") |
| 298 | return 2 |
| 299 | } else { |
| 300 | home, err := os.UserHomeDir() |
| 301 | if err != nil { |
| 302 | fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 303 | return 1 |
| 304 | } |
| 305 | for _, d := range []string{"code", "work"} { |
| 306 | p := filepath.Join(home, d) |
| 307 | if info, err := os.Stat(p); err == nil && info.IsDir() { |
| 308 | roots = append(roots, p) |
| 309 | } |
| 310 | } |
| 311 | if len(roots) == 0 { |
| 312 | fmt.Println("(no default scan roots exist: tried ~/code, ~/work)") |
| 313 | return 0 |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | db, rc := openFlowDBForWorkdir() |
| 318 | if rc != 0 { |
| 319 | return rc |
| 320 | } |
| 321 | defer db.Close() |
| 322 | |
| 323 | var candidates []string |
| 324 | seen := map[string]bool{} |
| 325 | for _, root := range roots { |
| 326 | found, err := scanForGitRepos(root, 3) |
| 327 | if err != nil { |
| 328 | fmt.Fprintf(os.Stderr, "warning: scan %s: %v\n", root, err) |
| 329 | continue |
| 330 | } |
| 331 | for _, p := range found { |
| 332 | if !seen[p] { |
| 333 | seen[p] = true |
| 334 | candidates = append(candidates, p) |
| 335 | } |
| 336 | } |
no test coverage detected