-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepository.go
More file actions
395 lines (313 loc) · 8.04 KB
/
repository.go
File metadata and controls
395 lines (313 loc) · 8.04 KB
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
package git
import (
"container/list"
"fmt"
"strings"
"time"
"github.com/mechmind/git-go/git"
"github.com/mechmind/git-go/history"
"github.com/mechmind/git-go/rawgit"
)
// repo.go ports
const (
BRANCH_PREFIX = "refs/heads/"
_PRETTY_LOG_FORMAT = `--pretty=format:%H`
)
type Repository struct {
Path string
repo *git.Repository
}
func InitRepository(path string, bare bool) error {
// FIXME: implement
return nil
}
func OpenRepository(path string) (*Repository, error) {
repo, err := git.OpenRepository(path)
if err != nil {
return nil, err
}
return &Repository{
Path: path,
repo: repo,
}, nil
}
type CloneRepoOptions struct {
Mirror bool
Bare bool
Quiet bool
Timeout time.Duration
}
type Branch struct {
Name string
Path string
}
func Clone(from, to string, opts CloneRepoOptions) (err error) {
// FIXME: implement
return nil
}
func Pull(repoPath string, all bool) error {
// FIXME: implement
return nil
}
func Push(repoPath, remote, branch string) error {
// FIXME: implement
return nil
}
func ResetHEAD(repoPath string, hard bool, revision string) error {
// FIXME: implement
return nil
}
// repo_object.go ports
type ObjectType string
const (
OBJECT_COMMIT ObjectType = "commit"
OBJECT_TREE ObjectType = "tree"
OBJECT_BLOB ObjectType = "blob"
OBJECT_TAG ObjectType = "tag"
)
// repo_branch.go ports
func IsReferenceExist(repoPath, name string) bool {
repo, err := OpenRepository(repoPath)
if err != nil {
return false
}
oid, err := repo.repo.ResolveRef(name)
return oid != nil
}
func IsBranchExist(repoPath, name string) bool {
repo, err := OpenRepository(repoPath)
if err != nil {
return false
}
return repo.IsBranchExist(name)
}
func (repo *Repository) IsBranchExist(name string) bool {
oid, _ := repo.repo.ResolveBranch(name)
return oid != nil
}
func (repo *Repository) GetHEADBranch() (*Branch, error) {
value, err := repo.repo.ReadRef("HEAD")
if err != nil {
return nil, err
}
branchPrefix := rawgit.RefPrefix + rawgit.RefBranchNS
if !strings.HasPrefix(value, branchPrefix) {
return nil, fmt.Errorf("invalid HEAD: %v", value)
}
branch := &Branch{
Name: value[len(branchPrefix):],
Path: value[len(rawgit.RefPrefix):],
}
return branch, nil
}
func (repo *Repository) SetDefaultBranch(name string) error {
ref := rawgit.RefPrefix + rawgit.RefBranchNS + name
return repo.repo.WriteRef("HEAD", ref)
}
func (repo *Repository) GetBranches() ([]string, error) {
heads, err := repo.repo.ListRefs(rawgit.RefBranchNS)
if err != nil {
return nil, err
}
branches := []string{}
for _, head := range heads {
branches = append(branches, head[len(rawgit.RefBranchNS):])
}
return branches, nil
}
func (repo *Repository) AddRemote(name, url string, fetch bool) error {
// FIXME: implement
return nil
}
func (repo *Repository) RemoveRemote(name string) error {
// FIXME: implement
return nil
}
// repo_commit.go ports
func (repo *Repository) GetBranchCommitID(name string) (string, error) {
oid, err := repo.repo.ResolveRef(rawgit.RefBranchNS + name)
if err != nil {
return "", err
}
return oid.String(), nil
}
func (repo *Repository) getCommit(id sha1) (*Commit, error) {
commit, err := repo.repo.OpenCommit(sha2oidp(id))
if err != nil {
return nil, err
}
return raw2commit(repo, commit)
}
func (repo *Repository) GetTagCommitID(name string) (string, error) {
oid, err := repo.repo.ResolveRef(name)
if err != nil {
return "", err
}
return oid.String(), nil
}
func (repo *Repository) openCommit(oid *rawgit.OID) (*Commit, error) {
commit, err := repo.repo.OpenCommit(oid)
if err != nil {
return nil, err
}
return raw2commit(repo, commit)
}
func (repo *Repository) GetCommit(commitID string) (*Commit, error) {
oid, err := rawgit.ParseOID(commitID)
if err != nil {
return nil, err
}
return repo.openCommit(oid)
}
func (repo *Repository) GetBranchCommit(name string) (*Commit, error) {
oid, err := repo.repo.ResolveBranch(name)
if err != nil {
return nil, err
}
return repo.openCommit(oid)
}
func (repo *Repository) GetTagCommit(name string) (*Commit, error) {
oid, otype, err := repo.repo.ResolveTag(name)
if err != nil {
return nil, err
}
if otype != rawgit.OTypeCommit {
return nil, fmt.Errorf("tag '%s' points to object of type %s, not a commit", name, otype.String())
}
return repo.openCommit(oid)
}
func (repo *Repository) getHEAD() (*Commit, error) {
oid, err := repo.repo.ResolveRef("HEAD")
if err != nil {
return nil, err
}
return repo.openCommit(oid)
}
func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
head, err := repo.getHEAD()
if err != nil {
return nil, err
}
return head.GetCommitByPath(relpath)
}
func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
oid, err := rawgit.ResolveName(repo.repo, revision)
if err != nil {
return 0, err
}
rawCommit, err := repo.repo.OpenCommit(oid)
if err != nil {
return 0, err
}
commit, err := raw2commit(repo, rawCommit)
if err != nil {
return 0, err
}
return repo.fileCommitsCount(commit, file)
}
func (repo *Repository) fileCommitsCount(commit *Commit, file string) (int64, error) {
pathCb := history.MakePathChecker(commit.repo.repo, file)
cmp := history.MakePathComparator(commit.repo.repo, file)
counter, result := history.MakeCounter(pathCb)
hist := history.New(commit.repo.repo)
_, err := hist.WalkFilteredHistory(sha2oidp(commit.ID), counter, cmp)
if err != nil {
return 0, err
}
return int64(result()), nil
}
func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (*list.List, error) {
oid, err := rawgit.ResolveName(repo.repo, revision)
if err != nil {
return nil, err
}
rawCommit, err := repo.repo.OpenCommit(oid)
if err != nil {
return nil, err
}
commit, err := raw2commit(repo, rawCommit)
if err != nil {
return nil, err
}
return repo.commitsByFileAndRange(commit, file, page)
}
func (repo *Repository) commitsByFileAndRange(commit *Commit, file string, page int) (*list.List, error) {
pathCb := history.MakePathChecker(commit.repo.repo, file)
cmp := history.MakePathComparator(commit.repo.repo, file)
pager := history.MakePager(commit.repo.repo, pathCb, (page-1)*CommitsRangeSize, CommitsRangeSize)
hist := history.New(commit.repo.repo)
result, err := hist.WalkFilteredHistory(sha2oidp(commit.ID), pager, cmp)
if err != nil {
return nil, err
}
return repo.raw2commitList(result)
}
func (repo *Repository) FilesCountBetween(startCommitID, endCommitID string) (int, error) {
// FIXME: implement
return -1, nil
}
// this implementation was borrowed as-is from git-module
func (repo *Repository) CommitsBetween(last *Commit, before *Commit) (*list.List, error) {
l := list.New()
if last == nil || last.ParentCount() == 0 {
return l, nil
}
var err error
cur := last
for {
if cur.ID.Equal(before.ID) {
break
}
l.PushBack(cur)
if cur.ParentCount() == 0 {
break
}
cur, err = cur.Parent(0)
if err != nil {
return nil, err
}
}
return l, nil
}
func (repo *Repository) CommitsBetweenIDs(last, before string) (*list.List, error) {
lastCommit, err := repo.GetCommit(last)
if err != nil {
return nil, err
}
beforeCommit, err := repo.GetCommit(before)
if err != nil {
return nil, err
}
return repo.CommitsBetween(lastCommit, beforeCommit)
}
func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) {
// FIXME: implement
return -1, nil
}
// repo_tree.go ports
func (repo *Repository) getTree(id sha1) (*Tree, error) {
_, _, err := repo.repo.StatObject(sha2oidp(id))
if err != nil {
return nil, ErrNotExist{id.String(), ""}
}
return NewTree(repo, id), nil
}
// Find the tree object in the repository.
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
id, err := NewIDFromString(idStr)
if err != nil {
return nil, err
}
return repo.getTree(id)
}
// own helpers
func (repo *Repository) raw2commitList(src *list.List) (*list.List, error) {
var err error
for cur := src.Front(); cur != nil; cur = cur.Next() {
cur.Value, err = raw2commit(repo, cur.Value.(*rawgit.Commit))
if err != nil {
return nil, err
}
}
return src, nil
}