-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitFunctions.go
101 lines (94 loc) · 2.74 KB
/
gitFunctions.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
package main
import (
"strings"
"os/exec"
"log"
"fmt"
)
type fileContributer struct {
Name string
Email string
totalCommits int
}
type fileContribution struct {
Name string
Email string
Commit string
AbbreviatedHash string
Hash string
DateTime string
}
func _prototype_getGitLog(path string) fileContributer {
args := []string{"log", "--format=%an|%ae|%f", path}
cmd := exec.Command("git", args...)
out, errCmd := cmd.CombinedOutput()
if errCmd != nil {
log.Fatalf("cmd.Run() failed with %s\n", errCmd)
}
outArray := strings.Split(string(out), "|")
c := fileContributer{}
if (len(outArray) > 1) {
c = fileContributer{
Name: outArray[0],
Email: outArray[1],
}
fmt.Println(path)
fmt.Println(c)
}
return c
}
// getGitLog returns the list of contributers of a given path
// TODO: return array of fileContributer instead
func getGitLog(path string, repoPath string) []fileContribution {
args := []string{"log", "--format=%an||%ae||%f||%h||%H||%aI", path}
cmd := exec.Command("git", args...)
cmd.Dir = repoPath
out, errCmd := cmd.CombinedOutput()
if errCmd != nil {
log.Fatalf("cmd.Run() failed with %s\n", errCmd)
}
contributionLog := strings.Split(string(out), "\n")
fileContribs := []fileContribution{}
for _, contribution := range contributionLog {
splitLog := strings.Split(contribution, "||")
if (len(splitLog) > 1) {
fileContribs = append(fileContribs, fileContribution{
Name: strings.Replace(splitLog[0], "'", " ", -1),
Email: splitLog[1],
Commit: splitLog[2],
AbbreviatedHash: splitLog[3],
Hash: splitLog[4],
DateTime: splitLog[5],
})
}
}
return fileContribs
}
// getGitRemoteUrl returns the web url for a repository
func getGitRemoteUrl(repoPath string) string {
args := []string{"config", "--get", "remote.origin.url"}
cmd := exec.Command("git", args...)
cmd.Dir = repoPath
out, errCmd := cmd.CombinedOutput()
if errCmd != nil {
log.Fatalf("cmd.Run() failed with %s\n", errCmd)
}
url := string(out)
url = strings.Replace(url, "[email protected]:", "https://github.com/", -1)
url = strings.Replace(url, ".git", "/", -1)
url = strings.Replace(url, "\n", "", -1)
return url
}
// buildGitHubUrl creates a url for a given file to find it on GitHub
func buildGitHubUrl(remoteUrl string, path string, isDir bool) string {
middlePart := "blob/master/"
if (isDir) {
middlePart = "tree/master/"
}
return remoteUrl + middlePart + path
}
// includePath evaluates whether to include a path in the resulting graph or not
func includePath(path string) bool {
// TODO: should be a list coming from .gitignore
return !strings.HasPrefix(path, ".") && !strings.HasPrefix(path, "node_modules")
}