detectGitRemote reads /.git/config (if present) and extracts the origin remote URL. Returns "" on any error or if the section isn't present. Handles `.git` being either a directory (normal repo) or a file (git worktree — we follow the gitdir pointer).
(path string)
| 430 | // present. Handles `.git` being either a directory (normal repo) or a |
| 431 | // file (git worktree — we follow the gitdir pointer). |
| 432 | func detectGitRemote(path string) string { |
| 433 | configPath := resolveGitConfigPath(path) |
| 434 | if configPath == "" { |
| 435 | return "" |
| 436 | } |
| 437 | f, err := os.Open(configPath) |
| 438 | if err != nil { |
| 439 | return "" |
| 440 | } |
| 441 | defer f.Close() |
| 442 | |
| 443 | scanner := bufio.NewScanner(f) |
| 444 | inOrigin := false |
| 445 | for scanner.Scan() { |
| 446 | line := scanner.Text() |
| 447 | trimmed := strings.TrimSpace(line) |
| 448 | if strings.HasPrefix(trimmed, "[") && strings.HasSuffix(trimmed, "]") { |
| 449 | // Section header. |
| 450 | inOrigin = (trimmed == `[remote "origin"]`) |
| 451 | continue |
| 452 | } |
| 453 | if !inOrigin { |
| 454 | continue |
| 455 | } |
| 456 | if m := gitRemoteURLRE.FindStringSubmatch(line); m != nil { |
| 457 | return m[1] |
| 458 | } |
| 459 | } |
| 460 | return "" |
| 461 | } |
| 462 | |
| 463 | // resolveGitConfigPath returns the absolute path to the git config for a |
| 464 | // repo rooted at path, or "" if none exists. Handles both the common case |