aboutsummaryrefslogtreecommitdiffstats
path: root/storage
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2020-05-24 16:53:30 +0200
committerGitHub <noreply@github.com>2020-05-24 16:53:30 +0200
commite7f544844d6d736acfd9d75ee0d4a9d37f450103 (patch)
treedc44247231e6603bf10acaee91af5523120b4b84 /storage
parent6d8103df45ce09ffd5323b4ef46d26440400a54f (diff)
parentbaf8c2761217cd457ef672972d5c1fb4d066e95a (diff)
downloadgo-git-e7f544844d6d736acfd9d75ee0d4a9d37f450103.tar.gz
Merge pull request #75 from mcuadros/scope-config
repository.ConfigScoped and worktree.Commit with empty CommitOptions
Diffstat (limited to 'storage')
-rw-r--r--storage/filesystem/config.go67
-rw-r--r--storage/filesystem/dotgit/dotgit.go50
-rw-r--r--storage/filesystem/dotgit/dotgit_test.go6
3 files changed, 23 insertions, 100 deletions
diff --git a/storage/filesystem/config.go b/storage/filesystem/config.go
index dbdce54..78a6464 100644
--- a/storage/filesystem/config.go
+++ b/storage/filesystem/config.go
@@ -1,11 +1,9 @@
package filesystem
import (
- stdioutil "io/ioutil"
"os"
"github.com/go-git/go-git/v5/config"
- format "github.com/go-git/go-git/v5/plumbing/format/config"
"github.com/go-git/go-git/v5/storage/filesystem/dotgit"
"github.com/go-git/go-git/v5/utils/ioutil"
)
@@ -14,73 +12,18 @@ type ConfigStorage struct {
dir *dotgit.DotGit
}
-func (c *ConfigStorage) Config() (*config.Config, error) {
- cfg := config.NewConfig()
-
- // local config (./.git/config)
- f, err := c.dir.LocalConfig()
- if err != nil {
- if os.IsNotExist(err) {
- return cfg, nil
- }
-
- return nil, err
- }
-
- defer ioutil.CheckClose(f, &err)
-
- b, err := stdioutil.ReadAll(f)
- if err != nil {
- return cfg, err
- }
-
- if err = cfg.UnmarshalScoped(format.LocalScope, b); err != nil {
- return cfg, err
- }
-
- // global config (~/.gitconfig)
- f, err = c.dir.GlobalConfig()
+func (c *ConfigStorage) Config() (conf *config.Config, err error) {
+ f, err := c.dir.Config()
if err != nil {
if os.IsNotExist(err) {
- return cfg, nil
+ return config.NewConfig(), nil
}
return nil, err
}
defer ioutil.CheckClose(f, &err)
-
- b, err = stdioutil.ReadAll(f)
- if err != nil {
- return cfg, err
- }
-
- if err = cfg.UnmarshalScoped(format.GlobalScope, b); err != nil {
- return cfg, err
- }
-
- // system config (/etc/gitconfig)
- f, err = c.dir.SystemConfig()
- if err != nil {
- if os.IsNotExist(err) {
- return cfg, nil
- }
-
- return nil, err
- }
-
- defer ioutil.CheckClose(f, &err)
-
- b, err = stdioutil.ReadAll(f)
- if err != nil {
- return cfg, err
- }
-
- if err = cfg.UnmarshalScoped(format.SystemScope, b); err != nil {
- return cfg, err
- }
-
- return cfg, err
+ return config.ReadConfig(f)
}
func (c *ConfigStorage) SetConfig(cfg *config.Config) (err error) {
@@ -88,7 +31,7 @@ func (c *ConfigStorage) SetConfig(cfg *config.Config) (err error) {
return err
}
- f, err := c.dir.LocalConfigWriter()
+ f, err := c.dir.ConfigWriter()
if err != nil {
return err
}
diff --git a/storage/filesystem/dotgit/dotgit.go b/storage/filesystem/dotgit/dotgit.go
index 8c3896b..83c7683 100644
--- a/storage/filesystem/dotgit/dotgit.go
+++ b/storage/filesystem/dotgit/dotgit.go
@@ -21,17 +21,15 @@ import (
)
const (
- suffix = ".git"
- packedRefsPath = "packed-refs"
- localConfigPath = "config"
- globalConfigPath = ".gitconfig"
- systemConfigPath = "/etc/gitconfig"
- indexPath = "index"
- shallowPath = "shallow"
- modulePath = "modules"
- objectsPath = "objects"
- packPath = "pack"
- refsPath = "refs"
+ suffix = ".git"
+ packedRefsPath = "packed-refs"
+ configPath = "config"
+ indexPath = "index"
+ shallowPath = "shallow"
+ modulePath = "modules"
+ objectsPath = "objects"
+ packPath = "pack"
+ refsPath = "refs"
tmpPackedRefsPrefix = "._packed-refs"
@@ -157,32 +155,14 @@ func (d *DotGit) Close() error {
return nil
}
-// LocalConfigWriter returns a file pointer for write to the local config file
-func (d *DotGit) LocalConfigWriter() (billy.File, error) {
- return d.fs.Create(localConfigPath)
+// ConfigWriter returns a file pointer for write to the config file
+func (d *DotGit) ConfigWriter() (billy.File, error) {
+ return d.fs.Create(configPath)
}
-// LocalConfig returns a file pointer for read to the local config file
-func (d *DotGit) LocalConfig() (billy.File, error) {
- return d.fs.Open(localConfigPath)
-}
-
-// GlobalConfigWriter returns a file pointer for write to the global config file
-func (d *DotGit) GlobalConfigWriter() (billy.File, error) {
- return osfs.New(os.Getenv("HOME")).Create(globalConfigPath)
-}
-
-// GlobalConfig returns a file pointer for read to the global config file
-func (d *DotGit) GlobalConfig() (billy.File, error) {
- return osfs.New(os.Getenv("HOME")).Open(globalConfigPath)
-}
-
-// SystemConfigWriter doesn't exist because we typically do not have permission
-// to write to files in /etc.
-
-// SystemConfig returns a file pointer for read to the system config file
-func (d *DotGit) SystemConfig() (billy.File, error) {
- return osfs.New("/").Open(systemConfigPath)
+// Config returns a file pointer for read to the config file
+func (d *DotGit) Config() (billy.File, error) {
+ return d.fs.Open(configPath)
}
// IndexWriter returns a file pointer for write to the index file
diff --git a/storage/filesystem/dotgit/dotgit_test.go b/storage/filesystem/dotgit/dotgit_test.go
index 519f601..0a72aa6 100644
--- a/storage/filesystem/dotgit/dotgit_test.go
+++ b/storage/filesystem/dotgit/dotgit_test.go
@@ -344,7 +344,7 @@ func (s *SuiteDotGit) TestConfig(c *C) {
fs := fixtures.Basic().ByTag(".git").One().DotGit()
dir := New(fs)
- file, err := dir.LocalConfig()
+ file, err := dir.Config()
c.Assert(err, IsNil)
c.Assert(filepath.Base(file.Name()), Equals, "config")
}
@@ -357,13 +357,13 @@ func (s *SuiteDotGit) TestConfigWriteAndConfig(c *C) {
fs := osfs.New(tmp)
dir := New(fs)
- f, err := dir.LocalConfigWriter()
+ f, err := dir.ConfigWriter()
c.Assert(err, IsNil)
_, err = f.Write([]byte("foo"))
c.Assert(err, IsNil)
- f, err = dir.LocalConfig()
+ f, err = dir.Config()
c.Assert(err, IsNil)
cnt, err := ioutil.ReadAll(f)