aboutsummaryrefslogtreecommitdiffstats
path: root/formats/idxfile/idxfile.go
blob: ce4c9760e1013260d53ac787eda5c0c065580881 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package idxfile

import "gopkg.in/src-d/go-git.v4/core"

const (
	// VersionSupported is the only idx version supported.
	VersionSupported = 2
)

var (
	idxHeader = []byte{255, 't', 'O', 'c'}
)

// An Idxfile represents an idx file in memory.
type Idxfile struct {
	Version          uint32
	Fanout           [255]uint32
	ObjectCount      uint32
	Entries          []Entry
	PackfileChecksum [20]byte
	IdxChecksum      [20]byte
}

// An Entry represents data about an object in the packfile: its hash,
// offset and CRC32 checksum.
type Entry struct {
	Hash   core.Hash
	CRC32  uint32
	Offset uint64
}

func (idx *Idxfile) Add(h core.Hash, offset uint64, crc32 uint32) {
	idx.Entries = append(idx.Entries, Entry{
		Hash:   h,
		Offset: offset,
		CRC32:  crc32,
	})
}

func (idx *Idxfile) isValid() bool {
	fanout := idx.calculateFanout()
	for k, c := range idx.Fanout {
		if fanout[k] != c {
			return false
		}
	}

	return true
}

func (idx *Idxfile) calculateFanout() [256]uint32 {
	fanout := [256]uint32{}
	var c uint32
	for _, e := range idx.Entries {
		c++
		fanout[e.Hash[0]] = c
	}

	var i uint32
	for k, c := range fanout {
		if c != 0 {
			i = c
		}

		fanout[k] = i
	}

	return fanout
}