aboutsummaryrefslogtreecommitdiffstats
path: root/storage
diff options
context:
space:
mode:
Diffstat (limited to 'storage')
-rw-r--r--storage/filesystem/config_test.go4
-rw-r--r--storage/filesystem/dotgit/dotgit.go6
-rw-r--r--storage/filesystem/dotgit/repository_filesystem.go111
-rw-r--r--storage/filesystem/dotgit/repository_filesystem_test.go124
-rw-r--r--storage/filesystem/dotgit/writers_test.go2
-rw-r--r--storage/memory/storage.go4
-rw-r--r--storage/transactional/config_test.go3
-rw-r--r--storage/transactional/index_test.go3
-rw-r--r--storage/transactional/object_test.go3
-rw-r--r--storage/transactional/reference_test.go3
-rw-r--r--storage/transactional/shallow_test.go3
11 files changed, 256 insertions, 10 deletions
diff --git a/storage/filesystem/config_test.go b/storage/filesystem/config_test.go
index fe84698..c092d14 100644
--- a/storage/filesystem/config_test.go
+++ b/storage/filesystem/config_test.go
@@ -4,12 +4,12 @@ import (
"io/ioutil"
"os"
+ "github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/storage/filesystem/dotgit"
- "github.com/go-git/go-billy/v5/osfs"
+ fixtures "github.com/go-git/go-git-fixtures/v4"
. "gopkg.in/check.v1"
- "github.com/go-git/go-git-fixtures/v4"
)
type ConfigSuite struct {
diff --git a/storage/filesystem/dotgit/dotgit.go b/storage/filesystem/dotgit/dotgit.go
index 83c7683..3840ea7 100644
--- a/storage/filesystem/dotgit/dotgit.go
+++ b/storage/filesystem/dotgit/dotgit.go
@@ -30,6 +30,12 @@ const (
objectsPath = "objects"
packPath = "pack"
refsPath = "refs"
+ branchesPath = "branches"
+ hooksPath = "hooks"
+ infoPath = "info"
+ remotesPath = "remotes"
+ logsPath = "logs"
+ worktreesPath = "worktrees"
tmpPackedRefsPrefix = "._packed-refs"
diff --git a/storage/filesystem/dotgit/repository_filesystem.go b/storage/filesystem/dotgit/repository_filesystem.go
new file mode 100644
index 0000000..8d243ef
--- /dev/null
+++ b/storage/filesystem/dotgit/repository_filesystem.go
@@ -0,0 +1,111 @@
+package dotgit
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+
+ "github.com/go-git/go-billy/v5"
+)
+
+// RepositoryFilesystem is a billy.Filesystem compatible object wrapper
+// which handles dot-git filesystem operations and supports commondir according to git scm layout:
+// https://github.com/git/git/blob/master/Documentation/gitrepository-layout.txt
+type RepositoryFilesystem struct {
+ dotGitFs billy.Filesystem
+ commonDotGitFs billy.Filesystem
+}
+
+func NewRepositoryFilesystem(dotGitFs, commonDotGitFs billy.Filesystem) *RepositoryFilesystem {
+ return &RepositoryFilesystem{
+ dotGitFs: dotGitFs,
+ commonDotGitFs: commonDotGitFs,
+ }
+}
+
+func (fs *RepositoryFilesystem) mapToRepositoryFsByPath(path string) billy.Filesystem {
+ // Nothing to decide if commondir not defined
+ if fs.commonDotGitFs == nil {
+ return fs.dotGitFs
+ }
+
+ cleanPath := filepath.Clean(path)
+
+ // Check exceptions for commondir (https://git-scm.com/docs/gitrepository-layout#Documentation/gitrepository-layout.txt)
+ switch cleanPath {
+ case fs.dotGitFs.Join(logsPath, "HEAD"):
+ return fs.dotGitFs
+ case fs.dotGitFs.Join(refsPath, "bisect"), fs.dotGitFs.Join(refsPath, "rewritten"), fs.dotGitFs.Join(refsPath, "worktree"):
+ return fs.dotGitFs
+ }
+
+ // Determine dot-git root by first path element.
+ // There are some elements which should always use commondir when commondir defined.
+ // Usual dot-git root will be used for the rest of files.
+ switch strings.Split(cleanPath, string(filepath.Separator))[0] {
+ case objectsPath, refsPath, packedRefsPath, configPath, branchesPath, hooksPath, infoPath, remotesPath, logsPath, shallowPath, worktreesPath:
+ return fs.commonDotGitFs
+ default:
+ return fs.dotGitFs
+ }
+}
+
+func (fs *RepositoryFilesystem) Create(filename string) (billy.File, error) {
+ return fs.mapToRepositoryFsByPath(filename).Create(filename)
+}
+
+func (fs *RepositoryFilesystem) Open(filename string) (billy.File, error) {
+ return fs.mapToRepositoryFsByPath(filename).Open(filename)
+}
+
+func (fs *RepositoryFilesystem) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error) {
+ return fs.mapToRepositoryFsByPath(filename).OpenFile(filename, flag, perm)
+}
+
+func (fs *RepositoryFilesystem) Stat(filename string) (os.FileInfo, error) {
+ return fs.mapToRepositoryFsByPath(filename).Stat(filename)
+}
+
+func (fs *RepositoryFilesystem) Rename(oldpath, newpath string) error {
+ return fs.mapToRepositoryFsByPath(oldpath).Rename(oldpath, newpath)
+}
+
+func (fs *RepositoryFilesystem) Remove(filename string) error {
+ return fs.mapToRepositoryFsByPath(filename).Remove(filename)
+}
+
+func (fs *RepositoryFilesystem) Join(elem ...string) string {
+ return fs.dotGitFs.Join(elem...)
+}
+
+func (fs *RepositoryFilesystem) TempFile(dir, prefix string) (billy.File, error) {
+ return fs.mapToRepositoryFsByPath(dir).TempFile(dir, prefix)
+}
+
+func (fs *RepositoryFilesystem) ReadDir(path string) ([]os.FileInfo, error) {
+ return fs.mapToRepositoryFsByPath(path).ReadDir(path)
+}
+
+func (fs *RepositoryFilesystem) MkdirAll(filename string, perm os.FileMode) error {
+ return fs.mapToRepositoryFsByPath(filename).MkdirAll(filename, perm)
+}
+
+func (fs *RepositoryFilesystem) Lstat(filename string) (os.FileInfo, error) {
+ return fs.mapToRepositoryFsByPath(filename).Lstat(filename)
+}
+
+func (fs *RepositoryFilesystem) Symlink(target, link string) error {
+ return fs.mapToRepositoryFsByPath(target).Symlink(target, link)
+}
+
+func (fs *RepositoryFilesystem) Readlink(link string) (string, error) {
+ return fs.mapToRepositoryFsByPath(link).Readlink(link)
+}
+
+func (fs *RepositoryFilesystem) Chroot(path string) (billy.Filesystem, error) {
+ return fs.mapToRepositoryFsByPath(path).Chroot(path)
+}
+
+func (fs *RepositoryFilesystem) Root() string {
+ return fs.dotGitFs.Root()
+}
diff --git a/storage/filesystem/dotgit/repository_filesystem_test.go b/storage/filesystem/dotgit/repository_filesystem_test.go
new file mode 100644
index 0000000..880ec0d
--- /dev/null
+++ b/storage/filesystem/dotgit/repository_filesystem_test.go
@@ -0,0 +1,124 @@
+package dotgit
+
+import (
+ "io/ioutil"
+ "log"
+ "os"
+
+ "github.com/go-git/go-billy/v5/osfs"
+
+ . "gopkg.in/check.v1"
+)
+
+func (s *SuiteDotGit) TestRepositoryFilesystem(c *C) {
+ dir, err := ioutil.TempDir("", "repository_filesystem")
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer os.RemoveAll(dir)
+
+ fs := osfs.New(dir)
+
+ err = fs.MkdirAll("dotGit", 0777)
+ c.Assert(err, IsNil)
+ dotGitFs, err := fs.Chroot("dotGit")
+ c.Assert(err, IsNil)
+
+ err = fs.MkdirAll("commonDotGit", 0777)
+ c.Assert(err, IsNil)
+ commonDotGitFs, err := fs.Chroot("commonDotGit")
+ c.Assert(err, IsNil)
+
+ repositoryFs := NewRepositoryFilesystem(dotGitFs, commonDotGitFs)
+ c.Assert(repositoryFs.Root(), Equals, dotGitFs.Root())
+
+ somedir, err := repositoryFs.Chroot("somedir")
+ c.Assert(err, IsNil)
+ c.Assert(somedir.Root(), Equals, repositoryFs.Join(dotGitFs.Root(), "somedir"))
+
+ _, err = repositoryFs.Create("somefile")
+ c.Assert(err, IsNil)
+
+ _, err = repositoryFs.Stat("somefile")
+ c.Assert(err, IsNil)
+
+ file, err := repositoryFs.Open("somefile")
+ c.Assert(err, IsNil)
+ err = file.Close()
+ c.Assert(err, IsNil)
+
+ file, err = repositoryFs.OpenFile("somefile", os.O_RDONLY, 0666)
+ c.Assert(err, IsNil)
+ err = file.Close()
+ c.Assert(err, IsNil)
+
+ file, err = repositoryFs.Create("somefile2")
+ c.Assert(err, IsNil)
+ err = file.Close()
+ c.Assert(err, IsNil)
+ _, err = repositoryFs.Stat("somefile2")
+ c.Assert(err, IsNil)
+ err = repositoryFs.Rename("somefile2", "newfile")
+ c.Assert(err, IsNil)
+
+ tempDir, err := repositoryFs.TempFile("tmp", "myprefix")
+ c.Assert(err, IsNil)
+ c.Assert(repositoryFs.Join(repositoryFs.Root(), "tmp", tempDir.Name()), Equals, repositoryFs.Join(dotGitFs.Root(), "tmp", tempDir.Name()))
+
+ err = repositoryFs.Symlink("newfile", "somelink")
+ c.Assert(err, IsNil)
+
+ _, err = repositoryFs.Lstat("somelink")
+ c.Assert(err, IsNil)
+
+ link, err := repositoryFs.Readlink("somelink")
+ c.Assert(err, IsNil)
+ c.Assert(link, Equals, "newfile")
+
+ err = repositoryFs.Remove("somelink")
+ c.Assert(err, IsNil)
+
+ _, err = repositoryFs.Stat("somelink")
+ c.Assert(os.IsNotExist(err), Equals, true)
+
+ dirs := []string{objectsPath, refsPath, packedRefsPath, configPath, branchesPath, hooksPath, infoPath, remotesPath, logsPath, shallowPath, worktreesPath}
+ for _, dir := range dirs {
+ err := repositoryFs.MkdirAll(dir, 0777)
+ c.Assert(err, IsNil)
+ _, err = commonDotGitFs.Stat(dir)
+ c.Assert(err, IsNil)
+ _, err = dotGitFs.Stat(dir)
+ c.Assert(os.IsNotExist(err), Equals, true)
+ }
+
+ exceptionsPaths := []string{repositoryFs.Join(logsPath, "HEAD"), repositoryFs.Join(refsPath, "bisect"), repositoryFs.Join(refsPath, "rewritten"), repositoryFs.Join(refsPath, "worktree")}
+ for _, path := range exceptionsPaths {
+ _, err := repositoryFs.Create(path)
+ c.Assert(err, IsNil)
+ _, err = commonDotGitFs.Stat(path)
+ c.Assert(os.IsNotExist(err), Equals, true)
+ _, err = dotGitFs.Stat(path)
+ c.Assert(err, IsNil)
+ }
+
+ err = repositoryFs.MkdirAll("refs/heads", 0777)
+ c.Assert(err, IsNil)
+ _, err = commonDotGitFs.Stat("refs/heads")
+ c.Assert(err, IsNil)
+ _, err = dotGitFs.Stat("refs/heads")
+ c.Assert(os.IsNotExist(err), Equals, true)
+
+ err = repositoryFs.MkdirAll("objects/pack", 0777)
+ c.Assert(err, IsNil)
+ _, err = commonDotGitFs.Stat("objects/pack")
+ c.Assert(err, IsNil)
+ _, err = dotGitFs.Stat("objects/pack")
+ c.Assert(os.IsNotExist(err), Equals, true)
+
+ err = repositoryFs.MkdirAll("a/b/c", 0777)
+ c.Assert(err, IsNil)
+ _, err = commonDotGitFs.Stat("a/b/c")
+ c.Assert(os.IsNotExist(err), Equals, true)
+ _, err = dotGitFs.Stat("a/b/c")
+ c.Assert(err, IsNil)
+}
diff --git a/storage/filesystem/dotgit/writers_test.go b/storage/filesystem/dotgit/writers_test.go
index 246d310..7147aec 100644
--- a/storage/filesystem/dotgit/writers_test.go
+++ b/storage/filesystem/dotgit/writers_test.go
@@ -13,8 +13,8 @@ import (
"github.com/go-git/go-git/v5/plumbing/format/packfile"
"github.com/go-git/go-billy/v5/osfs"
+ fixtures "github.com/go-git/go-git-fixtures/v4"
. "gopkg.in/check.v1"
- "github.com/go-git/go-git-fixtures/v4"
)
func (s *SuiteDotGit) TestNewObjectPack(c *C) {
diff --git a/storage/memory/storage.go b/storage/memory/storage.go
index fdf8fcf..a8e5669 100644
--- a/storage/memory/storage.go
+++ b/storage/memory/storage.go
@@ -195,10 +195,10 @@ func (o *ObjectStorage) DeleteOldObjectPackAndIndex(plumbing.Hash, time.Time) er
var errNotSupported = fmt.Errorf("Not supported")
-func (s *ObjectStorage) LooseObjectTime(hash plumbing.Hash) (time.Time, error) {
+func (o *ObjectStorage) LooseObjectTime(hash plumbing.Hash) (time.Time, error) {
return time.Time{}, errNotSupported
}
-func (s *ObjectStorage) DeleteLooseObject(plumbing.Hash) error {
+func (o *ObjectStorage) DeleteLooseObject(plumbing.Hash) error {
return errNotSupported
}
diff --git a/storage/transactional/config_test.go b/storage/transactional/config_test.go
index ec7ae89..1f3a572 100644
--- a/storage/transactional/config_test.go
+++ b/storage/transactional/config_test.go
@@ -1,9 +1,10 @@
package transactional
import (
- . "gopkg.in/check.v1"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/storage/memory"
+
+ . "gopkg.in/check.v1"
)
var _ = Suite(&ConfigSuite{})
diff --git a/storage/transactional/index_test.go b/storage/transactional/index_test.go
index 88fa1f5..0028c0e 100644
--- a/storage/transactional/index_test.go
+++ b/storage/transactional/index_test.go
@@ -1,9 +1,10 @@
package transactional
import (
- . "gopkg.in/check.v1"
"github.com/go-git/go-git/v5/plumbing/format/index"
"github.com/go-git/go-git/v5/storage/memory"
+
+ . "gopkg.in/check.v1"
)
var _ = Suite(&IndexSuite{})
diff --git a/storage/transactional/object_test.go b/storage/transactional/object_test.go
index e634409..df277c4 100644
--- a/storage/transactional/object_test.go
+++ b/storage/transactional/object_test.go
@@ -1,9 +1,10 @@
package transactional
import (
- . "gopkg.in/check.v1"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
+
+ . "gopkg.in/check.v1"
)
var _ = Suite(&ObjectSuite{})
diff --git a/storage/transactional/reference_test.go b/storage/transactional/reference_test.go
index a6bd1ce..05a4fcf 100644
--- a/storage/transactional/reference_test.go
+++ b/storage/transactional/reference_test.go
@@ -1,9 +1,10 @@
package transactional
import (
- . "gopkg.in/check.v1"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
+
+ . "gopkg.in/check.v1"
)
var _ = Suite(&ReferenceSuite{})
diff --git a/storage/transactional/shallow_test.go b/storage/transactional/shallow_test.go
index 1209fe6..15d423c 100644
--- a/storage/transactional/shallow_test.go
+++ b/storage/transactional/shallow_test.go
@@ -1,9 +1,10 @@
package transactional
import (
- . "gopkg.in/check.v1"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
+
+ . "gopkg.in/check.v1"
)
var _ = Suite(&ShallowSuite{})