Skip to content

Commit

Permalink
Merge pull request #105 from github/config-backwards-compatibility
Browse files Browse the repository at this point in the history
git.Repository: invoke `git config` more backwards-compatibly
  • Loading branch information
mhagger authored Dec 12, 2022
2 parents d1e15af + 6238db1 commit 0b6d3a2
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions git/gitconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"os/exec"
"strconv"
"strings"
)
Expand Down Expand Up @@ -114,14 +115,18 @@ func configKeyMatchesPrefix(key, prefix string) (bool, string) {
}

func (repo *Repository) ConfigStringDefault(key string, defaultValue string) (string, error) {
// Note that `git config --get` didn't get `--default` until Git
// 2.18 (released 2018-06-21).
cmd := repo.GitCommand(
"config",
"--default", defaultValue,
key,
"config", "--get", key,
)

out, err := cmd.Output()
if err != nil {
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
// This indicates that the value was not found.
return defaultValue, nil
}
return defaultValue, fmt.Errorf("running 'git config': %w", err)
}

Expand All @@ -133,15 +138,18 @@ func (repo *Repository) ConfigStringDefault(key string, defaultValue string) (st
}

func (repo *Repository) ConfigBoolDefault(key string, defaultValue bool) (bool, error) {
// Note that `git config --get` didn't get `--type=bool` or
// `--default` until Git 2.18 (released 2018-06-21).
cmd := repo.GitCommand(
"config",
"--type", "bool",
"--default", strconv.FormatBool(defaultValue),
key,
"config", "--get", "--bool", key,
)

out, err := cmd.Output()
if err != nil {
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
// This indicates that the value was not found.
return defaultValue, nil
}
return defaultValue, fmt.Errorf("running 'git config': %w", err)
}

Expand All @@ -155,15 +163,18 @@ func (repo *Repository) ConfigBoolDefault(key string, defaultValue bool) (bool,
}

func (repo *Repository) ConfigIntDefault(key string, defaultValue int) (int, error) {
// Note that `git config --get` didn't get `--type=int` or
// `--default` until Git 2.18 (released 2018-06-21).
cmd := repo.GitCommand(
"config",
"--type", "int",
"--default", strconv.Itoa(defaultValue),
key,
"config", "--get", "--int", key,
)

out, err := cmd.Output()
if err != nil {
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
// This indicates that the value was not found.
return defaultValue, nil
}
return defaultValue, fmt.Errorf("running 'git config': %w", err)
}

Expand Down

0 comments on commit 0b6d3a2

Please sign in to comment.