-
Notifications
You must be signed in to change notification settings - Fork 146
/
repository.go
77 lines (72 loc) · 1.56 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
package test
import (
"io"
"io/ioutil"
"os"
"path"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/storage/memory"
)
// Repository is a boilerplate sample repository (Hercules itself).
var Repository *git.Repository
// FakeChangeForName creates an artificial Git Change from a file name and two arbitrary hashes.
func FakeChangeForName(name string, hashFrom string, hashTo string) *object.Change {
return &object.Change{
From: object.ChangeEntry{Name: name, TreeEntry: object.TreeEntry{
Name: name, Hash: plumbing.NewHash(hashFrom),
}},
To: object.ChangeEntry{Name: name, TreeEntry: object.TreeEntry{
Name: name, Hash: plumbing.NewHash(hashTo),
}},
}
}
func init() {
cwd, err := os.Getwd()
if err == nil {
for true {
files, err := ioutil.ReadDir(cwd)
if err != nil {
break
}
found := false
for _, f := range files {
if f.Name() == "README.md" {
found = true
break
}
}
if found {
break
}
oldCwd := cwd
cwd = path.Dir(cwd)
if oldCwd == cwd {
break
}
}
Repository, err = git.PlainOpen(cwd)
if err == nil {
iter, err := Repository.CommitObjects()
if err == nil {
commits := -1
for ; err != io.EOF; _, err = iter.Next() {
if err != nil {
panic(err)
}
commits++
if commits >= 100 {
return
}
}
}
}
}
Repository, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: "https://github.com/src-d/hercules",
})
if err != nil {
panic(err)
}
}