-
Notifications
You must be signed in to change notification settings - Fork 65
/
main.go
231 lines (216 loc) · 5.45 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
package main
import (
"context"
"fmt"
"os"
"github.com/ejoffe/rake"
"github.com/ejoffe/spr/config"
"github.com/ejoffe/spr/config/config_parser"
"github.com/ejoffe/spr/git/realgit"
"github.com/ejoffe/spr/github/githubclient"
"github.com/ejoffe/spr/spr"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
var (
version = "dev"
commit = "dversion"
date = "unknown"
)
func init() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Logger = log.With().Caller().Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
func main() {
gitcmd := realgit.NewGitCmd(config.DefaultConfig())
// check that we are inside a git dir
var output string
err := gitcmd.Git("status --porcelain", &output)
if err != nil {
fmt.Println(output)
fmt.Println(err)
os.Exit(2)
}
cfg := config_parser.ParseConfig(gitcmd)
err = config_parser.CheckConfig(cfg)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
gitcmd = realgit.NewGitCmd(cfg)
ctx := context.Background()
client := githubclient.NewGitHubClient(ctx, cfg)
stackedpr := spr.NewStackedPR(cfg, client, gitcmd)
detailFlag := &cli.BoolFlag{
Name: "detail",
Value: false,
Usage: "Show detailed status bits output",
}
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{"\t"}}{{.}}
{{end}}
COMMANDS:
{{range .Commands}}{{if not .HideHelp}} {{join .Names ","}}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}
AUTHOR: {{range .Authors}}{{ . }}{{end}}
VERSION: fork of {{.Version}}
`
app := &cli.App{
Name: "spr",
Usage: "Stacked Pull Requests on GitHub",
HideVersion: true,
Version: fmt.Sprintf("%s : %s : %s\n", version, date, commit[:8]),
EnableBashCompletion: true,
Authors: []*cli.Author{
{
Name: "Eitan Joffe",
Email: "eitan@inigolabs.com",
},
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "detail",
Value: false,
Usage: "Show detailed status bits output",
},
&cli.BoolFlag{
Name: "profile",
Value: false,
Usage: "Show runtime profiling info",
},
&cli.BoolFlag{
Name: "verbose",
Value: false,
Usage: "Show verbose logging",
},
&cli.BoolFlag{
Name: "debug",
Value: false,
Usage: "Show runtime debug info",
},
},
Before: func(c *cli.Context) error {
if c.IsSet("debug") {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
rake.LoadSources(&cfg, rake.DebugWriter(os.Stdout))
}
if c.IsSet("profile") {
stackedpr.ProfilingEnable()
}
if c.IsSet("detail") || cfg.User.StatusBitsHeader {
stackedpr.DetailEnabled = true
}
if c.IsSet("verbose") {
cfg.User.LogGitCommands = true
cfg.User.LogGitHubCalls = true
}
client.MaybeStar(ctx, cfg)
return nil
},
Commands: []*cli.Command{
{
Name: "status",
Aliases: []string{"s", "st"},
Usage: "Show status of open pull requests",
Action: func(c *cli.Context) error {
stackedpr.StatusPullRequests(ctx)
return nil
},
Flags: []cli.Flag{
detailFlag,
},
},
{
Name: "sync",
Usage: "Synchronize local stack with remote",
Action: func(c *cli.Context) error {
stackedpr.SyncStack(ctx)
return nil
},
},
{
Name: "update",
Aliases: []string{"u", "up"},
Usage: "Update and create pull requests for updated commits in the stack",
Action: func(c *cli.Context) error {
if c.Bool("no-rebase") {
os.Setenv("SPR_NOREBASE", "true")
}
if c.IsSet("count") {
count := c.Uint("count")
stackedpr.UpdatePullRequests(ctx, c.StringSlice("reviewer"), &count)
} else {
stackedpr.UpdatePullRequests(ctx, c.StringSlice("reviewer"), nil)
}
return nil
},
Flags: []cli.Flag{
detailFlag,
&cli.StringSliceFlag{
Name: "reviewer",
Aliases: []string{"r"},
Usage: "Add the specified reviewer to newly created pull requests",
},
&cli.UintFlag{
Name: "count",
Aliases: []string{"c"},
Usage: "Update a specified number of pull requests from the bottom of the stack",
},
&cli.BoolFlag{
Name: "no-rebase",
Aliases: []string{"nr"},
Usage: "Disable rebasing",
},
},
},
{
Name: "merge",
Usage: "Merge all mergeable pull requests",
Action: func(c *cli.Context) error {
if c.IsSet("count") {
count := c.Uint("count")
stackedpr.MergePullRequests(ctx, &count)
} else {
stackedpr.MergePullRequests(ctx, nil)
stackedpr.UpdatePullRequests(ctx, nil, nil)
}
return nil
},
Flags: []cli.Flag{
detailFlag,
&cli.UintFlag{
Name: "count",
Aliases: []string{"c"},
Usage: "Merge a specified number of pull requests from the bottom of the stack",
},
},
},
{
Name: "check",
Usage: "Run pre merge checks (configured by MergeCheck in repository config)",
Action: func(c *cli.Context) error {
stackedpr.RunMergeCheck(ctx)
return nil
},
},
{
Name: "version",
Usage: "Show version info",
Action: func(c *cli.Context) error {
return cli.Exit(c.App.Version, 0)
},
},
},
After: func(c *cli.Context) error {
if c.IsSet("profile") {
stackedpr.ProfilingSummary()
}
return nil
},
}
app.Run(os.Args)
}