-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
303 lines (296 loc) · 6.93 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package main
import (
"log"
"net/url"
"os"
"os/exec"
"path"
"strings"
"github.com/urfave/cli"
)
func help() string {
var sb strings.Builder
out, err := exec.Command("git", "help").Output()
if err != nil {
log.Fatalln(err)
}
sb.Write(out)
sb.WriteString(`
These Backlog's git commands are provided by gitb:
pr Open the pull request list page in current repository
issue Open the issue list page in current project
browse Open other git page (e.g. branch, tree, tag, and more...) in current repository
help, h Shows a list of commands or help for one command
`)
return sb.String()
}
func main() {
app := cli.NewApp()
app.Name = name
app.Usage = usage
app.UsageText = usageText
app.CustomAppHelpTemplate = help()
app.Version = FmtVersion()
app.Commands = []cli.Command{
{
Name: "pr",
Usage: "Open the pull request list page in current repository",
Flags: []cli.Flag{
cli.StringFlag{
Name: "s, state",
Value: "open",
},
},
Action: func(c *cli.Context) error {
s := c.String("state")
repo, err := open(".")
if err != nil {
return exit(err)
}
return exit(repo.OpenPullRequestList(s))
},
Subcommands: []cli.Command{
{
Name: "show",
Usage: "Open the pull request page. When no specify <PR-ID>, open the PR page related to the current branch",
Action: func(c *cli.Context) error {
repo, err := open(".")
if err != nil {
return exit(err)
}
if c.Args().Present() {
return exit(repo.OpenPullRequestByID(c.Args().First()))
}
return exit(repo.OpenPullRequest())
},
},
{
Name: "add",
Usage: "Open the page to add pull request with current branch",
Flags: []cli.Flag{
cli.StringFlag{
Name: "b, base",
},
},
Action: func(c *cli.Context) error {
base := c.String("base")
repo, err := open(".")
if err != nil {
return exit(err)
}
return exit(repo.OpenAddPullRequest(base, ""))
},
},
{
Name: "blame",
Usage: "Show pull request id with git blame",
SkipFlagParsing: true,
Action: func(c *cli.Context) error {
repo, err := open(".")
if err != nil {
return exit(err)
}
if c.Args().Present() {
return exit(repo.BlamePR(c.Args()))
}
return exit(nil)
},
},
},
},
{
Name: "issue",
Usage: "Open the issue list page in current project",
Flags: []cli.Flag{
cli.StringFlag{
Name: "s, state",
Value: "not_closed",
},
},
Action: func(c *cli.Context) error {
s := c.String("state")
repo, err := open(".")
if err != nil {
return exit(err)
}
return exit(repo.OpenIssueList(s))
},
Subcommands: []cli.Command{
{
Name: "show",
Usage: "Open the issue page related to current branch",
Action: func(c *cli.Context) error {
repo, err := open(".")
if err != nil {
return exit(err)
}
return exit(repo.OpenIssue())
},
},
{
Name: "add",
Usage: "Open the page to add issue in current repository's project",
Action: func(c *cli.Context) error {
repo, err := open(".")
if err != nil {
return exit(err)
}
return exit(repo.OpenAddIssue())
},
},
},
},
{
Name: "browse",
Usage: "Open other git page (e.g. branch, tree, tag, and more...) in current repository",
Subcommands: []cli.Command{
{
Name: "branch",
Usage: "Open the branch list page in current repository",
Action: func(c *cli.Context) error {
repo, err := open(".")
if err != nil {
return exit(err)
}
return exit(repo.OpenBranchList())
},
},
{
Name: "tag",
Usage: "Open the tag list page in the current repository.",
Action: func(c *cli.Context) error {
repo, err := open(".")
if err != nil {
return exit(err)
}
return exit(repo.OpenTagList())
},
},
{
Name: "tree",
Usage: "Open the tree page in current branch",
Action: func(c *cli.Context) error {
repo, err := open(".")
if err != nil {
return exit(err)
}
return exit(repo.OpenTree(""))
},
},
{
Name: "history",
Usage: "Open the history page in current branch",
Action: func(c *cli.Context) error {
repo, err := open(".")
if err != nil {
return exit(err)
}
return exit(repo.OpenHistory(""))
},
},
{
Name: "network",
Usage: "Open the network page in current branch",
Action: func(c *cli.Context) error {
repo, err := open(".")
if err != nil {
return exit(err)
}
return exit(repo.OpenNetwork(""))
},
},
{
Name: "repo",
Usage: "Open the repository list page in current project",
Action: func(c *cli.Context) error {
repo, err := open(".")
if err != nil {
return exit(err)
}
return exit(repo.OpenRepositoryList())
},
},
{
Name: "show",
Usage: "Open the corresponding page to given file or directory in current project",
Flags: []cli.Flag{
cli.StringFlag{
Name: "l, line",
},
},
Action: func(c *cli.Context) error {
repo, err := open(".")
if err != nil {
return exit(err)
}
filePath := c.Args().First()
if !path.IsAbs(filePath) {
wd, err := os.Getwd()
if err != nil {
return exit(err)
}
filePath = path.Join(wd, filePath)
}
fileUrl, err := url.Parse(filePath)
if err != nil {
return exit(err)
}
fs, err := os.Stat(fileUrl.Path)
if err != nil {
return exit(err)
}
line := c.String("line")
if line == "" {
line = fileUrl.Fragment
}
return exit(repo.OpenObject(fileUrl.Path, fs.IsDir(), line))
},
},
{
Name: "commit",
Usage: "Open the network page in current branch",
Action: func(c *cli.Context) error {
repo, err := open(".")
if err != nil {
return exit(err)
}
hash := c.Args().First()
if len(hash) == 0 {
return exit(err)
}
return exit(repo.OpenCommit(hash))
},
},
},
},
}
app.OnUsageError = func(context *cli.Context, err error, isSubcommand bool) error {
if isSubcommand {
return err
}
if err := NewGitCmd(os.Args[1:]).Run(); err != nil {
return err
}
return nil
}
app.CommandNotFound = func(c *cli.Context, name string) {
args := append([]string{name}, os.Args[2:]...)
if err := NewGitCmd(args).Run(); err != nil {
log.Fatalln(err)
}
}
_ = app.Run(os.Args)
}
func exit(err error) error {
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
}
func open(path string) (*BacklogRepository, error) {
repo, err := OpenRepository(path)
if err != nil {
return nil, err
}
return NewBacklogRepository(repo), nil
}