Debian Codegraph @

plumbing/format/commitgraph/file.go

raw
1 package commitgraph
2
3 import (
4 "bytes"
5 "crypto"
6 encbin "encoding/binary"
7 "errors"
8 "io"
9 "time"
10
11 "github.com/go-git/go-git/v5/plumbing"
12 "github.com/go-git/go-git/v5/plumbing/hash"
13 "github.com/go-git/go-git/v5/utils/binary"
14 )
15
16 // Deprecated: This package uses the wrong types for Generation and Index in CommitData.
17 // Use the v2 package instead.
18
19 var (
20 // ErrUnsupportedVersion is returned by OpenFileIndex when the commit graph
21 // file version is not supported.
22 ErrUnsupportedVersion = errors.New("unsupported version")
23 // ErrUnsupportedHash is returned by OpenFileIndex when the commit graph
24 // hash function is not supported. Currently only SHA-1 is defined and
25 // supported
26 ErrUnsupportedHash = errors.New("unsupported hash algorithm")
27 // ErrMalformedCommitGraphFile is returned by OpenFileIndex when the commit
28 // graph file is corrupted.
29 ErrMalformedCommitGraphFile = errors.New("malformed commit graph file")
30
31 commitFileSignature = []byte{'C', 'G', 'P', 'H'}
32 oidFanoutSignature = []byte{'O', 'I', 'D', 'F'}
33 oidLookupSignature = []byte{'O', 'I', 'D', 'L'}
34 commitDataSignature = []byte{'C', 'D', 'A', 'T'}
35 extraEdgeListSignature = []byte{'E', 'D', 'G', 'E'}
36 lastSignature = []byte{0, 0, 0, 0}
37
38 parentNone = uint32(0x70000000)
39 parentOctopusUsed = uint32(0x80000000)
40 parentOctopusMask = uint32(0x7fffffff)
41 parentLast = uint32(0x80000000)
42 )
43
44 const commitDataSize = 16
45
46 type fileIndex struct {
47 reader io.ReaderAt
48 fanout [256]int
49 oidFanoutOffset int64
50 oidLookupOffset int64
51 commitDataOffset int64
52 extraEdgeListOffset int64
53 }
54
55 // OpenFileIndex opens a serialized commit graph file in the format described at
56 // https://github.com/git/git/blob/master/Documentation/technical/commit-graph-format.txt
57 //
58 // Deprecated: This package uses the wrong types for Generation and Index in CommitData.
59 // Use the v2 package instead.
60 func OpenFileIndex(reader io.ReaderAt) (Index, error) {
61 fi := &fileIndex{reader: reader}
62
63 if err := fi.verifyFileHeader(); err != nil {
64 return nil, err
65 }
66 if err := fi.readChunkHeaders(); err != nil {
67 return nil, err
68 }
69 if err := fi.readFanout(); err != nil {
70 return nil, err
71 }
72
73 return fi, nil
74 }
75
76 func (fi *fileIndex) verifyFileHeader() error {
77 // Verify file signature
78 signature := make([]byte, 4)
79 if _, err := fi.reader.ReadAt(signature, 0); err != nil {
80 return err
81 }
82 if !bytes.Equal(signature, commitFileSignature) {
83 return ErrMalformedCommitGraphFile
84 }
85
86 // Read and verify the file header
87 header := make([]byte, 4)
88 if _, err := fi.reader.ReadAt(header, 4); err != nil {
89 return err
90 }
91 if header[0] != 1 {
92 return ErrUnsupportedVersion
93 }
94 if hash.CryptoType == crypto.SHA1 {
95 if header[1] != 1 {
96 return ErrUnsupportedVersion
97 }
98 } else if hash.CryptoType == crypto.SHA256 {
99 if header[1] != 2 {
100 return ErrUnsupportedVersion
101 }
102 } else {
103 // Unknown hash type
104 return ErrUnsupportedVersion
105 }
106
107 return nil
108 }
109
110 func (fi *fileIndex) readChunkHeaders() error {
111 chunkID := make([]byte, 4)
112 for i := 0; ; i++ {
113 chunkHeader := io.NewSectionReader(fi.reader, 8+(int64(i)*12), 12)
114 if _, err := io.ReadAtLeast(chunkHeader, chunkID, 4); err != nil {
115 return err
116 }
117 chunkOffset, err := binary.ReadUint64(chunkHeader)
118 if err != nil {
119 return err
120 }
121
122 if bytes.Equal(chunkID, oidFanoutSignature) {
123 fi.oidFanoutOffset = int64(chunkOffset)
124 } else if bytes.Equal(chunkID, oidLookupSignature) {
125 fi.oidLookupOffset = int64(chunkOffset)
126 } else if bytes.Equal(chunkID, commitDataSignature) {
127 fi.commitDataOffset = int64(chunkOffset)
128 } else if bytes.Equal(chunkID, extraEdgeListSignature) {
129 fi.extraEdgeListOffset = int64(chunkOffset)
130 } else if bytes.Equal(chunkID, lastSignature) {
131 break
132 }
133 }
134
135 if fi.oidFanoutOffset <= 0 || fi.oidLookupOffset <= 0 || fi.commitDataOffset <= 0 {
136 return ErrMalformedCommitGraphFile
137 }
138
139 return nil
140 }
141
142 func (fi *fileIndex) readFanout() error {
143 fanoutReader := io.NewSectionReader(fi.reader, fi.oidFanoutOffset, 256*4)
144 for i := 0; i < 256; i++ {
145 fanoutValue, err := binary.ReadUint32(fanoutReader)
146 if err != nil {
147 return err
148 }
149 if fanoutValue > 0x7fffffff {
150 return ErrMalformedCommitGraphFile
151 }
152 fi.fanout[i] = int(fanoutValue)
153 }
154 return nil
155 }
156
157 func (fi *fileIndex) GetIndexByHash(h plumbing.Hash) (int, error) {
158 var oid plumbing.Hash
159
160 // Find the hash in the oid lookup table
161 var low int
162 if h[0] == 0 {
163 low = 0
164 } else {
165 low = fi.fanout[h[0]-1]
166 }
167 high := fi.fanout[h[0]]
168 for low < high {
169 mid := (low + high) >> 1
170 offset := fi.oidLookupOffset + int64(mid)*hash.Size
171 if _, err := fi.reader.ReadAt(oid[:], offset); err != nil {
172 return 0, err
173 }
174 cmp := bytes.Compare(h[:], oid[:])
175 if cmp < 0 {
176 high = mid
177 } else if cmp == 0 {
178 return mid, nil
179 } else {
180 low = mid + 1
181 }
182 }
183
184 return 0, plumbing.ErrObjectNotFound
185 }
186
187 func (fi *fileIndex) GetCommitDataByIndex(idx int) (*CommitData, error) {
188 if idx >= fi.fanout[0xff] {
189 return nil, plumbing.ErrObjectNotFound
190 }
191
192 offset := fi.commitDataOffset + int64(idx)*(hash.Size+commitDataSize)
193 commitDataReader := io.NewSectionReader(fi.reader, offset, hash.Size+commitDataSize)
194
195 treeHash, err := binary.ReadHash(commitDataReader)
196 if err != nil {
197 return nil, err
198 }
199 parent1, err := binary.ReadUint32(commitDataReader)
200 if err != nil {
201 return nil, err
202 }
203 parent2, err := binary.ReadUint32(commitDataReader)
204 if err != nil {
205 return nil, err
206 }
207 genAndTime, err := binary.ReadUint64(commitDataReader)
208 if err != nil {
209 return nil, err
210 }
211
212 var parentIndexes []int
213 if parent2&parentOctopusUsed == parentOctopusUsed {
214 // Octopus merge
215 parentIndexes = []int{int(parent1 & parentOctopusMask)}
216 offset := fi.extraEdgeListOffset + 4*int64(parent2&parentOctopusMask)
217 buf := make([]byte, 4)
218 for {
219 _, err := fi.reader.ReadAt(buf, offset)
220 if err != nil {
221 return nil, err
222 }
223
224 parent := encbin.BigEndian.Uint32(buf)
225 offset += 4
226 parentIndexes = append(parentIndexes, int(parent&parentOctopusMask))
227 if parent&parentLast == parentLast {
228 break
229 }
230 }
231 } else if parent2 != parentNone {
232 parentIndexes = []int{int(parent1 & parentOctopusMask), int(parent2 & parentOctopusMask)}
233 } else if parent1 != parentNone {
234 parentIndexes = []int{int(parent1 & parentOctopusMask)}
235 }
236
237 parentHashes, err := fi.getHashesFromIndexes(parentIndexes)
238 if err != nil {
239 return nil, err
240 }
241
242 return &CommitData{
243 TreeHash: treeHash,
244 ParentIndexes: parentIndexes,
245 ParentHashes: parentHashes,
246 Generation: int(genAndTime >> 34),
247 When: time.Unix(int64(genAndTime&0x3FFFFFFFF), 0),
248 }, nil
249 }
250
251 func (fi *fileIndex) getHashesFromIndexes(indexes []int) ([]plumbing.Hash, error) {
252 hashes := make([]plumbing.Hash, len(indexes))
253
254 for i, idx := range indexes {
255 if idx >= fi.fanout[0xff] {
256 return nil, ErrMalformedCommitGraphFile
257 }
258
259 offset := fi.oidLookupOffset + int64(idx)*hash.Size
260 if _, err := fi.reader.ReadAt(hashes[i][:], offset); err != nil {
261 return nil, err
262 }
263 }
264
265 return hashes, nil
266 }
267
268 // Hashes returns all the hashes that are available in the index
269 func (fi *fileIndex) Hashes() []plumbing.Hash {
270 hashes := make([]plumbing.Hash, fi.fanout[0xff])
271 for i := 0; i < fi.fanout[0xff]; i++ {
272 offset := fi.oidLookupOffset + int64(i)*hash.Size
273 if n, err := fi.reader.ReadAt(hashes[i][:], offset); err != nil || n < hash.Size {
274 return nil
275 }
276 }
277 return hashes
278 }