-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
365 lines (329 loc) · 9.1 KB
/
repo.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package main
import (
"bufio"
"bytes"
"compress/zlib"
"crypto/sha1"
"fmt"
"io"
"log"
"path"
"strings"
"unicode"
errors "github.com/cakemanny/git-remote-drive/errors"
store "github.com/cakemanny/git-remote-drive/store"
)
// Manager is an abstraction over some of the simplest operations you might
// want to perform against a git repository
type Manager interface {
// ListRefs lists all the references in a repository. You can assume it
// does this by scanning the refs directory of the repo.
ListRefs() ([]Ref, error)
// ReadRef reads the sha1 stored in a reference. For example, to read the
// objectname (sha1) of the head commit of the master branch, you might do
// this:
// sha1value, err := manager.ReadRef("refs/heads/master")
// if err != nil {
// // handle error
// }
// fmt.Println(sha1value)
ReadRef(name string) (string, error)
// WriteRef is the complement of ReadRef. It writes ref.Value to the path
// ref.Name relative to the repository root.
WriteRef(ref Ref) error
// ReadObject reads an object, including decompressing and pretty printing
// the output. Think git cat-file
ReadObject(sha string, contents io.Writer) error
// ReadRaw reads a git repo object without decompressing. Useful for
// quickly transfering an object without needing to understand it
ReadRaw(sha string, contents io.Writer) error
// WriteRaw is the complement to ReadRaw. It writes the contents of an
// object directly to the git repository assuming the stream is already
// compressed
WriteRaw(sha string, contents io.Reader) error
}
// RefLister is a subinterface of Manager only requiring the ListRefs method to
// be implemented.
type RefLister interface {
ListRefs() ([]Ref, error)
}
type RefReader interface {
ReadRef(name string) (string, error)
}
// prove at compile-time that RefLister is a subinterface on Manager
var manager0 Manager
var refLister0 RefLister = manager0
// Ref represents a git references
type Ref struct {
// c5d2d737af4b6203aa37ca2ca13476624d11f4ee
Value string
// refs/heads/master
Name string
}
// Commit represents a git commit object
type Commit struct {
// Tree is the reference to the tree object
Tree string
// Parents is a slice of references to the parent commits
Parents []string
}
type ObjectType uint
const (
BLOB ObjectType = iota
TREE
COMMIT
TAG
)
// Tree represents a git tree object
type Tree []struct {
Type ObjectType
Ref string
}
// storeManager is an implementation of the git repo Manager over a
// SimpleFileStore
type storeManager struct {
// relative path from the root of the store
// e.g. ".git" for a local repo or "user/reponame.git" for a remote say
basePath string
store store.SimpleFileStore
}
func (m storeManager) ListRefs() ([]Ref, error) {
var results []Ref
pathToRefs := path.Join(m.basePath, "refs")
onFile := func(p string) error {
if options.verbosity >= 1 {
log.Printf("reading file \"%s\"", p)
}
var sha1val strings.Builder
if err := m.store.Read(p, &sha1val); err != nil {
return fmt.Errorf("error reading \"%s\" in remote store: %v", p, err)
}
name := strings.TrimPrefix(strings.TrimPrefix(p, m.basePath), "/")
results = append(results, Ref{
strings.TrimRightFunc(sha1val.String(), unicode.IsSpace),
name,
})
return nil
}
var walk func(string) error
walk = func(baseDir string) error {
if options.verbosity >= 1 {
log.Printf("walking \"%s\"", baseDir)
}
list, err := m.store.List(baseDir)
if _, isNotFound := err.(errors.ErrNotFound); isNotFound {
return nil
}
if err != nil {
return fmt.Errorf("unable to list \"%s\" directory: %v", baseDir, err)
}
for _, entry := range list {
p := path.Join(baseDir, entry.Name)
if entry.IsFolder {
if err := walk(p); err != nil {
return err
}
} else {
if err := onFile(p); err != nil {
return err
}
}
}
return nil
}
if err := walk(pathToRefs); err != nil {
return nil, fmt.Errorf("walking refs dir: %v", err)
}
return results, nil
}
func (m storeManager) objectPath(sha string) (string, error) {
if len(sha) != 40 {
return "", fmt.Errorf("invalid sha: \"%s\"", sha)
}
// c5d2d737af4b6203aa37ca2ca13476624d11f4ee
// -> objects/c5/d2d737af4b6203aa37ca2ca13476624d11f4ee
subDir := sha[:2]
fileName := sha[2:]
return path.Join(m.basePath, "objects", subDir, fileName), nil
}
func (m storeManager) ReadRaw(sha string, contents io.Writer) error {
fullPath, err := m.objectPath(sha)
if err != nil {
return err
}
return m.store.Read(fullPath, contents)
}
func sha1Bytes(b []byte) (string, error) {
rdr := bytes.NewReader(b)
zlibReader, err := zlib.NewReader(rdr)
if err != nil {
return "", fmt.Errorf("inflating stream: %v", err)
}
defer zlibReader.Close()
// decompress
hasher := sha1.New()
io.Copy(hasher, zlibReader)
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}
// verifyObject checks that an object is present in the store and the sha1
// of the data matches.
func (m storeManager) verifyObject(sha string) error {
log.Println("verifying object", sha)
var buf bytes.Buffer
if err := m.ReadRaw(sha, &buf); err != nil {
return err
}
actualSha, err := sha1Bytes(buf.Bytes())
if err != nil {
return err
}
if sha != actualSha {
return fmt.Errorf("invalid object %s: sha1 of content is %s, "+
"compressed size is %d bytes", sha, actualSha, len(buf.Bytes()))
}
return nil
}
func (m storeManager) WriteRaw(sha string, contents io.Reader) error {
fullPath, err := m.objectPath(sha)
if err != nil {
return err
}
// Test path? If not exists create?
exists, err := m.store.TestPath(fullPath)
if err != nil {
return err
}
if exists {
// We surely don't need to "update" any objects
// so just verify it's ok
err := m.verifyObject(sha)
_, invalid := err.(errors.ErrInvalidObject)
if invalid {
if deleteErr := m.store.Delete(fullPath); deleteErr != nil {
return fmt.Errorf(
"object %s contains invalid data, but cannot be deleted: %v",
sha,
deleteErr,
)
}
return m.store.Create(fullPath, contents)
}
return err
}
return m.store.Create(fullPath, contents)
}
func (storeManager) ReadObject(sha string, contents io.Writer) error {
return errors.NotImplemented()
}
func (m storeManager) ReadRef(name string) (string, error) {
fullPath := path.Join(m.basePath, name)
var sb strings.Builder
if err := m.store.Read(fullPath, &sb); err != nil {
return "", err
}
// the ref file usually ends in a \n so trim that
return strings.TrimRight(sb.String(), "\n"), nil
}
func (m storeManager) WriteRef(ref Ref) error {
fullPath := path.Join(m.basePath, ref.Name)
exists, err := m.store.TestPath(fullPath)
if err != nil {
return err
}
var buf bytes.Buffer
buf.WriteString(ref.Value)
buf.WriteByte('\n')
writeMethod := m.store.Create
if exists {
writeMethod = m.store.Update
}
if err = writeMethod(fullPath, &buf); err != nil {
return fmt.Errorf("updating ref %s: %v", ref.Name, err)
}
return nil
}
func GetCommit(m Manager, ref string) (Commit, error) {
if len(ref) != 40 {
panic(ref)
}
var sb strings.Builder
if err := m.ReadObject(ref, &sb); err != nil {
return Commit{}, fmt.Errorf("reading object %s: %v", ref, err)
}
rdr := strings.NewReader(sb.String())
return ReadCommit(rdr)
}
func GetTree(m Manager, ref string) (Tree, error) {
if len(ref) != 40 {
panic(ref)
}
var sb strings.Builder
if err := m.ReadObject(ref, &sb); err != nil {
return nil, fmt.Errorf("reading object %s: %v", ref, err)
}
rdr := strings.NewReader(sb.String())
return ReadTree(rdr)
}
// ReadCommit reads commit information from a io.Reader which you can pretend
// supplies the same content as the output of
// git cat-file -p <ref>
func ReadCommit(rdr io.Reader) (Commit, error) {
result := Commit{}
scanner := bufio.NewScanner(rdr)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) == 0 {
// Remainder will just be commit message
// consume stream
for scanner.Scan() {
}
break
}
if len(fields) == 1 {
log.Fatalln("ReadCommit: unexpected number of fields in commit line:", line)
}
switch fields[0] {
case "tree":
result.Tree = fields[1]
case "parent":
result.Parents = append(result.Parents, fields[1])
case "author":
case "committer":
default:
log.Fatalln("ReadCommit: unexpected commit field:", fields[0], fields[1])
}
}
if err := scanner.Err(); err != nil {
return result, fmt.Errorf("reading commit object: %v", err)
}
return result, nil
}
func ReadTree(rdr io.Reader) (Tree, error) {
result := Tree{}
scanner := bufio.NewScanner(rdr)
for scanner.Scan() {
// <perms> <type> <sha1> <filename>
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) < 3 {
log.Fatalln("ReadTree: unexpected number of fields in tree line:", line)
}
type Item struct {
Type ObjectType
Ref string
}
switch fields[1] {
case "blob":
result = append(result, Item{Type: BLOB, Ref: fields[2]})
case "tree":
result = append(result, Item{Type: TREE, Ref: fields[2]})
default:
log.Fatalln("ReadTree: unexpected object type:", fields[1])
}
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("reading commit object: %v", err)
}
return result, nil
}