aboutsummaryrefslogtreecommitdiffstats
path: root/worktree_test.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2018-02-28 19:18:36 +0100
committerGitHub <noreply@github.com>2018-02-28 19:18:36 +0100
commit4397264e391b45a8eac147cc7373189d55c640cc (patch)
tree6198853fc7ca31ac16a0cbfa683828568452a6dd /worktree_test.go
parentdefd0b861ca79845c8f06f7c826c769012404bbd (diff)
parent6d23b50e27312f3ba3e839153c2c0db5237c827d (diff)
downloadgo-git-4397264e391b45a8eac147cc7373189d55c640cc.tar.gz
Merge pull request #739 from mcuadros/add
new methods Worktree.[AddGlob|RemoveBlob] and recursive Worktree.[Add|Remove]
Diffstat (limited to 'worktree_test.go')
-rw-r--r--worktree_test.go271
1 files changed, 271 insertions, 0 deletions
diff --git a/worktree_test.go b/worktree_test.go
index e51e89a..cb2e5e2 100644
--- a/worktree_test.go
+++ b/worktree_test.go
@@ -1115,6 +1115,40 @@ func (s *WorktreeSuite) TestAddUnmodified(c *C) {
c.Assert(err, IsNil)
}
+func (s *WorktreeSuite) TestAddRemoved(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ Filesystem: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{Force: true})
+ c.Assert(err, IsNil)
+
+ idx, err := w.r.Storer.Index()
+ c.Assert(err, IsNil)
+ c.Assert(idx.Entries, HasLen, 9)
+
+ err = w.Filesystem.Remove("LICENSE")
+ c.Assert(err, IsNil)
+
+ hash, err := w.Add("LICENSE")
+ c.Assert(err, IsNil)
+ c.Assert(hash.String(), Equals, "c192bd6a24ea1ab01d78686e417c8bdc7c3d197f")
+
+ e, err := idx.Entry("LICENSE")
+ c.Assert(err, IsNil)
+ c.Assert(e.Hash, Equals, hash)
+ c.Assert(e.Mode, Equals, filemode.Regular)
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status, HasLen, 1)
+
+ file := status.File("LICENSE")
+ c.Assert(file.Staging, Equals, Deleted)
+}
+
func (s *WorktreeSuite) TestAddSymlink(c *C) {
dir, err := ioutil.TempDir("", "checkout")
c.Assert(err, IsNil)
@@ -1141,7 +1175,124 @@ func (s *WorktreeSuite) TestAddSymlink(c *C) {
c.Assert(err, IsNil)
c.Assert(obj, NotNil)
c.Assert(obj.Size(), Equals, int64(3))
+}
+
+func (s *WorktreeSuite) TestAddDirectory(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ Filesystem: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{Force: true})
+ c.Assert(err, IsNil)
+
+ idx, err := w.r.Storer.Index()
+ c.Assert(err, IsNil)
+ c.Assert(idx.Entries, HasLen, 9)
+
+ err = util.WriteFile(w.Filesystem, "qux/foo", []byte("FOO"), 0755)
+ c.Assert(err, IsNil)
+ err = util.WriteFile(w.Filesystem, "qux/baz/bar", []byte("BAR"), 0755)
+ c.Assert(err, IsNil)
+
+ h, err := w.Add("qux")
+ c.Assert(err, IsNil)
+ c.Assert(h.IsZero(), Equals, true)
+
+ idx, err = w.r.Storer.Index()
+ c.Assert(err, IsNil)
+ c.Assert(idx.Entries, HasLen, 11)
+
+ e, err := idx.Entry("qux/foo")
+ c.Assert(err, IsNil)
+ c.Assert(e.Mode, Equals, filemode.Executable)
+
+ e, err = idx.Entry("qux/baz/bar")
+ c.Assert(err, IsNil)
+ c.Assert(e.Mode, Equals, filemode.Executable)
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status, HasLen, 2)
+
+ file := status.File("qux/foo")
+ c.Assert(file.Staging, Equals, Added)
+ c.Assert(file.Worktree, Equals, Unmodified)
+ file = status.File("qux/baz/bar")
+ c.Assert(file.Staging, Equals, Added)
+ c.Assert(file.Worktree, Equals, Unmodified)
+}
+
+func (s *WorktreeSuite) TestAddDirectoryErrorNotFound(c *C) {
+ r, _ := Init(memory.NewStorage(), memfs.New())
+ w, _ := r.Worktree()
+
+ h, err := w.Add("foo")
+ c.Assert(err, NotNil)
+ c.Assert(h.IsZero(), Equals, true)
+}
+
+func (s *WorktreeSuite) TestAddGlob(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ Filesystem: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{Force: true})
+ c.Assert(err, IsNil)
+
+ idx, err := w.r.Storer.Index()
+ c.Assert(err, IsNil)
+ c.Assert(idx.Entries, HasLen, 9)
+
+ err = util.WriteFile(w.Filesystem, "qux/qux", []byte("QUX"), 0755)
+ c.Assert(err, IsNil)
+ err = util.WriteFile(w.Filesystem, "qux/baz", []byte("BAZ"), 0755)
+ c.Assert(err, IsNil)
+ err = util.WriteFile(w.Filesystem, "qux/bar/baz", []byte("BAZ"), 0755)
+ c.Assert(err, IsNil)
+
+ err = w.AddGlob(w.Filesystem.Join("qux", "b*"))
+ c.Assert(err, IsNil)
+
+ idx, err = w.r.Storer.Index()
+ c.Assert(err, IsNil)
+ c.Assert(idx.Entries, HasLen, 11)
+
+ e, err := idx.Entry("qux/baz")
+ c.Assert(err, IsNil)
+ c.Assert(e.Mode, Equals, filemode.Executable)
+
+ e, err = idx.Entry("qux/bar/baz")
+ c.Assert(err, IsNil)
+ c.Assert(e.Mode, Equals, filemode.Executable)
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status, HasLen, 3)
+
+ file := status.File("qux/qux")
+ c.Assert(file.Staging, Equals, Untracked)
+ c.Assert(file.Worktree, Equals, Untracked)
+
+ file = status.File("qux/baz")
+ c.Assert(file.Staging, Equals, Added)
+ c.Assert(file.Worktree, Equals, Unmodified)
+
+ file = status.File("qux/bar/baz")
+ c.Assert(file.Staging, Equals, Added)
+ c.Assert(file.Worktree, Equals, Unmodified)
+}
+
+func (s *WorktreeSuite) TestAddGlobErrorNoMatches(c *C) {
+ r, _ := Init(memory.NewStorage(), memfs.New())
+ w, _ := r.Worktree()
+
+ err := w.AddGlob("foo")
+ c.Assert(err, Equals, ErrGlobNoMatches)
}
func (s *WorktreeSuite) TestRemove(c *C) {
@@ -1179,6 +1330,58 @@ func (s *WorktreeSuite) TestRemoveNotExistentEntry(c *C) {
c.Assert(err, NotNil)
}
+func (s *WorktreeSuite) TestRemoveDirectory(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ Filesystem: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{Force: true})
+ c.Assert(err, IsNil)
+
+ hash, err := w.Remove("json")
+ c.Assert(hash.IsZero(), Equals, true)
+ c.Assert(err, IsNil)
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status, HasLen, 2)
+ c.Assert(status.File("json/long.json").Staging, Equals, Deleted)
+ c.Assert(status.File("json/short.json").Staging, Equals, Deleted)
+
+ _, err = w.Filesystem.Stat("json")
+ c.Assert(os.IsNotExist(err), Equals, true)
+}
+
+func (s *WorktreeSuite) TestRemoveDirectoryUntracked(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ Filesystem: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{Force: true})
+ c.Assert(err, IsNil)
+
+ err = util.WriteFile(w.Filesystem, "json/foo", []byte("FOO"), 0755)
+ c.Assert(err, IsNil)
+
+ hash, err := w.Remove("json")
+ c.Assert(hash.IsZero(), Equals, true)
+ c.Assert(err, IsNil)
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status, HasLen, 3)
+ c.Assert(status.File("json/long.json").Staging, Equals, Deleted)
+ c.Assert(status.File("json/short.json").Staging, Equals, Deleted)
+ c.Assert(status.File("json/foo").Staging, Equals, Untracked)
+
+ _, err = w.Filesystem.Stat("json")
+ c.Assert(err, IsNil)
+}
+
func (s *WorktreeSuite) TestRemoveDeletedFromWorktree(c *C) {
fs := memfs.New()
w := &Worktree{
@@ -1202,6 +1405,74 @@ func (s *WorktreeSuite) TestRemoveDeletedFromWorktree(c *C) {
c.Assert(status.File("LICENSE").Staging, Equals, Deleted)
}
+func (s *WorktreeSuite) TestRemoveGlob(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ Filesystem: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{Force: true})
+ c.Assert(err, IsNil)
+
+ err = w.RemoveGlob(w.Filesystem.Join("json", "l*"))
+ c.Assert(err, IsNil)
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status, HasLen, 1)
+ c.Assert(status.File("json/long.json").Staging, Equals, Deleted)
+}
+
+func (s *WorktreeSuite) TestRemoveGlobDirectory(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ Filesystem: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{Force: true})
+ c.Assert(err, IsNil)
+
+ err = w.RemoveGlob("js*")
+ c.Assert(err, IsNil)
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status, HasLen, 2)
+ c.Assert(status.File("json/short.json").Staging, Equals, Deleted)
+ c.Assert(status.File("json/long.json").Staging, Equals, Deleted)
+
+ _, err = w.Filesystem.Stat("json")
+ c.Assert(os.IsNotExist(err), Equals, true)
+}
+
+func (s *WorktreeSuite) TestRemoveGlobDirectoryDeleted(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ Filesystem: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{Force: true})
+ c.Assert(err, IsNil)
+
+ err = fs.Remove("json/short.json")
+ c.Assert(err, IsNil)
+
+ err = util.WriteFile(w.Filesystem, "json/foo", []byte("FOO"), 0755)
+ c.Assert(err, IsNil)
+
+ err = w.RemoveGlob("js*")
+ c.Assert(err, IsNil)
+
+ status, err := w.Status()
+ c.Assert(err, IsNil)
+ c.Assert(status, HasLen, 3)
+ c.Assert(status.File("json/short.json").Staging, Equals, Deleted)
+ c.Assert(status.File("json/long.json").Staging, Equals, Deleted)
+}
+
func (s *WorktreeSuite) TestMove(c *C) {
fs := memfs.New()
w := &Worktree{