MCPcopy Create free account
hub / github.com/cli/cli / ReadBranchConfig

Method ReadBranchConfig

git/client.go:446–466  ·  view source on GitHub ↗

ReadBranchConfig parses the `branch.BRANCH.(remote|merge|pushremote|gh-merge-base)` part of git config. If no branch config is found or there is an error in the command, it returns an empty BranchConfig. Downstream consumers of ReadBranchConfig should consider the behavior they desire if this errors

(ctx context.Context, branch string)

Source from the content-addressed store, hash-verified

444// Downstream consumers of ReadBranchConfig should consider the behavior they desire if this errors,
445// as an empty config is not necessarily breaking.
446func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (BranchConfig, error) {
447 prefix := regexp.QuoteMeta(fmt.Sprintf("branch.%s.", branch))
448 args := []string{"config", "--get-regexp", fmt.Sprintf("^%s(remote|merge|pushremote|%s)$", prefix, MergeBaseConfig)}
449 cmd, err := c.Command(ctx, args...)
450 if err != nil {
451 return BranchConfig{}, err
452 }
453
454 branchCfgOut, err := cmd.Output()
455 if err != nil {
456 // This is the error we expect if the git command does not run successfully.
457 // If the ExitCode is 1, then we just didn't find any config for the branch.
458 var gitError *GitError
459 if ok := errors.As(err, &gitError); ok && gitError.ExitCode != 1 {
460 return BranchConfig{}, err
461 }
462 return BranchConfig{}, nil
463 }
464
465 return parseBranchConfig(outputLines(branchCfgOut)), nil
466}
467
468func parseBranchConfig(branchConfigLines []string) BranchConfig {
469 var cfg BranchConfig

Callers 1

Calls 4

CommandMethod · 0.95
parseBranchConfigFunction · 0.85
outputLinesFunction · 0.85
OutputMethod · 0.65

Tested by 1