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

Commit daa6b64

Browse files
committed
storage: support ConfigStorage, memory done, fs wip
1 parent 501a972 commit daa6b64

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

storage/filesystem/internal/dotgit/dotgit.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ import (
1212
const (
1313
suffix = ".git"
1414
packedRefsPath = "packed-refs"
15+
configPath = "config"
1516
)
1617

1718
var (
1819
// ErrNotFound is returned by New when the path is not found.
1920
ErrNotFound = errors.New("path not found")
20-
// ErrIdxNotFound is returned by Idxfile when the idx file is not found on the
21-
// repository.
21+
// ErrIdxNotFound is returned by Idxfile when the idx file is not found
2222
ErrIdxNotFound = errors.New("idx file not found")
2323
// ErrPackfileNotFound is returned by Packfile when the packfile is not found
24-
// on the repository.
2524
ErrPackfileNotFound = errors.New("packfile not found")
25+
// ErrConfigNotFound is returned by Config when the config is not found
26+
ErrConfigNotFound = errors.New("config file not found")
2627
)
2728

2829
// The DotGit type represents a local git repository on disk. This
@@ -103,3 +104,17 @@ func (d *DotGit) Idxfile() (fs.FS, string, error) {
103104

104105
return nil, "", ErrIdxNotFound
105106
}
107+
108+
// Config returns the path of the config file
109+
func (d *DotGit) Config() (fs.FS, string, error) {
110+
configFile := d.fs.Join(d.path, configPath)
111+
if _, err := d.fs.Stat(configFile); err != nil {
112+
if os.IsNotExist(err) {
113+
return nil, "", ErrNotFound
114+
}
115+
116+
return nil, "", err
117+
}
118+
119+
return d.fs, configFile, nil
120+
}

storage/filesystem/internal/dotgit/dotgit_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ func (s *SuiteDotGit) TestRefsFromHEADFile(c *C) {
143143
c.Assert(string(ref.Target()), Equals, "refs/heads/master")
144144
}
145145

146+
func (s *SuiteDotGit) TestConfig(c *C) {
147+
_, d := s.newFixtureDir(c, "spinnaker")
148+
fs, path, err := d.Config()
149+
c.Assert(err, IsNil)
150+
c.Assert(fs, NotNil)
151+
c.Assert(path, Not(Equals), "")
152+
}
153+
146154
func findReference(refs []*core.Reference, name string) *core.Reference {
147155
n := core.ReferenceName(name)
148156
for _, ref := range refs {

storage/memory/storage.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package memory
33
import (
44
"fmt"
55

6+
"gopkg.in/src-d/go-git.v4/config"
67
"gopkg.in/src-d/go-git.v4/core"
78
)
89

910
var ErrUnsupportedObjectType = fmt.Errorf("unsupported object type")
1011

1112
// Storage in memory storage system
1213
type Storage struct {
14+
c *ConfigStorage
1315
o *ObjectStorage
1416
r *ReferenceStorage
1517
}
@@ -19,7 +21,20 @@ func NewStorage() *Storage {
1921
return &Storage{}
2022
}
2123

22-
// ObjectStorage returns the ObjectStorage if not exists creates a new one
24+
// ConfigStorage return the ConfigStorage, if not exists create a new one
25+
func (s *Storage) ConfigStorage() config.ConfigStorage {
26+
if s.c != nil {
27+
return s.c
28+
}
29+
30+
s.c = &ConfigStorage{
31+
RemotesConfig: make(map[string]*config.RemoteConfig),
32+
}
33+
34+
return s.c
35+
}
36+
37+
// ObjectStorage returns the ObjectStorage, if not exists creates a new one
2338
func (s *Storage) ObjectStorage() core.ObjectStorage {
2439
if s.o != nil {
2540
return s.o
@@ -48,6 +63,32 @@ func (s *Storage) ReferenceStorage() core.ReferenceStorage {
4863
return s.r
4964
}
5065

66+
type ConfigStorage struct {
67+
RemotesConfig map[string]*config.RemoteConfig
68+
}
69+
70+
func (c *ConfigStorage) Remote(name string) (*config.RemoteConfig, error) {
71+
r, ok := c.RemotesConfig[name]
72+
if ok {
73+
return r, nil
74+
}
75+
76+
return nil, config.ErrRemoteConfigNotFound
77+
}
78+
79+
func (c *ConfigStorage) Remotes() ([]*config.RemoteConfig, error) {
80+
var o []*config.RemoteConfig
81+
for _, r := range c.RemotesConfig {
82+
o = append(o, r)
83+
}
84+
85+
return o, nil
86+
}
87+
func (c *ConfigStorage) SetRemote(r *config.RemoteConfig) error {
88+
c.RemotesConfig[r.Name] = r
89+
return nil
90+
}
91+
5192
// ObjectStorage is the implementation of core.ObjectStorage for memory.Object
5293
type ObjectStorage struct {
5394
Objects map[core.Hash]core.Object
@@ -57,17 +98,6 @@ type ObjectStorage struct {
5798
Tags map[core.Hash]core.Object
5899
}
59100

60-
// NewObjectStorage returns a new empty ObjectStorage
61-
func NewObjectStorage() *ObjectStorage {
62-
return &ObjectStorage{
63-
Objects: make(map[core.Hash]core.Object, 0),
64-
Commits: make(map[core.Hash]core.Object, 0),
65-
Trees: make(map[core.Hash]core.Object, 0),
66-
Blobs: make(map[core.Hash]core.Object, 0),
67-
Tags: make(map[core.Hash]core.Object, 0),
68-
}
69-
}
70-
71101
// NewObject creates a new MemoryObject
72102
func (o *ObjectStorage) NewObject() core.Object {
73103
return &core.MemoryObject{}

0 commit comments

Comments
 (0)