aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/commitgraph/encoder.go
blob: aac4d0ee8e579aaa9a787ea96f577e4398487c59 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package commitgraph

import (
	"crypto/sha1"
	"hash"
	"io"

	"gopkg.in/src-d/go-git.v4/plumbing"
	"gopkg.in/src-d/go-git.v4/utils/binary"
)

// Encoder writes MemoryIndex structs to an output stream.

type Encoder struct {
	io.Writer
	hash hash.Hash
}

// NewEncoder returns a new stream encoder that writes to w.

func NewEncoder(w io.Writer) *Encoder {
	h := sha1.New()
	mw := io.MultiWriter(w, h)
	return &Encoder{mw, h}
}

func (e *Encoder) Encode(idx Index) error {
	var err error

	// Get all the hashes in the memory index

	hashes := idx.Hashes()

	// Sort the hashes and build our index

	plumbing.HashesSort(hashes)
	hashToIndex := make(map[plumbing.Hash]uint32)
	hashFirstToCount := make(map[byte]uint32)
	for i, hash := range hashes {
		hashToIndex[hash] = uint32(i)
		hashFirstToCount[hash[0]]++
	}

	// Find out if we will need large edge table

	largeEdgesCount := 0
	for i := 0; i < len(hashes); i++ {
		v, _ := idx.GetNodeByIndex(i)
		if len(v.ParentHashes) > 2 {
			largeEdgesCount += len(v.ParentHashes) - 2
			break
		}
	}

	chunks := [][]byte{oidFanoutSignature, oidLookupSignature, commitDataSignature}
	chunkSizes := []uint64{4 * 256, uint64(len(hashes)) * 20, uint64(len(hashes)) * 36}
	if largeEdgesCount > 0 {
		chunks = append(chunks, largeEdgeListSignature)
		chunkSizes = append(chunkSizes, uint64(largeEdgesCount)*4)
	}

	// Write header

	if _, err = e.Write(commitFileSignature); err == nil {
		_, err = e.Write([]byte{1, 1, byte(len(chunks)), 0})
	}
	if err != nil {
		return err
	}

	// Write chunk headers

	offset := uint64(8 + len(chunks)*12 + 12)
	for i, signature := range chunks {
		if _, err = e.Write(signature); err == nil {
			err = binary.WriteUint64(e, offset)
		}
		if err != nil {
			return err
		}
		offset += chunkSizes[i]
	}
	if _, err = e.Write([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); err != nil {
		return err
	}

	// Write fanout

	var cumulative uint32
	for i := 0; i <= 0xff; i++ {
		if err = binary.WriteUint32(e, hashFirstToCount[byte(i)]+cumulative); err != nil {
			return err
		}
		cumulative += hashFirstToCount[byte(i)]
	}

	// Write OID lookup

	for _, hash := range hashes {
		if _, err = e.Write(hash[:]); err != nil {
			return err
		}
	}

	// Write commit data

	var largeEdges []uint32
	for _, hash := range hashes {
		origIndex, _ := idx.GetIndexByHash(hash)
		commitData, _ := idx.GetNodeByIndex(origIndex)
		if _, err := e.Write(commitData.TreeHash[:]); err != nil {
			return err
		}

		var parent1, parent2 uint32
		if len(commitData.ParentHashes) == 0 {
			parent1 = parentNone
			parent2 = parentNone
		} else if len(commitData.ParentHashes) == 1 {
			parent1 = hashToIndex[commitData.ParentHashes[0]]
			parent2 = parentNone
		} else if len(commitData.ParentHashes) == 2 {
			parent1 = hashToIndex[commitData.ParentHashes[0]]
			parent2 = hashToIndex[commitData.ParentHashes[1]]
		} else if len(commitData.ParentHashes) > 2 {
			parent1 = hashToIndex[commitData.ParentHashes[0]]
			parent2 = uint32(len(largeEdges)) | parentOctopusUsed
			for _, parentHash := range commitData.ParentHashes[1:] {
				largeEdges = append(largeEdges, hashToIndex[parentHash])
			}
			largeEdges[len(largeEdges)-1] |= parentLast
		}

		if err = binary.WriteUint32(e, parent1); err == nil {
			err = binary.WriteUint32(e, parent2)
		}
		if err != nil {
			return err
		}

		unixTime := uint64(commitData.When.Unix())
		unixTime |= uint64(commitData.Generation) << 34
		if err = binary.WriteUint64(e, unixTime); err != nil {
			return err
		}
	}

	// Write large edges if necessary

	for _, parent := range largeEdges {
		if err = binary.WriteUint32(e, parent); err != nil {
			return err
		}
	}

	// Write checksum

	if _, err := e.Write(e.hash.Sum(nil)[:20]); err != nil {
		return err
	}

	return nil
}