buildTestPack assembles a pack with the given objects, returning the raw pack bytes. Object offsets are not exposed because the only caller does not need them.
(t *testing.T, objects ...testPackObject)
| 74 | // pack bytes. Object offsets are not exposed because the only caller does |
| 75 | // not need them. |
| 76 | func buildTestPack(t *testing.T, objects ...testPackObject) []byte { |
| 77 | t.Helper() |
| 78 | |
| 79 | var body bytes.Buffer |
| 80 | body.WriteString("PACK") |
| 81 | require.NoError(t, binary.Write(&body, binary.BigEndian, uint32(2))) |
| 82 | require.NoError(t, binary.Write(&body, binary.BigEndian, uint32(len(objects)))) |
| 83 | |
| 84 | offsets := make([]int64, 0, len(objects)) |
| 85 | for _, obj := range objects { |
| 86 | offsets = append(offsets, int64(body.Len())) |
| 87 | declaredSize := obj.declaredSize |
| 88 | if declaredSize == 0 && len(obj.content) > 0 { |
| 89 | declaredSize = int64(len(obj.content)) |
| 90 | } |
| 91 | |
| 92 | writeTestObjectHeader(&body, obj.typ, declaredSize) |
| 93 | switch obj.typ { |
| 94 | case plumbing.REFDeltaObject: |
| 95 | body.Write(obj.reference[:]) |
| 96 | case plumbing.OFSDeltaObject: |
| 97 | distance := obj.offsetDeltaDistance |
| 98 | if distance == -1 { |
| 99 | // Reference the immediately preceding object. |
| 100 | distance = offsets[len(offsets)-1] - offsets[len(offsets)-2] |
| 101 | } |
| 102 | require.NoError(t, gogitbinary.WriteVariableWidthInt(&body, distance)) |
| 103 | } |
| 104 | body.Write(zlibCompress(t, obj.content)) |
| 105 | } |
| 106 | |
| 107 | sum := sha1.Sum(body.Bytes()) |
| 108 | body.Write(sum[:]) |
| 109 | return body.Bytes() |
| 110 | } |
| 111 | |
| 112 | func writeTestObjectHeader(w io.ByteWriter, typ plumbing.ObjectType, size int64) { |
| 113 | remaining := uint64(size) |
no test coverage detected
searching dependent graphs…