aboutsummaryrefslogtreecommitdiffstats
path: root/repository
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-07-09 08:47:46 +0200
committerGitHub <noreply@github.com>2019-07-09 08:47:46 +0200
commita5c42b7c11fc082d47027ba5e0dbdeddcc14e62e (patch)
treea279d04878f3f1b1e28089779d011c0cb4b7545d /repository
parenteef7333243252ae81cd43921beb8e0749a170585 (diff)
parentfb50d470483b91d98e27ca6f9f605f0c51af6a71 (diff)
downloadgit-bug-a5c42b7c11fc082d47027ba5e0dbdeddcc14e62e.tar.gz
Merge pull request #177 from A-Hilaly/git-version
Check git version in repo RmConfigs
Diffstat (limited to 'repository')
-rw-r--r--repository/git.go91
-rw-r--r--repository/git_test.go2
2 files changed, 84 insertions, 9 deletions
diff --git a/repository/git.go b/repository/git.go
index c8c45d6a..9ec7905a 100644
--- a/repository/git.go
+++ b/repository/git.go
@@ -3,7 +3,6 @@ package repository
import (
"bytes"
- "errors"
"fmt"
"io"
"os/exec"
@@ -11,6 +10,9 @@ import (
"strconv"
"strings"
+ "github.com/blang/semver"
+ "github.com/pkg/errors"
+
"github.com/MichaelMure/git-bug/util/git"
"github.com/MichaelMure/git-bug/util/lamport"
)
@@ -259,16 +261,89 @@ func (repo *GitRepo) ReadConfigString(key string) (string, error) {
return lines[0], nil
}
+func (repo *GitRepo) rmSection(keyPrefix string) error {
+ _, err := repo.runGitCommand("config", "--remove-section", keyPrefix)
+ return err
+}
+
+func (repo *GitRepo) unsetAll(keyPrefix string) error {
+ _, err := repo.runGitCommand("config", "--unset-all", keyPrefix)
+ return err
+}
+
+// return keyPrefix section
+// example: sectionFromKey(a.b.c.d) return a.b.c
+func sectionFromKey(keyPrefix string) string {
+ s := strings.Split(keyPrefix, ".")
+ if len(s) == 1 {
+ return keyPrefix
+ }
+
+ return strings.Join(s[:len(s)-1], ".")
+}
+
+// rmConfigs with git version lesser than 2.18
+func (repo *GitRepo) rmConfigsGitVersionLT218(keyPrefix string) error {
+ // try to remove key/value pair by key
+ err := repo.unsetAll(keyPrefix)
+ if err != nil {
+ return repo.rmSection(keyPrefix)
+ }
+
+ m, err := repo.ReadConfigs(sectionFromKey(keyPrefix))
+ if err != nil {
+ return err
+ }
+
+ // if section doesn't have any left key/value remove the section
+ if len(m) == 0 {
+ return repo.rmSection(sectionFromKey(keyPrefix))
+ }
+
+ return nil
+}
+
// RmConfigs remove all key/value pair matching the key prefix
func (repo *GitRepo) RmConfigs(keyPrefix string) error {
- // try to remove key/value pair by key
- _, err := repo.runGitCommand("config", "--unset-all", keyPrefix)
+ // starting from git 2.18.0 sections are automatically deleted when the last existing
+ // key/value is removed. Before 2.18.0 we should remove the section
+ // see https://github.com/git/git/blob/master/Documentation/RelNotes/2.18.0.txt#L379
+ lt218, err := repo.gitVersionLT218()
if err != nil {
- // try to remove section
- _, err = repo.runGitCommand("config", "--remove-section", keyPrefix)
+ return errors.Wrap(err, "getting git version")
}
- return err
+ if lt218 {
+ return repo.rmConfigsGitVersionLT218(keyPrefix)
+ }
+
+ err = repo.unsetAll(keyPrefix)
+ if err != nil {
+ return repo.rmSection(keyPrefix)
+ }
+
+ return nil
+}
+
+func (repo *GitRepo) gitVersionLT218() (bool, error) {
+ versionOut, err := repo.runGitCommand("version")
+ if err != nil {
+ return false, err
+ }
+
+ versionString := strings.Fields(versionOut)[2]
+ version, err := semver.Make(versionString)
+ if err != nil {
+ return false, err
+ }
+
+ version218string := "2.18.0"
+ gitVersion218, err := semver.Make(version218string)
+ if err != nil {
+ return false, err
+ }
+
+ return version.LT(gitVersion218), nil
}
// FetchRefs fetch git refs from a remote
@@ -428,7 +503,7 @@ func (repo *GitRepo) FindCommonAncestor(hash1 git.Hash, hash2 git.Hash) (git.Has
stdout, err := repo.runGitCommand("merge-base", string(hash1), string(hash2))
if err != nil {
- return "", nil
+ return "", err
}
return git.Hash(stdout), nil
@@ -439,7 +514,7 @@ func (repo *GitRepo) GetTreeHash(commit git.Hash) (git.Hash, error) {
stdout, err := repo.runGitCommand("rev-parse", string(commit)+"^{tree}")
if err != nil {
- return "", nil
+ return "", err
}
return git.Hash(stdout), nil
diff --git a/repository/git_test.go b/repository/git_test.go
index 8bd3aa8e..20bf6ec3 100644
--- a/repository/git_test.go
+++ b/repository/git_test.go
@@ -55,7 +55,7 @@ func TestConfig(t *testing.T) {
assert.Error(t, err)
err = repo.RmConfigs("section")
- assert.NoError(t, err)
+ assert.Error(t, err)
_, err = repo.ReadConfigString("section.key")
assert.Error(t, err)