| 1 |
package v2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
|
| 6 |
"github.com/go-git/go-git/v5/plumbing" |
| 7 |
) |
| 8 |
|
| 9 |
// MemoryIndex provides a way to build the commit-graph in memory |
| 10 |
// for later encoding to file. |
| 11 |
type MemoryIndex struct { |
| 12 |
commitData []commitData |
| 13 |
indexMap map[plumbing.Hash]uint32 |
| 14 |
hasGenerationV2 bool |
| 15 |
} |
| 16 |
|
| 17 |
type commitData struct { |
| 18 |
Hash plumbing.Hash |
| 19 |
*CommitData |
| 20 |
} |
| 21 |
|
| 22 |
// NewMemoryIndex creates in-memory commit graph representation |
| 23 |
func NewMemoryIndex() *MemoryIndex { |
| 24 |
return &MemoryIndex{ |
| 25 |
indexMap: make(map[plumbing.Hash]uint32), |
| 26 |
hasGenerationV2: true, |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
// GetIndexByHash gets the index in the commit graph from commit hash, if available |
| 31 |
func (mi *MemoryIndex) GetIndexByHash(h plumbing.Hash) (uint32, error) { |
| 32 |
i, ok := mi.indexMap[h] |
| 33 |
if ok { |
| 34 |
return i, nil |
| 35 |
} |
| 36 |
|
| 37 |
return 0, plumbing.ErrObjectNotFound |
| 38 |
} |
| 39 |
|
| 40 |
// GetHashByIndex gets the hash given an index in the commit graph |
| 41 |
func (mi *MemoryIndex) GetHashByIndex(i uint32) (plumbing.Hash, error) { |
| 42 |
if i >= uint32(len(mi.commitData)) { |
| 43 |
return plumbing.ZeroHash, plumbing.ErrObjectNotFound |
| 44 |
} |
| 45 |
|
| 46 |
return mi.commitData[i].Hash, nil |
| 47 |
} |
| 48 |
|
| 49 |
// GetCommitDataByIndex gets the commit node from the commit graph using index |
| 50 |
// obtained from child node, if available |
| 51 |
func (mi *MemoryIndex) GetCommitDataByIndex(i uint32) (*CommitData, error) { |
| 52 |
if i >= uint32(len(mi.commitData)) { |
| 53 |
return nil, plumbing.ErrObjectNotFound |
| 54 |
} |
| 55 |
|
| 56 |
commitData := mi.commitData[i] |
| 57 |
|
| 58 |
// Map parent hashes to parent indexes |
| 59 |
if commitData.ParentIndexes == nil { |
| 60 |
parentIndexes := make([]uint32, len(commitData.ParentHashes)) |
| 61 |
for i, parentHash := range commitData.ParentHashes { |
| 62 |
var err error |
| 63 |
if parentIndexes[i], err = mi.GetIndexByHash(parentHash); err != nil { |
| 64 |
return nil, err |
| 65 |
} |
| 66 |
} |
| 67 |
commitData.ParentIndexes = parentIndexes |
| 68 |
} |
| 69 |
|
| 70 |
return commitData.CommitData, nil |
| 71 |
} |
| 72 |
|
| 73 |
// Hashes returns all the hashes that are available in the index |
| 74 |
func (mi *MemoryIndex) Hashes() []plumbing.Hash { |
| 75 |
hashes := make([]plumbing.Hash, 0, len(mi.indexMap)) |
| 76 |
for k := range mi.indexMap { |
| 77 |
hashes = append(hashes, k) |
| 78 |
} |
| 79 |
return hashes |
| 80 |
} |
| 81 |
|
| 82 |
// Add adds new node to the memory index |
| 83 |
func (mi *MemoryIndex) Add(hash plumbing.Hash, data *CommitData) { |
| 84 |
// The parent indexes are calculated lazily in GetNodeByIndex |
| 85 |
// which allows adding nodes out of order as long as all parents |
| 86 |
// are eventually resolved |
| 87 |
data.ParentIndexes = nil |
| 88 |
mi.indexMap[hash] = uint32(len(mi.commitData)) |
| 89 |
mi.commitData = append(mi.commitData, commitData{Hash: hash, CommitData: data}) |
| 90 |
if data.GenerationV2 == math.MaxUint64 { // if GenerationV2 is not available reset it to zero |
| 91 |
data.GenerationV2 = 0 |
| 92 |
} |
| 93 |
mi.hasGenerationV2 = mi.hasGenerationV2 && data.GenerationV2 != 0 |
| 94 |
} |
| 95 |
|
| 96 |
func (mi *MemoryIndex) HasGenerationV2() bool { |
| 97 |
return mi.hasGenerationV2 |
| 98 |
} |
| 99 |
|
| 100 |
// Close closes the index |
| 101 |
func (mi *MemoryIndex) Close() error { |
| 102 |
return nil |
| 103 |
} |
| 104 |
|
| 105 |
func (mi *MemoryIndex) MaximumNumberOfHashes() uint32 { |
| 106 |
return uint32(len(mi.indexMap)) |
| 107 |
} |