MCPcopy Index your code
hub / github.com/coder/coder / findRepoRoot

Function findRepoRoot

agent/agentgit/agentgit.go:330–349  ·  view source on GitHub ↗

findRepoRoot uses `git rev-parse --show-toplevel` to find the repository root for the given path.

(gitBin string, p string)

Source from the content-addressed store, hash-verified

328// findRepoRoot uses `git rev-parse --show-toplevel` to find the
329// repository root for the given path.
330func 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.

Callers 1

SubscribeMethod · 0.85

Calls 3

CommandContextMethod · 0.65
OutputMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected