aboutsummaryrefslogtreecommitdiffstats
path: root/storage/filesystem
diff options
context:
space:
mode:
Diffstat (limited to 'storage/filesystem')
-rw-r--r--storage/filesystem/internal/dotgit/dotgit.go23
-rw-r--r--storage/filesystem/internal/dotgit/dotgit_test.go67
-rw-r--r--storage/filesystem/shallow.go51
-rw-r--r--storage/filesystem/storage.go2
4 files changed, 142 insertions, 1 deletions
diff --git a/storage/filesystem/internal/dotgit/dotgit.go b/storage/filesystem/internal/dotgit/dotgit.go
index 3a6227e..3405908 100644
--- a/storage/filesystem/internal/dotgit/dotgit.go
+++ b/storage/filesystem/internal/dotgit/dotgit.go
@@ -17,6 +17,7 @@ const (
suffix = ".git"
packedRefsPath = "packed-refs"
configPath = "config"
+ shallowPath = "shallow"
objectsPath = "objects"
packPath = "pack"
@@ -60,15 +61,35 @@ func New(fs fs.Filesystem) *DotGit {
return &DotGit{fs: fs}
}
+// ConfigWriter returns a file pointer for write to the config file
func (d *DotGit) ConfigWriter() (fs.File, error) {
return d.fs.Create(configPath)
}
-// Config returns the path of the config file
+// Config returns a file pointer for read to the config file
func (d *DotGit) Config() (fs.File, error) {
return d.fs.Open(configPath)
}
+// ShallowWriter returns a file pointer for write to the shallow file
+func (d *DotGit) ShallowWriter() (fs.File, error) {
+ return d.fs.Create(shallowPath)
+}
+
+// Shallow returns a file pointer for read to the shallow file
+func (d *DotGit) Shallow() (fs.File, error) {
+ f, err := d.fs.Open(shallowPath)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return nil, nil
+ }
+
+ return nil, err
+ }
+
+ return f, nil
+}
+
// NewObjectPack return a writer for a new packfile, it saves the packfile to
// disk and also generates and save the index for the given packfile.
func (d *DotGit) NewObjectPack() (*PackWriter, error) {
diff --git a/storage/filesystem/internal/dotgit/dotgit_test.go b/storage/filesystem/internal/dotgit/dotgit_test.go
index 3a1f194..b645a85 100644
--- a/storage/filesystem/internal/dotgit/dotgit_test.go
+++ b/storage/filesystem/internal/dotgit/dotgit_test.go
@@ -130,6 +130,61 @@ func (s *SuiteDotGit) TestConfig(c *C) {
c.Assert(filepath.Base(file.Filename()), Equals, "config")
}
+func (s *SuiteDotGit) TestConfigWriteAndConfig(c *C) {
+ tmp, err := ioutil.TempDir("", "dot-git")
+ c.Assert(err, IsNil)
+ defer os.RemoveAll(tmp)
+
+ fs := osfs.New(tmp)
+ dir := New(fs)
+
+ f, err := dir.ConfigWriter()
+ c.Assert(err, IsNil)
+
+ _, err = f.Write([]byte("foo"))
+ c.Assert(err, IsNil)
+
+ f, err = dir.Config()
+ c.Assert(err, IsNil)
+
+ cnt, err := ioutil.ReadAll(f)
+ c.Assert(err, IsNil)
+
+ c.Assert(string(cnt), Equals, "foo")
+}
+
+func (s *SuiteDotGit) TestShallow(c *C) {
+ fs := fixtures.Basic().ByTag(".git").One().DotGit()
+ dir := New(fs)
+
+ file, err := dir.Shallow()
+ c.Assert(err, IsNil)
+ c.Assert(file, IsNil)
+}
+
+func (s *SuiteDotGit) TestShallowWriteAndShallow(c *C) {
+ tmp, err := ioutil.TempDir("", "dot-git")
+ c.Assert(err, IsNil)
+ defer os.RemoveAll(tmp)
+
+ fs := osfs.New(tmp)
+ dir := New(fs)
+
+ f, err := dir.ShallowWriter()
+ c.Assert(err, IsNil)
+
+ _, err = f.Write([]byte("foo"))
+ c.Assert(err, IsNil)
+
+ f, err = dir.Shallow()
+ c.Assert(err, IsNil)
+
+ cnt, err := ioutil.ReadAll(f)
+ c.Assert(err, IsNil)
+
+ c.Assert(string(cnt), Equals, "foo")
+}
+
func findReference(refs []*plumbing.Reference, name string) *plumbing.Reference {
n := plumbing.ReferenceName(name)
for _, ref := range refs {
@@ -222,6 +277,18 @@ func (s *SuiteDotGit) TestObjects(c *C) {
c.Assert(hashes[2].String(), Equals, "03db8e1fbe133a480f2867aac478fd866686d69e")
}
+func (s *SuiteDotGit) TestObjectsNoFolder(c *C) {
+ tmp, err := ioutil.TempDir("", "dot-git")
+ c.Assert(err, IsNil)
+ defer os.RemoveAll(tmp)
+
+ fs := osfs.New(tmp)
+ dir := New(fs)
+ hash, err := dir.Objects()
+ c.Assert(err, IsNil)
+ c.Assert(hash, HasLen, 0)
+}
+
func (s *SuiteDotGit) TestObject(c *C) {
fs := fixtures.ByTag(".git").ByTag("unpacked").One().DotGit()
dir := New(fs)
diff --git a/storage/filesystem/shallow.go b/storage/filesystem/shallow.go
new file mode 100644
index 0000000..ec8d20e
--- /dev/null
+++ b/storage/filesystem/shallow.go
@@ -0,0 +1,51 @@
+package filesystem
+
+import (
+ "bufio"
+ "fmt"
+
+ "gopkg.in/src-d/go-git.v4/plumbing"
+ "gopkg.in/src-d/go-git.v4/storage/filesystem/internal/dotgit"
+)
+
+// ShallowStorage where the shallow commits are stored, an internal to
+// manipulate the shallow file
+type ShallowStorage struct {
+ dir *dotgit.DotGit
+}
+
+// SetShallow save the shallows in the shallow file in the .git folder as one
+// commit per line represented by 40-byte hexadecimal object terminated by a
+// newline.
+func (s *ShallowStorage) SetShallow(commits []plumbing.Hash) error {
+ f, err := s.dir.ShallowWriter()
+ if err != nil {
+ return err
+ }
+
+ defer f.Close()
+ for _, h := range commits {
+ if _, err := fmt.Fprintf(f, "%s\n", h); err != err {
+ return err
+ }
+ }
+
+ return nil
+}
+
+// Shallow return the shallow commits reading from shallo file from .git
+func (s *ShallowStorage) Shallow() ([]plumbing.Hash, error) {
+ f, err := s.dir.Shallow()
+ if err != nil {
+ return nil, err
+ }
+
+ var hash []plumbing.Hash
+
+ scn := bufio.NewScanner(f)
+ for scn.Scan() {
+ hash = append(hash, plumbing.NewHash(scn.Text()))
+ }
+
+ return hash, scn.Err()
+}
diff --git a/storage/filesystem/storage.go b/storage/filesystem/storage.go
index 7e05cd3..e414428 100644
--- a/storage/filesystem/storage.go
+++ b/storage/filesystem/storage.go
@@ -12,6 +12,7 @@ import (
type Storage struct {
ObjectStorage
ReferenceStorage
+ ShallowStorage
ConfigStorage
}
@@ -26,6 +27,7 @@ func NewStorage(fs fs.Filesystem) (*Storage, error) {
return &Storage{
ObjectStorage: o,
ReferenceStorage: ReferenceStorage{dir: dir},
+ ShallowStorage: ShallowStorage{dir: dir},
ConfigStorage: ConfigStorage{dir: dir},
}, nil
}