MCPcopy Create free account
hub / github.com/bufbuild/buf / GetRefsForGitCommitAndRemote

Function GetRefsForGitCommitAndRemote

private/pkg/git/git.go:318–356  ·  view source on GitHub ↗

GetRefsForGitCommitAndRemote returns all refs pointing to a given commit based on the given remote for the given directory. Querying the remote for refs information requires passing the environment for permissions.

(
	ctx context.Context,
	envContainer app.EnvContainer,
	dir string,
	remote string,
	gitCommitSha string,
)

Source from the content-addressed store, hash-verified

316// given remote for the given directory. Querying the remote for refs information requires
317// passing the environment for permissions.
318func GetRefsForGitCommitAndRemote(
319 ctx context.Context,
320 envContainer app.EnvContainer,
321 dir string,
322 remote string,
323 gitCommitSha string,
324) ([]string, error) {
325 stdout := bytes.NewBuffer(nil)
326 stderr := bytes.NewBuffer(nil)
327 if err := xexec.Run(
328 ctx,
329 gitCommand,
330 xexec.WithArgs("ls-remote", "--heads", "--tags", remote),
331 xexec.WithStdout(stdout),
332 xexec.WithStderr(stderr),
333 xexec.WithDir(dir),
334 xexec.WithEnv(app.Environ(envContainer)),
335 ); err != nil {
336 return nil, fmt.Errorf("failed to get refs for remote %s: %w: %s", remote, err, stderr.String())
337 }
338 scanner := bufio.NewScanner(stdout)
339 var refs []string
340 for scanner.Scan() {
341 line := strings.TrimSpace(scanner.Text())
342 if ref, found := strings.CutPrefix(line, gitCommitSha); found {
343 ref = strings.TrimSpace(ref)
344 if tag, isTag := strings.CutPrefix(ref, tagsPrefix); isTag {
345 // Remove the ^{} suffix for pseudo-ref tags
346 tag, _ = strings.CutSuffix(tag, pseudoRefSuffix)
347 refs = append(refs, tag)
348 continue
349 }
350 if branch, isBranchHead := strings.CutPrefix(ref, headsPrefix); isBranchHead {
351 refs = append(refs, branch)
352 }
353 }
354 }
355 return refs, nil
356}
357
358// IsValidRef returns whether or not ref is a valid git ref for the git
359// repository that contains dir. Returns nil if the ref is valid.

Callers 1

Calls 2

StringMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…