Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

config: Config, bare flag #177

Merged
merged 3 commits into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var (

// Config contains the repository configuration
type Config struct {
Core struct {
IsBare bool
}
Remotes map[string]*RemoteConfig
}

Expand Down
21 changes: 21 additions & 0 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 21 additions & 0 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
57 changes: 41 additions & 16 deletions storage/filesystem/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package filesystem
import (
"os"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra space

"fmt"

"gopkg.in/src-d/go-git.v4/config"
gitconfig "gopkg.in/src-d/go-git.v4/plumbing/format/config"
"gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit"
)

const (
remoteSection = "remote"
coreSection = "core"
fetchKey = "fetch"
urlKey = "url"
bareKey = "bare"
)

type ConfigStorage struct {
Expand All @@ -26,11 +30,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
}
Expand All @@ -57,6 +58,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) {
Expand All @@ -83,6 +99,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))

Expand All @@ -91,8 +118,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 {
Expand All @@ -106,13 +141,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
}
1 change: 1 addition & 0 deletions storage/test/storage_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down