-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch.go
399 lines (344 loc) · 10.5 KB
/
branch.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package gitcmd
import (
"strings"
"github.com/gookit/color/colorp"
"github.com/gookit/gcli/v3"
"github.com/gookit/gcli/v3/gflag"
"github.com/gookit/gitw"
"github.com/gookit/gitw/brinfo"
"github.com/gookit/gitw/gitutil"
"github.com/gookit/goutil/basefn"
"github.com/gookit/goutil/cliutil"
"github.com/gookit/goutil/strutil/textutil"
"github.com/gookit/goutil/sysutil/cmdr"
"github.com/gookit/goutil/timex"
"github.com/inhere/kite-go/internal/app"
"github.com/inhere/kite-go/internal/apputil"
"github.com/inhere/kite-go/internal/biz/cmdbiz"
"github.com/inhere/kite-go/pkg/cmdutil"
)
// NewBranchCmd instance
func NewBranchCmd() *gcli.Command {
return &gcli.Command{
Name: "branch",
Desc: "git branch commands extension",
Aliases: []string{"br"},
Subs: []*gcli.Command{
BranchDeleteCmd,
BranchCreateCmd,
BranchListCmd,
BranchSetupCmd,
},
}
}
var bdOpts = struct {
cmdbiz.CommonOpts
Fetch bool `flag:"desc=fetch remote branches before search;shorts=f"`
Remote string `flag:"desc=only find branches on the remote;shorts=r"`
}{}
// BranchDeleteCmd quickly delete branches
var BranchDeleteCmd = &gcli.Command{
Name: "delete",
Desc: "delete branches",
Aliases: []string{"del", "rm"},
Examples: `
# Delete local branches
{$fullCmd} fix_1 fix_2 fea_1
{$fullCmd} "fea*" # use glob match
{$fullCmd} "prefix:fea" # use prefix match
{$fullCmd} "suffix:_dev" # use suffix match
{$fullCmd} "regex:fix_\d+" # use regex match
# Delete remote branches
{$fullCmd} -r origin fix_* # use glob match
`,
Config: func(c *gcli.Command) {
bdOpts.BindCommonFlags(c)
c.MustFromStruct(&bdOpts)
c.AddArg("branches", "the branch names or match pattern(can with match mode)", true, true)
},
Func: func(c *gcli.Command, args []string) error {
rp := app.Gitx().LoadRepo(blOpts.Workdir)
if blOpts.Fetch {
if err := rp.FetchAll("-np"); err != nil {
return err
}
}
matcher := brinfo.NewMulti()
patterns := c.Arg("branches").Strings()
var branches []string
for _, pattern := range patterns {
if gitutil.IsBranchName(pattern) {
branches = append(branches, pattern)
continue
}
// as match pattern
matcher.Add(brinfo.NewMatcher(pattern))
}
bis := rp.BranchInfos()
opt := &gitw.SearchOpt{Flag: gitw.BrSearchLocal, Remote: bdOpts.Remote}
if bdOpts.Remote != "" {
opt.Flag = gitw.BrSearchRemote
}
brs := bis.SearchV2(matcher, opt)
num := len(brs)
rp.SetDryRun(blOpts.DryRun)
colorp.Cyanf("Will delete %d branches, Glob match number %d\n", num+len(branches), num)
if blOpts.Confirm && !cliutil.Confirm("Do you want to continue?") {
colorp.Cyanln("Canceled")
return nil
}
for _, info := range brs {
// git branch -d feature-*
// git push origin --delete feature-*
colorp.Warnf("Do delete branch: %s\n", info.Name)
if err := rp.BranchDelete(info.Short, info.Remote); err != nil {
return err
}
}
return nil
},
}
var blOpts = struct {
cmdbiz.CommonOpts
Remote string `flag:"desc=only show branches on the remote;shorts=r"`
Match string `flag:"desc=the branch name match pattern, default use glob mode;shorts=p,m"`
Limit int `flag:"desc=limit the max match branches;shorts=n"`
All bool `flag:"desc=display all branches;shorts=a"`
Exec string `flag:"desc=execute command for each branch;shorts=x"`
Delete bool `flag:"desc=delete matched branches;shorts=d"`
Fetch bool `flag:"desc=fetch remote branches before search;shorts=f"`
Update bool `flag:"desc=update branches after exec/delete;shorts=u"`
}{}
// BranchListCmd instance
var BranchListCmd = &gcli.Command{
Name: "list",
Desc: "list or search branches on local or remote",
Aliases: []string{"search", "ls"},
Examples: `
# List branches by glob pattern
{$fullCmd} -m "fea*"
{$fullCmd} -m "glob:fix_2[1|2]*"
# List branches by prefix pattern
{$fullCmd} -m "prefix:fea-"
# List branches by regex pattern
{$fullCmd} -m "regex:fea_\d+"
# match like fix_23_04
{$fullCmd} -m "regex:f[a-z]*_[0-9]+_\d+"
# Find and delete remote branches
{$fullCmd} -r origin -m fix_* --delete
# or
{$fullCmd} -r origin -m fix_* -x "git push {remote} --delete {branch}"
`,
Config: func(c *gcli.Command) {
blOpts.BindCommonFlags(c)
c.MustFromStruct(&blOpts, gflag.TagRuleNamed)
c.AddArg("match", "the branch name match pattern, same as --match|-m|-p").WithAfterFn(func(arg *gcli.CliArg) error {
blOpts.Match = arg.String()
return nil
})
},
Func: func(c *gcli.Command, args []string) error {
rp := app.Gitx().LoadRepo(blOpts.Workdir)
if blOpts.Fetch {
if err := rp.FetchAll("-np"); err != nil {
return err
}
}
colorp.Infoln("Load repo branches ...")
bis := rp.BranchInfos()
if blOpts.Match == "" {
tle := "Local"
var brs []*gitw.BranchInfo
if blOpts.All {
tle = "Local+Remotes"
brs = bis.All()
} else if blOpts.Remote != "" {
tle = blOpts.Remote
brs = bis.Remotes(blOpts.Remote)
} else {
brs = bis.Locales()
}
colorp.Cyanf("Branches on %q(total: %d)\n", tle, len(brs))
for i, info := range brs {
colorp.Infof(" %-24s %s\n", info.Short, info.HashMsg)
if blOpts.Limit > 0 && i+1 >= blOpts.Limit {
break
}
}
return nil
}
// ------ search branches
tle := "Local"
if blOpts.All {
tle = "Local+Remotes"
} else if blOpts.Remote != "" {
tle = blOpts.Remote
}
matcher := brinfo.NewMatcher(blOpts.Match)
opt := &gitw.SearchOpt{Flag: gitw.BrSearchLocal, Remote: blOpts.Remote, Limit: blOpts.Limit}
brs := bis.SearchV2(matcher, opt)
colorp.Cyanf("Branches on %q(found: %d, %s)\n", tle, len(brs), matcher.String())
if blOpts.Confirm && !cliutil.Confirm("Do you want to continue?") {
colorp.Cyanln("Canceled")
return nil
}
spl := textutil.NewVarReplacer("{,}")
rp.SetDryRun(blOpts.DryRun)
for _, info := range brs {
if blOpts.Delete {
// git branch -d feature-*
// git push origin --delete feature-*
colorp.Warnf("Do delete branch: %s\n", info.Name)
if err := rp.BranchDelete(info.Short, info.Remote); err != nil {
return err
}
} else if blOpts.Exec != "" {
vs := map[string]string{
"branch": info.Short,
"hash": info.Hash,
"remote": info.Remote,
}
execCmd := cmdr.NewCmdline(spl.RenderSimple(blOpts.Exec, vs)).
WithWorkDir(rp.Dir()).
WithDryRun(blOpts.DryRun).
PrintCmdline()
if err := execCmd.Run(); err != nil {
return err
}
} else {
colorp.Infof(" %-24s %s\n", info.Short, info.HashMsg)
}
}
if blOpts.Update {
if err := rp.FetchAll("-np"); err != nil {
return err
}
}
return nil
},
}
var bcOpts = struct {
cmdbiz.CommonOpts
notToSrc bool
}{}
// BranchCreateCmd instance
var BranchCreateCmd = &gcli.Command{
Name: "new",
Desc: "create and checkout new branch for development",
Help: `Workflow:
1. git checkout DEFAULT_BRANCH
2. git pull -np SOURCE_REMOTE DEFAULT_BRANCH
3. git checkout -b NEW_BRANCH
4. git push -u DEFAULT_REMOTE NEW_BRANCH
5. git push SOURCE_REMOTE NEW_BRANCH
Note:
- if branch name is "fix_" or contains "{ymd}", will auto add current date string.
eg: fix_ => fix_210101
`,
Aliases: []string{"n", "create"},
Config: func(c *gcli.Command) {
bcOpts.BindCommonFlags(c)
c.BoolOpt2(&bcOpts.notToSrc, "not-to-src, nts", "dont push new branch to the source remote")
c.AddArg("branch", "the new branch name, allow vars: {ymd}", true)
},
Func: func(c *gcli.Command, args []string) error {
cfg := apputil.GitCfgByCmdID(c)
rp := cfg.LoadRepo(upOpts.Workdir)
if err := rp.Check(); err != nil {
return err
}
defRemote := rp.DefaultRemote
srcRemote := rp.SourceRemote
defBranch := rp.DefaultBranch
newBranch := c.Arg("branch").String()
ymdString := timex.Now().DateFormat("ymd")
if newBranch == "fix_" { // keyword: fix_
newBranch = "fix_" + ymdString
} else if strings.Contains(newBranch, "{ymd}") { // fix_{ymd}
newBranch = strings.Replace(newBranch, "{ymd}", ymdString, -1)
}
rr := cmdutil.NewRunner(func(rr *cmdutil.Runner) {
rr.DryRun = acpOpts.DryRun
rr.Confirm = acpOpts.Confirm
rr.OutToStd = true
})
if rp.HasLocalBranch(newBranch) {
c.Infof("TIP: local ")
rr.GitCmd("checkout", newBranch).GitCmd("pull", "-np")
return rr.Run()
}
// fetch remotes and check branch exists
colorp.Infoln("Fetch remotes and check branch exists")
rr.GitCmd("fetch", "--all", "-np")
if err := rr.RunReset(); err != nil {
return err
}
rr.Reset()
if rp.HasOriginBranch(newBranch) {
colorp.Warnf("the branch %q already exists in remote %q\n", newBranch, defRemote)
return rp.QuickRun("checkout", newBranch)
}
if rp.HasSourceBranch(newBranch) {
colorp.Warnf("the branch %q already exists in remote %q\n", newBranch, srcRemote)
rr.GitCmd("checkout", newBranch).GitCmd("push", "-u", defRemote, newBranch)
return rr.Run()
}
// do checkout new branch and push to remote
colorp.Infoln("Do checkout new branch and push to remote")
curBranch := rp.CurBranchName()
if defBranch != curBranch {
rr.GitCmd("checkout", defBranch)
}
rr.GitCmd("pull", srcRemote, defBranch)
rr.GitCmd("checkout", "-b", newBranch)
rr.GitCmd("push", "-u", defRemote, newBranch)
if !bcOpts.notToSrc {
rr.GitCmd("push", srcRemote, newBranch)
}
return rr.Run()
},
}
var bsOpts = struct {
cmdbiz.CommonOpts
notToSrc bool
}{}
// BranchSetupCmd instance
var BranchSetupCmd = &gcli.Command{
Name: "setup",
Desc: "setup a new checkout branch on fork develop mode",
Help: `Workflow:
git fetch DEFAULT_REMOTE
if DEFAULT_REMOTE/BRANCH exist:
git branch --set-upstream-to=DEFAULT_REMOTE/BRANCH
else:
git push --set-upstream DEFAULT_REMOTE BRANCH
git push SOURCE_REMOTE BRANCH
`,
Aliases: []string{"init"},
Config: func(c *gcli.Command) {
bsOpts.BindCommonFlags(c)
c.BoolOpt2(&bsOpts.notToSrc, "not-to-src, nts", "dont push branch to the source remote")
},
Func: func(c *gcli.Command, args []string) (err error) {
cfg := apputil.GitCfgByCmdID(c)
rp := cfg.LoadRepo(bsOpts.Workdir)
if err := rp.Check(); err != nil {
return err
}
if err := rp.FetchOrigin(); err != nil {
return err
}
// git fetch origin
// exist: git branch --set-upstream-to=DEFAULT_REMOTE/BRANCH
// not exist: git push --set-upstream DEFAULT_REMOTE BRANCH
brName := rp.CurBranchName()
if rp.HasOriginBranch(brName) {
err = rp.Cmd("branch").Argf("--set-upstream-to=%s/%s", rp.DefaultRemote, brName).Run()
} else {
err = rp.Cmd("push", "-u", rp.DefaultRemote, brName).Run()
}
return basefn.CallOn(!bsOpts.notToSrc, func() error {
return rp.Cmd("push", rp.SourceRemote, brName).Run()
})
},
}