diff options
-rw-r--r-- | config/config.go | 3 | ||||
-rw-r--r-- | repository.go | 21 | ||||
-rw-r--r-- | repository_test.go | 21 | ||||
-rw-r--r-- | storage/filesystem/config.go | 56 | ||||
-rw-r--r-- | storage/test/storage_suite.go | 1 |
5 files changed, 86 insertions, 16 deletions
diff --git a/config/config.go b/config/config.go index ea8ae07..dc76571 100644 --- a/config/config.go +++ b/config/config.go @@ -26,6 +26,9 @@ var ( // Config contains the repository configuration type Config struct { + Core struct { + IsBare bool + } Remotes map[string]*RemoteConfig } diff --git a/repository.go b/repository.go index 816cd09..ae73885 100644 --- a/repository.go +++ b/repository.go @@ -58,6 +58,11 @@ func NewRepository(s Storer) (*Repository, error) { }, nil } +// Config return the repository config +func (r *Repository) Config() (*config.Config, error) { + return r.s.Config() +} + // Remote return a remote if exists func (r *Repository) Remote(name string) (*Remote, error) { cfg, err := r.s.Config() @@ -142,6 +147,12 @@ func (r *Repository) Clone(o *CloneOptions) error { return err } + // marks the repository as bare in the config, until we have Worktree, all + // the repository are bare + if err := r.setIsBare(true); err != nil { + return err + } + c := &config.RemoteConfig{ Name: o.RemoteName, URL: o.URL, @@ -174,6 +185,16 @@ func (r *Repository) Clone(o *CloneOptions) error { return r.createReferences(head) } +func (r *Repository) setIsBare(isBare bool) error { + cfg, err := r.s.Config() + if err != nil { + return err + } + + cfg.Core.IsBare = isBare + return r.s.SetConfig(cfg) +} + const refspecSingleBranch = "+refs/heads/%s:refs/remotes/%s/%[1]s" func (r *Repository) updateRemoteConfig( diff --git a/repository_test.go b/repository_test.go index 8ac3d9e..b8f57cd 100644 --- a/repository_test.go +++ b/repository_test.go @@ -118,6 +118,27 @@ func (s *RepositorySuite) TestClone(c *C) { c.Assert(branch.Hash().String(), Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") } +func (s *RepositorySuite) TestCloneConfig(c *C) { + r := NewMemoryRepository() + + head, err := r.Head() + c.Assert(err, Equals, plumbing.ErrReferenceNotFound) + c.Assert(head, IsNil) + + err = r.Clone(&CloneOptions{ + URL: s.GetBasicLocalRepositoryURL(), + }) + + c.Assert(err, IsNil) + + cfg, err := r.Config() + c.Assert(err, IsNil) + + c.Assert(cfg.Core.IsBare, Equals, true) + c.Assert(cfg.Remotes, HasLen, 1) + c.Assert(cfg.Remotes["origin"].Name, Equals, "origin") + c.Assert(cfg.Remotes["origin"].URL, Not(Equals), "") +} func (s *RepositorySuite) TestCloneNonEmpty(c *C) { r := NewMemoryRepository() diff --git a/storage/filesystem/config.go b/storage/filesystem/config.go index 510f51c..07e0433 100644 --- a/storage/filesystem/config.go +++ b/storage/filesystem/config.go @@ -1,6 +1,7 @@ package filesystem import ( + "fmt" "os" "gopkg.in/src-d/go-git.v4/config" @@ -10,8 +11,10 @@ import ( const ( remoteSection = "remote" + coreSection = "core" fetchKey = "fetch" urlKey = "url" + bareKey = "bare" ) type ConfigStorage struct { @@ -26,11 +29,8 @@ func (c *ConfigStorage) Config() (*config.Config, error) { return nil, err } - sect := ini.Section(remoteSection) - for _, s := range sect.Subsections { - r := c.unmarshalRemote(s) - cfg.Remotes[r.Name] = r - } + c.unmarshalCore(cfg, ini) + c.unmarshalRemotes(cfg, ini) return cfg, nil } @@ -57,6 +57,21 @@ func (c *ConfigStorage) unmarshal() (*gitconfig.Config, error) { return cfg, nil } +func (c *ConfigStorage) unmarshalCore(cfg *config.Config, ini *gitconfig.Config) { + s := ini.Section(coreSection) + if s.Options.Get(bareKey) == "true" { + cfg.Core.IsBare = true + } +} + +func (c *ConfigStorage) unmarshalRemotes(cfg *config.Config, ini *gitconfig.Config) { + s := ini.Section(remoteSection) + for _, sub := range s.Subsections { + r := c.unmarshalRemote(sub) + cfg.Remotes[r.Name] = r + } +} + func (c *ConfigStorage) unmarshalRemote(s *gitconfig.Subsection) *config.RemoteConfig { fetch := []config.RefSpec{} for _, f := range s.Options.GetAll(fetchKey) { @@ -83,6 +98,17 @@ func (c *ConfigStorage) SetConfig(cfg *config.Config) error { return err } + c.marshalCore(cfg, ini) + c.marshalRemotes(cfg, ini) + return c.marshal(ini) +} + +func (c *ConfigStorage) marshalCore(cfg *config.Config, ini *gitconfig.Config) { + s := ini.Section(coreSection) + s.AddOption(bareKey, fmt.Sprintf("%t", cfg.Core.IsBare)) +} + +func (c *ConfigStorage) marshalRemotes(cfg *config.Config, ini *gitconfig.Config) { s := ini.Section(remoteSection) s.Subsections = make(gitconfig.Subsections, len(cfg.Remotes)) @@ -91,8 +117,16 @@ func (c *ConfigStorage) SetConfig(cfg *config.Config) error { s.Subsections[i] = c.marshalRemote(r) i++ } +} - return c.marshal(ini) +func (c *ConfigStorage) marshalRemote(r *config.RemoteConfig) *gitconfig.Subsection { + s := &gitconfig.Subsection{Name: r.Name} + s.AddOption(urlKey, r.URL) + for _, rs := range r.Fetch { + s.AddOption(fetchKey, rs.String()) + } + + return s } func (c *ConfigStorage) marshal(ini *gitconfig.Config) error { @@ -106,13 +140,3 @@ func (c *ConfigStorage) marshal(ini *gitconfig.Config) error { e := gitconfig.NewEncoder(f) return e.Encode(ini) } - -func (c *ConfigStorage) marshalRemote(r *config.RemoteConfig) *gitconfig.Subsection { - s := &gitconfig.Subsection{Name: r.Name} - s.AddOption(urlKey, r.URL) - for _, rs := range r.Fetch { - s.AddOption(fetchKey, rs.String()) - } - - return s -} diff --git a/storage/test/storage_suite.go b/storage/test/storage_suite.go index 22225a5..732c7a9 100644 --- a/storage/test/storage_suite.go +++ b/storage/test/storage_suite.go @@ -265,6 +265,7 @@ func (s *BaseStorageSuite) TestIterReferences(c *C) { func (s *BaseStorageSuite) TestSetConfigAndConfig(c *C) { expected := config.NewConfig() + expected.Core.IsBare = true expected.Remotes["foo"] = &config.RemoteConfig{ Name: "foo", URL: "http://foo/bar.git", |