findRepoRoot returns the first ancestor of dir containing a .git entry, or ("", false) when dir is not inside a git repository.
(dir string)
| 25 | // findRepoRoot returns the first ancestor of dir containing a .git entry, or |
| 26 | // ("", false) when dir is not inside a git repository. |
| 27 | func findRepoRoot(dir string) (string, bool) { |
| 28 | if v, ok := repoRootCache.Load(dir); ok { |
| 29 | root := v.(string) |
| 30 | return root, root != "" |
| 31 | } |
| 32 | |
| 33 | current := dir |
| 34 | for { |
| 35 | if _, err := os.Stat(filepath.Join(current, ".git")); err == nil { |
| 36 | repoRootCache.Store(dir, current) |
| 37 | return current, true |
| 38 | } |
| 39 | parent := filepath.Dir(current) |
| 40 | if parent == current { |
| 41 | break |
| 42 | } |
| 43 | current = parent |
| 44 | } |
| 45 | |
| 46 | repoRootCache.Store(dir, "") |
| 47 | return "", false |
| 48 | } |
| 49 | |
| 50 | // filterGitignored marks entries matching gitignore rules as excluded (false). |
| 51 | // All .gitignore files from the repo root down to each candidate file's |
no test coverage detected
searching dependent graphs…