findRepoRoot uses `git rev-parse --show-toplevel` to find the repository root for the given path.
(gitBin string, p string)
| 328 | // findRepoRoot uses `git rev-parse --show-toplevel` to find the |
| 329 | // repository root for the given path. |
| 330 | func findRepoRoot(gitBin string, p string) (string, error) { |
| 331 | // If p is a file, start from its parent directory. |
| 332 | dir := p |
| 333 | if info, err := os.Stat(dir); err != nil || !info.IsDir() { |
| 334 | dir = filepath.Dir(dir) |
| 335 | } |
| 336 | cmd := exec.CommandContext(context.Background(), gitBin, "rev-parse", "--show-toplevel") |
| 337 | cmd.Dir = dir |
| 338 | out, err := cmd.Output() |
| 339 | if err != nil { |
| 340 | return "", xerrors.Errorf("no git repo found for %s", p) |
| 341 | } |
| 342 | root := filepath.FromSlash(strings.TrimSpace(string(out))) |
| 343 | // Resolve symlinks and short (8.3) names on Windows so the |
| 344 | // returned root matches paths produced by Go's filepath APIs. |
| 345 | if resolved, evalErr := filepath.EvalSymlinks(root); evalErr == nil { |
| 346 | root = resolved |
| 347 | } |
| 348 | return root, nil |
| 349 | } |
| 350 | |
| 351 | // getRepoChanges reads the current state of a git repository using |
| 352 | // the git CLI. It returns branch, remote origin, and a unified diff. |
no test coverage detected