aboutsummaryrefslogtreecommitdiffstats
path: root/worktree_test.go
diff options
context:
space:
mode:
authorTimofey Kirillov <timofey.kirillov@flant.com>2019-03-28 20:32:01 +0000
committerTimofey Kirillov <timofey.kirillov@flant.com>2020-06-15 17:16:42 +0300
commit63c42e530e9ffdf070f74a2674aa1a1fc24703a8 (patch)
tree41daf4cb42a97aa209f198e0b5299e048957d80f /worktree_test.go
parent5cafe4097c72c0673255fdf8cac44fc513c6042c (diff)
downloadgo-git-63c42e530e9ffdf070f74a2674aa1a1fc24703a8.tar.gz
Support `.git/commondir` repository layout
Git creates `.git/commondir` when there are custom worktrees (see "git worktree add" related commands). `.git/commondir` in such case contains a link to another dot-git repository tree, which could contain some folders like: - objects; - config; - refs; - etc. In this PR a new dotgit.RepositoryFilesystem struct is defined, which is billy.Filesystem interface compatible object-wrapper, that can handle commondir and dispatch all operations to the correct file path. `git.PlainOpen` remain unchanged, but `git.PlainOpenWithOptions` has a new option: `PlainOpenOptions.EnableDotGitCommonDir=true|false` (which is false by default). When `EnableDotGitCommonDir=true` repository-open procedure will read `.git/commondir` (if it exists) and then create dotgit.RepositoryFilesystem object initialized with 2 filesystems. This object then passed into storage and then into dotgit.DotGit as `billy.Filesystem` interface. This object will catch all filesystem operations and dispatch to the correct repository-filesystem (dot-git or common-dot-git) according to the rules described in the doc: https://git-scm.com/docs/gitrepository-layout#Documentation/gitrepository-layout.txt. EnableDotGitCommonDir option will only work with the filesystem-backed storage. Also worktree_test.go has been adopted from an older, already existing existing PR: https://github.com/src-d/go-git/pull/1098. This PR needs new fixtures added in the following PR: https://github.com/go-git/go-git-fixtures/pull/1.
Diffstat (limited to 'worktree_test.go')
-rw-r--r--worktree_test.go75
1 files changed, 75 insertions, 0 deletions
diff --git a/worktree_test.go b/worktree_test.go
index 72bcbd9..c808ebd 100644
--- a/worktree_test.go
+++ b/worktree_test.go
@@ -2052,3 +2052,78 @@ func (s *WorktreeSuite) TestAddAndCommit(c *C) {
})
c.Assert(err, IsNil)
}
+
+func (s *WorktreeSuite) TestLinkedWorktree(c *C) {
+ fs := fixtures.ByTag("linked-worktree").One().Worktree()
+
+ // Open main repo.
+ {
+ fs, err := fs.Chroot("main")
+ c.Assert(err, IsNil)
+ repo, err := PlainOpenWithOptions(fs.Root(), &PlainOpenOptions{EnableDotGitCommonDir: true})
+ c.Assert(err, IsNil)
+
+ wt, err := repo.Worktree()
+ c.Assert(err, IsNil)
+
+ status, err := wt.Status()
+ c.Assert(err, IsNil)
+ c.Assert(len(status), Equals, 2) // 2 files
+
+ head, err := repo.Head()
+ c.Assert(err, IsNil)
+ c.Assert(string(head.Name()), Equals, "refs/heads/master")
+ }
+
+ // Open linked-worktree #1.
+ {
+ fs, err := fs.Chroot("linked-worktree-1")
+ c.Assert(err, IsNil)
+ repo, err := PlainOpenWithOptions(fs.Root(), &PlainOpenOptions{EnableDotGitCommonDir: true})
+ c.Assert(err, IsNil)
+
+ wt, err := repo.Worktree()
+ c.Assert(err, IsNil)
+
+ status, err := wt.Status()
+ c.Assert(err, IsNil)
+ c.Assert(len(status), Equals, 3) // 3 files
+
+ _, ok := status["linked-worktree-1-unique-file.txt"]
+ c.Assert(ok, Equals, true)
+
+ head, err := repo.Head()
+ c.Assert(err, IsNil)
+ c.Assert(string(head.Name()), Equals, "refs/heads/linked-worktree-1")
+ }
+
+ // Open linked-worktree #2.
+ {
+ fs, err := fs.Chroot("linked-worktree-2")
+ c.Assert(err, IsNil)
+ repo, err := PlainOpenWithOptions(fs.Root(), &PlainOpenOptions{EnableDotGitCommonDir: true})
+ c.Assert(err, IsNil)
+
+ wt, err := repo.Worktree()
+ c.Assert(err, IsNil)
+
+ status, err := wt.Status()
+ c.Assert(err, IsNil)
+ c.Assert(len(status), Equals, 3) // 3 files
+
+ _, ok := status["linked-worktree-2-unique-file.txt"]
+ c.Assert(ok, Equals, true)
+
+ head, err := repo.Head()
+ c.Assert(err, IsNil)
+ c.Assert(string(head.Name()), Equals, "refs/heads/branch-with-different-name")
+ }
+
+ // Open linked-worktree #2.
+ {
+ fs, err := fs.Chroot("linked-worktree-invalid-commondir")
+ c.Assert(err, IsNil)
+ _, err = PlainOpenWithOptions(fs.Root(), &PlainOpenOptions{EnableDotGitCommonDir: true})
+ c.Assert(err, Equals, ErrRepositoryIncomplete)
+ }
+}