-
Notifications
You must be signed in to change notification settings - Fork 1
/
repo.go
155 lines (131 loc) · 2.6 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
package git
import (
"encoding/hex"
"errors"
"github.com/dkolbly/logging"
"io/ioutil"
"os"
"path"
"strings"
)
var ErrNoBranch = errors.New("no such branch")
var ErrNoTag = errors.New("no such tag")
var log = logging.New("git")
type Git struct {
stores []Store
}
func New() *Git {
return &Git{}
}
func (g *Git) AddStore(s Store) {
g.stores = append(g.stores, s)
}
type Store interface {
GetNamed(RefType, string) *NamedRef
Get(obj *Ptr) GitObject
EnumerateTo(chan<- Ptr)
}
// optional interface
type NameEnumerater interface {
NameEnumerate(t RefType) ([]NamedRef, error)
}
func Open(d string) (*Git, error) {
g := &Git{}
Bare(g, d)
lst, err := ioutil.ReadDir(path.Join(d, "objects/pack"))
if err == nil {
for _, f := range lst {
if strings.HasSuffix(f.Name(), ".pack") {
pfile := path.Join(d, "objects/pack", f.Name())
_, err := IncludePackFile(g, pfile)
if err != nil {
log.Error("Rats: %s", err)
}
}
}
}
return g, nil
}
func (g *Git) Get(p *Ptr) GitObject {
for _, store := range g.stores {
o := store.Get(p)
if o != nil {
return o
}
}
return nil
}
func (g *Git) Enumerate() <-chan Ptr {
ch := make(chan Ptr, 10000)
go g.enumerateTo(ch)
return ch
}
func (g *Git) enumerateTo(to chan<- Ptr) {
defer close(to)
for _, store := range g.stores {
store.EnumerateTo(to)
}
}
func (p *PackFile) EnumerateTo(to chan<- Ptr) {
for _, item := range p.indexContents {
to <- item
}
}
func (g *GitDir) EnumerateTo(to chan<- Ptr) {
// walk the loose objects
f, err := os.Open(path.Join(g.Dir, "objects"))
if err != nil {
return
}
defer f.Close()
lst, err := f.Readdirnames(-1)
if err != nil {
return
}
for _, major := range lst {
if len(major) != 2 {
continue
}
b0, err := hex.DecodeString(major)
if err != nil || len(b0) != 1 {
continue
}
sub, err := os.Open(path.Join(g.Dir, "objects", major))
if err != nil {
continue
}
sublst, err := sub.Readdirnames(-1)
sub.Close()
if err != nil {
continue
}
for _, minor := range sublst {
b1, err := hex.DecodeString(minor)
if err == nil && len(b1) == 19 {
var p Ptr
p.hash[0] = b0[0]
copy(p.hash[1:], b1)
to <- p
}
}
}
}
func (g *Git) enumNamed(t RefType) ([]NamedRef, error) {
var lst []NamedRef
for _, store := range g.stores {
if ne, ok := store.(NameEnumerater); ok {
more, err := ne.NameEnumerate(t)
if err != nil {
return nil, err
}
lst = append(lst, more...)
}
}
return lst, nil
}
func (g *Git) Branches() ([]NamedRef, error) {
return g.enumNamed(Head)
}
func (g *Git) Tags() ([]NamedRef, error) {
return g.enumNamed(Tag)
}