-
Notifications
You must be signed in to change notification settings - Fork 10
/
commit_test.go
471 lines (373 loc) · 12.9 KB
/
commit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package gitobj
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCommitReturnsCorrectObjectType(t *testing.T) {
assert.Equal(t, CommitObjectType, new(Commit).Type())
}
func TestCommitEncoding(t *testing.T) {
author := &Signature{Name: "John Doe", Email: "john@example.com", When: time.Now()}
committer := &Signature{Name: "Jane Doe", Email: "jane@example.com", When: time.Now()}
sig := "-----BEGIN PGP SIGNATURE-----\n<signature>\n-----END PGP SIGNATURE-----"
c := &Commit{
Author: author.String(),
Committer: committer.String(),
ParentIDs: [][]byte{
[]byte("aaaaaaaaaaaaaaaaaaaa"), []byte("bbbbbbbbbbbbbbbbbbbb"),
},
TreeID: []byte("cccccccccccccccccccc"),
ExtraHeaders: []*ExtraHeader{
{"foo", "bar"},
{"gpgsig", sig},
},
Message: "initial commit",
}
buf := new(bytes.Buffer)
_, err := c.Encode(buf)
assert.Nil(t, err)
assertLine(t, buf, "tree 6363636363636363636363636363636363636363")
assertLine(t, buf, "parent 6161616161616161616161616161616161616161")
assertLine(t, buf, "parent 6262626262626262626262626262626262626262")
assertLine(t, buf, "author %s", author.String())
assertLine(t, buf, "committer %s", committer.String())
assertLine(t, buf, "foo bar")
assertLine(t, buf, "gpgsig -----BEGIN PGP SIGNATURE-----")
assertLine(t, buf, " <signature>")
assertLine(t, buf, " -----END PGP SIGNATURE-----")
assertLine(t, buf, "")
assertLine(t, buf, "initial commit")
assert.Equal(t, 0, buf.Len())
}
func TestCommitDecoding(t *testing.T) {
author := &Signature{Name: "John Doe", Email: "john@example.com", When: time.Now()}
committer := &Signature{Name: "Jane Doe", Email: "jane@example.com", When: time.Now()}
p1 := []byte("aaaaaaaaaaaaaaaaaaaa")
p2 := []byte("bbbbbbbbbbbbbbbbbbbb")
treeId := []byte("cccccccccccccccccccc")
from := new(bytes.Buffer)
fmt.Fprintf(from, "author %s\n", author)
fmt.Fprintf(from, "committer %s\n", committer)
fmt.Fprintf(from, "parent %s\n", hex.EncodeToString(p1))
fmt.Fprintf(from, "parent %s\n", hex.EncodeToString(p2))
fmt.Fprintf(from, "foo bar\n")
fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
fmt.Fprintf(from, "\ninitial commit\n")
flen := from.Len()
commit := new(Commit)
n, err := commit.Decode(sha1.New(), from, int64(flen))
assert.Nil(t, err)
assert.Equal(t, flen, n)
assert.Equal(t, author.String(), commit.Author)
assert.Equal(t, committer.String(), commit.Committer)
assert.Equal(t, [][]byte{p1, p2}, commit.ParentIDs)
assert.Equal(t, 1, len(commit.ExtraHeaders))
assert.Equal(t, "foo", commit.ExtraHeaders[0].K)
assert.Equal(t, "bar", commit.ExtraHeaders[0].V)
assert.Equal(t, "initial commit", commit.Message)
}
func TestCommitDecodingWithEmptyName(t *testing.T) {
author := &Signature{Name: "", Email: "john@example.com", When: time.Now()}
committer := &Signature{Name: "", Email: "jane@example.com", When: time.Now()}
treeId := []byte("cccccccccccccccccccc")
from := new(bytes.Buffer)
fmt.Fprintf(from, "author %s\n", author)
fmt.Fprintf(from, "committer %s\n", committer)
fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
fmt.Fprintf(from, "\ninitial commit\n")
flen := from.Len()
commit := new(Commit)
n, err := commit.Decode(sha1.New(), from, int64(flen))
assert.Nil(t, err)
assert.Equal(t, flen, n)
assert.Equal(t, author.String(), commit.Author)
assert.Equal(t, committer.String(), commit.Committer)
assert.Equal(t, "initial commit", commit.Message)
}
func TestCommitDecodingWithLargeCommitMessage(t *testing.T) {
message := "This message text is, with newline, exactly 64 characters long. "
// This message will be exactly 10 MiB in size when part of the commit.
longMessage := strings.Repeat(message, (10*1024*1024/64)-1)
longMessage += strings.TrimSpace(message)
author := &Signature{Name: "", Email: "john@example.com", When: time.Now()}
committer := &Signature{Name: "", Email: "jane@example.com", When: time.Now()}
treeId := []byte("cccccccccccccccccccc")
from := new(bytes.Buffer)
fmt.Fprintf(from, "author %s\n", author)
fmt.Fprintf(from, "committer %s\n", committer)
fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
fmt.Fprintf(from, "\n%s\n", longMessage)
flen := from.Len()
commit := new(Commit)
n, err := commit.Decode(sha1.New(), from, int64(flen))
assert.Nil(t, err)
assert.Equal(t, flen, n)
assert.Equal(t, author.String(), commit.Author)
assert.Equal(t, committer.String(), commit.Committer)
assert.Equal(t, longMessage, commit.Message)
}
func TestCommitDecodingWithMessageKeywordPrefix(t *testing.T) {
author := &Signature{Name: "John Doe", Email: "john@example.com", When: time.Now()}
committer := &Signature{Name: "Jane Doe", Email: "jane@example.com", When: time.Now()}
treeId := []byte("aaaaaaaaaaaaaaaaaaaa")
treeIdAscii := hex.EncodeToString(treeId)
from := new(bytes.Buffer)
fmt.Fprintf(from, "author %s\n", author)
fmt.Fprintf(from, "committer %s\n", committer)
fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
fmt.Fprintf(from, "\nfirst line\n\nsecond line\n")
flen := from.Len()
commit := new(Commit)
n, err := commit.Decode(sha1.New(), from, int64(flen))
assert.NoError(t, err)
assert.Equal(t, flen, n)
assert.Equal(t, author.String(), commit.Author)
assert.Equal(t, committer.String(), commit.Committer)
assert.Equal(t, treeIdAscii, hex.EncodeToString(commit.TreeID))
assert.Equal(t, "first line\n\nsecond line", commit.Message)
}
func TestCommitDecodingWithWhitespace(t *testing.T) {
author := &Signature{Name: "John Doe", Email: "john@example.com", When: time.Now()}
committer := &Signature{Name: "Jane Doe", Email: "jane@example.com", When: time.Now()}
treeId := []byte("aaaaaaaaaaaaaaaaaaaa")
treeIdAscii := hex.EncodeToString(treeId)
from := new(bytes.Buffer)
fmt.Fprintf(from, "author %s\n", author)
fmt.Fprintf(from, "committer %s\n", committer)
fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
fmt.Fprintf(from, "\ntree <- initial commit\n")
flen := from.Len()
commit := new(Commit)
n, err := commit.Decode(sha1.New(), from, int64(flen))
assert.NoError(t, err)
assert.Equal(t, flen, n)
assert.Equal(t, author.String(), commit.Author)
assert.Equal(t, committer.String(), commit.Committer)
assert.Equal(t, treeIdAscii, hex.EncodeToString(commit.TreeID))
assert.Equal(t, "tree <- initial commit", commit.Message)
}
func TestCommitDecodingMultilineHeader(t *testing.T) {
author := &Signature{Name: "", Email: "john@example.com", When: time.Now()}
committer := &Signature{Name: "", Email: "jane@example.com", When: time.Now()}
treeId := []byte("cccccccccccccccccccc")
from := new(bytes.Buffer)
fmt.Fprintf(from, "author %s\n", author)
fmt.Fprintf(from, "committer %s\n", committer)
fmt.Fprintf(from, "tree %s\n", hex.EncodeToString(treeId))
fmt.Fprintf(from, "gpgsig -----BEGIN PGP SIGNATURE-----\n")
fmt.Fprintf(from, " <signature>\n")
fmt.Fprintf(from, " -----END PGP SIGNATURE-----\n")
fmt.Fprintf(from, "\ninitial commit\n")
flen := from.Len()
commit := new(Commit)
n, err := commit.Decode(sha1.New(), from, int64(flen))
require.Nil(t, err)
require.Equal(t, flen, n)
require.Len(t, commit.ExtraHeaders, 1)
hdr := commit.ExtraHeaders[0]
assert.Equal(t, "gpgsig", hdr.K)
assert.EqualValues(t, []string{
"-----BEGIN PGP SIGNATURE-----",
"<signature>",
"-----END PGP SIGNATURE-----"},
strings.Split(hdr.V, "\n"))
}
func TestCommitDecodingMessageWithLineStartingWithTree(t *testing.T) {
from := new(bytes.Buffer)
// The tricky part here that we're testing is the "tree support" in the
// `mergetag` header, which we should not try to parse as a tree header.
// Note also that this entry contains trailing whitespace which must not
// be trimmed.
fmt.Fprintf(from, `tree e8ad84c41c2acde27c77fa212b8865cd3acfe6fb
parent b343c8beec664ef6f0e9964d3001c7c7966331ae
parent 1e8a52e18cfb381bc9cc1f0b720540364d2a6edd
author Pat Doe <pdoe@example.org> 1337892984 -0700
committer Pat Doe <pdoe@example.org> 1337892984 -0700
mergetag object 1e8a52e18cfb381bc9cc1f0b720540364d2a6edd
type commit
tag random
tagger J. Roe <jroe@example.ca> 1337889148 -0600
Random changes
This text contains some
tree support code.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Not a real signature
-----END PGP SIGNATURE-----
Merge tag 'random' of git://git.example.ca/git/
`)
flen := from.Len()
commit := new(Commit)
n, err := commit.Decode(sha1.New(), from, int64(flen))
require.Nil(t, err)
require.Equal(t, flen, n)
require.Equal(t, commit.ExtraHeaders, []*ExtraHeader{
{
K: "mergetag",
V: `object 1e8a52e18cfb381bc9cc1f0b720540364d2a6edd
type commit
tag random
tagger J. Roe <jroe@example.ca> 1337889148 -0600
Random changes
This text contains some
tree support code.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Not a real signature
-----END PGP SIGNATURE-----`},
},
)
require.Equal(t, commit.Message, "Merge tag 'random' of git://git.example.ca/git/")
}
func assertLine(t *testing.T, buf *bytes.Buffer, wanted string, args ...interface{}) {
got, err := buf.ReadString('\n')
if err == io.EOF {
err = nil
}
assert.Nil(t, err)
assert.Equal(t, fmt.Sprintf(wanted, args...), strings.TrimSuffix(got, "\n"))
}
func TestCommitEqualReturnsTrueWithIdenticalCommits(t *testing.T) {
c1 := &Commit{
Author: "Jane Doe <jane@example.com> 1503956287 -0400",
Committer: "Jane Doe <jane@example.com> 1503956287 -0400",
ParentIDs: [][]byte{make([]byte, 20)},
TreeID: make([]byte, 20),
ExtraHeaders: []*ExtraHeader{
{K: "Signed-off-by", V: "Joe Smith"},
},
Message: "initial commit",
}
c2 := &Commit{
Author: "Jane Doe <jane@example.com> 1503956287 -0400",
Committer: "Jane Doe <jane@example.com> 1503956287 -0400",
ParentIDs: [][]byte{make([]byte, 20)},
TreeID: make([]byte, 20),
ExtraHeaders: []*ExtraHeader{
{K: "Signed-off-by", V: "Joe Smith"},
},
Message: "initial commit",
}
assert.True(t, c1.Equal(c2))
}
func TestCommitEqualReturnsFalseWithDifferentParentCounts(t *testing.T) {
c1 := &Commit{
ParentIDs: [][]byte{make([]byte, 20), make([]byte, 20)},
}
c2 := &Commit{
ParentIDs: [][]byte{make([]byte, 20)},
}
assert.False(t, c1.Equal(c2))
}
func TestCommitEqualReturnsFalseWithDifferentParentsIds(t *testing.T) {
c1 := &Commit{
ParentIDs: [][]byte{make([]byte, 20)},
}
c2 := &Commit{
ParentIDs: [][]byte{make([]byte, 20)},
}
c1.ParentIDs[0][1] = 0x1
assert.False(t, c1.Equal(c2))
}
func TestCommitEqualReturnsFalseWithDifferentHeaderCounts(t *testing.T) {
c1 := &Commit{
ExtraHeaders: []*ExtraHeader{
{K: "Signed-off-by", V: "Joe Smith"},
{K: "GPG-Signature", V: "..."},
},
}
c2 := &Commit{
ExtraHeaders: []*ExtraHeader{
{K: "Signed-off-by", V: "Joe Smith"},
},
}
assert.False(t, c1.Equal(c2))
}
func TestCommitEqualReturnsFalseWithDifferentHeaders(t *testing.T) {
c1 := &Commit{
ExtraHeaders: []*ExtraHeader{
{K: "Signed-off-by", V: "Joe Smith"},
},
}
c2 := &Commit{
ExtraHeaders: []*ExtraHeader{
{K: "Signed-off-by", V: "Jane Smith"},
},
}
assert.False(t, c1.Equal(c2))
}
func TestCommitEqualReturnsFalseWithDifferentAuthors(t *testing.T) {
c1 := &Commit{
Author: "Jane Doe <jane@example.com> 1503956287 -0400",
}
c2 := &Commit{
Author: "John Doe <john@example.com> 1503956287 -0400",
}
assert.False(t, c1.Equal(c2))
}
func TestCommitEqualReturnsFalseWithDifferentCommitters(t *testing.T) {
c1 := &Commit{
Committer: "Jane Doe <jane@example.com> 1503956287 -0400",
}
c2 := &Commit{
Committer: "John Doe <john@example.com> 1503956287 -0400",
}
assert.False(t, c1.Equal(c2))
}
func TestCommitEqualReturnsFalseWithDifferentMessages(t *testing.T) {
c1 := &Commit{
Message: "initial commit",
}
c2 := &Commit{
Message: "not the initial commit",
}
assert.False(t, c1.Equal(c2))
}
func TestCommitEqualReturnsFalseWithDifferentTreeIDs(t *testing.T) {
c1 := &Commit{
TreeID: make([]byte, 20),
}
c2 := &Commit{
TreeID: make([]byte, 20),
}
c1.TreeID[0] = 0x1
assert.False(t, c1.Equal(c2))
}
func TestCommitEqualReturnsFalseWhenOneCommitIsNil(t *testing.T) {
c1 := &Commit{
Author: "Jane Doe <jane@example.com> 1503956287 -0400",
Committer: "Jane Doe <jane@example.com> 1503956287 -0400",
ParentIDs: [][]byte{make([]byte, 20)},
TreeID: make([]byte, 20),
ExtraHeaders: []*ExtraHeader{
{K: "Signed-off-by", V: "Joe Smith"},
},
Message: "initial commit",
}
c2 := (*Commit)(nil)
assert.False(t, c1.Equal(c2))
}
func TestCommitEqualReturnsTrueWhenBothCommitsAreNil(t *testing.T) {
c1 := (*Commit)(nil)
c2 := (*Commit)(nil)
assert.True(t, c1.Equal(c2))
}
func TestCommitInvalidMultilineNonExtraHeader(t *testing.T) {
cc := `tree 2aedfd35087c75d17bdbaf4dd56069d44fc75b71
parent 75158117eb8efe60453f8c077527ac3530c81e38
author Jane Doe <jane@example.com> 1503956287 -0400
committer Jane Doe <jane@example.com> 1503956287 -0400
foo bar
initial commit`
var c Commit
_, err := c.Decode(sha1.New(), strings.NewReader(cc), int64(len(cc)))
assert.NoError(t, err)
}