Debian Codegraph @

plumbing/format/commitgraph/v2/encoder.go

raw
1 package v2
2
3 import (
4 "crypto"
5 "io"
6 "math"
7
8 "github.com/go-git/go-git/v5/plumbing"
9 "github.com/go-git/go-git/v5/plumbing/hash"
10 "github.com/go-git/go-git/v5/utils/binary"
11 )
12
13 // Encoder writes MemoryIndex structs to an output stream.
14 type Encoder struct {
15 io.Writer
16 hash hash.Hash
17 }
18
19 // NewEncoder returns a new stream encoder that writes to w.
20 func NewEncoder(w io.Writer) *Encoder {
21 h := hash.New(hash.CryptoType)
22 mw := io.MultiWriter(w, h)
23 return &Encoder{mw, h}
24 }
25
26 // Encode writes an index into the commit-graph file
27 func (e *Encoder) Encode(idx Index) error {
28 // Get all the hashes in the input index
29 hashes := idx.Hashes()
30
31 // Sort the inout and prepare helper structures we'll need for encoding
32 hashToIndex, fanout, extraEdgesCount, generationV2OverflowCount := e.prepare(idx, hashes)
33
34 chunkSignatures := [][]byte{OIDFanoutChunk.Signature(), OIDLookupChunk.Signature(), CommitDataChunk.Signature()}
35 chunkSizes := []uint64{szUint32 * lenFanout, uint64(len(hashes)) * hash.Size, uint64(len(hashes)) * (hash.Size + szCommitData)}
36 if extraEdgesCount > 0 {
37 chunkSignatures = append(chunkSignatures, ExtraEdgeListChunk.Signature())
38 chunkSizes = append(chunkSizes, uint64(extraEdgesCount)*szUint32)
39 }
40 if idx.HasGenerationV2() {
41 chunkSignatures = append(chunkSignatures, GenerationDataChunk.Signature())
42 chunkSizes = append(chunkSizes, uint64(len(hashes))*szUint32)
43 if generationV2OverflowCount > 0 {
44 chunkSignatures = append(chunkSignatures, GenerationDataOverflowChunk.Signature())
45 chunkSizes = append(chunkSizes, uint64(generationV2OverflowCount)*szUint64)
46 }
47 }
48
49 if err := e.encodeFileHeader(len(chunkSignatures)); err != nil {
50 return err
51 }
52 if err := e.encodeChunkHeaders(chunkSignatures, chunkSizes); err != nil {
53 return err
54 }
55 if err := e.encodeFanout(fanout); err != nil {
56 return err
57 }
58 if err := e.encodeOidLookup(hashes); err != nil {
59 return err
60 }
61
62 extraEdges, generationV2Data, err := e.encodeCommitData(hashes, hashToIndex, idx)
63 if err != nil {
64 return err
65 }
66 if err = e.encodeExtraEdges(extraEdges); err != nil {
67 return err
68 }
69 if idx.HasGenerationV2() {
70 overflows, err := e.encodeGenerationV2Data(generationV2Data)
71 if err != nil {
72 return err
73 }
74 if err = e.encodeGenerationV2Overflow(overflows); err != nil {
75 return err
76 }
77 }
78
79 return e.encodeChecksum()
80 }
81
82 func (e *Encoder) prepare(idx Index, hashes []plumbing.Hash) (hashToIndex map[plumbing.Hash]uint32, fanout []uint32, extraEdgesCount uint32, generationV2OverflowCount uint32) {
83 // Sort the hashes and build our index
84 plumbing.HashesSort(hashes)
85 hashToIndex = make(map[plumbing.Hash]uint32)
86 fanout = make([]uint32, lenFanout)
87 for i, hash := range hashes {
88 hashToIndex[hash] = uint32(i)
89 fanout[hash[0]]++
90 }
91
92 // Convert the fanout to cumulative values
93 for i := 1; i < lenFanout; i++ {
94 fanout[i] += fanout[i-1]
95 }
96
97 hasGenerationV2 := idx.HasGenerationV2()
98
99 // Find out if we will need extra edge table
100 for i := 0; i < len(hashes); i++ {
101 v, _ := idx.GetCommitDataByIndex(uint32(i))
102 if len(v.ParentHashes) > 2 {
103 extraEdgesCount += uint32(len(v.ParentHashes) - 1)
104 }
105 if hasGenerationV2 && v.GenerationV2Data() > math.MaxUint32 {
106 generationV2OverflowCount++
107 }
108 }
109
110 return
111 }
112
113 func (e *Encoder) encodeFileHeader(chunkCount int) (err error) {
114 if _, err = e.Write(commitFileSignature); err == nil {
115 version := byte(1)
116 if hash.CryptoType == crypto.SHA256 {
117 version = byte(2)
118 }
119 _, err = e.Write([]byte{1, version, byte(chunkCount), 0})
120 }
121 return
122 }
123
124 func (e *Encoder) encodeChunkHeaders(chunkSignatures [][]byte, chunkSizes []uint64) (err error) {
125 // 8 bytes of file header, 12 bytes for each chunk header and 12 byte for terminator
126 offset := uint64(szSignature + szHeader + (len(chunkSignatures)+1)*(szChunkSig+szUint64))
127 for i, signature := range chunkSignatures {
128 if _, err = e.Write(signature); err == nil {
129 err = binary.WriteUint64(e, offset)
130 }
131 if err != nil {
132 return
133 }
134 offset += chunkSizes[i]
135 }
136 if _, err = e.Write(ZeroChunk.Signature()); err == nil {
137 err = binary.WriteUint64(e, offset)
138 }
139 return
140 }
141
142 func (e *Encoder) encodeFanout(fanout []uint32) (err error) {
143 for i := 0; i <= 0xff; i++ {
144 if err = binary.WriteUint32(e, fanout[i]); err != nil {
145 return
146 }
147 }
148 return
149 }
150
151 func (e *Encoder) encodeOidLookup(hashes []plumbing.Hash) (err error) {
152 for _, hash := range hashes {
153 if _, err = e.Write(hash[:]); err != nil {
154 return err
155 }
156 }
157 return
158 }
159
160 func (e *Encoder) encodeCommitData(hashes []plumbing.Hash, hashToIndex map[plumbing.Hash]uint32, idx Index) (extraEdges []uint32, generationV2Data []uint64, err error) {
161 if idx.HasGenerationV2() {
162 generationV2Data = make([]uint64, 0, len(hashes))
163 }
164 for _, hash := range hashes {
165 origIndex, _ := idx.GetIndexByHash(hash)
166 commitData, _ := idx.GetCommitDataByIndex(origIndex)
167 if _, err = e.Write(commitData.TreeHash[:]); err != nil {
168 return
169 }
170
171 var parent1, parent2 uint32
172 if len(commitData.ParentHashes) == 0 {
173 parent1 = parentNone
174 parent2 = parentNone
175 } else if len(commitData.ParentHashes) == 1 {
176 parent1 = hashToIndex[commitData.ParentHashes[0]]
177 parent2 = parentNone
178 } else if len(commitData.ParentHashes) == 2 {
179 parent1 = hashToIndex[commitData.ParentHashes[0]]
180 parent2 = hashToIndex[commitData.ParentHashes[1]]
181 } else if len(commitData.ParentHashes) > 2 {
182 parent1 = hashToIndex[commitData.ParentHashes[0]]
183 parent2 = uint32(len(extraEdges)) | parentOctopusUsed
184 for _, parentHash := range commitData.ParentHashes[1:] {
185 extraEdges = append(extraEdges, hashToIndex[parentHash])
186 }
187 extraEdges[len(extraEdges)-1] |= parentLast
188 }
189
190 if err = binary.WriteUint32(e, parent1); err == nil {
191 err = binary.WriteUint32(e, parent2)
192 }
193 if err != nil {
194 return
195 }
196
197 unixTime := uint64(commitData.When.Unix())
198 unixTime |= uint64(commitData.Generation) << 34
199 if err = binary.WriteUint64(e, unixTime); err != nil {
200 return
201 }
202 if generationV2Data != nil {
203 generationV2Data = append(generationV2Data, commitData.GenerationV2Data())
204 }
205 }
206 return
207 }
208
209 func (e *Encoder) encodeExtraEdges(extraEdges []uint32) (err error) {
210 for _, parent := range extraEdges {
211 if err = binary.WriteUint32(e, parent); err != nil {
212 return
213 }
214 }
215 return
216 }
217
218 func (e *Encoder) encodeGenerationV2Data(generationV2Data []uint64) (overflows []uint64, err error) {
219 head := 0
220 for _, data := range generationV2Data {
221 if data >= 0x80000000 {
222 // overflow
223 if err = binary.WriteUint32(e, uint32(head)|0x80000000); err != nil {
224 return nil, err
225 }
226 generationV2Data[head] = data
227 head++
228 continue
229 }
230 if err = binary.WriteUint32(e, uint32(data)); err != nil {
231 return nil, err
232 }
233 }
234
235 return generationV2Data[:head], nil
236 }
237
238 func (e *Encoder) encodeGenerationV2Overflow(overflows []uint64) (err error) {
239 for _, overflow := range overflows {
240 if err = binary.WriteUint64(e, overflow); err != nil {
241 return
242 }
243 }
244 return
245 }
246
247 func (e *Encoder) encodeChecksum() error {
248 _, err := e.Write(e.hash.Sum(nil)[:hash.Size])
249 return err
250 }