buildSyntheticCommitChain constructs a linear chain of n commits plus (when branch=true) a second branch fork from the second commit. The commits share a tree to keep the pack small. Returns the raw pack bytes and the expected (commit -> parent hashes) map.
(t *testing.T, n int, branch bool)
| 19 | // commits share a tree to keep the pack small. Returns the raw pack |
| 20 | // bytes and the expected (commit -> parent hashes) map. |
| 21 | func buildSyntheticCommitChain(t *testing.T, n int, branch bool) ([]byte, map[plumbing.Hash][]plumbing.Hash) { |
| 22 | t.Helper() |
| 23 | store := memory.NewStorage() |
| 24 | |
| 25 | // One shared tree to keep the pack lean. |
| 26 | tree := &object.Tree{Entries: []object.TreeEntry{ |
| 27 | {Name: "f", Mode: 0o100644, Hash: writeBlob(t, store, "v")}, |
| 28 | }} |
| 29 | treeObj := store.NewEncodedObject() |
| 30 | if err := tree.Encode(treeObj); err != nil { |
| 31 | t.Fatalf("tree encode: %v", err) |
| 32 | } |
| 33 | treeHash, err := store.SetEncodedObject(treeObj) |
| 34 | if err != nil { |
| 35 | t.Fatalf("tree set: %v", err) |
| 36 | } |
| 37 | |
| 38 | hashes := []plumbing.Hash{treeHash} |
| 39 | expected := map[plumbing.Hash][]plumbing.Hash{} |
| 40 | |
| 41 | var prev plumbing.Hash |
| 42 | for i := range n { |
| 43 | c := &object.Commit{ |
| 44 | TreeHash: treeHash, |
| 45 | Author: object.Signature{Name: "T", Email: "t@example", When: time.Unix(int64(i), 0)}, |
| 46 | Committer: object.Signature{Name: "T", Email: "t@example", When: time.Unix(int64(i), 0)}, |
| 47 | Message: "c" + string(rune('0'+i)), |
| 48 | } |
| 49 | if !prev.IsZero() { |
| 50 | c.ParentHashes = []plumbing.Hash{prev} |
| 51 | } |
| 52 | obj := store.NewEncodedObject() |
| 53 | if err := c.Encode(obj); err != nil { |
| 54 | t.Fatalf("commit encode: %v", err) |
| 55 | } |
| 56 | h, err := store.SetEncodedObject(obj) |
| 57 | if err != nil { |
| 58 | t.Fatalf("commit set: %v", err) |
| 59 | } |
| 60 | hashes = append(hashes, h) |
| 61 | if prev.IsZero() { |
| 62 | expected[h] = nil |
| 63 | } else { |
| 64 | expected[h] = []plumbing.Hash{prev} |
| 65 | } |
| 66 | prev = h |
| 67 | } |
| 68 | |
| 69 | if branch && n >= 2 { |
| 70 | // Find the second commit's hash (index 1 of commits, hashes[2]) |
| 71 | fork := hashes[2] |
| 72 | c := &object.Commit{ |
| 73 | TreeHash: treeHash, |
| 74 | Author: object.Signature{Name: "T", Email: "t@example", When: time.Unix(int64(n+1), 0)}, |
| 75 | Committer: object.Signature{Name: "T", Email: "t@example", When: time.Unix(int64(n+1), 0)}, |
| 76 | Message: "branch", |
| 77 | ParentHashes: []plumbing.Hash{fork}, |
| 78 | } |
no test coverage detected