aboutsummaryrefslogtreecommitdiffstats
path: root/worktree_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'worktree_test.go')
-rw-r--r--worktree_test.go289
1 files changed, 266 insertions, 23 deletions
diff --git a/worktree_test.go b/worktree_test.go
index 5330c67..59197a6 100644
--- a/worktree_test.go
+++ b/worktree_test.go
@@ -3,9 +3,12 @@ package git
import (
"io/ioutil"
"os"
+ "path/filepath"
+ "gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
"gopkg.in/src-d/go-git.v4/plumbing/format/index"
+ "gopkg.in/src-d/go-git.v4/plumbing/object"
"github.com/src-d/go-git-fixtures"
. "gopkg.in/check.v1"
@@ -26,16 +29,13 @@ func (s *WorktreeSuite) SetUpTest(c *C) {
}
func (s *WorktreeSuite) TestCheckout(c *C) {
- h, err := s.Repository.Head()
- c.Assert(err, IsNil)
-
fs := memfs.New()
w := &Worktree{
r: s.Repository,
fs: fs,
}
- err = w.Checkout(h.Hash())
+ err := w.Checkout(&CheckoutOptions{})
c.Assert(err, IsNil)
entries, err := fs.ReadDir("/")
@@ -54,17 +54,14 @@ func (s *WorktreeSuite) TestCheckout(c *C) {
c.Assert(idx.Entries, HasLen, 9)
}
-func (s *WorktreeSuite) TestCheckoutIndexmemfs(c *C) {
- h, err := s.Repository.Head()
- c.Assert(err, IsNil)
-
+func (s *WorktreeSuite) TestCheckoutIndexMem(c *C) {
fs := memfs.New()
w := &Worktree{
r: s.Repository,
fs: fs,
}
- err = w.Checkout(h.Hash())
+ err := w.Checkout(&CheckoutOptions{})
c.Assert(err, IsNil)
idx, err := s.Repository.Storer.Index()
@@ -85,19 +82,16 @@ func (s *WorktreeSuite) TestCheckoutIndexmemfs(c *C) {
}
func (s *WorktreeSuite) TestCheckoutIndexOS(c *C) {
- h, err := s.Repository.Head()
- c.Assert(err, IsNil)
-
dir, err := ioutil.TempDir("", "checkout")
defer os.RemoveAll(dir)
- fs := osfs.New(dir)
+ fs := osfs.New(filepath.Join(dir, "worktree"))
w := &Worktree{
r: s.Repository,
fs: fs,
}
- err = w.Checkout(h.Hash())
+ err = w.Checkout(&CheckoutOptions{})
c.Assert(err, IsNil)
idx, err := s.Repository.Storer.Index()
@@ -116,41 +110,247 @@ func (s *WorktreeSuite) TestCheckoutIndexOS(c *C) {
c.Assert(idx.Entries[0].GID, Not(Equals), uint32(0))
}
+func (s *WorktreeSuite) TestCheckoutChange(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ fs: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{})
+ c.Assert(err, IsNil)
+ head, err := w.r.Head()
+ c.Assert(err, IsNil)
+ c.Assert(head.Name().String(), Equals, "refs/heads/master")
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status.IsClean(), Equals, true)
+
+ _, err = fs.Stat("README")
+ c.Assert(err, Equals, os.ErrNotExist)
+ _, err = fs.Stat("vendor")
+ c.Assert(err, Equals, nil)
+
+ err = w.Checkout(&CheckoutOptions{
+ Branch: "refs/heads/branch",
+ })
+ c.Assert(err, IsNil)
+
+ status, err = w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status.IsClean(), Equals, true)
+
+ _, err = fs.Stat("README")
+ c.Assert(err, Equals, nil)
+ _, err = fs.Stat("vendor")
+ c.Assert(err, Equals, os.ErrNotExist)
+
+ head, err = w.r.Head()
+ c.Assert(err, IsNil)
+ c.Assert(head.Name().String(), Equals, "refs/heads/branch")
+}
+
+func (s *WorktreeSuite) TestCheckoutTag(c *C) {
+ f := fixtures.ByTag("tags").One()
+
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.NewRepository(f),
+ fs: fs,
+ }
+
+ // we delete the index, since the fixture comes with a real index
+ err := w.r.Storer.SetIndex(&index.Index{Version: 2})
+ c.Assert(err, IsNil)
+
+ err = w.Checkout(&CheckoutOptions{})
+ c.Assert(err, IsNil)
+ head, err := w.r.Head()
+ c.Assert(err, IsNil)
+ c.Assert(head.Name().String(), Equals, "refs/heads/master")
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status.IsClean(), Equals, true)
+
+ err = w.Checkout(&CheckoutOptions{Branch: "refs/tags/lightweight-tag"})
+ c.Assert(err, IsNil)
+ head, err = w.r.Head()
+ c.Assert(err, IsNil)
+ c.Assert(head.Name().String(), Equals, "HEAD")
+ c.Assert(head.Hash().String(), Equals, "f7b877701fbf855b44c0a9e86f3fdce2c298b07f")
+
+ err = w.Checkout(&CheckoutOptions{Branch: "refs/tags/commit-tag"})
+ c.Assert(err, IsNil)
+ head, err = w.r.Head()
+ c.Assert(err, IsNil)
+ c.Assert(head.Name().String(), Equals, "HEAD")
+ c.Assert(head.Hash().String(), Equals, "f7b877701fbf855b44c0a9e86f3fdce2c298b07f")
+
+ err = w.Checkout(&CheckoutOptions{Branch: "refs/tags/tree-tag"})
+ c.Assert(err, NotNil)
+ head, err = w.r.Head()
+ c.Assert(err, IsNil)
+ c.Assert(head.Name().String(), Equals, "HEAD")
+}
+
+func (s *WorktreeSuite) TestCheckoutBisect(c *C) {
+ s.testCheckoutBisect(c, "https://github.com/src-d/go-git.git")
+}
+
+func (s *WorktreeSuite) TestCheckoutBisectSubmodules(c *C) {
+ c.Skip("not-submodule-support")
+ s.testCheckoutBisect(c, "https://github.com/git-fixtures/submodule.git")
+}
+
+// TestCheckoutBisect simulates a git bisect going through the git history and
+// checking every commit over the previous commit
+func (s *WorktreeSuite) testCheckoutBisect(c *C, url string) {
+ f := fixtures.ByURL(url).One()
+
+ w := &Worktree{
+ r: s.NewRepository(f),
+ fs: memfs.New(),
+ }
+
+ // we delete the index, since the fixture comes with a real index
+ err := w.r.Storer.SetIndex(&index.Index{Version: 2})
+ c.Assert(err, IsNil)
+
+ iter, err := w.r.Log(&LogOptions{})
+ c.Assert(err, IsNil)
+
+ iter.ForEach(func(commit *object.Commit) error {
+ err := w.Checkout(&CheckoutOptions{Hash: commit.Hash})
+ c.Assert(err, IsNil)
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status.IsClean(), Equals, true)
+
+ return nil
+ })
+}
+
func (s *WorktreeSuite) TestStatus(c *C) {
- h, err := s.Repository.Head()
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ fs: fs,
+ }
+
+ status, err := w.Status()
c.Assert(err, IsNil)
+ c.Assert(status.IsClean(), Equals, false)
+ c.Assert(status, HasLen, 9)
+}
+func (s *WorktreeSuite) TestReset(c *C) {
fs := memfs.New()
w := &Worktree{
r: s.Repository,
fs: fs,
}
- err = w.Checkout(h.Hash())
+ commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9")
+
+ err := w.Checkout(&CheckoutOptions{})
c.Assert(err, IsNil)
- status, err := w.Status()
+ branch, err := w.r.Reference(plumbing.Master, false)
c.Assert(err, IsNil)
+ c.Assert(branch.Hash(), Not(Equals), commit)
- c.Assert(status.IsClean(), Equals, true)
+ err = w.Reset(&ResetOptions{Commit: commit})
+ c.Assert(err, IsNil)
+
+ branch, err = w.r.Reference(plumbing.Master, false)
+ c.Assert(err, IsNil)
+ c.Assert(branch.Hash(), Equals, commit)
}
-func (s *WorktreeSuite) TestStatusModified(c *C) {
- c.Assert(s.Repository.Storer.SetIndex(&index.Index{Version: 2}), IsNil)
+func (s *WorktreeSuite) TestResetMerge(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ fs: fs,
+ }
+
+ commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9")
+
+ err := w.Checkout(&CheckoutOptions{})
+ c.Assert(err, IsNil)
+
+ f, err := fs.Create(".gitignore")
+ c.Assert(err, IsNil)
+ _, err = f.Write([]byte("foo"))
+ c.Assert(err, IsNil)
+ err = f.Close()
+ c.Assert(err, IsNil)
+
+ err = w.Reset(&ResetOptions{Mode: MergeReset, Commit: commit})
+ c.Assert(err, Equals, ErrUnstaggedChanges)
- h, err := s.Repository.Head()
+ branch, err := w.r.Reference(plumbing.Master, false)
c.Assert(err, IsNil)
+ c.Assert(branch.Hash(), Not(Equals), commit)
+}
+func (s *WorktreeSuite) TestResetHard(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ fs: fs,
+ }
+
+ commit := plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9")
+
+ err := w.Checkout(&CheckoutOptions{})
+ c.Assert(err, IsNil)
+
+ f, err := fs.Create(".gitignore")
+ c.Assert(err, IsNil)
+ _, err = f.Write([]byte("foo"))
+ c.Assert(err, IsNil)
+ err = f.Close()
+ c.Assert(err, IsNil)
+
+ err = w.Reset(&ResetOptions{Mode: HardReset, Commit: commit})
+ c.Assert(err, IsNil)
+
+ branch, err := w.r.Reference(plumbing.Master, false)
+ c.Assert(err, IsNil)
+ c.Assert(branch.Hash(), Equals, commit)
+}
+
+func (s *WorktreeSuite) TestStatusAfterCheckout(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ fs: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{Force: true})
+ c.Assert(err, IsNil)
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status.IsClean(), Equals, true)
+
+}
+
+func (s *WorktreeSuite) TestStatusModified(c *C) {
dir, err := ioutil.TempDir("", "status")
defer os.RemoveAll(dir)
- fs := osfs.New(dir)
+ fs := osfs.New(filepath.Join(dir, "worktree"))
w := &Worktree{
r: s.Repository,
fs: fs,
}
- err = w.Checkout(h.Hash())
+ err = w.Checkout(&CheckoutOptions{})
c.Assert(err, IsNil)
f, err := fs.Create(".gitignore")
@@ -163,6 +363,49 @@ func (s *WorktreeSuite) TestStatusModified(c *C) {
status, err := w.Status()
c.Assert(err, IsNil)
c.Assert(status.IsClean(), Equals, false)
+ c.Assert(status.File(".gitignore").Worktree, Equals, Modified)
+}
+
+func (s *WorktreeSuite) TestStatusUntracked(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ fs: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{Force: true})
+ c.Assert(err, IsNil)
+
+ f, err := w.fs.Create("foo")
+ c.Assert(err, IsNil)
+ c.Assert(f.Close(), IsNil)
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status.File("foo").Staging, Equals, Untracked)
+ c.Assert(status.File("foo").Worktree, Equals, Untracked)
+}
+
+func (s *WorktreeSuite) TestStatusDeleted(c *C) {
+ dir, err := ioutil.TempDir("", "status")
+ defer os.RemoveAll(dir)
+
+ fs := osfs.New(filepath.Join(dir, "worktree"))
+ w := &Worktree{
+ r: s.Repository,
+ fs: fs,
+ }
+
+ err = w.Checkout(&CheckoutOptions{})
+ c.Assert(err, IsNil)
+
+ err = fs.Remove(".gitignore")
+ c.Assert(err, IsNil)
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status.IsClean(), Equals, false)
+ c.Assert(status.File(".gitignore").Worktree, Equals, Deleted)
}
func (s *WorktreeSuite) TestSubmodule(c *C) {