|
| 1 | +package commitgraph |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha1" |
| 5 | + "hash" |
| 6 | + "io" |
| 7 | + |
| 8 | + "gopkg.in/src-d/go-git.v4/plumbing" |
| 9 | + "gopkg.in/src-d/go-git.v4/utils/binary" |
| 10 | +) |
| 11 | + |
| 12 | +// Encoder writes MemoryIndex structs to an output stream. |
| 13 | +type Encoder struct { |
| 14 | + io.Writer |
| 15 | + hash hash.Hash |
| 16 | +} |
| 17 | + |
| 18 | +// NewEncoder returns a new stream encoder that writes to w. |
| 19 | +func NewEncoder(w io.Writer) *Encoder { |
| 20 | + h := sha1.New() |
| 21 | + mw := io.MultiWriter(w, h) |
| 22 | + return &Encoder{mw, h} |
| 23 | +} |
| 24 | + |
| 25 | +func (e *Encoder) Encode(idx Index) error { |
| 26 | + var err error |
| 27 | + |
| 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, largeEdgesCount := e.prepare(idx, hashes) |
| 33 | + |
| 34 | + chunkSignatures := [][]byte{oidFanoutSignature, oidLookupSignature, commitDataSignature} |
| 35 | + chunkSizes := []uint64{4 * 256, uint64(len(hashes)) * 20, uint64(len(hashes)) * 36} |
| 36 | + if largeEdgesCount > 0 { |
| 37 | + chunkSignatures = append(chunkSignatures, largeEdgeListSignature) |
| 38 | + chunkSizes = append(chunkSizes, uint64(largeEdgesCount)*4) |
| 39 | + } |
| 40 | + |
| 41 | + if err = e.encodeFileHeader(len(chunkSignatures)); err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + if err = e.encodeChunkHeaders(chunkSignatures, chunkSizes); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + if err = e.encodeFanout(fanout); err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + if err = e.encodeOidLookup(hashes); err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + if largeEdges, err := e.encodeCommitData(hashes, hashToIndex, idx); err == nil { |
| 54 | + if err = e.encodeLargeEdges(largeEdges); err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + } |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + return e.encodeChecksum() |
| 62 | +} |
| 63 | + |
| 64 | +func (e *Encoder) prepare(idx Index, hashes []plumbing.Hash) (hashToIndex map[plumbing.Hash]uint32, fanout []uint32, largeEdgesCount uint32) { |
| 65 | + // Sort the hashes and build our index |
| 66 | + plumbing.HashesSort(hashes) |
| 67 | + hashToIndex = make(map[plumbing.Hash]uint32) |
| 68 | + fanout = make([]uint32, 256) |
| 69 | + for i, hash := range hashes { |
| 70 | + hashToIndex[hash] = uint32(i) |
| 71 | + fanout[hash[0]]++ |
| 72 | + } |
| 73 | + |
| 74 | + // Convert the fanout to cumulative values |
| 75 | + for i := 1; i <= 0xff; i++ { |
| 76 | + fanout[i] += fanout[i-1] |
| 77 | + } |
| 78 | + |
| 79 | + // Find out if we will need large edge table |
| 80 | + for i := 0; i < len(hashes); i++ { |
| 81 | + v, _ := idx.GetNodeByIndex(i) |
| 82 | + if len(v.ParentHashes) > 2 { |
| 83 | + largeEdgesCount += uint32(len(v.ParentHashes) - 1) |
| 84 | + break |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return |
| 89 | +} |
| 90 | + |
| 91 | +func (e *Encoder) encodeFileHeader(chunkCount int) (err error) { |
| 92 | + if _, err = e.Write(commitFileSignature); err == nil { |
| 93 | + _, err = e.Write([]byte{1, 1, byte(chunkCount), 0}) |
| 94 | + } |
| 95 | + return |
| 96 | +} |
| 97 | + |
| 98 | +func (e *Encoder) encodeChunkHeaders(chunkSignatures [][]byte, chunkSizes []uint64) (err error) { |
| 99 | + // 8 bytes of file header, 12 bytes for each chunk header and 12 byte for terminator |
| 100 | + offset := uint64(8 + len(chunkSignatures)*12 + 12) |
| 101 | + for i, signature := range chunkSignatures { |
| 102 | + if _, err = e.Write(signature); err == nil { |
| 103 | + err = binary.WriteUint64(e, offset) |
| 104 | + } |
| 105 | + if err != nil { |
| 106 | + return |
| 107 | + } |
| 108 | + offset += chunkSizes[i] |
| 109 | + } |
| 110 | + if _, err = e.Write(lastSignature); err == nil { |
| 111 | + err = binary.WriteUint64(e, offset) |
| 112 | + } |
| 113 | + return |
| 114 | +} |
| 115 | + |
| 116 | +func (e *Encoder) encodeFanout(fanout []uint32) (err error) { |
| 117 | + for i := 0; i <= 0xff; i++ { |
| 118 | + if err = binary.WriteUint32(e, fanout[i]); err != nil { |
| 119 | + return |
| 120 | + } |
| 121 | + } |
| 122 | + return |
| 123 | +} |
| 124 | + |
| 125 | +func (e *Encoder) encodeOidLookup(hashes []plumbing.Hash) (err error) { |
| 126 | + for _, hash := range hashes { |
| 127 | + if _, err = e.Write(hash[:]); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + } |
| 131 | + return |
| 132 | +} |
| 133 | + |
| 134 | +func (e *Encoder) encodeCommitData(hashes []plumbing.Hash, hashToIndex map[plumbing.Hash]uint32, idx Index) (largeEdges []uint32, err error) { |
| 135 | + for _, hash := range hashes { |
| 136 | + origIndex, _ := idx.GetIndexByHash(hash) |
| 137 | + commitData, _ := idx.GetNodeByIndex(origIndex) |
| 138 | + if _, err = e.Write(commitData.TreeHash[:]); err != nil { |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + var parent1, parent2 uint32 |
| 143 | + if len(commitData.ParentHashes) == 0 { |
| 144 | + parent1 = parentNone |
| 145 | + parent2 = parentNone |
| 146 | + } else if len(commitData.ParentHashes) == 1 { |
| 147 | + parent1 = hashToIndex[commitData.ParentHashes[0]] |
| 148 | + parent2 = parentNone |
| 149 | + } else if len(commitData.ParentHashes) == 2 { |
| 150 | + parent1 = hashToIndex[commitData.ParentHashes[0]] |
| 151 | + parent2 = hashToIndex[commitData.ParentHashes[1]] |
| 152 | + } else if len(commitData.ParentHashes) > 2 { |
| 153 | + parent1 = hashToIndex[commitData.ParentHashes[0]] |
| 154 | + parent2 = uint32(len(largeEdges)) | parentOctopusUsed |
| 155 | + for _, parentHash := range commitData.ParentHashes[1:] { |
| 156 | + largeEdges = append(largeEdges, hashToIndex[parentHash]) |
| 157 | + } |
| 158 | + largeEdges[len(largeEdges)-1] |= parentLast |
| 159 | + } |
| 160 | + |
| 161 | + if err = binary.WriteUint32(e, parent1); err == nil { |
| 162 | + err = binary.WriteUint32(e, parent2) |
| 163 | + } |
| 164 | + if err != nil { |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + unixTime := uint64(commitData.When.Unix()) |
| 169 | + unixTime |= uint64(commitData.Generation) << 34 |
| 170 | + if err = binary.WriteUint64(e, unixTime); err != nil { |
| 171 | + return |
| 172 | + } |
| 173 | + } |
| 174 | + return |
| 175 | +} |
| 176 | + |
| 177 | +func (e *Encoder) encodeLargeEdges(largeEdges []uint32) (err error) { |
| 178 | + for _, parent := range largeEdges { |
| 179 | + if err = binary.WriteUint32(e, parent); err != nil { |
| 180 | + return |
| 181 | + } |
| 182 | + } |
| 183 | + return |
| 184 | +} |
| 185 | + |
| 186 | +func (e *Encoder) encodeChecksum() error { |
| 187 | + _, err := e.Write(e.hash.Sum(nil)[:20]) |
| 188 | + return err |
| 189 | +} |
0 commit comments