GetConfig returns the entries from gitconfig. If `prefix` is provided, then only include entries in that section, which must match the at a component boundary (as defined by `configKeyMatchesPrefix()`), and strip off the prefix in the keys that are returned.
(prefix string)
| 36 | // `configKeyMatchesPrefix()`), and strip off the prefix in the keys |
| 37 | // that are returned. |
| 38 | func (repo *Repository) GetConfig(prefix string) (*Config, error) { |
| 39 | cmd := repo.GitCommand("config", "--list", "-z") |
| 40 | |
| 41 | out, err := cmd.Output() |
| 42 | if err != nil { |
| 43 | return nil, fmt.Errorf("reading git configuration: %w", err) |
| 44 | } |
| 45 | |
| 46 | config := Config{ |
| 47 | Prefix: prefix, |
| 48 | } |
| 49 | |
| 50 | for len(out) > 0 { |
| 51 | keyEnd := bytes.IndexByte(out, '\n') |
| 52 | if keyEnd == -1 { |
| 53 | return nil, errors.New("invalid output from 'git config'") |
| 54 | } |
| 55 | key := string(out[:keyEnd]) |
| 56 | out = out[keyEnd+1:] |
| 57 | valueEnd := bytes.IndexByte(out, 0) |
| 58 | if valueEnd == -1 { |
| 59 | return nil, errors.New("invalid output from 'git config'") |
| 60 | } |
| 61 | value := string(out[:valueEnd]) |
| 62 | out = out[valueEnd+1:] |
| 63 | |
| 64 | ok, rest := configKeyMatchesPrefix(key, prefix) |
| 65 | if !ok { |
| 66 | continue |
| 67 | } |
| 68 | |
| 69 | entry := ConfigEntry{ |
| 70 | Key: rest, |
| 71 | Value: value, |
| 72 | } |
| 73 | config.Entries = append(config.Entries, entry) |
| 74 | } |
| 75 | |
| 76 | return &config, nil |
| 77 | } |
| 78 | |
| 79 | // FullKey returns the full gitconfig key name for the relative key |
| 80 | // name `key`. |
nothing calls this directly
no test coverage detected