package git import ( "bytes" "strings" "time" "golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp/armor" "golang.org/x/crypto/openpgp/errors" "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing/object" "gopkg.in/src-d/go-git.v4/plumbing/storer" "gopkg.in/src-d/go-git.v4/storage/memory" . "gopkg.in/check.v1" "gopkg.in/src-d/go-billy.v4/memfs" "gopkg.in/src-d/go-billy.v4/util" ) func (s *WorktreeSuite) TestCommitInvalidOptions(c *C) { r, err := Init(memory.NewStorage(), memfs.New()) c.Assert(err, IsNil) w, err := r.Worktree() c.Assert(err, IsNil) hash, err := w.Commit("", &CommitOptions{}) c.Assert(err, Equals, ErrMissingAuthor) c.Assert(hash.IsZero(), Equals, true) } func (s *WorktreeSuite) TestCommitInitial(c *C) { expected := plumbing.NewHash("98c4ac7c29c913f7461eae06e024dc18e80d23a4") fs := memfs.New() storage := memory.NewStorage() r, err := Init(storage, fs) c.Assert(err, IsNil) w, err := r.Worktree() c.Assert(err, IsNil) util.WriteFile(fs, "foo", []byte("foo"), 0644) _, err = w.Add("foo") c.Assert(err, IsNil) hash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature()}) c.Assert(hash, Equals, expected) c.Assert(err, IsNil) assertStorageStatus(c, r, 1, 1, 1, expected) } func (s *WorktreeSuite) TestCommitParent(c *C) { expected := plumbing.NewHash("ef3ca05477530b37f48564be33ddd48063fc7a22") fs := memfs.New() w := &Worktree{ r: s.Repository, Filesystem: fs, } err := w.Checkout(&CheckoutOptions{}) c.Assert(err, IsNil) util.WriteFile(fs, "foo", []byte("foo"), 0644) _, err = w.Add("foo") c.Assert(err, IsNil) hash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature()}) c.Assert(hash, Equals, expected) c.Assert(err, IsNil) assertStorageStatus(c, s.Repository, 13, 11, 10, expected) } func (s *WorktreeSuite) TestCommitAll(c *C) { expected := plumbing.NewHash("aede6f8c9c1c7ec9ca8d287c64b8ed151276fa28") fs := memfs.New() w := &Worktree{ r: s.Repository, Filesystem: fs, } err := w.Checkout(&CheckoutOptions{}) c.Assert(err, IsNil) util.WriteFile(fs, "LICENSE", []byte("foo"), 0644) util.WriteFile(fs, "foo", []byte("foo"), 0644) hash, err := w.Commit("foo\n", &CommitOptions{ All: true, Author: defaultSignature(), }) c.Assert(hash, Equals, expected) c.Assert(err, IsNil) assertStorageStatus(c, s.Repository, 13, 11, 10, expected) } func (s *WorktreeSuite) TestRemoveAndCommitAll(c *C) { expected := plumbing.NewHash("907cd576c6ced2ecd3dab34a72bf9cf65944b9a9") fs := memfs.New() w := &Worktree{ r: s.Repository, Filesystem: fs, } err := w.Checkout(&CheckoutOptions{}) c.Assert(err, IsNil) util.WriteFile(fs, "foo", []byte("foo"), 0644) _, err = w.Add("foo") c.Assert(err, IsNil) _, errFirst := w.Commit("Add in Repo\n", &CommitOptions{ Author: defaultSignature(), }) c.Assert(errFirst, IsNil) errRemove := fs.Remove("foo") c.Assert(errRemove, IsNil) hash, errSecond := w.Commit("Remove foo\n", &CommitOptions{ All: true, Author: defaultSignature(), }) c.Assert(errSecond, IsNil) c.Assert(hash, Equals, expected) c.Assert(err, IsNil) assertStorageStatus(c, s.Repository, 13, 11, 11, expected) } func (s *WorktreeSuite) TestCommitSign(c *C) { fs := memfs.New() storage := memory.NewStorage() r, err := Init(storage, fs) c.Assert(err, IsNil) w, err := r.Worktree() c.Assert(err, IsNil) util.WriteFile(fs, "foo", []byte("foo"), 0644) _, err = w.Add("foo") c.Assert(err, IsNil) key := commitSignKey(c, true) hash, err := w.Commit("foo\n", &CommitOptions{Author: defaultSignature(), SignKey: key}) c.Assert(err, IsNil) // Verify the commit. pks := new(bytes.Buffer) pkw, err := armor.Encode(pks, openpgp.PublicKeyType, nil) c.Assert(err, IsNil) err = key.Serialize(pkw) c.Assert(err, IsNil) err = pkw.Close() c.Assert(err, IsNil) expectedCommit, err := r.CommitObject(hash) c.Assert(err, IsNil) actual, err := expectedCommit.Verify(pks.String()) c.Assert(err, IsNil) c.Assert(actual.PrimaryKey, DeepEquals, key.PrimaryKey) } func (s *WorktreeSuite) TestCommitSignBadKey(c *C) { fs := memfs.New() storage := memory.NewStorage() r, err := Init(storage, fs) c.Assert(err, IsNil) w, err := r.Worktree() c.Assert(err, IsNil) util.WriteFile(fs, "foo", []byte("foo"), 0644) _, err = w.Add("foo") c.Assert(err, IsNil) key := commitSignKey(c, false) _, err = w.Commit("foo\n", &CommitOptions{Author: defaultSignature(), SignKey: key}) c.Assert(err, Equals, errors.InvalidArgumentError("signing key is encrypted")) } func assertStorageStatus( c *C, r *Repository, treesCount, blobCount, commitCount int, head plumbing.Hash, ) { trees, err := r.Storer.IterEncodedObjects(plumbing.TreeObject) c.Assert(err, IsNil) blobs, err := r.Storer.IterEncodedObjects(plumbing.BlobObject) c.Assert(err, IsNil) commits, err := r.Storer.IterEncodedObjects(plumbing.CommitObject) c.Assert(err, IsNil) c.Assert(lenIterEncodedObjects(trees), Equals, treesCount) c.Assert(lenIterEncodedObjects(blobs), Equals, blobCount) c.Assert(lenIterEncodedObjects(commits), Equals, commitCount) ref, err := r.Head() c.Assert(err, IsNil) c.Assert(ref.Hash(), Equals, head) } func lenIterEncodedObjects(iter storer.EncodedObjectIter) int { count := 0 iter.ForEach(func(plumbing.EncodedObject) error { count++ return nil }) return count } func defaultSignature() *object.Signature { when, _ := time.Parse(object.DateFormat, "Thu May 04 00:03:43 2017 +0200") return &object.Signature{ Name: "foo", Email: "foo@foo.foo", When: when, } } func commitSignKey(c *C, decrypt bool) *openpgp.Entity { s := strings.NewReader(armoredKeyRing) es, err := openpgp.ReadArmoredKeyRing(s) c.Assert(err, IsNil) c.Assert(es, HasLen, 1) c.Assert(es[0].Identities, HasLen, 1) _, ok := es[0].Identities["foo bar "] c.Assert(ok, Equals, true) key := es[0] if decrypt { err = key.PrivateKey.Decrypt([]byte(keyPassphrase)) c.Assert(err, IsNil) } return key } const armoredKeyRing = ` -----BEGIN PGP PRIV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