forked from src-d/gitbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.go
175 lines (139 loc) · 2.99 KB
/
checksum.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package gitbase
import (
"bytes"
"crypto/sha1"
"encoding/base64"
"io"
"sort"
"strings"
"gopkg.in/src-d/go-git.v4/plumbing"
)
type checksumable struct {
pool *RepositoryPool
}
func (c *checksumable) Checksum() (string, error) {
hash := sha1.New()
iter, err := c.pool.RepoIter()
if err != nil {
return "", err
}
defer iter.Close()
var checksums checksums
for {
hash.Reset()
repo, err := iter.Next()
if err == io.EOF {
break
}
if err != nil {
return "", err
}
bytes, err := readChecksum(repo)
if err != nil {
return "", err
}
if _, err = hash.Write(bytes); err != nil {
return "", err
}
bytes, err = readRefs(repo)
if err != nil {
return "", err
}
if _, err = hash.Write(bytes); err != nil {
return "", err
}
c := checksum{
name: repo.ID(),
hash: hash.Sum(nil),
}
checksums = append(checksums, c)
}
sort.Stable(checksums)
hash.Reset()
for _, c := range checksums {
if _, err = hash.Write(c.hash); err != nil {
return "", err
}
}
return base64.StdEncoding.EncodeToString(hash.Sum(nil)), nil
}
func readChecksum(r *Repository) ([]byte, error) {
fs, err := r.FS()
if err != nil {
return nil, err
}
dot, packfiles, err := repositoryPackfiles(fs)
if err != nil {
return nil, err
}
var result []byte
for _, p := range packfiles {
f, err := dot.ObjectPack(p)
if err != nil {
return nil, err
}
if _, err = f.Seek(-20, io.SeekEnd); err != nil {
return nil, err
}
var checksum = make([]byte, 20)
if _, err = io.ReadFull(f, checksum); err != nil {
return nil, err
}
if err = f.Close(); err != nil {
return nil, err
}
result = append(result, checksum...)
}
return result, nil
}
type reference struct {
name string
hash string
}
type references []reference
type byHashAndName []reference
func (b byHashAndName) Len() int { return len(b) }
func (b byHashAndName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byHashAndName) Less(i, j int) bool {
if cmp := strings.Compare(b[i].hash, b[j].hash); cmp != 0 {
return cmp < 0
}
return strings.Compare(b[i].name, b[j].name) < 0
}
type checksum struct {
name string
hash []byte
}
type checksums []checksum
func (b checksums) Len() int { return len(b) }
func (b checksums) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b checksums) Less(i, j int) bool {
if cmp := bytes.Compare(b[i].hash, b[j].hash); cmp != 0 {
return cmp < 0
}
return strings.Compare(b[i].name, b[j].name) < 0
}
func readRefs(repo *Repository) ([]byte, error) {
buf := bytes.NewBuffer(nil)
refs, err := repo.References()
if err != nil {
return nil, err
}
var references []reference
err = refs.ForEach(func(r *plumbing.Reference) error {
references = append(references, reference{
name: string(r.Name()),
hash: r.Hash().String(),
})
return nil
})
if err != nil {
return nil, err
}
sort.Stable(byHashAndName(references))
for _, r := range references {
buf.WriteString(r.name)
buf.WriteString(r.hash)
}
return buf.Bytes(), nil
}