getRepositorySlugFromDirPreferringUpstream extracts the repository slug (owner/repo) for a git working directory, preferring the 'upstream' remote when available.
(dir string)
| 242 | // getRepositorySlugFromDirPreferringUpstream extracts the repository slug (owner/repo) |
| 243 | // for a git working directory, preferring the 'upstream' remote when available. |
| 244 | func getRepositorySlugFromDirPreferringUpstream(dir string) string { |
| 245 | gitArgs := func(args ...string) *exec.Cmd { |
| 246 | if dir != "" { |
| 247 | return exec.Command("git", append([]string{"-C", dir}, args...)...) |
| 248 | } |
| 249 | return exec.Command("git", args...) |
| 250 | } |
| 251 | |
| 252 | if output, err := gitArgs("config", "--get", "remote.upstream.url").Output(); err == nil { |
| 253 | upstreamURL := strings.TrimSpace(string(output)) |
| 254 | if upstreamURL != "" { |
| 255 | slug := parseGitHubRepoSlugFromURL(upstreamURL) |
| 256 | if slug != "" { |
| 257 | gitLog.Printf("Repository slug from upstream remote: %s", slug) |
| 258 | return slug |
| 259 | } |
| 260 | gitLog.Printf("Unable to parse repository slug from upstream remote URL %q; falling back", upstreamURL) |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | remoteURL, _, err := resolveRemoteURL(dir) |
| 265 | if err != nil { |
| 266 | if dir == "" { |
| 267 | gitLog.Printf("Failed to resolve remote URL: %v", err) |
| 268 | } else { |
| 269 | gitLog.Printf("Failed to resolve remote URL for path: %v", err) |
| 270 | } |
| 271 | return "" |
| 272 | } |
| 273 | |
| 274 | slug := parseGitHubRepoSlugFromURL(remoteURL) |
| 275 | if slug != "" { |
| 276 | if dir == "" { |
| 277 | gitLog.Printf("Repository slug: %s", slug) |
| 278 | } else { |
| 279 | gitLog.Printf("Repository slug for path: %s", slug) |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | return slug |
| 284 | } |
| 285 | |
| 286 | // getRepositorySlugFromRemoteForPath extracts the repository slug (owner/repo) from the git remote URL |
| 287 | // of the repository containing the specified file path. |
no test coverage detected