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

Function getRepositorySlugFromDirPreferringUpstream

pkg/cli/git.go:244–284  ·  view source on GitHub ↗

getRepositorySlugFromDirPreferringUpstream extracts the repository slug (owner/repo) for a git working directory, preferring the 'upstream' remote when available.

(dir string)

Source from the content-addressed store, hash-verified

242// getRepositorySlugFromDirPreferringUpstream extracts the repository slug (owner/repo)
243// for a git working directory, preferring the 'upstream' remote when available.
244func 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.

Calls 3

resolveRemoteURLFunction · 0.85
PrintfMethod · 0.45

Tested by

no test coverage detected