-
Notifications
You must be signed in to change notification settings - Fork 2
/
repo.go
136 lines (103 loc) · 2.75 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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"code.google.com/p/go-uuid/uuid"
"github.com/kr/fs"
)
func getAuthors(repoPath string) []string {
// XXX stub
authors := []string{}
return authors
}
func getDiff(repoPath string, currentCommit string, previousCommit string) []byte {
diffCmd := exec.Command("git", "--git-dir="+repoPath+"/.git", "diff", currentCommit, previousCommit)
diffOut, diffErr := diffCmd.Output()
check(diffErr)
return diffOut
}
func getCommits(repoPath string) ([]string, map[string]map[string]string) {
commits := map[string]map[string]string{}
order := []string{}
revCmd := exec.Command("git", "--git-dir="+repoPath+"/.git", "rev-list", "--all", "--no-merges", "--pretty=format:\"%H|%an|%at\"")
revOut, revErr := revCmd.Output()
check(revErr)
lines := strings.Split(string(revOut), "\n")
prevCommit := ""
for i, val := range lines {
if i%2 != 0 {
// XXX error check to ensure there are exactly 3 splits
components := strings.Split(val[1:len(val)-1], "|")
if _, ok := commits[prevCommit]; ok {
commits[prevCommit]["prevCommit"] = components[0]
}
order = append(order, components[0])
kvs := map[string]string{}
kvs["prevCommit"] = components[0]
kvs["author"] = components[1]
kvs["timestamp"] = components[2]
commits[components[0]] = kvs
prevCommit = components[0]
}
}
return order, commits
}
func getFiles(repoPath string) []string {
files := []string{}
walker := fs.Walk(repoPath)
for walker.Step() {
if err := walker.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
if !strings.Contains(walker.Path(), ".git") && strings.Contains(walker.Path(), ".") {
name := walker.Path()
f, err := os.Open(name)
if err == nil {
defer f.Close()
fi, err := f.Stat()
if err == nil {
switch mode := fi.Mode(); {
case mode.IsDir():
continue
case mode.IsRegular():
files = append(files, walker.Path())
}
}
}
}
}
return files
}
func getFileContents(filename string) []byte {
data, err := ioutil.ReadFile(filename)
check(err)
return data
}
func checkoutCommit(repoPath string, commit string) {
cwd, wdErr := os.Getwd()
check(wdErr)
chErr := os.Chdir(repoPath)
check(chErr)
checkoutCmd := exec.Command("git", "checkout", commit)
_, checkoutErr := checkoutCmd.Output()
check(checkoutErr)
chErr = os.Chdir(cwd)
check(chErr)
}
func cloneRepo(repo string) string {
uuidRepo := uuid.New()
// XXX don't forget to cleanup after we're finished
err := os.Mkdir("/tmp/"+uuidRepo, 0644)
check(err)
if repo != "" {
cloneCmd := exec.Command("git", "clone", repo, "/tmp/"+uuidRepo)
cloneOut, cloneErr := cloneCmd.Output()
check(cloneErr)
fmt.Println(string(cloneOut))
}
return "/tmp/" + uuidRepo
}