forked from nicolasdilley/Gomela
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
executable file
·64 lines (46 loc) · 1.28 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
package main
import (
"os"
"strings"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/storage/memory"
)
func CloneRepo(url string, commit string) (string, string, error) {
// var last_commit_hash string
// Tempdir to clone the repository
var err error
dir := PROJECTS_FOLDER + "/" + strings.Replace(url, "/", AUTHOR_PROJECT_SEP, -1)
_, err1 := os.Stat(dir)
if os.IsNotExist(err1) {
// Clones the repository into the given dir, just as a normal git clone does
var r *git.Repository
r, err = git.PlainClone(dir, false, &git.CloneOptions{
URL: "https://github.com/" + url,
Progress: os.Stdout,
})
// var head *plumbing.Reference
// head, err = r.Head()
var w *git.Worktree
w, err = r.Worktree()
err = w.Checkout(&git.CheckoutOptions{
Hash: plumbing.NewHash(commit),
})
}
return dir, commit, err
}
func CloneRepoAndGetCommit(url string) string {
var err error
// Clones the repository into the given dir, just as a normal git clone does
var r *git.Repository
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: "https://github.com/" + url,
Progress: os.Stdout,
})
var head *plumbing.Reference
head, err = r.Head()
if err != nil {
panic(err)
}
return head.Hash().String()
}