scanForGitRepos walks root up to maxDepth levels, recording parent directories of any `.git` subdirectory it finds. maxDepth of 3 means root/a/b/c/.git is the deepest match. Does not recurse into .git itself or into discovered repos (once found, we stop descending into that tree).
(root string, maxDepth int)
| 390 | // root/a/b/c/.git is the deepest match. Does not recurse into .git itself |
| 391 | // or into discovered repos (once found, we stop descending into that tree). |
| 392 | func scanForGitRepos(root string, maxDepth int) ([]string, error) { |
| 393 | var found []string |
| 394 | rootDepth := strings.Count(strings.TrimRight(root, string(os.PathSeparator)), string(os.PathSeparator)) |
| 395 | |
| 396 | err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { |
| 397 | if err != nil { |
| 398 | // Permission denied etc — keep walking other branches. |
| 399 | return nil |
| 400 | } |
| 401 | if !d.IsDir() { |
| 402 | return nil |
| 403 | } |
| 404 | // Depth check. |
| 405 | depth := strings.Count(strings.TrimRight(path, string(os.PathSeparator)), string(os.PathSeparator)) - rootDepth |
| 406 | if depth > maxDepth { |
| 407 | return filepath.SkipDir |
| 408 | } |
| 409 | // Check if this dir contains .git (file or dir). |
| 410 | gitPath := filepath.Join(path, ".git") |
| 411 | if info, err := os.Stat(gitPath); err == nil && (info.IsDir() || info.Mode().IsRegular()) { |
| 412 | found = append(found, path) |
| 413 | return filepath.SkipDir |
| 414 | } |
| 415 | return nil |
| 416 | }) |
| 417 | if err != nil { |
| 418 | return nil, err |
| 419 | } |
| 420 | return found, nil |
| 421 | } |
| 422 | |
| 423 | // gitRemoteRE matches the url = <value> line under the origin section of a |
| 424 | // .git/config. The whole [remote "origin"] block lookup is done manually |
no outgoing calls