fork of go-git with some jj specific features
at v4.3.1 4.1 kB view raw
1package git 2 3import ( 4 "time" 5 6 "gopkg.in/src-d/go-git.v4/plumbing" 7 "gopkg.in/src-d/go-git.v4/plumbing/object" 8 "gopkg.in/src-d/go-git.v4/plumbing/storer" 9 "gopkg.in/src-d/go-git.v4/storage/memory" 10 11 . "gopkg.in/check.v1" 12 "gopkg.in/src-d/go-billy.v4/memfs" 13 "gopkg.in/src-d/go-billy.v4/util" 14) 15 16func (s *WorktreeSuite) TestCommitInvalidOptions(c *C) { 17 r, err := Init(memory.NewStorage(), memfs.New()) 18 c.Assert(err, IsNil) 19 20 w, err := r.Worktree() 21 c.Assert(err, IsNil) 22 23 hash, err := w.Commit("", &CommitOptions{}) 24 c.Assert(err, Equals, ErrMissingAuthor) 25 c.Assert(hash.IsZero(), Equals, true) 26} 27 28func (s *WorktreeSuite) TestCommitInitial(c *C) { 29 expected := plumbing.NewHash("98c4ac7c29c913f7461eae06e024dc18e80d23a4") 30 31 fs := memfs.New() 32 storage := memory.NewStorage() 33 34 r, err := Init(storage, fs) 35 c.Assert(err, IsNil) 36 37 w, err := r.Worktree() 38 c.Assert(err, IsNil) 39 40 util.WriteFile(fs, "foo", []byte("foo"), 0644) 41 42 _, err = w.Add("foo") 43 c.Assert(err, IsNil) 44 45 hash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature()}) 46 c.Assert(hash, Equals, expected) 47 c.Assert(err, IsNil) 48 49 assertStorageStatus(c, r, 1, 1, 1, expected) 50} 51 52func (s *WorktreeSuite) TestCommitParent(c *C) { 53 expected := plumbing.NewHash("ef3ca05477530b37f48564be33ddd48063fc7a22") 54 55 fs := memfs.New() 56 w := &Worktree{ 57 r: s.Repository, 58 Filesystem: fs, 59 } 60 61 err := w.Checkout(&CheckoutOptions{}) 62 c.Assert(err, IsNil) 63 64 util.WriteFile(fs, "foo", []byte("foo"), 0644) 65 66 _, err = w.Add("foo") 67 c.Assert(err, IsNil) 68 69 hash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature()}) 70 c.Assert(hash, Equals, expected) 71 c.Assert(err, IsNil) 72 73 assertStorageStatus(c, s.Repository, 13, 11, 10, expected) 74} 75 76func (s *WorktreeSuite) TestCommitAll(c *C) { 77 expected := plumbing.NewHash("aede6f8c9c1c7ec9ca8d287c64b8ed151276fa28") 78 79 fs := memfs.New() 80 w := &Worktree{ 81 r: s.Repository, 82 Filesystem: fs, 83 } 84 85 err := w.Checkout(&CheckoutOptions{}) 86 c.Assert(err, IsNil) 87 88 util.WriteFile(fs, "LICENSE", []byte("foo"), 0644) 89 util.WriteFile(fs, "foo", []byte("foo"), 0644) 90 91 hash, err := w.Commit("foo\n", &CommitOptions{ 92 All: true, 93 Author: defaultSignature(), 94 }) 95 96 c.Assert(hash, Equals, expected) 97 c.Assert(err, IsNil) 98 99 assertStorageStatus(c, s.Repository, 13, 11, 10, expected) 100} 101 102func (s *WorktreeSuite) TestRemoveAndCommitAll(c *C) { 103 expected := plumbing.NewHash("907cd576c6ced2ecd3dab34a72bf9cf65944b9a9") 104 105 fs := memfs.New() 106 w := &Worktree{ 107 r: s.Repository, 108 Filesystem: fs, 109 } 110 111 err := w.Checkout(&CheckoutOptions{}) 112 c.Assert(err, IsNil) 113 114 util.WriteFile(fs, "foo", []byte("foo"), 0644) 115 _, err = w.Add("foo") 116 c.Assert(err, IsNil) 117 118 _, errFirst := w.Commit("Add in Repo\n", &CommitOptions{ 119 Author: defaultSignature(), 120 }) 121 c.Assert(errFirst, IsNil) 122 123 errRemove := fs.Remove("foo") 124 c.Assert(errRemove, IsNil) 125 126 hash, errSecond := w.Commit("Remove foo\n", &CommitOptions{ 127 All: true, 128 Author: defaultSignature(), 129 }) 130 c.Assert(errSecond, IsNil) 131 132 c.Assert(hash, Equals, expected) 133 c.Assert(err, IsNil) 134 135 assertStorageStatus(c, s.Repository, 13, 11, 11, expected) 136} 137 138func assertStorageStatus( 139 c *C, r *Repository, 140 treesCount, blobCount, commitCount int, head plumbing.Hash, 141) { 142 trees, err := r.Storer.IterEncodedObjects(plumbing.TreeObject) 143 c.Assert(err, IsNil) 144 blobs, err := r.Storer.IterEncodedObjects(plumbing.BlobObject) 145 c.Assert(err, IsNil) 146 commits, err := r.Storer.IterEncodedObjects(plumbing.CommitObject) 147 c.Assert(err, IsNil) 148 149 c.Assert(lenIterEncodedObjects(trees), Equals, treesCount) 150 c.Assert(lenIterEncodedObjects(blobs), Equals, blobCount) 151 c.Assert(lenIterEncodedObjects(commits), Equals, commitCount) 152 153 ref, err := r.Head() 154 c.Assert(err, IsNil) 155 c.Assert(ref.Hash(), Equals, head) 156} 157 158func lenIterEncodedObjects(iter storer.EncodedObjectIter) int { 159 count := 0 160 iter.ForEach(func(plumbing.EncodedObject) error { 161 count++ 162 return nil 163 }) 164 165 return count 166} 167 168func defaultSignature() *object.Signature { 169 when, _ := time.Parse(object.DateFormat, "Thu May 04 00:03:43 2017 +0200") 170 return &object.Signature{ 171 Name: "foo", 172 Email: "foo@foo.foo", 173 When: when, 174 } 175}