aboutsummaryrefslogtreecommitdiffstats
path: root/storage/filesystem
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2020-04-07 23:18:13 +0200
committerGitHub <noreply@github.com>2020-04-07 23:18:13 +0200
commit3127ad9a44a2ee935502816065dfe39f494f583d (patch)
treec5b7892e36ba31890e5c60571082deacf255bca7 /storage/filesystem
parent73c52edaad2dae256be61bd1dbbab08e1092f58e (diff)
parent68bffab4d6fa84260cc4287f979a6feceaf92ff8 (diff)
downloadgo-git-3127ad9a44a2ee935502816065dfe39f494f583d.tar.gz
Merge pull request #20 from quorumcontrol/feature/other-configs
plumbing: format, config.Merged to allow access to local and global config
Diffstat (limited to 'storage/filesystem')
-rw-r--r--storage/filesystem/config.go52
-rw-r--r--storage/filesystem/dotgit/dotgit.go50
-rw-r--r--storage/filesystem/dotgit/dotgit_test.go6
3 files changed, 86 insertions, 22 deletions
diff --git a/storage/filesystem/config.go b/storage/filesystem/config.go
index 01b35b4..dbdce54 100644
--- a/storage/filesystem/config.go
+++ b/storage/filesystem/config.go
@@ -5,6 +5,7 @@ import (
"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"
)
@@ -13,10 +14,11 @@ type ConfigStorage struct {
dir *dotgit.DotGit
}
-func (c *ConfigStorage) Config() (conf *config.Config, err error) {
+func (c *ConfigStorage) Config() (*config.Config, error) {
cfg := config.NewConfig()
- f, err := c.dir.Config()
+ // local config (./.git/config)
+ f, err := c.dir.LocalConfig()
if err != nil {
if os.IsNotExist(err) {
return cfg, nil
@@ -29,13 +31,55 @@ func (c *ConfigStorage) Config() (conf *config.Config, err error) {
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()
+ if err != nil {
+ if os.IsNotExist(err) {
+ return cfg, nil
+ }
+
return nil, err
}
- if err = cfg.Unmarshal(b); err != nil {
+ 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
}
@@ -44,7 +88,7 @@ func (c *ConfigStorage) SetConfig(cfg *config.Config) (err error) {
return err
}
- f, err := c.dir.ConfigWriter()
+ f, err := c.dir.LocalConfigWriter()
if err != nil {
return err
}
diff --git a/storage/filesystem/dotgit/dotgit.go b/storage/filesystem/dotgit/dotgit.go
index 3ce9dae..503ce18 100644
--- a/storage/filesystem/dotgit/dotgit.go
+++ b/storage/filesystem/dotgit/dotgit.go
@@ -21,15 +21,17 @@ import (
)
const (
- suffix = ".git"
- packedRefsPath = "packed-refs"
- configPath = "config"
- indexPath = "index"
- shallowPath = "shallow"
- modulePath = "modules"
- objectsPath = "objects"
- packPath = "pack"
- refsPath = "refs"
+ suffix = ".git"
+ packedRefsPath = "packed-refs"
+ localConfigPath = "config"
+ globalConfigPath = ".gitconfig"
+ systemConfigPath = "/etc/gitconfig"
+ indexPath = "index"
+ shallowPath = "shallow"
+ modulePath = "modules"
+ objectsPath = "objects"
+ packPath = "pack"
+ refsPath = "refs"
tmpPackedRefsPrefix = "._packed-refs"
@@ -152,14 +154,32 @@ func (d *DotGit) Close() error {
return nil
}
-// ConfigWriter returns a file pointer for write to the config file
-func (d *DotGit) ConfigWriter() (billy.File, error) {
- return d.fs.Create(configPath)
+// LocalConfigWriter returns a file pointer for write to the local config file
+func (d *DotGit) LocalConfigWriter() (billy.File, error) {
+ return d.fs.Create(localConfigPath)
}
-// Config returns a file pointer for read to the config file
-func (d *DotGit) Config() (billy.File, error) {
- return d.fs.Open(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)
}
// 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 cf81c23..d57ff15 100644
--- a/storage/filesystem/dotgit/dotgit_test.go
+++ b/storage/filesystem/dotgit/dotgit_test.go
@@ -332,7 +332,7 @@ func (s *SuiteDotGit) TestConfig(c *C) {
fs := fixtures.Basic().ByTag(".git").One().DotGit()
dir := New(fs)
- file, err := dir.Config()
+ file, err := dir.LocalConfig()
c.Assert(err, IsNil)
c.Assert(filepath.Base(file.Name()), Equals, "config")
}
@@ -345,13 +345,13 @@ func (s *SuiteDotGit) TestConfigWriteAndConfig(c *C) {
fs := osfs.New(tmp)
dir := New(fs)
- f, err := dir.ConfigWriter()
+ f, err := dir.LocalConfigWriter()
c.Assert(err, IsNil)
_, err = f.Write([]byte("foo"))
c.Assert(err, IsNil)
- f, err = dir.Config()
+ f, err = dir.LocalConfig()
c.Assert(err, IsNil)
cnt, err := ioutil.ReadAll(f)