diff options
author | Amine Hilaly <hilalyamine@gmail.com> | 2019-07-08 23:57:19 +0200 |
---|---|---|
committer | Amine Hilaly <hilalyamine@gmail.com> | 2019-07-08 23:57:19 +0200 |
commit | fb50d470483b91d98e27ca6f9f605f0c51af6a71 (patch) | |
tree | a279d04878f3f1b1e28089779d011c0cb4b7545d /repository | |
parent | 76db2f42ebc356a0c347558133dc67ff6cb0d72d (diff) | |
download | git-bug-fb50d470483b91d98e27ca6f9f605f0c51af6a71.tar.gz |
repository: RmConfigs usage of git version lt 2.18
Diffstat (limited to 'repository')
-rw-r--r-- | repository/git.go | 58 |
1 files changed, 48 insertions, 10 deletions
diff --git a/repository/git.go b/repository/git.go index fcf5e70c..9ec7905a 100644 --- a/repository/git.go +++ b/repository/git.go @@ -261,6 +261,48 @@ 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 { // starting from git 2.18.0 sections are automatically deleted when the last existing @@ -272,19 +314,15 @@ func (repo *GitRepo) RmConfigs(keyPrefix string) error { } if lt218 { - // try to remove key/value pair by key - _, err := repo.runGitCommand("config", "--unset-all", keyPrefix) - if err != nil { - // try to remove section - _, err = repo.runGitCommand("config", "--remove-section", keyPrefix) - } + return repo.rmConfigsGitVersionLT218(keyPrefix) + } - return err + err = repo.unsetAll(keyPrefix) + if err != nil { + return repo.rmSection(keyPrefix) } - // try to remove key/value pair by key - _, err = repo.runGitCommand("config", "--unset-all", keyPrefix) - return err + return nil } func (repo *GitRepo) gitVersionLT218() (bool, error) { |