aboutsummaryrefslogtreecommitdiffstats
path: root/storage/filesystem
diff options
context:
space:
mode:
authorSunny <me@darkowlzz.space>2017-12-06 18:51:56 +0530
committerMáximo Cuadros <mcuadros@gmail.com>2017-12-06 14:21:56 +0100
commiteb74b0e1a0ae20075435aded41bffc522bc3554c (patch)
tree573952f76c2c311d1cf0cbf7627c6af4a68071dc /storage/filesystem
parentf96d46d38f67604f204711ea0e2832e6e047e2ad (diff)
downloadgo-git-eb74b0e1a0ae20075435aded41bffc522bc3554c.tar.gz
storage: filesystem, add support for git alternates (#663)
This change adds a new method Alternates() in DotGit to check and query alternate source.
Diffstat (limited to 'storage/filesystem')
-rw-r--r--storage/filesystem/internal/dotgit/dotgit.go45
-rw-r--r--storage/filesystem/internal/dotgit/dotgit_test.go53
-rw-r--r--storage/filesystem/object.go21
3 files changed, 118 insertions, 1 deletions
diff --git a/storage/filesystem/internal/dotgit/dotgit.go b/storage/filesystem/internal/dotgit/dotgit.go
index 11b2512..ebc6bad 100644
--- a/storage/filesystem/internal/dotgit/dotgit.go
+++ b/storage/filesystem/internal/dotgit/dotgit.go
@@ -8,9 +8,11 @@ import (
"io"
stdioutil "io/ioutil"
"os"
+ "path/filepath"
"strings"
"time"
+ "gopkg.in/src-d/go-billy.v4/osfs"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/utils/ioutil"
@@ -756,11 +758,52 @@ func (d *DotGit) PackRefs() (err error) {
return nil
}
-// Module return a billy.Filesystem poiting to the module folder
+// Module return a billy.Filesystem pointing to the module folder
func (d *DotGit) Module(name string) (billy.Filesystem, error) {
return d.fs.Chroot(d.fs.Join(modulePath, name))
}
+// Alternates returns DotGit(s) based off paths in objects/info/alternates if
+// available. This can be used to checks if it's a shared repository.
+func (d *DotGit) Alternates() ([]*DotGit, error) {
+ altpath := d.fs.Join("objects", "info", "alternates")
+ f, err := d.fs.Open(altpath)
+ if err != nil {
+ return nil, err
+ }
+ defer f.Close()
+
+ var alternates []*DotGit
+
+ // Read alternate paths line-by-line and create DotGit objects.
+ scanner := bufio.NewScanner(f)
+ for scanner.Scan() {
+ path := scanner.Text()
+ if !filepath.IsAbs(path) {
+ // For relative paths, we can perform an internal conversion to
+ // slash so that they work cross-platform.
+ slashPath := filepath.ToSlash(path)
+ // If the path is not absolute, it must be relative to object
+ // database (.git/objects/info).
+ // https://www.kernel.org/pub/software/scm/git/docs/gitrepository-layout.html
+ // Hence, derive a path relative to DotGit's root.
+ // "../../../reponame/.git/" -> "../../reponame/.git"
+ // Remove the first ../
+ relpath := filepath.Join(strings.Split(slashPath, "/")[1:]...)
+ normalPath := filepath.FromSlash(relpath)
+ path = filepath.Join(d.fs.Root(), normalPath)
+ }
+ fs := osfs.New(filepath.Dir(path))
+ alternates = append(alternates, New(fs))
+ }
+
+ if err = scanner.Err(); err != nil {
+ return nil, err
+ }
+
+ return alternates, nil
+}
+
func isHex(s string) bool {
for _, b := range []byte(s) {
if isNum(b) {
diff --git a/storage/filesystem/internal/dotgit/dotgit_test.go b/storage/filesystem/internal/dotgit/dotgit_test.go
index 0ed9ec5..2c43295 100644
--- a/storage/filesystem/internal/dotgit/dotgit_test.go
+++ b/storage/filesystem/internal/dotgit/dotgit_test.go
@@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
+ "runtime"
"strings"
"testing"
@@ -615,3 +616,55 @@ func (s *SuiteDotGit) TestPackRefs(c *C) {
c.Assert(ref, NotNil)
c.Assert(ref.Hash().String(), Equals, "b8d3ffab552895c19b9fcf7aa264d277cde33881")
}
+
+func (s *SuiteDotGit) TestAlternates(c *C) {
+ tmp, err := ioutil.TempDir("", "dot-git")
+ c.Assert(err, IsNil)
+ defer os.RemoveAll(tmp)
+
+ // Create a new billy fs.
+ fs := osfs.New(tmp)
+
+ // Create a new dotgit object and initialize.
+ dir := New(fs)
+ err = dir.Initialize()
+ c.Assert(err, IsNil)
+
+ // Create alternates file.
+ altpath := filepath.Join("objects", "info", "alternates")
+ f, err := fs.Create(altpath)
+ c.Assert(err, IsNil)
+
+ // Multiple alternates.
+ var strContent string
+ if runtime.GOOS == "windows" {
+ strContent = "C:\\Users\\username\\repo1\\.git\\objects\r\n..\\..\\..\\rep2\\.git\\objects"
+ } else {
+ strContent = "/Users/username/rep1//.git/objects\n../../../rep2//.git/objects"
+ }
+ content := []byte(strContent)
+ f.Write(content)
+ f.Close()
+
+ dotgits, err := dir.Alternates()
+ c.Assert(err, IsNil)
+ if runtime.GOOS == "windows" {
+ c.Assert(dotgits[0].fs.Root(), Equals, "C:\\Users\\username\\repo1\\.git")
+ } else {
+ c.Assert(dotgits[0].fs.Root(), Equals, "/Users/username/rep1/.git")
+ }
+
+ // For relative path:
+ // /some/absolute/path/to/dot-git -> /some/absolute/path
+ pathx := strings.Split(tmp, string(filepath.Separator))
+ pathx = pathx[:len(pathx)-2]
+ // Use string.Join() to avoid malformed absolutepath on windows
+ // C:Users\\User\\... instead of C:\\Users\\appveyor\\... .
+ resolvedPath := strings.Join(pathx, string(filepath.Separator))
+ // Append the alternate path to the resolvedPath
+ expectedPath := filepath.Join(string(filepath.Separator), resolvedPath, "rep2", ".git")
+ if runtime.GOOS == "windows" {
+ expectedPath = filepath.Join(resolvedPath, "rep2", ".git")
+ }
+ c.Assert(dotgits[1].fs.Root(), Equals, expectedPath)
+}
diff --git a/storage/filesystem/object.go b/storage/filesystem/object.go
index 6ca67cc..fd52ed5 100644
--- a/storage/filesystem/object.go
+++ b/storage/filesystem/object.go
@@ -160,6 +160,27 @@ func (s *ObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (p
obj, err = s.getFromPackfile(h, false)
}
+ // If the error is still object not found, check if it's a shared object
+ // repository.
+ if err == plumbing.ErrObjectNotFound {
+ dotgits, e := s.dir.Alternates()
+ if e == nil {
+ // Create a new object storage with the DotGit(s) and check for the
+ // required hash object. Skip when not found.
+ for _, dg := range dotgits {
+ o, oe := newObjectStorage(dg)
+ if oe != nil {
+ continue
+ }
+ enobj, enerr := o.EncodedObject(t, h)
+ if enerr != nil {
+ continue
+ }
+ return enobj, nil
+ }
+ }
+ }
+
if err != nil {
return nil, err
}