Debian Codegraph @

commit_test.go

raw
1 package gitobj
2
3 import (
4 "bytes"
5 "crypto/sha1"
6 "encoding/hex"
7 "fmt"
8 "io"
9 "strings"
10 "testing"
11 "time"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 )
16
17 func TestCommitReturnsCorrectObjectType(t *testing.T) {
18 assert.Equal(t, CommitObjectType, new(Commit).Type())
19 }
20
21 func TestCommitEncoding(t *testing.T) {
22 author := &Signature{Name: "John Doe", Email: "[email protected]", When: time.Now()}
23 committer := &Signature{Name: "Jane Doe", Email: "[email protected]", When: time.Now()}
24
25 sig := "-----BEGIN PGP SIGNATURE-----\n<signature>\n-----END PGP SIGNATURE-----"
26
27 c := &Commit{
28 Author: author.String(),
29 Committer: committer.String(),
30 ParentIDs: [][]byte{
31 []byte("aaaaaaaaaaaaaaaaaaaa"), []byte("bbbbbbbbbbbbbbbbbbbb"),
32 },
33 TreeID: []byte("cccccccccccccccccccc"),
34 ExtraHeaders: []*ExtraHeader{
35 {"foo", "bar"},
36 {"gpgsig", sig},
37 },
38 Message: "initial commit",
39 }
40
41 buf := new(bytes.Buffer)
42
43 _, err := c.Encode(buf)
44 assert.Nil(t, err)
45
46 assertLine(t, buf, "tree 6363636363636363636363636363636363636363")
47 assertLine(t, buf, "parent 6161616161616161616161616161616161616161")
48 assertLine(t, buf, "parent 6262626262626262626262626262626262626262")
49 assertLine(t, buf, "author %s", author.String())
50 assertLine(t, buf, "committer %s", committer.String())
51 assertLine(t, buf, "foo bar")
52 assertLine(t, buf, "gpgsig -----BEGIN PGP SIGNATURE-----")
53 assertLine(t, buf, " <signature>")
54 assertLine(t, buf, " -----END PGP SIGNATURE-----")
55 assertLine(t, buf, "")
56 assertLine(t, buf, "initial commit")
57
58 assert.Equal(t, 0, buf.Len())
59 }
60
61 func TestCommitDecoding(t *testing.T) {
62 author := &Signature{Name: "John Doe", Email: "[email protected]", When: time.Now()}
63 committer := &Signature{Name: "Jane Doe", Email: "[email protected]", When: time.Now()}
64
65 p1 := []byte("aaaaaaaaaaaaaaaaaaaa")
66 p2 := []byte("bbbbbbbbbbbbbbbbbbbb")
67 treeId := []byte("cccccccccccccccccccc")
68
69 from := new(bytes.Buffer)
70 fmt.Fprintf(from, "author %s\n", author)
71 fmt.Fprintf(from, "committer %s\n", committer)
72 fmt.Fprintf(from, "parent %s\n", hex.EncodeToString(p1))
73 fmt.Fprintf(from, "parent %s\n", hex.EncodeToString(p2))
74 fmt.Fprintf(from, "foo bar\n")
75 fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
76 fmt.Fprintf(from, "\ninitial commit\n")
77
78 flen := from.Len()
79
80 commit := new(Commit)
81 n, err := commit.Decode(sha1.New(), from, int64(flen))
82
83 assert.Nil(t, err)
84 assert.Equal(t, flen, n)
85
86 assert.Equal(t, author.String(), commit.Author)
87 assert.Equal(t, committer.String(), commit.Committer)
88 assert.Equal(t, [][]byte{p1, p2}, commit.ParentIDs)
89 assert.Equal(t, 1, len(commit.ExtraHeaders))
90 assert.Equal(t, "foo", commit.ExtraHeaders[0].K)
91 assert.Equal(t, "bar", commit.ExtraHeaders[0].V)
92 assert.Equal(t, "initial commit", commit.Message)
93 }
94
95 func TestCommitDecodingWithEmptyName(t *testing.T) {
96 author := &Signature{Name: "", Email: "[email protected]", When: time.Now()}
97 committer := &Signature{Name: "", Email: "[email protected]", When: time.Now()}
98
99 treeId := []byte("cccccccccccccccccccc")
100
101 from := new(bytes.Buffer)
102
103 fmt.Fprintf(from, "author %s\n", author)
104 fmt.Fprintf(from, "committer %s\n", committer)
105 fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
106 fmt.Fprintf(from, "\ninitial commit\n")
107
108 flen := from.Len()
109
110 commit := new(Commit)
111 n, err := commit.Decode(sha1.New(), from, int64(flen))
112
113 assert.Nil(t, err)
114 assert.Equal(t, flen, n)
115
116 assert.Equal(t, author.String(), commit.Author)
117 assert.Equal(t, committer.String(), commit.Committer)
118 assert.Equal(t, "initial commit", commit.Message)
119 }
120
121 func TestCommitDecodingWithLargeCommitMessage(t *testing.T) {
122 message := "This message text is, with newline, exactly 64 characters long. "
123 // This message will be exactly 10 MiB in size when part of the commit.
124 longMessage := strings.Repeat(message, (10*1024*1024/64)-1)
125 longMessage += strings.TrimSpace(message)
126
127 author := &Signature{Name: "", Email: "[email protected]", When: time.Now()}
128 committer := &Signature{Name: "", Email: "[email protected]", When: time.Now()}
129
130 treeId := []byte("cccccccccccccccccccc")
131
132 from := new(bytes.Buffer)
133
134 fmt.Fprintf(from, "author %s\n", author)
135 fmt.Fprintf(from, "committer %s\n", committer)
136 fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
137 fmt.Fprintf(from, "\n%s\n", longMessage)
138
139 flen := from.Len()
140
141 commit := new(Commit)
142 n, err := commit.Decode(sha1.New(), from, int64(flen))
143
144 assert.Nil(t, err)
145 assert.Equal(t, flen, n)
146
147 assert.Equal(t, author.String(), commit.Author)
148 assert.Equal(t, committer.String(), commit.Committer)
149 assert.Equal(t, longMessage, commit.Message)
150 }
151
152 func TestCommitDecodingWithMessageKeywordPrefix(t *testing.T) {
153 author := &Signature{Name: "John Doe", Email: "[email protected]", When: time.Now()}
154 committer := &Signature{Name: "Jane Doe", Email: "[email protected]", When: time.Now()}
155
156 treeId := []byte("aaaaaaaaaaaaaaaaaaaa")
157 treeIdAscii := hex.EncodeToString(treeId)
158
159 from := new(bytes.Buffer)
160 fmt.Fprintf(from, "author %s\n", author)
161 fmt.Fprintf(from, "committer %s\n", committer)
162 fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
163 fmt.Fprintf(from, "\nfirst line\n\nsecond line\n")
164
165 flen := from.Len()
166
167 commit := new(Commit)
168 n, err := commit.Decode(sha1.New(), from, int64(flen))
169
170 assert.NoError(t, err)
171 assert.Equal(t, flen, n)
172
173 assert.Equal(t, author.String(), commit.Author)
174 assert.Equal(t, committer.String(), commit.Committer)
175 assert.Equal(t, treeIdAscii, hex.EncodeToString(commit.TreeID))
176 assert.Equal(t, "first line\n\nsecond line", commit.Message)
177 }
178
179 func TestCommitDecodingWithWhitespace(t *testing.T) {
180 author := &Signature{Name: "John Doe", Email: "[email protected]", When: time.Now()}
181 committer := &Signature{Name: "Jane Doe", Email: "[email protected]", When: time.Now()}
182
183 treeId := []byte("aaaaaaaaaaaaaaaaaaaa")
184 treeIdAscii := hex.EncodeToString(treeId)
185
186 from := new(bytes.Buffer)
187 fmt.Fprintf(from, "author %s\n", author)
188 fmt.Fprintf(from, "committer %s\n", committer)
189 fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
190 fmt.Fprintf(from, "\ntree <- initial commit\n")
191
192 flen := from.Len()
193
194 commit := new(Commit)
195 n, err := commit.Decode(sha1.New(), from, int64(flen))
196
197 assert.NoError(t, err)
198 assert.Equal(t, flen, n)
199
200 assert.Equal(t, author.String(), commit.Author)
201 assert.Equal(t, committer.String(), commit.Committer)
202 assert.Equal(t, treeIdAscii, hex.EncodeToString(commit.TreeID))
203 assert.Equal(t, "tree <- initial commit", commit.Message)
204 }
205
206 func TestCommitDecodingMultilineHeader(t *testing.T) {
207 author := &Signature{Name: "", Email: "[email protected]", When: time.Now()}
208 committer := &Signature{Name: "", Email: "[email protected]", When: time.Now()}
209
210 treeId := []byte("cccccccccccccccccccc")
211
212 from := new(bytes.Buffer)
213
214 fmt.Fprintf(from, "author %s\n", author)
215 fmt.Fprintf(from, "committer %s\n", committer)
216 fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
217 fmt.Fprintf(from, "gpgsig -----BEGIN PGP SIGNATURE-----\n")
218 fmt.Fprintf(from, " <signature>\n")
219 fmt.Fprintf(from, " -----END PGP SIGNATURE-----\n")
220 fmt.Fprintf(from, "\ninitial commit\n")
221
222 flen := from.Len()
223
224 commit := new(Commit)
225 n, err := commit.Decode(sha1.New(), from, int64(flen))
226
227 require.Nil(t, err)
228 require.Equal(t, flen, n)
229 require.Len(t, commit.ExtraHeaders, 1)
230
231 hdr := commit.ExtraHeaders[0]
232
233 assert.Equal(t, "gpgsig", hdr.K)
234 assert.EqualValues(t, []string{
235 "-----BEGIN PGP SIGNATURE-----",
236 "<signature>",
237 "-----END PGP SIGNATURE-----"},
238 strings.Split(hdr.V, "\n"))
239 }
240
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
300 func assertLine(t *testing.T, buf *bytes.Buffer, wanted string, args ...interface{}) {
301 got, err := buf.ReadString('\n')
302 if err == io.EOF {
303 err = nil
304 }
305
306 assert.Nil(t, err)
307 assert.Equal(t, fmt.Sprintf(wanted, args...), strings.TrimSuffix(got, "\n"))
308 }
309
310 func TestCommitEqualReturnsTrueWithIdenticalCommits(t *testing.T) {
311 c1 := &Commit{
312 Author: "Jane Doe <[email protected]> 1503956287 -0400",
313 Committer: "Jane Doe <[email protected]> 1503956287 -0400",
314 ParentIDs: [][]byte{make([]byte, 20)},
315 TreeID: make([]byte, 20),
316 ExtraHeaders: []*ExtraHeader{
317 {K: "Signed-off-by", V: "Joe Smith"},
318 },
319 Message: "initial commit",
320 }
321 c2 := &Commit{
322 Author: "Jane Doe <[email protected]> 1503956287 -0400",
323 Committer: "Jane Doe <[email protected]> 1503956287 -0400",
324 ParentIDs: [][]byte{make([]byte, 20)},
325 TreeID: make([]byte, 20),
326 ExtraHeaders: []*ExtraHeader{
327 {K: "Signed-off-by", V: "Joe Smith"},
328 },
329 Message: "initial commit",
330 }
331
332 assert.True(t, c1.Equal(c2))
333 }
334
335 func TestCommitEqualReturnsFalseWithDifferentParentCounts(t *testing.T) {
336 c1 := &Commit{
337 ParentIDs: [][]byte{make([]byte, 20), make([]byte, 20)},
338 }
339 c2 := &Commit{
340 ParentIDs: [][]byte{make([]byte, 20)},
341 }
342
343 assert.False(t, c1.Equal(c2))
344 }
345
346 func TestCommitEqualReturnsFalseWithDifferentParentsIds(t *testing.T) {
347 c1 := &Commit{
348 ParentIDs: [][]byte{make([]byte, 20)},
349 }
350 c2 := &Commit{
351 ParentIDs: [][]byte{make([]byte, 20)},
352 }
353
354 c1.ParentIDs[0][1] = 0x1
355
356 assert.False(t, c1.Equal(c2))
357 }
358
359 func TestCommitEqualReturnsFalseWithDifferentHeaderCounts(t *testing.T) {
360 c1 := &Commit{
361 ExtraHeaders: []*ExtraHeader{
362 {K: "Signed-off-by", V: "Joe Smith"},
363 {K: "GPG-Signature", V: "..."},
364 },
365 }
366 c2 := &Commit{
367 ExtraHeaders: []*ExtraHeader{
368 {K: "Signed-off-by", V: "Joe Smith"},
369 },
370 }
371
372 assert.False(t, c1.Equal(c2))
373 }
374
375 func TestCommitEqualReturnsFalseWithDifferentHeaders(t *testing.T) {
376 c1 := &Commit{
377 ExtraHeaders: []*ExtraHeader{
378 {K: "Signed-off-by", V: "Joe Smith"},
379 },
380 }
381 c2 := &Commit{
382 ExtraHeaders: []*ExtraHeader{
383 {K: "Signed-off-by", V: "Jane Smith"},
384 },
385 }
386
387 assert.False(t, c1.Equal(c2))
388 }
389
390 func TestCommitEqualReturnsFalseWithDifferentAuthors(t *testing.T) {
391 c1 := &Commit{
392 Author: "Jane Doe <[email protected]> 1503956287 -0400",
393 }
394 c2 := &Commit{
395 Author: "John Doe <[email protected]> 1503956287 -0400",
396 }
397
398 assert.False(t, c1.Equal(c2))
399 }
400
401 func TestCommitEqualReturnsFalseWithDifferentCommitters(t *testing.T) {
402 c1 := &Commit{
403 Committer: "Jane Doe <[email protected]> 1503956287 -0400",
404 }
405 c2 := &Commit{
406 Committer: "John Doe <[email protected]> 1503956287 -0400",
407 }
408
409 assert.False(t, c1.Equal(c2))
410 }
411
412 func TestCommitEqualReturnsFalseWithDifferentMessages(t *testing.T) {
413 c1 := &Commit{
414 Message: "initial commit",
415 }
416 c2 := &Commit{
417 Message: "not the initial commit",
418 }
419
420 assert.False(t, c1.Equal(c2))
421 }
422
423 func TestCommitEqualReturnsFalseWithDifferentTreeIDs(t *testing.T) {
424 c1 := &Commit{
425 TreeID: make([]byte, 20),
426 }
427 c2 := &Commit{
428 TreeID: make([]byte, 20),
429 }
430
431 c1.TreeID[0] = 0x1
432
433 assert.False(t, c1.Equal(c2))
434 }
435
436 func TestCommitEqualReturnsFalseWhenOneCommitIsNil(t *testing.T) {
437 c1 := &Commit{
438 Author: "Jane Doe <[email protected]> 1503956287 -0400",
439 Committer: "Jane Doe <[email protected]> 1503956287 -0400",
440 ParentIDs: [][]byte{make([]byte, 20)},
441 TreeID: make([]byte, 20),
442 ExtraHeaders: []*ExtraHeader{
443 {K: "Signed-off-by", V: "Joe Smith"},
444 },
445 Message: "initial commit",
446 }
447 c2 := (*Commit)(nil)
448
449 assert.False(t, c1.Equal(c2))
450 }
451
452 func TestCommitEqualReturnsTrueWhenBothCommitsAreNil(t *testing.T) {
453 c1 := (*Commit)(nil)
454 c2 := (*Commit)(nil)
455
456 assert.True(t, c1.Equal(c2))
457 }