diff options
author | Michael Muré <batolettre@gmail.com> | 2018-09-24 15:21:34 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-09-24 15:21:34 +0200 |
commit | 666586c5b92b64a64aea22927cfb3754861623a1 (patch) | |
tree | fa2e850fbc8a0f694ced781cc1a599a6dce1e535 /repository/git.go | |
parent | a122d533f6b3e46ab4c79bd39bbb764d59824983 (diff) | |
download | git-bug-666586c5b92b64a64aea22927cfb3754861623a1.tar.gz |
repo: add functions to read/write git config
Diffstat (limited to 'repository/git.go')
-rw-r--r-- | repository/git.go | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/repository/git.go b/repository/git.go index db411792..51c4f46b 100644 --- a/repository/git.go +++ b/repository/git.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "os" "os/exec" "path" "strings" @@ -66,11 +65,6 @@ func (repo *GitRepo) runGitCommand(args ...string) (string, error) { return repo.runGitCommandWithStdin(nil, args...) } -// Run the given git command using the same stdin, stdout, and stderr as the review tool. -func (repo *GitRepo) runGitCommandInline(args ...string) error { - return repo.runGitCommandWithIO(os.Stdin, os.Stdout, os.Stderr, args...) -} - // NewGitRepo determines if the given working directory is inside of a git repository, // and returns the corresponding GitRepo instance if it is. func NewGitRepo(path string, witnesser func(repo *GitRepo) error) (*GitRepo, error) { @@ -165,6 +159,41 @@ func (repo *GitRepo) GetCoreEditor() (string, error) { return repo.runGitCommand("var", "GIT_EDITOR") } +// StoreConfig store a single key/value pair in the config of the repo +func (repo *GitRepo) StoreConfig(key string, value string) error { + _, err := repo.runGitCommand("config", "--replace-all", key, value) + + return err +} + +// ReadConfigs read all key/value pair matching the key prefix +func (repo *GitRepo) ReadConfigs(keyPrefix string) (map[string]string, error) { + stdout, err := repo.runGitCommand("config", "--get-regexp", keyPrefix) + + if err != nil { + return nil, err + } + + lines := strings.Split(stdout, "\n") + + result := make(map[string]string, len(lines)) + + for _, line := range lines { + if strings.TrimSpace(line) == "" { + continue + } + + parts := strings.Fields(line) + if len(parts) != 2 { + return nil, fmt.Errorf("bad git config: %s", line) + } + + result[parts[0]] = parts[1] + } + + return result, nil +} + // FetchRefs fetch git refs from a remote func (repo *GitRepo) FetchRefs(remote, refSpec string) (string, error) { stdout, err := repo.runGitCommand("fetch", remote, refSpec) |