aboutsummaryrefslogtreecommitdiffstats
path: root/storage/filesystem/shallow.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-12-15 13:45:52 +0100
committerGitHub <noreply@github.com>2016-12-15 13:45:52 +0100
commit249c4137f4f34992c9bb6a60954e30a27994add7 (patch)
tree1188a410d368c619548d74798c474e04469eabae /storage/filesystem/shallow.go
parentf01fd176ff61a3f37d096939690aa103de054ecc (diff)
downloadgo-git-249c4137f4f34992c9bb6a60954e30a27994add7.tar.gz
storage: shallow storage (#180)
* storage: shallow storage * changes * changes
Diffstat (limited to 'storage/filesystem/shallow.go')
-rw-r--r--storage/filesystem/shallow.go51
1 files changed, 51 insertions, 0 deletions
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()
+}