| 1 |
package object |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"strings" |
| 10 |
"time" |
| 11 |
|
| 12 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 13 |
"github.com/go-git/go-git/v5/plumbing" |
| 14 |
"github.com/go-git/go-git/v5/plumbing/cache" |
| 15 |
|
| 16 |
"github.com/go-git/go-git/v5/storage/filesystem" |
| 17 |
. "gopkg.in/check.v1" |
| 18 |
) |
| 19 |
|
| 20 |
type SuiteCommit struct { |
| 21 |
BaseObjectsSuite |
| 22 |
Commit *Commit |
| 23 |
} |
| 24 |
|
| 25 |
var _ = Suite(&SuiteCommit{}) |
| 26 |
|
| 27 |
func (s *SuiteCommit) SetUpSuite(c *C) { |
| 28 |
s.BaseObjectsSuite.SetUpSuite(c) |
| 29 |
|
| 30 |
hash := plumbing.NewHash("1669dce138d9b841a518c64b10914d88f5e488ea") |
| 31 |
|
| 32 |
s.Commit = s.commit(c, hash) |
| 33 |
} |
| 34 |
|
| 35 |
func (s *SuiteCommit) TestDecodeNonCommit(c *C) { |
| 36 |
hash := plumbing.NewHash("9a48f23120e880dfbe41f7c9b7b708e9ee62a492") |
| 37 |
blob, err := s.Storer.EncodedObject(plumbing.AnyObject, hash) |
| 38 |
c.Assert(err, IsNil) |
| 39 |
|
| 40 |
commit := &Commit{} |
| 41 |
err = commit.Decode(blob) |
| 42 |
c.Assert(err, Equals, ErrUnsupportedObject) |
| 43 |
} |
| 44 |
|
| 45 |
func (s *SuiteCommit) TestDecodeClearsExistingState(c *C) { |
| 46 |
const raw = "tree eba74343e2f15d62adedfd8c883ee0262b5c8021\n\nfresh message\n" |
| 47 |
|
| 48 |
staleSrc := &plumbing.MemoryObject{} |
| 49 |
commit := &Commit{ |
| 50 |
Hash: plumbing.NewHash("1111111111111111111111111111111111111111"), |
| 51 |
Author: Signature{Name: "Stale Author", Email: "[email protected]", When: time.Unix(1, 0).UTC()}, |
| 52 |
Committer: Signature{Name: "Stale Committer", Email: "[email protected]", When: time.Unix(2, 0).UTC()}, |
| 53 |
MergeTag: "stale merge tag", |
| 54 |
PGPSignature: "stale signature", |
| 55 |
Message: "stale message", |
| 56 |
TreeHash: plumbing.NewHash("2222222222222222222222222222222222222222"), |
| 57 |
ParentHashes: []plumbing.Hash{ |
| 58 |
plumbing.NewHash("3333333333333333333333333333333333333333"), |
| 59 |
}, |
| 60 |
Encoding: MessageEncoding("latin-1"), |
| 61 |
ExtraHeaders: []ExtraHeader{ |
| 62 |
{Key: "x-stale", Value: "stale"}, |
| 63 |
}, |
| 64 |
s: s.Storer, |
| 65 |
src: staleSrc, |
| 66 |
} |
| 67 |
|
| 68 |
obj := &plumbing.MemoryObject{} |
| 69 |
obj.SetType(plumbing.CommitObject) |
| 70 |
_, err := obj.Write([]byte(raw)) |
| 71 |
c.Assert(err, IsNil) |
| 72 |
|
| 73 |
c.Assert(commit.Decode(obj), IsNil) |
| 74 |
c.Assert(commit.Hash, Equals, obj.Hash()) |
| 75 |
c.Assert(commit.Author, DeepEquals, Signature{}) |
| 76 |
c.Assert(commit.Committer, DeepEquals, Signature{}) |
| 77 |
c.Assert(commit.MergeTag, Equals, "") |
| 78 |
c.Assert(commit.PGPSignature, Equals, "") |
| 79 |
c.Assert(commit.Message, Equals, "fresh message\n") |
| 80 |
c.Assert(commit.TreeHash.String(), Equals, "eba74343e2f15d62adedfd8c883ee0262b5c8021") |
| 81 |
c.Assert(commit.ParentHashes, IsNil) |
| 82 |
c.Assert(commit.Encoding, Equals, defaultUtf8CommitMessageEncoding) |
| 83 |
c.Assert(commit.ExtraHeaders, IsNil) |
| 84 |
c.Assert(commit.s, Equals, s.Storer) |
| 85 |
c.Assert(commit.src, Equals, obj) |
| 86 |
} |
| 87 |
|
| 88 |
func (s *SuiteCommit) TestType(c *C) { |
| 89 |
c.Assert(s.Commit.Type(), Equals, plumbing.CommitObject) |
| 90 |
} |
| 91 |
|
| 92 |
func (s *SuiteCommit) TestTree(c *C) { |
| 93 |
tree, err := s.Commit.Tree() |
| 94 |
c.Assert(err, IsNil) |
| 95 |
c.Assert(tree.ID().String(), Equals, "eba74343e2f15d62adedfd8c883ee0262b5c8021") |
| 96 |
} |
| 97 |
|
| 98 |
func (s *SuiteCommit) TestParents(c *C) { |
| 99 |
expected := []string{ |
| 100 |
"35e85108805c84807bc66a02d91535e1e24b38b9", |
| 101 |
"a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69", |
| 102 |
} |
| 103 |
|
| 104 |
var output []string |
| 105 |
i := s.Commit.Parents() |
| 106 |
err := i.ForEach(func(commit *Commit) error { |
| 107 |
output = append(output, commit.ID().String()) |
| 108 |
return nil |
| 109 |
}) |
| 110 |
|
| 111 |
c.Assert(err, IsNil) |
| 112 |
c.Assert(output, DeepEquals, expected) |
| 113 |
|
| 114 |
i.Close() |
| 115 |
} |
| 116 |
|
| 117 |
func (s *SuiteCommit) TestParent(c *C) { |
| 118 |
commit, err := s.Commit.Parent(1) |
| 119 |
c.Assert(err, IsNil) |
| 120 |
c.Assert(commit.Hash.String(), Equals, "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69") |
| 121 |
} |
| 122 |
|
| 123 |
func (s *SuiteCommit) TestParentNotFound(c *C) { |
| 124 |
commit, err := s.Commit.Parent(42) |
| 125 |
c.Assert(err, Equals, ErrParentNotFound) |
| 126 |
c.Assert(commit, IsNil) |
| 127 |
} |
| 128 |
|
| 129 |
func (s *SuiteCommit) TestPatch(c *C) { |
| 130 |
from := s.commit(c, plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294")) |
| 131 |
to := s.commit(c, plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) |
| 132 |
|
| 133 |
patch, err := from.Patch(to) |
| 134 |
c.Assert(err, IsNil) |
| 135 |
|
| 136 |
buf := bytes.NewBuffer(nil) |
| 137 |
err = patch.Encode(buf) |
| 138 |
c.Assert(err, IsNil) |
| 139 |
|
| 140 |
c.Assert(buf.String(), Equals, `diff --git a/vendor/foo.go b/vendor/foo.go |
| 141 |
new file mode 100644 |
| 142 |
index 0000000000000000000000000000000000000000..9dea2395f5403188298c1dabe8bdafe562c491e3 |
| 143 |
--- /dev/null |
| 144 |
+++ b/vendor/foo.go |
| 145 |
@@ -0,0 +1,7 @@ |
| 146 |
+package main |
| 147 |
+ |
| 148 |
+import "fmt" |
| 149 |
+ |
| 150 |
+func main() { |
| 151 |
+ fmt.Println("Hello, playground") |
| 152 |
+} |
| 153 |
`) |
| 154 |
c.Assert(buf.String(), Equals, patch.String()) |
| 155 |
|
| 156 |
from = s.commit(c, plumbing.NewHash("b8e471f58bcbca63b07bda20e428190409c2db47")) |
| 157 |
to = s.commit(c, plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9")) |
| 158 |
|
| 159 |
patch, err = from.Patch(to) |
| 160 |
c.Assert(err, IsNil) |
| 161 |
|
| 162 |
buf.Reset() |
| 163 |
err = patch.Encode(buf) |
| 164 |
c.Assert(err, IsNil) |
| 165 |
|
| 166 |
c.Assert(buf.String(), Equals, `diff --git a/CHANGELOG b/CHANGELOG |
| 167 |
deleted file mode 100644 |
| 168 |
index d3ff53e0564a9f87d8e84b6e28e5060e517008aa..0000000000000000000000000000000000000000 |
| 169 |
--- a/CHANGELOG |
| 170 |
+++ /dev/null |
| 171 |
@@ -1 +0,0 @@ |
| 172 |
-Initial changelog |
| 173 |
diff --git a/binary.jpg b/binary.jpg |
| 174 |
new file mode 100644 |
| 175 |
index 0000000000000000000000000000000000000000..d5c0f4ab811897cadf03aec358ae60d21f91c50d |
| 176 |
Binary files /dev/null and b/binary.jpg differ |
| 177 |
`) |
| 178 |
|
| 179 |
c.Assert(buf.String(), Equals, patch.String()) |
| 180 |
} |
| 181 |
|
| 182 |
func (s *SuiteCommit) TestPatchContext(c *C) { |
| 183 |
from := s.commit(c, plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294")) |
| 184 |
to := s.commit(c, plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) |
| 185 |
|
| 186 |
patch, err := from.PatchContext(context.Background(), to) |
| 187 |
c.Assert(err, IsNil) |
| 188 |
|
| 189 |
buf := bytes.NewBuffer(nil) |
| 190 |
err = patch.Encode(buf) |
| 191 |
c.Assert(err, IsNil) |
| 192 |
|
| 193 |
c.Assert(buf.String(), Equals, `diff --git a/vendor/foo.go b/vendor/foo.go |
| 194 |
new file mode 100644 |
| 195 |
index 0000000000000000000000000000000000000000..9dea2395f5403188298c1dabe8bdafe562c491e3 |
| 196 |
--- /dev/null |
| 197 |
+++ b/vendor/foo.go |
| 198 |
@@ -0,0 +1,7 @@ |
| 199 |
+package main |
| 200 |
+ |
| 201 |
+import "fmt" |
| 202 |
+ |
| 203 |
+func main() { |
| 204 |
+ fmt.Println("Hello, playground") |
| 205 |
+} |
| 206 |
`) |
| 207 |
c.Assert(buf.String(), Equals, patch.String()) |
| 208 |
|
| 209 |
from = s.commit(c, plumbing.NewHash("b8e471f58bcbca63b07bda20e428190409c2db47")) |
| 210 |
to = s.commit(c, plumbing.NewHash("35e85108805c84807bc66a02d91535e1e24b38b9")) |
| 211 |
|
| 212 |
patch, err = from.PatchContext(context.Background(), to) |
| 213 |
c.Assert(err, IsNil) |
| 214 |
|
| 215 |
buf.Reset() |
| 216 |
err = patch.Encode(buf) |
| 217 |
c.Assert(err, IsNil) |
| 218 |
|
| 219 |
c.Assert(buf.String(), Equals, `diff --git a/CHANGELOG b/CHANGELOG |
| 220 |
deleted file mode 100644 |
| 221 |
index d3ff53e0564a9f87d8e84b6e28e5060e517008aa..0000000000000000000000000000000000000000 |
| 222 |
--- a/CHANGELOG |
| 223 |
+++ /dev/null |
| 224 |
@@ -1 +0,0 @@ |
| 225 |
-Initial changelog |
| 226 |
diff --git a/binary.jpg b/binary.jpg |
| 227 |
new file mode 100644 |
| 228 |
index 0000000000000000000000000000000000000000..d5c0f4ab811897cadf03aec358ae60d21f91c50d |
| 229 |
Binary files /dev/null and b/binary.jpg differ |
| 230 |
`) |
| 231 |
|
| 232 |
c.Assert(buf.String(), Equals, patch.String()) |
| 233 |
} |
| 234 |
|
| 235 |
func (s *SuiteCommit) TestPatchContext_ToNil(c *C) { |
| 236 |
from := s.commit(c, plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294")) |
| 237 |
|
| 238 |
patch, err := from.PatchContext(context.Background(), nil) |
| 239 |
c.Assert(err, IsNil) |
| 240 |
|
| 241 |
c.Assert(len(patch.String()), Equals, 242679) |
| 242 |
} |
| 243 |
|
| 244 |
func (s *SuiteCommit) TestCommitEncodeDecodeIdempotent(c *C) { |
| 245 |
pgpsignature := `-----BEGIN PGP SIGNATURE----- |
| 246 |
|
| 247 |
iQEcBAABAgAGBQJTZbQlAAoJEF0+sviABDDrZbQH/09PfE51KPVPlanr6q1v4/Ut |
| 248 |
LQxfojUWiLQdg2ESJItkcuweYg+kc3HCyFejeDIBw9dpXt00rY26p05qrpnG+85b |
| 249 |
hM1/PswpPLuBSr+oCIDj5GMC2r2iEKsfv2fJbNW8iWAXVLoWZRF8B0MfqX/YTMbm |
| 250 |
ecorc4iXzQu7tupRihslbNkfvfciMnSDeSvzCpWAHl7h8Wj6hhqePmLm9lAYqnKp |
| 251 |
8S5B/1SSQuEAjRZgI4IexpZoeKGVDptPHxLLS38fozsyi0QyDyzEgJxcJQVMXxVi |
| 252 |
RUysgqjcpT8+iQM1PblGfHR4XAhuOqN5Fx06PSaFZhqvWFezJ28/CLyX5q+oIVk= |
| 253 |
=EFTF |
| 254 |
-----END PGP SIGNATURE----- |
| 255 |
` |
| 256 |
|
| 257 |
tag := fmt.Sprintf(`object f000000000000000000000000000000000000000 |
| 258 |
type commit |
| 259 |
tag change |
| 260 |
tagger Foo <[email protected]> 1695827841 -0400 |
| 261 |
|
| 262 |
change |
| 263 |
%s |
| 264 |
`, pgpsignature) |
| 265 |
|
| 266 |
ts, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05-07:00") |
| 267 |
c.Assert(err, IsNil) |
| 268 |
commits := []*Commit{ |
| 269 |
{ |
| 270 |
Author: Signature{Name: "Foo", Email: "[email protected]", When: ts}, |
| 271 |
Committer: Signature{Name: "Bar", Email: "[email protected]", When: ts}, |
| 272 |
Message: "Message\n\nFoo\nBar\nWith trailing blank lines\n\n", |
| 273 |
TreeHash: plumbing.NewHash("f000000000000000000000000000000000000001"), |
| 274 |
ParentHashes: []plumbing.Hash{plumbing.NewHash("f000000000000000000000000000000000000002")}, |
| 275 |
Encoding: defaultUtf8CommitMessageEncoding, |
| 276 |
}, |
| 277 |
{ |
| 278 |
Author: Signature{Name: "Foo", Email: "[email protected]", When: ts}, |
| 279 |
Committer: Signature{Name: "Bar", Email: "[email protected]", When: ts}, |
| 280 |
Message: "Message\n\nFoo\nBar\nWith no trailing blank lines", |
| 281 |
TreeHash: plumbing.NewHash("0000000000000000000000000000000000000003"), |
| 282 |
ParentHashes: []plumbing.Hash{ |
| 283 |
plumbing.NewHash("f000000000000000000000000000000000000004"), |
| 284 |
plumbing.NewHash("f000000000000000000000000000000000000005"), |
| 285 |
plumbing.NewHash("f000000000000000000000000000000000000006"), |
| 286 |
plumbing.NewHash("f000000000000000000000000000000000000007"), |
| 287 |
}, |
| 288 |
Encoding: MessageEncoding("ISO-8859-1"), |
| 289 |
}, |
| 290 |
{ |
| 291 |
Author: Signature{Name: "Foo", Email: "[email protected]", When: ts}, |
| 292 |
Committer: Signature{Name: "Bar", Email: "[email protected]", When: ts}, |
| 293 |
Message: "Testing mergetag\n\nHere, commit is not signed", |
| 294 |
TreeHash: plumbing.NewHash("f000000000000000000000000000000000000001"), |
| 295 |
ParentHashes: []plumbing.Hash{ |
| 296 |
plumbing.NewHash("f000000000000000000000000000000000000002"), |
| 297 |
plumbing.NewHash("f000000000000000000000000000000000000003"), |
| 298 |
}, |
| 299 |
MergeTag: tag, |
| 300 |
Encoding: defaultUtf8CommitMessageEncoding, |
| 301 |
}, |
| 302 |
{ |
| 303 |
Author: Signature{Name: "Foo", Email: "[email protected]", When: ts}, |
| 304 |
Committer: Signature{Name: "Bar", Email: "[email protected]", When: ts}, |
| 305 |
Message: "Testing mergetag\n\nHere, commit is also signed", |
| 306 |
TreeHash: plumbing.NewHash("f000000000000000000000000000000000000001"), |
| 307 |
ParentHashes: []plumbing.Hash{ |
| 308 |
plumbing.NewHash("f000000000000000000000000000000000000002"), |
| 309 |
plumbing.NewHash("f000000000000000000000000000000000000003"), |
| 310 |
}, |
| 311 |
MergeTag: tag, |
| 312 |
PGPSignature: pgpsignature, |
| 313 |
Encoding: defaultUtf8CommitMessageEncoding, |
| 314 |
}, |
| 315 |
} |
| 316 |
for _, commit := range commits { |
| 317 |
obj := &plumbing.MemoryObject{} |
| 318 |
err = commit.Encode(obj) |
| 319 |
c.Assert(err, IsNil) |
| 320 |
newCommit := &Commit{} |
| 321 |
err = newCommit.Decode(obj) |
| 322 |
c.Assert(err, IsNil) |
| 323 |
commit.Hash = obj.Hash() |
| 324 |
commit.src = obj |
| 325 |
c.Assert(newCommit, DeepEquals, commit) |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
func (s *SuiteCommit) TestFile(c *C) { |
| 330 |
file, err := s.Commit.File("CHANGELOG") |
| 331 |
c.Assert(err, IsNil) |
| 332 |
c.Assert(file.Name, Equals, "CHANGELOG") |
| 333 |
} |
| 334 |
|
| 335 |
func (s *SuiteCommit) TestNumParents(c *C) { |
| 336 |
c.Assert(s.Commit.NumParents(), Equals, 2) |
| 337 |
} |
| 338 |
|
| 339 |
func (s *SuiteCommit) TestString(c *C) { |
| 340 |
c.Assert(s.Commit.String(), Equals, ""+ |
| 341 |
"commit 1669dce138d9b841a518c64b10914d88f5e488ea\n"+ |
| 342 |
"Author: Máximo Cuadros Ortiz <[email protected]>\n"+ |
| 343 |
"Date: Tue Mar 31 13:48:14 2015 +0200\n"+ |
| 344 |
"\n"+ |
| 345 |
" Merge branch 'master' of github.com:tyba/git-fixture\n"+ |
| 346 |
"\n", |
| 347 |
) |
| 348 |
} |
| 349 |
|
| 350 |
func (s *SuiteCommit) TestStringMultiLine(c *C) { |
| 351 |
hash := plumbing.NewHash("e7d896db87294e33ca3202e536d4d9bb16023db3") |
| 352 |
|
| 353 |
f := fixtures.ByURL("https://github.com/src-d/go-git.git").One() |
| 354 |
sto := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault()) |
| 355 |
|
| 356 |
o, err := sto.EncodedObject(plumbing.CommitObject, hash) |
| 357 |
c.Assert(err, IsNil) |
| 358 |
commit, err := DecodeCommit(sto, o) |
| 359 |
c.Assert(err, IsNil) |
| 360 |
|
| 361 |
c.Assert(commit.String(), Equals, ""+ |
| 362 |
"commit e7d896db87294e33ca3202e536d4d9bb16023db3\n"+ |
| 363 |
"Author: Alberto Cortés <[email protected]>\n"+ |
| 364 |
"Date: Wed Jan 27 11:13:49 2016 +0100\n"+ |
| 365 |
"\n"+ |
| 366 |
" fix zlib invalid header error\n"+ |
| 367 |
"\n"+ |
| 368 |
" The return value of reads to the packfile were being ignored, so zlib\n"+ |
| 369 |
" was getting invalid data on it read buffers.\n"+ |
| 370 |
"\n", |
| 371 |
) |
| 372 |
} |
| 373 |
|
| 374 |
func (s *SuiteCommit) TestCommitIterNext(c *C) { |
| 375 |
i := s.Commit.Parents() |
| 376 |
|
| 377 |
commit, err := i.Next() |
| 378 |
c.Assert(err, IsNil) |
| 379 |
c.Assert(commit.ID().String(), Equals, "35e85108805c84807bc66a02d91535e1e24b38b9") |
| 380 |
|
| 381 |
commit, err = i.Next() |
| 382 |
c.Assert(err, IsNil) |
| 383 |
c.Assert(commit.ID().String(), Equals, "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69") |
| 384 |
|
| 385 |
commit, err = i.Next() |
| 386 |
c.Assert(err, Equals, io.EOF) |
| 387 |
c.Assert(commit, IsNil) |
| 388 |
} |
| 389 |
|
| 390 |
func (s *SuiteCommit) TestLongCommitMessageSerialization(c *C) { |
| 391 |
encoded := &plumbing.MemoryObject{} |
| 392 |
decoded := &Commit{} |
| 393 |
commit := *s.Commit |
| 394 |
|
| 395 |
longMessage := "my message: message\n\n" + strings.Repeat("test", 4096) + "\nOK" |
| 396 |
commit.Message = longMessage |
| 397 |
|
| 398 |
err := commit.Encode(encoded) |
| 399 |
c.Assert(err, IsNil) |
| 400 |
|
| 401 |
err = decoded.Decode(encoded) |
| 402 |
c.Assert(err, IsNil) |
| 403 |
c.Assert(decoded.Message, Equals, longMessage) |
| 404 |
} |
| 405 |
|
| 406 |
func (s *SuiteCommit) TestPGPSignatureSerialization(c *C) { |
| 407 |
pgpsignature := `-----BEGIN PGP SIGNATURE----- |
| 408 |
|
| 409 |
iQEcBAABAgAGBQJTZbQlAAoJEF0+sviABDDrZbQH/09PfE51KPVPlanr6q1v4/Ut |
| 410 |
LQxfojUWiLQdg2ESJItkcuweYg+kc3HCyFejeDIBw9dpXt00rY26p05qrpnG+85b |
| 411 |
hM1/PswpPLuBSr+oCIDj5GMC2r2iEKsfv2fJbNW8iWAXVLoWZRF8B0MfqX/YTMbm |
| 412 |
ecorc4iXzQu7tupRihslbNkfvfciMnSDeSvzCpWAHl7h8Wj6hhqePmLm9lAYqnKp |
| 413 |
8S5B/1SSQuEAjRZgI4IexpZoeKGVDptPHxLLS38fozsyi0QyDyzEgJxcJQVMXxVi |
| 414 |
RUysgqjcpT8+iQM1PblGfHR4XAhuOqN5Fx06PSaFZhqvWFezJ28/CLyX5q+oIVk= |
| 415 |
=EFTF |
| 416 |
-----END PGP SIGNATURE----- |
| 417 |
` |
| 418 |
broken := beginpgp + "\nsome\ntrash\n" + endpgp + "text\n" |
| 419 |
|
| 420 |
cases := []struct { |
| 421 |
name string |
| 422 |
pgpSignature string |
| 423 |
authorName string |
| 424 |
}{ |
| 425 |
{name: "pgp signature", pgpSignature: pgpsignature}, |
| 426 |
{name: "pgp signature with leading empty line", pgpSignature: "\n" + pgpsignature}, |
| 427 |
{name: "beginpgp marker in author name is not parsed as signature", authorName: beginpgp}, |
| 428 |
{name: "garbage between begin and end markers round-trips", pgpSignature: broken}, |
| 429 |
} |
| 430 |
|
| 431 |
for _, tc := range cases { |
| 432 |
c.Log(tc.name) |
| 433 |
|
| 434 |
commit := *s.Commit |
| 435 |
commit.PGPSignature = tc.pgpSignature |
| 436 |
if tc.authorName != "" { |
| 437 |
commit.Author.Name = tc.authorName |
| 438 |
} |
| 439 |
|
| 440 |
encoded := &plumbing.MemoryObject{} |
| 441 |
decoded := &Commit{} |
| 442 |
err := commit.Encode(encoded) |
| 443 |
c.Assert(err, IsNil) |
| 444 |
|
| 445 |
err = decoded.Decode(encoded) |
| 446 |
c.Assert(err, IsNil) |
| 447 |
c.Assert(decoded.PGPSignature, Equals, tc.pgpSignature) |
| 448 |
if tc.authorName != "" { |
| 449 |
c.Assert(decoded.Author.Name, Equals, tc.authorName) |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
func (s *SuiteCommit) TestStat(c *C) { |
| 455 |
aCommit := s.commit(c, plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) |
| 456 |
fileStats, err := aCommit.Stats() |
| 457 |
c.Assert(err, IsNil) |
| 458 |
|
| 459 |
c.Assert(fileStats[0].Name, Equals, "vendor/foo.go") |
| 460 |
c.Assert(fileStats[0].Addition, Equals, 7) |
| 461 |
c.Assert(fileStats[0].Deletion, Equals, 0) |
| 462 |
c.Assert(fileStats[0].String(), Equals, " vendor/foo.go | 7 +++++++\n") |
| 463 |
|
| 464 |
// Stats for another commit. |
| 465 |
aCommit = s.commit(c, plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294")) |
| 466 |
fileStats, err = aCommit.Stats() |
| 467 |
c.Assert(err, IsNil) |
| 468 |
|
| 469 |
c.Assert(fileStats[0].Name, Equals, "go/example.go") |
| 470 |
c.Assert(fileStats[0].Addition, Equals, 142) |
| 471 |
c.Assert(fileStats[0].Deletion, Equals, 0) |
| 472 |
c.Assert(fileStats[0].String(), Equals, " go/example.go | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++\n") |
| 473 |
|
| 474 |
c.Assert(fileStats[1].Name, Equals, "php/crappy.php") |
| 475 |
c.Assert(fileStats[1].Addition, Equals, 259) |
| 476 |
c.Assert(fileStats[1].Deletion, Equals, 0) |
| 477 |
c.Assert(fileStats[1].String(), Equals, " php/crappy.php | 259 +++++++++++++++++++++++++++++++++++++++++++++++++++++\n") |
| 478 |
} |
| 479 |
|
| 480 |
func (s *SuiteCommit) TestVerify(c *C) { |
| 481 |
ts := time.Unix(1617402711, 0) |
| 482 |
loc, _ := time.LoadLocation("UTC") |
| 483 |
commit := &Commit{ |
| 484 |
Hash: plumbing.NewHash("1eca38290a3131d0c90709496a9b2207a872631e"), |
| 485 |
Author: Signature{Name: "go-git", Email: "[email protected]", When: ts.In(loc)}, |
| 486 |
Committer: Signature{Name: "go-git", Email: "[email protected]", When: ts.In(loc)}, |
| 487 |
Message: `test |
| 488 |
`, |
| 489 |
TreeHash: plumbing.NewHash("52a266a58f2c028ad7de4dfd3a72fdf76b0d4e24"), |
| 490 |
ParentHashes: []plumbing.Hash{plumbing.NewHash("e4fbb611cd14149c7a78e9c08425f59f4b736a9a")}, |
| 491 |
PGPSignature: ` |
| 492 |
-----BEGIN PGP SIGNATURE----- |
| 493 |
|
| 494 |
iHUEABYKAB0WIQTMqU0ycQ3f6g3PMoWMmmmF4LuV8QUCYGebVwAKCRCMmmmF4LuV |
| 495 |
8VtyAP9LbuXAhtK6FQqOjKybBwlV70rLcXVP24ubDuz88VVwSgD+LuObsasWq6/U |
| 496 |
TssDKHUR2taa53bQYjkZQBpvvwOrLgc= |
| 497 |
=YQUf |
| 498 |
-----END PGP SIGNATURE----- |
| 499 |
`, |
| 500 |
} |
| 501 |
|
| 502 |
armoredKeyRing := ` |
| 503 |
-----BEGIN PGP PUBLIC KEY BLOCK----- |
| 504 |
|
| 505 |
mDMEYGeSihYJKwYBBAHaRw8BAQdAIs9A3YD/EghhAOkHDkxlUkpqYrXUXebLfmmX |
| 506 |
+pdEK6C0D2dvLWdpdCB0ZXN0IGtleYiPBBMWCgA3FiEEzKlNMnEN3+oNzzKFjJpp |
| 507 |
heC7lfEFAmBnkooCGyMECwkIBwUVCgkICwUWAwIBAAIeAQIXgAAKCRCMmmmF4LuV |
| 508 |
8a3jAQCi4hSqjj6J3ch290FvQaYPGwR+EMQTMBG54t+NN6sDfgD/aZy41+0dnFKl |
| 509 |
qM/wLW5Wr9XvwH+1zXXbuSvfxasHowq4OARgZ5KKEgorBgEEAZdVAQUBAQdAXoQz |
| 510 |
VTYug16SisAoSrxFnOmxmFu6efYgCAwXu0ZuvzsDAQgHiHgEGBYKACAWIQTMqU0y |
| 511 |
cQ3f6g3PMoWMmmmF4LuV8QUCYGeSigIbDAAKCRCMmmmF4LuV8Q4QAQCKW5FnEdWW |
| 512 |
lHYKeByw3JugnlZ0U3V/R20bCwDglst5UQEAtkN2iZkHtkPly9xapsfNqnrt2gTt |
| 513 |
YIefGtzXfldDxg4= |
| 514 |
=Psht |
| 515 |
-----END PGP PUBLIC KEY BLOCK----- |
| 516 |
` |
| 517 |
|
| 518 |
e, err := commit.Verify(armoredKeyRing) |
| 519 |
c.Assert(err, IsNil) |
| 520 |
|
| 521 |
_, ok := e.Identities["go-git test key"] |
| 522 |
c.Assert(ok, Equals, true) |
| 523 |
} |
| 524 |
|
| 525 |
func (s *SuiteCommit) TestPatchCancel(c *C) { |
| 526 |
from := s.commit(c, plumbing.NewHash("918c48b83bd081e863dbe1b80f8998f058cd8294")) |
| 527 |
to := s.commit(c, plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) |
| 528 |
|
| 529 |
ctx, cancel := context.WithCancel(context.Background()) |
| 530 |
cancel() |
| 531 |
patch, err := from.PatchContext(ctx, to) |
| 532 |
c.Assert(patch, IsNil) |
| 533 |
c.Assert(err, ErrorMatches, "operation canceled") |
| 534 |
|
| 535 |
} |
| 536 |
|
| 537 |
func (s *SuiteCommit) TestDecodeRequiresTreeFirst(c *C) { |
| 538 |
const ( |
| 539 |
validTree = "eba74343e2f15d62adedfd8c883ee0262b5c8021" |
| 540 |
validParent = "35e85108805c84807bc66a02d91535e1e24b38b9" |
| 541 |
validIdent = "Foo <[email protected]> 1427802494 +0200" |
| 542 |
) |
| 543 |
|
| 544 |
cases := []struct { |
| 545 |
name string |
| 546 |
raw string |
| 547 |
}{ |
| 548 |
{ |
| 549 |
name: "missing tree", |
| 550 |
raw: "author " + validIdent + "\ncommitter " + validIdent + "\n\nmsg\n", |
| 551 |
}, |
| 552 |
{ |
| 553 |
name: "parent before tree", |
| 554 |
raw: "parent " + validParent + "\ntree " + validTree + "\nauthor " + validIdent + "\ncommitter " + validIdent + "\n\nmsg\n", |
| 555 |
}, |
| 556 |
{ |
| 557 |
name: "extra header before tree", |
| 558 |
raw: "x-extra hi\ntree " + validTree + "\nauthor " + validIdent + "\ncommitter " + validIdent + "\n\nmsg\n", |
| 559 |
}, |
| 560 |
{ |
| 561 |
name: "empty", |
| 562 |
raw: "", |
| 563 |
}, |
| 564 |
{ |
| 565 |
name: "non-hex tree value", |
| 566 |
raw: "tree zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz\nauthor " + validIdent + "\ncommitter " + validIdent + "\n\nmsg\n", |
| 567 |
}, |
| 568 |
{ |
| 569 |
name: "tree value too short", |
| 570 |
raw: "tree abcd\nauthor " + validIdent + "\ncommitter " + validIdent + "\n\nmsg\n", |
| 571 |
}, |
| 572 |
{ |
| 573 |
name: "tree value too long", |
| 574 |
raw: "tree " + validTree + "00\nauthor " + validIdent + "\ncommitter " + validIdent + "\n\nmsg\n", |
| 575 |
}, |
| 576 |
{ |
| 577 |
name: "tree value missing", |
| 578 |
raw: "tree\nauthor " + validIdent + "\ncommitter " + validIdent + "\n\nmsg\n", |
| 579 |
}, |
| 580 |
{ |
| 581 |
name: "non-hex parent value", |
| 582 |
raw: "tree " + validTree + "\nparent zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz\nauthor " + validIdent + "\ncommitter " + validIdent + "\n\nmsg\n", |
| 583 |
}, |
| 584 |
{ |
| 585 |
name: "parent value too short", |
| 586 |
raw: "tree " + validTree + "\nparent abcd\nauthor " + validIdent + "\ncommitter " + validIdent + "\n\nmsg\n", |
| 587 |
}, |
| 588 |
} |
| 589 |
|
| 590 |
for _, tc := range cases { |
| 591 |
c.Log(tc.name) |
| 592 |
obj := &plumbing.MemoryObject{} |
| 593 |
obj.SetType(plumbing.CommitObject) |
| 594 |
_, err := obj.Write([]byte(tc.raw)) |
| 595 |
c.Assert(err, IsNil) |
| 596 |
|
| 597 |
err = (&Commit{}).Decode(obj) |
| 598 |
c.Assert(errors.Is(err, ErrMalformedCommit), Equals, true) |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
func (s *SuiteCommit) TestDecodeFirstOccurrenceWins(c *C) { |
| 603 |
const ( |
| 604 |
treeA = "eba74343e2f15d62adedfd8c883ee0262b5c8021" |
| 605 |
treeB = "0000000000000000000000000000000000000001" |
| 606 |
parentA = "35e85108805c84807bc66a02d91535e1e24b38b9" |
| 607 |
parentB = "a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69" |
| 608 |
parentC = "0000000000000000000000000000000000000002" |
| 609 |
identA = "Alice <[email protected]> 1427802494 +0200" |
| 610 |
identB = "Bob <[email protected]> 1427802495 +0200" |
| 611 |
identAuthor = "Author Name <[email protected]> 1500000000 +0000" |
| 612 |
identCommit = "Commit Name <[email protected]> 1500000001 +0000" |
| 613 |
) |
| 614 |
|
| 615 |
cases := []struct { |
| 616 |
name string |
| 617 |
raw string |
| 618 |
assert func(*Commit) |
| 619 |
}{ |
| 620 |
{ |
| 621 |
name: "duplicate tree drops the second", |
| 622 |
raw: "tree " + treeA + "\ntree " + treeB + |
| 623 |
"\nauthor " + identAuthor + "\ncommitter " + identCommit + "\n\nmsg\n", |
| 624 |
assert: func(commit *Commit) { |
| 625 |
c.Assert(commit.TreeHash.String(), Equals, treeA) |
| 626 |
}, |
| 627 |
}, |
| 628 |
{ |
| 629 |
name: "duplicate author drops the second", |
| 630 |
raw: "tree " + treeA + "\nauthor " + identA + "\nauthor " + identB + |
| 631 |
"\ncommitter " + identCommit + "\n\nmsg\n", |
| 632 |
assert: func(commit *Commit) { |
| 633 |
c.Assert(commit.Author.Name, Equals, "Alice") |
| 634 |
}, |
| 635 |
}, |
| 636 |
{ |
| 637 |
name: "duplicate committer drops the second", |
| 638 |
raw: "tree " + treeA + "\nauthor " + identAuthor + |
| 639 |
"\ncommitter " + identA + "\ncommitter " + identB + "\n\nmsg\n", |
| 640 |
assert: func(commit *Commit) { |
| 641 |
c.Assert(commit.Committer.Name, Equals, "Alice") |
| 642 |
}, |
| 643 |
}, |
| 644 |
{ |
| 645 |
name: "parent after author is dropped", |
| 646 |
raw: "tree " + treeA + "\nparent " + parentA + |
| 647 |
"\nauthor " + identAuthor + "\nparent " + parentC + |
| 648 |
"\ncommitter " + identCommit + "\n\nmsg\n", |
| 649 |
assert: func(commit *Commit) { |
| 650 |
c.Assert(commit.ParentHashes, DeepEquals, []plumbing.Hash{plumbing.NewHash(parentA)}) |
| 651 |
}, |
| 652 |
}, |
| 653 |
{ |
| 654 |
name: "parent interleaved with extras is dropped", |
| 655 |
raw: "tree " + treeA + "\nparent " + parentA + "\nparent " + parentB + |
| 656 |
"\nx-other thing\nparent " + parentC + |
| 657 |
"\nauthor " + identAuthor + "\ncommitter " + identCommit + "\n\nmsg\n", |
| 658 |
assert: func(commit *Commit) { |
| 659 |
c.Assert(commit.ParentHashes, DeepEquals, []plumbing.Hash{ |
| 660 |
plumbing.NewHash(parentA), |
| 661 |
plumbing.NewHash(parentB), |
| 662 |
}) |
| 663 |
}, |
| 664 |
}, |
| 665 |
{ |
| 666 |
name: "missing committer is allowed (zero-valued)", |
| 667 |
raw: "tree " + treeA + "\nauthor " + identAuthor + |
| 668 |
"\n\nmsg\n", |
| 669 |
assert: func(commit *Commit) { |
| 670 |
c.Assert(commit.Author.Name, Equals, "Author Name") |
| 671 |
c.Assert(commit.Committer.Name, Equals, "") |
| 672 |
}, |
| 673 |
}, |
| 674 |
{ |
| 675 |
name: "encoding between author and committer drops committer", |
| 676 |
raw: "tree " + treeA + "\nauthor " + identAuthor + |
| 677 |
"\nencoding latin-1\ncommitter " + identCommit + "\n\nmsg\n", |
| 678 |
assert: func(commit *Commit) { |
| 679 |
c.Assert(commit.Author.Name, Equals, "Author Name") |
| 680 |
c.Assert(commit.Encoding, Equals, MessageEncoding("latin-1")) |
| 681 |
// committer was not at its canonical position |
| 682 |
// (immediately after author) so it is dropped, matching |
| 683 |
// upstream's parse_commit_date returning 0 and the |
| 684 |
// subsequent standard_header_field filter. |
| 685 |
c.Assert(commit.Committer.Name, Equals, "") |
| 686 |
}, |
| 687 |
}, |
| 688 |
{ |
| 689 |
name: "author out of canonical position is dropped", |
| 690 |
raw: "tree " + treeA + "\nencoding latin-1\nauthor " + identAuthor + |
| 691 |
"\ncommitter " + identCommit + "\n\nmsg\n", |
| 692 |
assert: func(commit *Commit) { |
| 693 |
c.Assert(commit.Encoding, Equals, MessageEncoding("latin-1")) |
| 694 |
c.Assert(commit.Author.Name, Equals, "") |
| 695 |
c.Assert(commit.Committer.Name, Equals, "") |
| 696 |
}, |
| 697 |
}, |
| 698 |
{ |
| 699 |
// Multiple gpgsig headers concatenate into a single |
| 700 |
// signature buffer, mirroring upstream's |
| 701 |
// parse_buffer_signed_by_header (commit.c:1186). |
| 702 |
name: "multiple gpgsig headers concatenate", |
| 703 |
raw: "tree " + treeA + "\nauthor " + identAuthor + |
| 704 |
"\ncommitter " + identCommit + |
| 705 |
"\ngpgsig firstline\n morefirst\ngpgsig secondline\n moresecond\n\nmsg\n", |
| 706 |
assert: func(commit *Commit) { |
| 707 |
c.Assert(commit.PGPSignature, Equals, "firstline\nmorefirst\nsecondline\nmoresecond\n") |
| 708 |
}, |
| 709 |
}, |
| 710 |
{ |
| 711 |
name: "gpgsig-sha256 headers are not exposed as extras", |
| 712 |
raw: "tree " + treeA + "\nauthor " + identAuthor + |
| 713 |
"\ncommitter " + identCommit + |
| 714 |
"\ngpgsig-sha256 firstline\n morefirst\ngpgsig-sha256 secondline\n moresecond\n\nmsg\n", |
| 715 |
assert: func(commit *Commit) { |
| 716 |
c.Assert(commit.PGPSignature, Equals, "") |
| 717 |
c.Assert(len(commit.ExtraHeaders), Equals, 0) |
| 718 |
}, |
| 719 |
}, |
| 720 |
} |
| 721 |
|
| 722 |
for _, tc := range cases { |
| 723 |
c.Log(tc.name) |
| 724 |
obj := &plumbing.MemoryObject{} |
| 725 |
obj.SetType(plumbing.CommitObject) |
| 726 |
_, err := obj.Write([]byte(tc.raw)) |
| 727 |
c.Assert(err, IsNil) |
| 728 |
|
| 729 |
commit := &Commit{} |
| 730 |
c.Assert(commit.Decode(obj), IsNil) |
| 731 |
tc.assert(commit) |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
func (s *SuiteCommit) TestMalformedHeader(c *C) { |
| 736 |
encoded := &plumbing.MemoryObject{} |
| 737 |
decoded := &Commit{} |
| 738 |
commit := *s.Commit |
| 739 |
|
| 740 |
commit.PGPSignature = "\n" |
| 741 |
commit.Author.Name = "\n" |
| 742 |
commit.Author.Email = "\n" |
| 743 |
commit.Committer.Name = "\n" |
| 744 |
commit.Committer.Email = "\n" |
| 745 |
|
| 746 |
err := commit.Encode(encoded) |
| 747 |
c.Assert(err, IsNil) |
| 748 |
|
| 749 |
err = decoded.Decode(encoded) |
| 750 |
c.Assert(err, IsNil) |
| 751 |
} |
| 752 |
|
| 753 |
func (s *SuiteCommit) TestEncodeWithoutSignature(c *C) { |
| 754 |
tests := []struct { |
| 755 |
name string |
| 756 |
commitRaw string |
| 757 |
mutate func(*Commit) |
| 758 |
expected string |
| 759 |
}{ |
| 760 |
{ |
| 761 |
name: "commit without signature", |
| 762 |
commitRaw: `tree eba74343e2f15d62adedfd8c883ee0262b5c8021 |
| 763 |
parent 35e85108805c84807bc66a02d91535e1e24b38b9 |
| 764 |
parent a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69 |
| 765 |
author Máximo Cuadros Ortiz <[email protected]> 1427802494 +0200 |
| 766 |
committer Máximo Cuadros Ortiz <[email protected]> 1427802494 +0200 |
| 767 |
|
| 768 |
Merge branch 'master' of github.com:tyba/git-fixture |
| 769 |
`, |
| 770 |
expected: `tree eba74343e2f15d62adedfd8c883ee0262b5c8021 |
| 771 |
parent 35e85108805c84807bc66a02d91535e1e24b38b9 |
| 772 |
parent a5b8b09e2f8fcb0bb99d3ccb0958157b40890d69 |
| 773 |
author Máximo Cuadros Ortiz <[email protected]> 1427802494 +0200 |
| 774 |
committer Máximo Cuadros Ortiz <[email protected]> 1427802494 +0200 |
| 775 |
|
| 776 |
Merge branch 'master' of github.com:tyba/git-fixture |
| 777 |
`, |
| 778 |
}, |
| 779 |
{ |
| 780 |
name: "commit with change-id and gpgsig", |
| 781 |
commitRaw: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 782 |
author John Doe <[email protected]> 1755280730 -0700 |
| 783 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 784 |
change-id wxmuynokkzxmuwxwvnnpnptoyuypknwv |
| 785 |
gpgsig -----BEGIN PGP SIGNATURE----- |
| 786 |
iHUEABMIAB0WIQSZpnSpGKbQbDaLe5iiNQl48cTY5gUCaJ91XQAKCRCiNQl48cTY |
| 787 |
5vCYAP9Sf1yV9oUviRIxEA+4rsGIx0hI6kqFajJ/3TtBjyCTggD+PFnKOxdXeFL2 |
| 788 |
GLwcCzFIsmQmkLxuLypsg+vueDSLpsM= |
| 789 |
=VucY |
| 790 |
-----END PGP SIGNATURE----- |
| 791 |
|
| 792 |
initial commit |
| 793 |
|
| 794 |
Change-Id: I6a6a696432d51cbff02d53234ccaca6b151afc34 |
| 795 |
`, |
| 796 |
expected: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 797 |
author John Doe <[email protected]> 1755280730 -0700 |
| 798 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 799 |
change-id wxmuynokkzxmuwxwvnnpnptoyuypknwv |
| 800 |
|
| 801 |
initial commit |
| 802 |
|
| 803 |
Change-Id: I6a6a696432d51cbff02d53234ccaca6b151afc34 |
| 804 |
`, |
| 805 |
}, |
| 806 |
{ |
| 807 |
name: "gpgsig-sha256", |
| 808 |
commitRaw: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 809 |
author John Doe <[email protected]> 1755280730 -0700 |
| 810 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 811 |
change-id wxmuynokkzxmuwxwvnnpnptoyuypknwv |
| 812 |
gpgsig-sha256 -----BEGIN PGP SIGNATURE----- |
| 813 |
iHUEABMIAB0WIQSZpnSpGKbQbDaLe5iiNQl48cTY5gUCaJ91XQAKCRCiNQl48cTY |
| 814 |
5vCYAP9Sf1yV9oUviRIxEA+4rsGIx0hI6kqFajJ/3TtBjyCTggD+PFnKOxdXeFL2 |
| 815 |
GLwcCzFIsmQmkLxuLypsg+vueDSLpsM= |
| 816 |
=VucY |
| 817 |
-----END PGP SIGNATURE----- |
| 818 |
|
| 819 |
initial commit |
| 820 |
|
| 821 |
Change-Id: I6a6a696432d51cbff02d53234ccaca6b151afc34 |
| 822 |
`, |
| 823 |
expected: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 824 |
author John Doe <[email protected]> 1755280730 -0700 |
| 825 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 826 |
change-id wxmuynokkzxmuwxwvnnpnptoyuypknwv |
| 827 |
|
| 828 |
initial commit |
| 829 |
|
| 830 |
Change-Id: I6a6a696432d51cbff02d53234ccaca6b151afc34 |
| 831 |
`, |
| 832 |
}, |
| 833 |
{ |
| 834 |
name: "gpgsig + gpgsig-sha256", |
| 835 |
commitRaw: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 836 |
author John Doe <[email protected]> 1755280730 -0700 |
| 837 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 838 |
change-id wxmuynokkzxmuwxwvnnpnptoyuypknwv |
| 839 |
gpgsig -----BEGIN PGP SIGNATURE----- |
| 840 |
iHUEABMIAB0WIQSZpnSpGKbQbDaLe5iiNQl48cTY5gUCaJ91XQAKCRCiNQl48cTY |
| 841 |
GLwcCzFIsmQmkLxuLypsg+vueDSLpsM= |
| 842 |
=VucY |
| 843 |
-----END PGP SIGNATURE----- |
| 844 |
gpgsig-sha256 -----BEGIN PGP SIGNATURE----- |
| 845 |
iHUEABMIAB0WIQSZpnSpGKbQbDaLe5iiNQl48cTY5gUCaJ91XQAKCRCiNQl48cTY |
| 846 |
=VucY |
| 847 |
-----END PGP SIGNATURE----- |
| 848 |
|
| 849 |
initial commit |
| 850 |
|
| 851 |
Change-Id: I6a6a696432d51cbff02d53234ccaca6b151afc34 |
| 852 |
`, |
| 853 |
expected: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 854 |
author John Doe <[email protected]> 1755280730 -0700 |
| 855 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 856 |
change-id wxmuynokkzxmuwxwvnnpnptoyuypknwv |
| 857 |
|
| 858 |
initial commit |
| 859 |
|
| 860 |
Change-Id: I6a6a696432d51cbff02d53234ccaca6b151afc34 |
| 861 |
`, |
| 862 |
}, |
| 863 |
{ |
| 864 |
name: "non-canonical gpgsig-prefixed extra header is preserved", |
| 865 |
commitRaw: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 866 |
author John Doe <[email protected]> 1755280730 -0700 |
| 867 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 868 |
gpgsig-key-id ABCDEF0123456789 |
| 869 |
gpgsig -----BEGIN PGP SIGNATURE----- |
| 870 |
sigline1 |
| 871 |
-----END PGP SIGNATURE----- |
| 872 |
|
| 873 |
initial commit |
| 874 |
`, |
| 875 |
expected: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 876 |
author John Doe <[email protected]> 1755280730 -0700 |
| 877 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 878 |
gpgsig-key-id ABCDEF0123456789 |
| 879 |
|
| 880 |
initial commit |
| 881 |
`, |
| 882 |
}, |
| 883 |
{ |
| 884 |
name: "raw bytes preserved when only PGPSignature is mutated", |
| 885 |
commitRaw: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 886 |
author John Doe <[email protected]> 1755280730 -0700 |
| 887 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 888 |
gpgsig -----BEGIN PGP SIGNATURE----- |
| 889 |
sigline1 |
| 890 |
-----END PGP SIGNATURE----- |
| 891 |
gpgsig-sha256 -----BEGIN PGP SIGNATURE----- |
| 892 |
sha256line1 |
| 893 |
-----END PGP SIGNATURE----- |
| 894 |
|
| 895 |
initial commit |
| 896 |
`, |
| 897 |
mutate: func(c *Commit) { |
| 898 |
c.PGPSignature = "different signature value" |
| 899 |
}, |
| 900 |
expected: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 901 |
author John Doe <[email protected]> 1755280730 -0700 |
| 902 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 903 |
|
| 904 |
initial commit |
| 905 |
`, |
| 906 |
}, |
| 907 |
{ |
| 908 |
name: "timezone-only change triggers struct-encode", |
| 909 |
commitRaw: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 910 |
author John Doe <[email protected]> 1755280730 -0700 |
| 911 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 912 |
gpgsig -----BEGIN PGP SIGNATURE----- |
| 913 |
sigline1 |
| 914 |
-----END PGP SIGNATURE----- |
| 915 |
|
| 916 |
initial commit |
| 917 |
`, |
| 918 |
mutate: func(c *Commit) { |
| 919 |
tz := time.FixedZone("CEST", 2*60*60) |
| 920 |
c.Author.When = c.Author.When.In(tz) |
| 921 |
c.Committer.When = c.Committer.When.In(tz) |
| 922 |
}, |
| 923 |
expected: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 924 |
author John Doe <[email protected]> 1755280730 +0200 |
| 925 |
committer John Doe <[email protected]> 1755280730 +0200 |
| 926 |
|
| 927 |
initial commit |
| 928 |
`, |
| 929 |
}, |
| 930 |
{ |
| 931 |
name: "field mutation triggers struct-encode", |
| 932 |
commitRaw: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 933 |
author John Doe <[email protected]> 1755280730 -0700 |
| 934 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 935 |
gpgsig -----BEGIN PGP SIGNATURE----- |
| 936 |
sigline1 |
| 937 |
-----END PGP SIGNATURE----- |
| 938 |
|
| 939 |
initial commit |
| 940 |
`, |
| 941 |
mutate: func(c *Commit) { |
| 942 |
c.Message = "rewritten message\n" |
| 943 |
}, |
| 944 |
expected: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 945 |
author John Doe <[email protected]> 1755280730 -0700 |
| 946 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 947 |
|
| 948 |
rewritten message |
| 949 |
`, |
| 950 |
}, |
| 951 |
{ |
| 952 |
name: "standard-keyed ExtraHeaders are dropped on encode", |
| 953 |
commitRaw: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 954 |
author John Doe <[email protected]> 1755280730 -0700 |
| 955 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 956 |
|
| 957 |
initial commit |
| 958 |
`, |
| 959 |
mutate: func(c *Commit) { |
| 960 |
c.Message = "rewritten message\n" |
| 961 |
c.ExtraHeaders = []ExtraHeader{ |
| 962 |
{Key: "tree", Value: "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"}, |
| 963 |
{Key: "parent", Value: "feedfacefeedfacefeedfacefeedfacefeedface"}, |
| 964 |
{Key: "author", Value: "Fake <[email protected]> 0 +0000"}, |
| 965 |
{Key: "committer", Value: "Fake <[email protected]> 0 +0000"}, |
| 966 |
{Key: "encoding", Value: "fake-enc"}, |
| 967 |
{Key: "mergetag", Value: "fake mergetag"}, |
| 968 |
{Key: "gpgsig", Value: "fake-sig"}, |
| 969 |
{Key: "gpgsig-sha256", Value: "fake-sig-256"}, |
| 970 |
{Key: "x-real-extra", Value: "kept"}, |
| 971 |
} |
| 972 |
}, |
| 973 |
expected: `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 974 |
author John Doe <[email protected]> 1755280730 -0700 |
| 975 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 976 |
x-real-extra kept |
| 977 |
|
| 978 |
rewritten message |
| 979 |
`, |
| 980 |
}, |
| 981 |
} |
| 982 |
|
| 983 |
for _, tc := range tests { |
| 984 |
c.Log(tc.name) |
| 985 |
|
| 986 |
obj := &plumbing.MemoryObject{} |
| 987 |
obj.SetType(plumbing.CommitObject) |
| 988 |
obj.Write([]byte(tc.commitRaw)) |
| 989 |
|
| 990 |
commit, err := DecodeCommit(s.Storer, obj) |
| 991 |
c.Assert(err, IsNil) |
| 992 |
if tc.mutate != nil { |
| 993 |
tc.mutate(commit) |
| 994 |
} |
| 995 |
|
| 996 |
encoded := &plumbing.MemoryObject{} |
| 997 |
err = commit.EncodeWithoutSignature(encoded) |
| 998 |
c.Assert(err, IsNil) |
| 999 |
|
| 1000 |
er, err := encoded.Reader() |
| 1001 |
c.Assert(err, IsNil) |
| 1002 |
|
| 1003 |
payload, err := io.ReadAll(er) |
| 1004 |
c.Assert(err, IsNil) |
| 1005 |
|
| 1006 |
c.Assert(string(payload), Equals, tc.expected) |
| 1007 |
} |
| 1008 |
} |
| 1009 |
|
| 1010 |
func (s *SuiteCommit) TestEncodeExtraHeaders(c *C) { |
| 1011 |
object := &plumbing.MemoryObject{} |
| 1012 |
object.SetType(plumbing.CommitObject) |
| 1013 |
object.Write([]byte(`tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 1014 |
author John Doe <[email protected]> 1755280730 -0700 |
| 1015 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 1016 |
continuedheader to be |
| 1017 |
continued |
| 1018 |
continuedheader to be |
| 1019 |
continued |
| 1020 |
on |
| 1021 |
more than |
| 1022 |
a single line |
| 1023 |
simpleflag |
| 1024 |
value no key |
| 1025 |
|
| 1026 |
initial commit |
| 1027 |
`)) |
| 1028 |
|
| 1029 |
commit, err := DecodeCommit(s.Storer, object) |
| 1030 |
c.Assert(err, IsNil) |
| 1031 |
|
| 1032 |
c.Assert(commit.ExtraHeaders, DeepEquals, []ExtraHeader{ |
| 1033 |
ExtraHeader{ |
| 1034 |
Key: "continuedheader", |
| 1035 |
Value: "to be\ncontinued", |
| 1036 |
}, |
| 1037 |
ExtraHeader{ |
| 1038 |
Key: "continuedheader", |
| 1039 |
Value: "to be\ncontinued\non\nmore than\na single line", |
| 1040 |
}, |
| 1041 |
ExtraHeader{ |
| 1042 |
Key: "simpleflag", |
| 1043 |
Value: "", |
| 1044 |
}, |
| 1045 |
ExtraHeader{ |
| 1046 |
Key: "", |
| 1047 |
Value: "value no key", |
| 1048 |
}, |
| 1049 |
}) |
| 1050 |
|
| 1051 |
// Similar to TestString since no signature |
| 1052 |
encoded := &plumbing.MemoryObject{} |
| 1053 |
err = commit.EncodeWithoutSignature(encoded) |
| 1054 |
er, err := encoded.Reader() |
| 1055 |
c.Assert(err, IsNil) |
| 1056 |
payload, err := io.ReadAll(er) |
| 1057 |
c.Assert(err, IsNil) |
| 1058 |
|
| 1059 |
c.Assert(string(payload), Equals, `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 1060 |
author John Doe <[email protected]> 1755280730 -0700 |
| 1061 |
committer John Doe <[email protected]> 1755280730 -0700 |
| 1062 |
continuedheader to be |
| 1063 |
continued |
| 1064 |
continuedheader to be |
| 1065 |
continued |
| 1066 |
on |
| 1067 |
more than |
| 1068 |
a single line |
| 1069 |
simpleflag |
| 1070 |
value no key |
| 1071 |
|
| 1072 |
initial commit |
| 1073 |
`) |
| 1074 |
} |
| 1075 |
|
| 1076 |
func (s *SuiteCommit) TestLess(c *C) { |
| 1077 |
when1 := time.Now() |
| 1078 |
when2 := when1.Add(time.Hour) |
| 1079 |
|
| 1080 |
hash1 := plumbing.NewHash("1669dce138d9b841a518c64b10914d88f5e488ea") |
| 1081 |
hash2 := plumbing.NewHash("2669dce138d9b841a518c64b10914d88f5e488ea") |
| 1082 |
|
| 1083 |
commitLessTests := []struct { |
| 1084 |
Committer1When, Committer2When time.Time |
| 1085 |
Author1When, Author2When time.Time |
| 1086 |
Hash1, Hash2 plumbing.Hash |
| 1087 |
Exp bool |
| 1088 |
}{ |
| 1089 |
{when1, when1, when1, when1, hash1, hash2, true}, |
| 1090 |
{when1, when1, when1, when1, hash2, hash1, false}, |
| 1091 |
{when1, when1, when1, when2, hash1, hash2, true}, |
| 1092 |
{when1, when1, when1, when2, hash2, hash1, true}, |
| 1093 |
{when1, when1, when2, when1, hash1, hash2, false}, |
| 1094 |
{when1, when1, when2, when1, hash2, hash1, false}, |
| 1095 |
{when1, when1, when2, when2, hash1, hash2, true}, |
| 1096 |
{when1, when1, when2, when2, hash2, hash1, false}, |
| 1097 |
{when1, when2, when1, when1, hash1, hash2, true}, |
| 1098 |
{when1, when2, when1, when1, hash2, hash1, true}, |
| 1099 |
{when1, when2, when1, when2, hash1, hash2, true}, |
| 1100 |
{when1, when2, when1, when2, hash2, hash1, true}, |
| 1101 |
{when1, when2, when2, when1, hash1, hash2, true}, |
| 1102 |
{when1, when2, when2, when1, hash2, hash1, true}, |
| 1103 |
{when1, when2, when2, when2, hash1, hash2, true}, |
| 1104 |
{when1, when2, when2, when2, hash2, hash1, true}, |
| 1105 |
{when2, when1, when1, when1, hash1, hash2, false}, |
| 1106 |
{when2, when1, when1, when1, hash2, hash1, false}, |
| 1107 |
{when2, when1, when1, when2, hash1, hash2, false}, |
| 1108 |
{when2, when1, when1, when2, hash2, hash1, false}, |
| 1109 |
{when2, when1, when2, when1, hash1, hash2, false}, |
| 1110 |
{when2, when1, when2, when1, hash2, hash1, false}, |
| 1111 |
{when2, when1, when2, when2, hash1, hash2, false}, |
| 1112 |
{when2, when1, when2, when2, hash2, hash1, false}, |
| 1113 |
{when2, when2, when1, when1, hash1, hash2, true}, |
| 1114 |
{when2, when2, when1, when1, hash2, hash1, false}, |
| 1115 |
{when2, when2, when1, when2, hash1, hash2, true}, |
| 1116 |
{when2, when2, when1, when2, hash2, hash1, true}, |
| 1117 |
{when2, when2, when2, when1, hash1, hash2, false}, |
| 1118 |
{when2, when2, when2, when1, hash2, hash1, false}, |
| 1119 |
{when2, when2, when2, when2, hash1, hash2, true}, |
| 1120 |
{when2, when2, when2, when2, hash2, hash1, false}, |
| 1121 |
} |
| 1122 |
|
| 1123 |
for _, t := range commitLessTests { |
| 1124 |
commit1 := &Commit{ |
| 1125 |
Hash: t.Hash1, |
| 1126 |
Author: Signature{ |
| 1127 |
When: t.Author1When, |
| 1128 |
}, |
| 1129 |
Committer: Signature{ |
| 1130 |
When: t.Committer1When, |
| 1131 |
}, |
| 1132 |
} |
| 1133 |
commit2 := &Commit{ |
| 1134 |
Hash: t.Hash2, |
| 1135 |
Author: Signature{ |
| 1136 |
When: t.Author2When, |
| 1137 |
}, |
| 1138 |
Committer: Signature{ |
| 1139 |
When: t.Committer2When, |
| 1140 |
}, |
| 1141 |
} |
| 1142 |
c.Assert(commit1.Less(commit2), Equals, t.Exp) |
| 1143 |
} |
| 1144 |
} |