aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/object
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-05-05 11:34:50 +0200
committerGitHub <noreply@github.com>2017-05-05 11:34:50 +0200
commitced875aec7bef9113e1c37b1b811a59e17dbd138 (patch)
tree3cf652b8cfa94e011d4d7b5addfd13945870cda8 /plumbing/object
parente80cdbabb92a1ec35ffad536f52d3ff04b548fd1 (diff)
parent3713157d189a109bdccdb055200defb17297b6de (diff)
downloadgo-git-ced875aec7bef9113e1c37b1b811a59e17dbd138.tar.gz
Merge pull request #375 from mcuadros/commit
worktree: Commit method implementation
Diffstat (limited to 'plumbing/object')
-rw-r--r--plumbing/object/blob.go7
-rw-r--r--plumbing/object/commit.go31
-rw-r--r--plumbing/object/commit_test.go14
-rw-r--r--plumbing/object/tag_test.go35
-rw-r--r--plumbing/object/tree.go14
5 files changed, 54 insertions, 47 deletions
diff --git a/plumbing/object/blob.go b/plumbing/object/blob.go
index 2771416..2608477 100644
--- a/plumbing/object/blob.go
+++ b/plumbing/object/blob.go
@@ -68,18 +68,23 @@ func (b *Blob) Decode(o plumbing.EncodedObject) error {
// Encode transforms a Blob into a plumbing.EncodedObject.
func (b *Blob) Encode(o plumbing.EncodedObject) error {
+ o.SetType(plumbing.BlobObject)
+
w, err := o.Writer()
if err != nil {
return err
}
+
defer ioutil.CheckClose(w, &err)
+
r, err := b.Reader()
if err != nil {
return err
}
+
defer ioutil.CheckClose(r, &err)
+
_, err = io.Copy(w, r)
- o.SetType(plumbing.BlobObject)
return err
}
diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go
index ffbb9f9..0a20ae6 100644
--- a/plumbing/object/commit.go
+++ b/plumbing/object/commit.go
@@ -30,10 +30,12 @@ type Commit struct {
Committer Signature
// Message is the commit message, contains arbitrary text.
Message string
+ // TreeHash is the hash of the root tree of the commit.
+ TreeHash plumbing.Hash
+ // ParentHashes are the hashes of the parent commits of the commit.
+ ParentHashes []plumbing.Hash
- tree plumbing.Hash
- parents []plumbing.Hash
- s storer.EncodedObjectStorer
+ s storer.EncodedObjectStorer
}
// GetCommit gets a commit from an object storer and decodes it.
@@ -59,19 +61,19 @@ func DecodeCommit(s storer.EncodedObjectStorer, o plumbing.EncodedObject) (*Comm
// Tree returns the Tree from the commit.
func (c *Commit) Tree() (*Tree, error) {
- return GetTree(c.s, c.tree)
+ return GetTree(c.s, c.TreeHash)
}
// Parents return a CommitIter to the parent Commits.
func (c *Commit) Parents() CommitIter {
return NewCommitIter(c.s,
- storer.NewEncodedObjectLookupIter(c.s, plumbing.CommitObject, c.parents),
+ storer.NewEncodedObjectLookupIter(c.s, plumbing.CommitObject, c.ParentHashes),
)
}
// NumParents returns the number of parents in a commit.
func (c *Commit) NumParents() int {
- return len(c.parents)
+ return len(c.ParentHashes)
}
// File returns the file with the specified "path" in the commit and a
@@ -144,9 +146,9 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) {
split := bytes.SplitN(line, []byte{' '}, 2)
switch string(split[0]) {
case "tree":
- c.tree = plumbing.NewHash(string(split[1]))
+ c.TreeHash = plumbing.NewHash(string(split[1]))
case "parent":
- c.parents = append(c.parents, plumbing.NewHash(string(split[1])))
+ c.ParentHashes = append(c.ParentHashes, plumbing.NewHash(string(split[1])))
case "author":
c.Author.Decode(split[1])
case "committer":
@@ -169,30 +171,39 @@ func (b *Commit) Encode(o plumbing.EncodedObject) error {
if err != nil {
return err
}
+
defer ioutil.CheckClose(w, &err)
- if _, err = fmt.Fprintf(w, "tree %s\n", b.tree.String()); err != nil {
+
+ if _, err = fmt.Fprintf(w, "tree %s\n", b.TreeHash.String()); err != nil {
return err
}
- for _, parent := range b.parents {
+
+ for _, parent := range b.ParentHashes {
if _, err = fmt.Fprintf(w, "parent %s\n", parent.String()); err != nil {
return err
}
}
+
if _, err = fmt.Fprint(w, "author "); err != nil {
return err
}
+
if err = b.Author.Encode(w); err != nil {
return err
}
+
if _, err = fmt.Fprint(w, "\ncommitter "); err != nil {
return err
}
+
if err = b.Committer.Encode(w); err != nil {
return err
}
+
if _, err = fmt.Fprintf(w, "\n\n%s", b.Message); err != nil {
return err
}
+
return err
}
diff --git a/plumbing/object/commit_test.go b/plumbing/object/commit_test.go
index c1f49db..87e80a5 100644
--- a/plumbing/object/commit_test.go
+++ b/plumbing/object/commit_test.go
@@ -71,18 +71,18 @@ func (s *SuiteCommit) TestCommitEncodeDecodeIdempotent(c *C) {
c.Assert(err, IsNil)
commits := []*Commit{
{
- Author: Signature{Name: "Foo", Email: "foo@example.local", When: ts},
- Committer: Signature{Name: "Bar", Email: "bar@example.local", When: ts},
- Message: "Message\n\nFoo\nBar\nWith trailing blank lines\n\n",
- tree: plumbing.NewHash("f000000000000000000000000000000000000001"),
- parents: []plumbing.Hash{plumbing.NewHash("f000000000000000000000000000000000000002")},
+ Author: Signature{Name: "Foo", Email: "foo@example.local", When: ts},
+ Committer: Signature{Name: "Bar", Email: "bar@example.local", When: ts},
+ Message: "Message\n\nFoo\nBar\nWith trailing blank lines\n\n",
+ TreeHash: plumbing.NewHash("f000000000000000000000000000000000000001"),
+ ParentHashes: []plumbing.Hash{plumbing.NewHash("f000000000000000000000000000000000000002")},
},
{
Author: Signature{Name: "Foo", Email: "foo@example.local", When: ts},
Committer: Signature{Name: "Bar", Email: "bar@example.local", When: ts},
Message: "Message\n\nFoo\nBar\nWith no trailing blank lines",
- tree: plumbing.NewHash("0000000000000000000000000000000000000003"),
- parents: []plumbing.Hash{
+ TreeHash: plumbing.NewHash("0000000000000000000000000000000000000003"),
+ ParentHashes: []plumbing.Hash{
plumbing.NewHash("f000000000000000000000000000000000000004"),
plumbing.NewHash("f000000000000000000000000000000000000005"),
plumbing.NewHash("f000000000000000000000000000000000000006"),
diff --git a/plumbing/object/tag_test.go b/plumbing/object/tag_test.go
index f121ce9..9f2d28c 100644
--- a/plumbing/object/tag_test.go
+++ b/plumbing/object/tag_test.go
@@ -234,33 +234,32 @@ func (s *TagSuite) TestString(c *C) {
)
}
-func (s *TagSuite) TestTagToTagString(c *C) {
+func (s *TagSuite) TestStringNonCommit(c *C) {
store := memory.NewStorage()
- tagOneHash := plumbing.NewHash("TAGONE")
- tagTwoHash := plumbing.NewHash("TAGTWO")
-
- tagOne := &Tag{
- Target: tagTwoHash,
- Hash: tagOneHash,
+ target := &Tag{
+ Target: plumbing.NewHash("TAGONE"),
Name: "TAG ONE",
+ Message: "tag one",
TargetType: plumbing.TagObject,
}
- tagTwo := &Tag{
- Target: tagOneHash,
- Hash: tagTwoHash,
+
+ targetObj := &plumbing.MemoryObject{}
+ target.Encode(targetObj)
+ store.SetEncodedObject(targetObj)
+
+ tag := &Tag{
+ Target: targetObj.Hash(),
Name: "TAG TWO",
+ Message: "tag two",
TargetType: plumbing.TagObject,
}
- oOne := &plumbing.MemoryObject{}
- tagOne.Encode(oOne)
- oTwo := &plumbing.MemoryObject{}
- tagTwo.Encode(oTwo)
- store.SetEncodedObject(oOne)
- store.SetEncodedObject(oTwo)
+ tagObj := &plumbing.MemoryObject{}
+ tag.Encode(tagObj)
+ store.SetEncodedObject(tagObj)
- tag, err := GetTag(store, tagOneHash)
+ tag, err := GetTag(store, tagObj.Hash())
c.Assert(err, IsNil)
c.Assert(tag.String(), Equals,
@@ -268,7 +267,7 @@ func (s *TagSuite) TestTagToTagString(c *C) {
"Tagger: <>\n"+
"Date: Mon Jan 01 00:00:00 0001 +0000\n"+
"\n"+
- "\n")
+ "tag two\n")
}
func (s *TagSuite) TestLongTagNameSerialization(c *C) {
diff --git a/plumbing/object/tree.go b/plumbing/object/tree.go
index d2265a8..25687b0 100644
--- a/plumbing/object/tree.go
+++ b/plumbing/object/tree.go
@@ -240,29 +240,21 @@ func (t *Tree) Encode(o plumbing.EncodedObject) error {
return err
}
- var size int
defer ioutil.CheckClose(w, &err)
for _, entry := range t.Entries {
- n, err := fmt.Fprintf(w, "%o %s", entry.Mode, entry.Name)
- if err != nil {
+ if _, err := fmt.Fprintf(w, "%o %s", entry.Mode, entry.Name); err != nil {
return err
}
- size += n
- n, err = w.Write([]byte{0x00})
- if err != nil {
+ if _, err = w.Write([]byte{0x00}); err != nil {
return err
}
- size += n
- n, err = w.Write([]byte(entry.Hash[:]))
- if err != nil {
+ if _, err = w.Write([]byte(entry.Hash[:])); err != nil {
return err
}
- size += n
}
- o.SetSize(int64(size))
return err
}