From 666586c5b92b64a64aea22927cfb3754861623a1 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Mon, 24 Sep 2018 15:21:34 +0200 Subject: repo: add functions to read/write git config --- repository/git.go | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) (limited to 'repository/git.go') diff --git a/repository/git.go b/repository/git.go index db411792e..51c4f46bd 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) -- cgit v1.2.3