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

Method ReadBranchConfig

git/client.go:445–465  ·  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

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

Callers 1

Calls 4

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

Tested by 1