MCPcopy Create free account
hub / github.com/github/gh-aw / findGitRootForPath

Function findGitRootForPath

pkg/cli/git.go:28–53  ·  view source on GitHub ↗

findGitRootForPath finds the root directory of the git repository containing the specified path

(path string)

Source from the content-addressed store, hash-verified

26
27// findGitRootForPath finds the root directory of the git repository containing the specified path
28func findGitRootForPath(path string) (string, error) {
29 gitLog.Printf("Finding git root for path: %s", path)
30
31 // Get absolute path first
32 absPath, err := filepath.Abs(path)
33 if err != nil {
34 return "", fmt.Errorf("failed to get absolute path: %w", err)
35 }
36
37 // Validate the absolute path
38 absPath, err = fileutil.ValidateAbsolutePath(absPath)
39 if err != nil {
40 return "", fmt.Errorf("invalid path: %w", err)
41 }
42
43 // Use the directory containing the file
44 dir := filepath.Dir(absPath)
45
46 // Find git root using filesystem traversal from the file's directory
47 gitRoot, err := gitutil.FindGitRootFrom(dir)
48 if err != nil {
49 return "", fmt.Errorf("failed to get repository root for path %s: %w", path, err)
50 }
51 gitLog.Printf("Found git root for path: %s", gitRoot)
52 return gitRoot, nil
53}
54
55// parseGitHubRepoSlugFromURL extracts owner/repo from a GitHub URL
56// Supports HTTPS (https://github.com/owner/repo), SCP-style SSH (git@github.com:owner/repo),

Callers 3

resolveImportPathMethod · 0.85
TestFindGitRootForPathFunction · 0.85

Calls 4

ValidateAbsolutePathFunction · 0.92
FindGitRootFromFunction · 0.92
PrintfMethod · 0.45
ErrorfMethod · 0.45

Tested by 1

TestFindGitRootForPathFunction · 0.68