aboutsummaryrefslogtreecommitdiffstats
path: root/repository
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-24 15:21:34 +0200
committerMichael Muré <batolettre@gmail.com>2018-09-24 15:21:34 +0200
commit666586c5b92b64a64aea22927cfb3754861623a1 (patch)
treefa2e850fbc8a0f694ced781cc1a599a6dce1e535 /repository
parenta122d533f6b3e46ab4c79bd39bbb764d59824983 (diff)
downloadgit-bug-666586c5b92b64a64aea22927cfb3754861623a1.tar.gz
repo: add functions to read/write git config
Diffstat (limited to 'repository')
-rw-r--r--repository/git.go41
-rw-r--r--repository/mock_repo.go20
-rw-r--r--repository/repo.go7
3 files changed, 62 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)
diff --git a/repository/mock_repo.go b/repository/mock_repo.go
index 2b911783..2389a8e0 100644
--- a/repository/mock_repo.go
+++ b/repository/mock_repo.go
@@ -3,6 +3,7 @@ package repository
import (
"crypto/sha1"
"fmt"
+ "strings"
"github.com/MichaelMure/git-bug/util/git"
"github.com/MichaelMure/git-bug/util/lamport"
@@ -10,6 +11,7 @@ import (
// mockRepoForTest defines an instance of Repo that can be used for testing.
type mockRepoForTest struct {
+ config map[string]string
blobs map[git.Hash][]byte
trees map[git.Hash]string
commits map[git.Hash]commit
@@ -25,6 +27,7 @@ type commit struct {
func NewMockRepoForTest() *mockRepoForTest {
return &mockRepoForTest{
+ config: make(map[string]string),
blobs: make(map[git.Hash][]byte),
trees: make(map[git.Hash]string),
commits: make(map[git.Hash]commit),
@@ -53,6 +56,23 @@ func (r *mockRepoForTest) GetCoreEditor() (string, error) {
return "vi", nil
}
+func (r *mockRepoForTest) StoreConfig(key string, value string) error {
+ r.config[key] = value
+ return nil
+}
+
+func (r *mockRepoForTest) ReadConfigs(keyPrefix string) (map[string]string, error) {
+ result := make(map[string]string)
+
+ for key, val := range r.config {
+ if strings.HasPrefix(key, keyPrefix) {
+ result[key] = val
+ }
+ }
+
+ return result, nil
+}
+
// PushRefs push git refs to a remote
func (r *mockRepoForTest) PushRefs(remote string, refSpec string) (string, error) {
return "", nil
diff --git a/repository/repo.go b/repository/repo.go
index 0cddf38a..c029a145 100644
--- a/repository/repo.go
+++ b/repository/repo.go
@@ -9,6 +9,7 @@ import (
"github.com/MichaelMure/git-bug/util/lamport"
)
+// RepoCommon represent the common function the we want all the repo to implement
type RepoCommon interface {
// GetPath returns the path to the repo.
GetPath() string
@@ -21,6 +22,12 @@ type RepoCommon interface {
// GetCoreEditor returns the name of the editor that the user has used to configure git.
GetCoreEditor() (string, error)
+
+ // StoreConfig store a single key/value pair in the config of the repo
+ StoreConfig(key string, value string) error
+
+ // ReadConfigs read all key/value pair matching the key prefix
+ ReadConfigs(keyPrefix string) (map[string]string, error)
}
// Repo represents a source code repository.