This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
repository.go
581 lines (524 loc) · 14.8 KB
/
repository.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Copyright (c) 2013 Patrick Gundlach, speedata (Berlin, Germany)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package gogit
import (
"bytes"
"compress/zlib"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"sync"
"github.com/speedata/mmap-go"
)
// A Repository is the base of all other actions. If you need to lookup a
// commit, tree or blob, you do it from here.
type Repository struct {
Path string
indexfiles []*idxFile
}
type SHA1 [20]byte
// Who am I?
type ObjectType int
const (
ObjectCommit ObjectType = 0x10
ObjectTree ObjectType = 0x20
ObjectBlob ObjectType = 0x30
ObjectTag ObjectType = 0x40
)
func (t ObjectType) String() string {
switch t {
case ObjectCommit:
return "Commit"
case ObjectTree:
return "Tree"
case ObjectBlob:
return "Blob"
default:
return ""
}
}
type Object struct {
Type ObjectType
Oid *Oid
}
// idx-file
type idxFile struct {
packpath string
fanoutTable [256]int64
// These tables are sub-slices of the whole idx file as an mmap
shaTable []byte
offsetTable []byte
offset8Table []byte
}
func readIdxFile(path string) (*idxFile, error) {
ifile := &idxFile{}
ifile.packpath = path[0:len(path)-3] + "pack"
idxf, err := os.Open(path)
if err != nil {
return nil, err
}
defer idxf.Close()
idxMmap, err := mmap.Map(idxf, mmap.RDONLY, 0)
if err != nil {
return nil, err
}
defer idxMmap.Unmap()
if !bytes.HasPrefix(idxMmap, []byte{255, 't', 'O', 'c'}) {
return nil, errors.New("Not version 2 index file")
}
for i := range ifile.fanoutTable {
pos := 8 + i*4
u32 := binary.BigEndian.Uint32(idxMmap[pos : pos+4])
ifile.fanoutTable[i] = int64(u32)
}
numObjects := ifile.fanoutTable[byte(255)]
shaStart := int64(8 + 256*4)
ifile.shaTable = make([]byte, len(idxMmap[shaStart : shaStart+20*numObjects]))
copy(ifile.shaTable, idxMmap[shaStart : shaStart+20*numObjects])
offsetStart := shaStart + 24*numObjects
ifile.offsetTable = make([]byte, len(idxMmap[offsetStart : offsetStart+4*numObjects]))
copy(ifile.offsetTable, idxMmap[offsetStart : offsetStart+4*numObjects])
offset8Start := offsetStart + 4*numObjects
ifile.offset8Table = make([]byte, len(idxMmap[offset8Start : len(idxMmap)-40]))
copy(ifile.offset8Table, idxMmap[offset8Start : len(idxMmap)-40])
fi, err := os.Open(ifile.packpath)
if err != nil {
return nil, err
}
defer fi.Close()
packVersion := make([]byte, 8)
_, err = io.ReadFull(fi, packVersion)
if err != nil {
return nil, err
}
if !bytes.HasPrefix(packVersion, []byte{'P', 'A', 'C', 'K'}) {
return nil, errors.New("Pack file does not start with 'PACK'")
}
return ifile, nil
}
func (idx *idxFile) offsetForSHA(target SHA1) uint64 {
// Restrict search between shas that start with the correct
// byte, thanks to the fanoutTable.
var startSearch int64
if target[0] > 0 {
startSearch = idx.fanoutTable[target[0]-1]
}
endSearch := idx.fanoutTable[target[0]]
// Search for the position of the target sha1.
var exactMatch bool
found := sort.Search(int(endSearch-startSearch), func(i int) bool {
cpos := (startSearch + int64(i)) * 20
comp := bytes.Compare(target[:], idx.shaTable[cpos:cpos+20])
if comp == 0 {
exactMatch = true
}
return comp <= 0
})
if !exactMatch {
return 0
}
// We found it, now read value
pos := (startSearch + int64(found)) * 4
offset32 := binary.BigEndian.Uint32(idx.offsetTable[pos : pos+4])
offset := uint64(offset32)
// If msb is set, offset is actually an index into the "big" table where each
// offset is 8 bytes instead of 4 bytes.
if offset&0x80000000 == 0x80000000 {
pos := int64(offset&0x7FFFFFFF) * 8
offset = binary.BigEndian.Uint64(idx.offset8Table[pos : pos+8])
}
return offset
}
// If the object is stored in its own file (i.e not in a pack file),
// this function returns the full path to the object file.
// It does not test if the file exists.
func filepathFromSHA1(rootdir, sha1 string) string {
return filepath.Join(rootdir, "objects", sha1[:2], sha1[2:])
}
// zlibReaderPool holds zlib Readers.
var zlibReaderPool sync.Pool
// Read deflated object from the file.
func readCompressedDataFromFile(file *os.File, start int64, inflatedSize int64) ([]byte, error) {
_, err := file.Seek(start, os.SEEK_SET)
if err != nil {
return nil, err
}
z := zlibReaderPool.Get()
if z != nil {
err = z.(zlib.Resetter).Reset(file, nil)
} else {
z, err = zlib.NewReader(file)
}
if z != nil {
defer zlibReaderPool.Put(z)
}
if err != nil {
return nil, err
}
rc := z.(io.ReadCloser)
defer rc.Close()
zbuf := make([]byte, inflatedSize)
if _, err := io.ReadFull(rc, zbuf); err != nil {
return nil, err
}
return zbuf, nil
}
// buf must be large enough to read the number.
func readLittleEndianBase128Number(buf []byte) (int64, int) {
zpos := 0
toread := int64(buf[zpos] & 0x7f)
shift := uint64(0)
for buf[zpos]&0x80 > 0 {
zpos += 1
shift += 7
toread |= int64(buf[zpos]&0x7f) << shift
}
zpos += 1
return toread, zpos
}
// We take “delta instructions”, a base object, the expected length
// of the resulting object and we can create a resulting object.
func applyDelta(b []byte, base []byte, resultLen int64) ([]byte, error) {
resultObject := make([]byte, resultLen)
var resultpos uint64
var basepos uint64
zpos := 0
for zpos < len(b) {
// two modes: copy and insert. copy reads offset and len from the delta
// instructions and copy len bytes from offset into the resulting object
// insert takes up to 127 bytes and insert them into the
// resulting object
opcode := b[zpos]
zpos += 1
if opcode&0x80 > 0 {
// Copy from base to dest
copy_offset := uint64(0)
copy_length := uint64(0)
shift := uint(0)
for i := 0; i < 4; i++ {
if opcode&0x01 > 0 {
copy_offset |= uint64(b[zpos]) << shift
zpos += 1
}
opcode >>= 1
shift += 8
}
shift = 0
for i := 0; i < 3; i++ {
if opcode&0x01 > 0 {
copy_length |= uint64(b[zpos]) << shift
zpos += 1
}
opcode >>= 1
shift += 8
}
if copy_length == 0 {
copy_length = 1 << 16
}
basepos = copy_offset
for i := uint64(0); i < copy_length; i++ {
resultObject[resultpos] = base[basepos]
resultpos++
basepos++
}
} else if opcode > 0 {
// insert n bytes at the end of the resulting object. n==opcode
for i := 0; i < int(opcode); i++ {
resultObject[resultpos] = b[zpos]
resultpos++
zpos++
}
} else {
return nil, fmt.Errorf("opcode == 0")
}
}
// TODO: check if resultlen == resultpos
return resultObject, nil
}
// The object length in a packfile is a bit more difficult than
// just reading the bytes. The first byte has the length in its
// lowest four bits, and if bit 7 is set, it means 'more' bytes
// will follow. These are added to the »left side« of the length
func readLenInPackFile(buf []byte) (length int, advance int) {
advance = 0
shift := [...]byte{0, 4, 11, 18, 25, 32, 39, 46, 53, 60}
length = int(buf[advance] & 0x0F)
for buf[advance]&0x80 > 0 {
advance += 1
length += (int(buf[advance]&0x7F) << shift[advance])
}
advance++
return
}
// Read from a pack file (given by path) at position offset. If this is a
// non-delta object, the (inflated) bytes are just returned, if the object
// is a deltafied-object, we have to apply the delta to base objects
// before hand.
func readObjectBytes(path string, offset uint64, sizeonly bool) (ot ObjectType, length int64, data []byte, err error) {
offsetInt := int64(offset)
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close()
pos, err := file.Seek(offsetInt, os.SEEK_SET)
if err != nil {
return
}
if pos != offsetInt {
err = errors.New("Seek went wrong")
return
}
buf := make([]byte, 1024)
n, err := file.Read(buf)
if err != nil {
if err == io.EOF && n > 0 {
// Ignore EOF error and move on
err = nil
} else {
return
}
}
if n == 0 {
err = errors.New("Nothing read from pack file")
return
}
ot = ObjectType(buf[0] & 0x70)
l, p := readLenInPackFile(buf)
pos = int64(p)
length = int64(l)
var baseObjectOffset uint64
switch ot {
case ObjectCommit, ObjectTree, ObjectBlob, ObjectTag:
if sizeonly {
// if we are only interested in the size of the object,
// we don't need to do more expensive stuff
return
}
data, err = readCompressedDataFromFile(file, offsetInt+pos, length)
return
case 0x60:
// DELTA_ENCODED object w/ offset to base
// Read the offset first, then calculate the starting point
// of the base object
num := int64(buf[pos]) & 0x7f
for buf[pos]&0x80 > 0 {
pos = pos + 1
num = ((num + 1) << 7) | int64(buf[pos]&0x7f)
}
baseObjectOffset = uint64(offsetInt - num)
pos = pos + 1
case 0x70:
// DELTA_ENCODED object w/ base BINARY_OBJID
err = fmt.Errorf("not implemented yet")
return
}
var base []byte
ot, _, base, err = readObjectBytes(path, baseObjectOffset, false)
if err != nil {
return
}
b, err := readCompressedDataFromFile(file, offsetInt+pos, length)
if err != nil {
return
}
zpos := 0
// This is the length of the base object. Do we need to know it?
_, bytesRead := readLittleEndianBase128Number(b)
zpos += bytesRead
resultObjectLength, bytesRead := readLittleEndianBase128Number(b[zpos:])
zpos += bytesRead
if sizeonly {
// if we are only interested in the size of the object,
// we don't need to do more expensive stuff
length = resultObjectLength
return
}
data, err = applyDelta(b[zpos:], base, resultObjectLength)
return
}
// Return length as integer from zero terminated string
// and the beginning of the real object
func getLengthZeroTerminated(b []byte) (int64, int64) {
i := 0
var pos int
for b[i] != 0 {
i++
}
pos = i
i--
var length int64
var pow int64
pow = 1
for i >= 0 {
length = length + (int64(b[i])-48)*pow
pow = pow * 10
i--
}
return length, int64(pos) + 1
}
// Read the contents of the object file at path.
// Return the content type, the contents of the file and error, if any
func readObjectFile(path string, sizeonly bool) (ot ObjectType, length int64, data []byte, err error) {
var file *os.File
file, err = os.Open(path)
if err != nil {
return
}
defer file.Close()
r, err := zlib.NewReader(file)
if err != nil {
return
}
defer r.Close()
first_buffer_size := int64(1024)
b := make([]byte, first_buffer_size)
n, err := r.Read(b)
if err != nil {
if err == io.EOF && n > 0 {
// Ignore EOF error on read
err = nil
} else {
return
}
}
spaceposition := int64(bytes.IndexByte(b, ' '))
// "tree", "commit", "blob", ...
objecttypeString := string(b[:spaceposition])
switch objecttypeString {
case "blob":
ot = ObjectBlob
case "tree":
ot = ObjectTree
case "commit":
ot = ObjectCommit
case "tag":
ot = ObjectTag
}
// length starts at the position after the space
var objstart int64
length, objstart = getLengthZeroTerminated(b[spaceposition+1:])
if sizeonly {
// if we are only interested in the size of the object,
// we don't need to do more expensive stuff
return
}
objstart += spaceposition + 1
// if the size of our buffer is less than the object length + the bytes
// in front of the object (example: "commit 234\0") we need to increase
// the size of the buffer and read the rest. Warning: this should only
// be done on small files
if int64(n) < length+objstart {
remainingSize := length - first_buffer_size + objstart
remainingBuf := make([]byte, remainingSize)
n = 0
var count int64
for count < remainingSize {
n, err = r.Read(remainingBuf[count:])
if err != nil && err != io.EOF {
return
}
count += int64(n)
}
b = append(b, remainingBuf...)
}
data = b[objstart : objstart+length]
if err == io.EOF {
// The last read yielded exactly the right number of bytes
// as well as an EOF. Ignore the EOF; we succeeded.
err = nil
}
return
}
func (repos *Repository) getRawObject(oid *Oid) (ObjectType, int64, []byte, error) {
// first we need to find out where the commit is stored
objpath := filepathFromSHA1(repos.Path, oid.String())
_, err := os.Stat(objpath)
if os.IsNotExist(err) {
// doesn't exist, let's look if we find the object somewhere else
for _, indexfile := range repos.indexfiles {
if offset := indexfile.offsetForSHA(oid.Bytes); offset != 0 {
return readObjectBytes(indexfile.packpath, offset, false)
}
}
return 0, 0, nil, errObjNotFound
}
return readObjectFile(objpath, false)
}
// Open the repository at the given path.
func OpenRepository(path string) (*Repository, error) {
root := new(Repository)
path, err := filepath.Abs(path)
if err != nil {
return nil, err
}
root.Path = path
fm, err := os.Stat(path)
if err != nil {
return nil, err
}
if !fm.IsDir() {
return nil, errors.New(fmt.Sprintf("%q is not a directory."))
}
indexfiles, err := filepath.Glob(filepath.Join(path, "objects/pack/*.idx"))
if err != nil {
return nil, err
}
root.indexfiles = make([]*idxFile, len(indexfiles))
for i, indexfile := range indexfiles {
idx, err := readIdxFile(indexfile)
if err != nil {
return nil, err
}
root.indexfiles[i] = idx
}
return root, nil
}
// Get the type of an object.
func (repos *Repository) Type(oid *Oid) (ObjectType, error) {
objtype, _, _, err := repos.getRawObject(oid)
if err != nil {
return 0, err
}
return objtype, nil
}
// Get (inflated) size of an object.
func (repos *Repository) ObjectSize(oid *Oid) (int64, error) {
// todo: this is mostly the same as getRawObject -> merge
// difference is the boolean in readObjectBytes and readObjectFile
objpath := filepathFromSHA1(repos.Path, oid.String())
_, err := os.Stat(objpath)
if os.IsNotExist(err) {
// doesn't exist, let's look if we find the object somewhere else
for _, indexfile := range repos.indexfiles {
if offset := indexfile.offsetForSHA(oid.Bytes); offset != 0 {
_, length, _, err := readObjectBytes(indexfile.packpath, offset, true)
return length, err
}
}
return 0, errors.New("Object not found")
}
_, length, _, err := readObjectFile(objpath, true)
return length, err
}