-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit2cypher.go
186 lines (160 loc) · 5.12 KB
/
git2cypher.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
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
package main
import (
"fmt"
"flag"
"path/filepath"
"os"
"log"
"strings"
)
type fileInfo struct {
Name string
Path string
Url string
Size int64
Level int
IsDir bool
Id string
Extension string
ModTime int64
ParentPath string
Contributions []fileContribution
CommitCount int
}
var nodes []fileInfo
var processedFiles = make(map[string]bool)
var processedNodes = make(map[string]bool)
var processedContributersSum = make(map[string]int)
var processedContributionsSum = make(map[string]int)
var verbose bool
var simplified bool
var repoPath string
var gitRepoUrl string
// initFlags parses the command line flags
func initFlags() {
flag.BoolVar(&verbose, "verbose", false, "logs iteration through file tree to console")
flag.BoolVar(&simplified, "simplified", false, "won't create a distinct relationship for each commit when set")
flag.StringVar(&repoPath, "path", ".", "the full path of the repository")
flag.Parse()
}
// getFileExtension returns the extension for a given file's full name
func getFileExtension (info os.FileInfo) string {
if (info.IsDir() == false) {
stringSegments := strings.Split(info.Name(), ".")
return stringSegments[len(stringSegments) - 1]
}
return ""
}
// verboseLog writes a string to stdOut if the verbose flag is set
func verboseLog(toLog string) {
if (verbose) {
fmt.Println(toLog)
}
}
func main() {
initFlags()
verboseLog("repoPath: " + repoPath)
gitRepoUrl = getGitRemoteUrl(repoPath)
verboseLog("gitRepoUrl: " + gitRepoUrl)
err := filepath.Walk(repoPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if repoPath != "." {
path = path[len(repoPath):len(path)]
}
if (len(path) > 0 && includePath(path)) {
verboseLog("path: " + path)
pathSegments := strings.Split(path, "/")
fileDepth := len(pathSegments) - 1
fileName := info.Name()
verboseLog("fileName: " + fileName)
if (processedFiles[path] != true) {
parentPath := strings.Join(pathSegments[:len(pathSegments)-1], "/")
verboseLog("parentPath: " + parentPath)
parentDepth := fileDepth - 1
if (parentDepth < 0) {
parentDepth = 0
}
contributions := getGitLog(path, repoPath)
nodes = append(nodes, fileInfo{
Name: fileName,
Path: path,
Url: buildGitHubUrl(gitRepoUrl, path, info.IsDir()),
Size: info.Size(),
Level: fileDepth,
Extension: getFileExtension(info),
Id: createCypherFriendlyVarName(path, fileDepth),
IsDir: info.IsDir(),
ModTime: info.ModTime().Unix(),
ParentPath: parentPath,
Contributions: contributions,
CommitCount: len(contributions),
})
processedFiles[path] = true
}
}
return nil
})
verboseLog("")
verboseLog("------------------------------------------------------------------------")
verboseLog("")
for _, currentFile := range nodes {
var processedContributers = make(map[string]int)
var processedContributions = make(map[string]int)
fmt.Println(":BEGIN")
label := getLabelForFileNode(currentFile)
if (!processedNodes[currentFile.Path]) {
fmt.Println(fileInfoToCypher(currentFile, label))
processedNodes[currentFile.Path] = true
}
if (label == "file") {
for _, contribution := range currentFile.Contributions {
contributerId := createCypherFriendlyVarName(contribution.Name, 0)
if (processedContributers[contributerId] < 1) {
fmt.Println(contributerToCypher(contributerId, contribution.Name, contribution.Email))
processedContributers[contributerId] = 0
}
processedContributers[contributerId] += 1
processedContributersSum[contributerId] += 1
contributionId := currentFile.Id + "__" + contributerId
if (processedContributions[contributionId] < 1) {
fmt.Println(contributionToCypher(currentFile.Id, contributerId, contributionId))
processedContributions[contributionId] = 0
}
if (simplified != true) {
fmt.Println(commitToCypher(currentFile.Id, contributerId, contribution))
}
processedContributions[contributionId] += 1
processedContributionsSum[contributionId] += 1
}
}
fmt.Println(";")
fmt.Println(":COMMIT")
if (len(currentFile.ParentPath) > 0) {
fmt.Println(":BEGIN")
fmt.Println(folderStructureToCypher(currentFile))
fmt.Println(";")
fmt.Println(":COMMIT")
}
}
for contributerId, contributionCount := range processedContributersSum {
fmt.Println(":BEGIN")
fmt.Println(contributerToCypherUpdate(contributerId, contributionCount))
fmt.Println(";")
fmt.Println(":COMMIT")
}
for contributionId, commitCount := range processedContributionsSum {
fmt.Println(":BEGIN")
fmt.Println(contributionToCypherUpdate(contributionId, commitCount))
fmt.Println(";")
fmt.Println(":COMMIT")
}
fmt.Println(":BEGIN")
fmt.Println(removeProperty("_tempId"))
fmt.Println(";")
fmt.Println(":COMMIT")
if err != nil {
log.Println(err)
}
}