-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolder.go
101 lines (90 loc) · 2.2 KB
/
folder.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
package arq
import (
"context"
"fmt"
"io"
"log"
"path"
"regexp"
"sort"
"strconv"
"github.com/rclone/rclone/fs"
)
type Folder struct {
uuid string
computer *Computer
fInfo *FolderInfo
}
func (f *Folder) FindMaster(ctx context.Context) (ShaHash, error) {
obj, err := f.computer.NewObject(ctx, path.Join("bucketdata", f.uuid, "refs", "heads", "master"))
var sh ShaHash
if err != nil {
return sh, err
}
reader, err := obj.Open(ctx)
if err != nil {
return sh, err
}
defer reader.Close()
b := make([]byte, 40)
if _, err = io.ReadAtLeast(reader, b, 40); err != nil {
return sh, fmt.Errorf("failed to read hash: %s", err)
}
return DecodeShaHash(b)
}
type RefListEntry struct {
Name int
o fs.Object
}
var refRegex = regexp.MustCompile("[0-9]+")
// ListRefs returns a sorted list of RefListEntry, corresponding to all the
// commits in `refs/logs/master`.
//
// The list is sorted in reverse chronological order (the most recent commit is
// first).
func (f *Folder) ListRefs(ctx context.Context) ([]RefListEntry, error) {
dirEntries, err := f.computer.List(ctx, path.Join("bucketdata", f.uuid, "refs", "logs", "master"))
if err != nil {
return nil, err
}
refs := make([]RefListEntry, 0, len(dirEntries))
for _, entry := range dirEntries {
fName := path.Base(entry.String())
if !refRegex.MatchString(fName) {
continue
}
var name int
if name, err = strconv.Atoi(fName); err != nil {
log.Println(err)
continue
}
o, ok := entry.(fs.Object)
if !ok {
continue
}
refs = append(refs, RefListEntry{
Name: name,
o: o,
})
}
sort.Slice(refs, func(i, j int) bool {
return refs[i].Name > refs[j].Name
})
return refs, nil
}
type RefEntry struct {
OldHeadStretchKey bool `plist:"oldHeadStretchKey"`
NewHeadSha1 string `plist:"newHeadSHA1"`
NewHeadStretchKey bool `plist:"newHeadStretchKey"`
PackSha1 string `plist:"packSHA1"`
}
func (f *Folder) RefEntry(ctx context.Context, name int) (RefEntry, error) {
var re RefEntry
fName := path.Join("bucketdata", f.uuid, "refs", "logs", "master", strconv.Itoa(name))
obj, err := f.computer.NewObject(ctx, fName)
if err != nil {
return re, err
}
err = unmarshalPlist(ctx, obj, &re)
return re, err
}