forked from alaingilbert/git2graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (159 loc) · 3.6 KB
/
main.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
package main
import (
"git2graph/git2graph"
"os"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)
func bootstrap(c *cli.Context) error {
var nodes []map[string]interface{}
var err error
fromFlag := c.Int("from")
sizeFlag := c.Int("size")
contextFlag := c.Bool("context")
jsonFlag := c.String("json")
fileFlag := c.String("file")
git2graph.DebugMode = c.Bool("debug")
repoFlag := c.Bool("repo")
git2graph.NoOutput = c.Bool("no-output")
repoLinearFlag := c.Bool("repo-linear")
seqIds := c.Bool("seq-ids")
logLevel := c.String("log")
setLogLevel(logLevel)
if repoFlag {
nodes, err = git2graph.GetInputNodesFromRepo(seqIds)
} else if repoLinearFlag {
nodes, err = git2graph.GetInputNodesFromRepo(seqIds)
git2graph.SerializeOutput(nodes)
return err
} else if jsonFlag != "" {
nodes, err = git2graph.GetInputNodesFromJSON([]byte(jsonFlag))
} else if fileFlag != "" {
nodes, err = git2graph.GetInputNodesFromFile(fileFlag)
} else {
cli.ShowAppHelp(c)
return err
}
if err != nil {
log.Error(err)
return err
}
myColors := git2graph.DefaultColors
out, err := git2graph.BuildTree(nodes, myColors)
if err != nil {
log.Error(err)
return err
}
for _, node := range out {
delete(node, "parentsPaths")
}
var tmp []map[string]interface{}
if fromFlag >= 0 && sizeFlag >= 1 {
// TODO: include context (nodes before "from" that have parents inside or after the range)
if contextFlag {
for _, node := range out {
hasParentsInContext := false
for _, nodeParent := range node["parents_paths"].([]git2graph.Path) {
if nodeParent.Path[len(nodeParent.Path)-1].Y >= fromFlag {
hasParentsInContext = true
}
}
if hasParentsInContext ||
node["idx"].(int) >= fromFlag && node["idx"].(int) < fromFlag+sizeFlag {
tmp = append(tmp, node)
}
if node["idx"].(int) >= fromFlag+sizeFlag-1 {
break
}
}
} else {
tmp = out[fromFlag : fromFlag+sizeFlag]
}
} else {
tmp = out
}
git2graph.SerializeOutput(tmp)
return err
}
func setLogLevel(logLevel string) {
switch logLevel {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
case "fatal":
log.SetLevel(log.FatalLevel)
case "panic":
log.SetLevel(log.PanicLevel)
default:
log.SetLevel(log.WarnLevel)
}
}
func init() {
log.SetLevel(log.WarnLevel)
}
func main() {
var authors []cli.Author
// Collaborators, add your name here :)
authors = append(authors, cli.Author{"Alain Gilbert", "[email protected]"})
app := cli.NewApp()
app.Authors = authors
app.Version = "0.0.0"
app.Name = "git2graph"
app.Usage = "Take a git tree, make a graph structure"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "f, file",
Usage: "File",
},
cli.StringFlag{
Name: "j, json",
Usage: "Json input",
},
cli.StringFlag{
Name: "L, log",
Usage: "Log level",
},
cli.BoolFlag{
Name: "d, debug",
Usage: "Debug mode",
},
cli.BoolFlag{
Name: "r, repo",
Usage: "Repository",
},
cli.BoolFlag{
Name: "l, repo-linear",
Usage: "Repository linear history",
},
cli.BoolFlag{
Name: "s, seq-ids",
Usage: "Use sequentiel ids instead of sha for linear history",
},
cli.BoolFlag{
Name: "n, no-output",
Usage: "No output",
},
// TODO: From should be a sha
cli.IntFlag{
Name: "from",
Usage: "From",
Value: -1,
},
cli.IntFlag{
Name: "size",
Usage: "Size",
Value: -1,
},
cli.BoolFlag{
Name: "context",
Usage: "Include context",
},
}
app.Action = bootstrap
app.Run(os.Args)
}