Skip to content

Commit 30ed74d

Browse files
authored
Merge pull request #29 from bk2204/mergetag-parsing
Mergetag parsing
2 parents 5f09182 + 5b81d5b commit 30ed74d

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

commit.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (c *Commit) Decode(hash hash.Hash, from io.Reader, size int64) (n int, err
107107
continue
108108
}
109109

110-
if fields := strings.Fields(text); !finishedHeaders {
110+
if fields := strings.Split(text, " "); !finishedHeaders {
111111
if len(fields) == 0 {
112112
// Executing in this block means that we got a
113113
// whitespace-only line, while parsing a header.
@@ -123,13 +123,13 @@ func (c *Commit) Decode(hash hash.Hash, from io.Reader, size int64) (n int, err
123123
case "tree":
124124
id, err := hex.DecodeString(fields[1])
125125
if err != nil {
126-
return n, err
126+
return n, fmt.Errorf("error parsing tree: %s", err)
127127
}
128128
c.TreeID = id
129129
case "parent":
130130
id, err := hex.DecodeString(fields[1])
131131
if err != nil {
132-
return n, err
132+
return n, fmt.Errorf("error parsing parent: %s", err)
133133
}
134134
c.ParentIDs = append(c.ParentIDs, id)
135135
case "author":

commit_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,65 @@ func TestCommitDecodingMultilineHeader(t *testing.T) {
238238
strings.Split(hdr.V, "\n"))
239239
}
240240

241+
func TestCommitDecodingMessageWithLineStartingWithTree(t *testing.T) {
242+
from := new(bytes.Buffer)
243+
244+
// The tricky part here that we're testing is the "tree support" in the
245+
// `mergetag` header, which we should not try to parse as a tree header.
246+
// Note also that this entry contains trailing whitespace which must not
247+
// be trimmed.
248+
fmt.Fprintf(from, `tree e8ad84c41c2acde27c77fa212b8865cd3acfe6fb
249+
parent b343c8beec664ef6f0e9964d3001c7c7966331ae
250+
parent 1e8a52e18cfb381bc9cc1f0b720540364d2a6edd
251+
author Pat Doe <[email protected]> 1337892984 -0700
252+
committer Pat Doe <[email protected]> 1337892984 -0700
253+
mergetag object 1e8a52e18cfb381bc9cc1f0b720540364d2a6edd
254+
type commit
255+
tag random
256+
tagger J. Roe <[email protected]> 1337889148 -0600
257+
258+
Random changes
259+
260+
This text contains some
261+
tree support code.
262+
-----BEGIN PGP SIGNATURE-----
263+
Version: GnuPG v1.4.11 (GNU/Linux)
264+
265+
Not a real signature
266+
-----END PGP SIGNATURE-----
267+
268+
Merge tag 'random' of git://git.example.ca/git/
269+
`)
270+
271+
flen := from.Len()
272+
273+
commit := new(Commit)
274+
n, err := commit.Decode(sha1.New(), from, int64(flen))
275+
276+
require.Nil(t, err)
277+
require.Equal(t, flen, n)
278+
require.Equal(t, commit.ExtraHeaders, []*ExtraHeader{
279+
{
280+
K: "mergetag",
281+
V: `object 1e8a52e18cfb381bc9cc1f0b720540364d2a6edd
282+
type commit
283+
tag random
284+
tagger J. Roe <[email protected]> 1337889148 -0600
285+
286+
Random changes
287+
288+
This text contains some
289+
tree support code.
290+
-----BEGIN PGP SIGNATURE-----
291+
Version: GnuPG v1.4.11 (GNU/Linux)
292+
293+
Not a real signature
294+
-----END PGP SIGNATURE-----`},
295+
},
296+
)
297+
require.Equal(t, commit.Message, "Merge tag 'random' of git://git.example.ca/git/")
298+
}
299+
241300
func assertLine(t *testing.T, buf *bytes.Buffer, wanted string, args ...interface{}) {
242301
got, err := buf.ReadString('\n')
243302
if err == io.EOF {

0 commit comments

Comments
 (0)