-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitw.go
620 lines (514 loc) · 13.4 KB
/
gitw.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Package gitw git command wrapper, git changelog, repo information and some git tools.
package gitw
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"syscall"
"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/sysutil/cmdr"
)
// some from: https://github.com/github/hub/blob/master/cmd/cmd.go
const (
// GitDir name
GitDir = ".git"
// HeadFile in .git/
HeadFile = "HEAD"
// ConfFile in .git/
ConfFile = "config"
// GitHubHost name
GitHubHost = "github.com"
// GitHubURL string
GitHubURL = "https://github.com"
// GitHubGit string
GitHubGit = "[email protected]"
)
// git host type
const (
TypeGitHub = "github"
TypeGitlab = "gitlab"
TypeDefault = "git"
)
const (
// DefaultBin name
DefaultBin = "git"
// DefaultBranchName value
DefaultBranchName = "master"
// DefaultRemoteName value
DefaultRemoteName = "origin"
)
// GitWrap is a project-wide struct that represents a command to be run in the console.
type GitWrap struct {
// Workdir for run git
Workdir string
// Bin git bin name. default is "git"
Bin string
// Args for run git. contains git command name.
Args []string
// Stdin more settings
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
// DryRun if True, not real execute command
DryRun bool
// BeforeExec command hook.
//
// Usage: gw.BeforeExec = gitw.PrintCmdline
BeforeExec func(gw *GitWrap)
}
// New create instance with args
func New(args ...string) *GitWrap {
return &GitWrap{
Bin: DefaultBin,
Args: args,
// Stdin: os.Stdin, // not init stdin
Stdout: os.Stdout,
Stderr: os.Stderr,
}
}
// Cmd create instance with git cmd and args
func Cmd(cmd string, args ...string) *GitWrap {
return New(cmd).WithArgs(args)
}
// NewWithArgs create instance with git cmd and args
func NewWithArgs(cmd string, args ...string) *GitWrap {
return New(cmd).WithArgs(args)
}
// NewWithWorkdir create instance with workdir and args
func NewWithWorkdir(workdir string, args ...string) *GitWrap {
return New(args...).WithWorkDir(workdir)
}
// New git wrap from current instance, can with args
func (gw *GitWrap) New(args ...string) *GitWrap {
nw := *gw
nw.Args = args
return &nw
}
// Sub new sub git cmd from current instance, can with args
func (gw *GitWrap) Sub(cmd string, args ...string) *GitWrap {
return gw.Cmd(cmd, args...)
}
// Cmd new git wrap from current instance, can with args
func (gw *GitWrap) Cmd(cmd string, args ...string) *GitWrap {
nw := *gw
nw.Args = []string{cmd}
if len(args) > 0 {
nw.WithArgs(args)
}
return &nw
}
// WithFn for setting gw
func (gw *GitWrap) WithFn(fn func(gw *GitWrap)) *GitWrap {
fn(gw)
return gw
}
// -------------------------------------------------
// config the git command
// -------------------------------------------------
// String to command line
func (gw *GitWrap) String() string {
return gw.Cmdline()
}
// Cmdline to command line
func (gw *GitWrap) Cmdline() string {
b := new(strings.Builder)
b.WriteString(gw.Bin)
for _, a := range gw.Args {
b.WriteByte(' ')
if strings.ContainsRune(a, '"') {
b.WriteString(fmt.Sprintf(`'%s'`, a))
} else if a == "" || strings.ContainsRune(a, '\'') || strings.ContainsRune(a, ' ') {
b.WriteString(fmt.Sprintf(`"%s"`, a))
} else {
b.WriteString(a)
}
}
return b.String()
}
// IsGitRepo return the work dir is a git repo.
func (gw *GitWrap) IsGitRepo() bool {
return fsutil.IsDir(gw.GitDir())
}
// GitDir return .git data dir
func (gw *GitWrap) GitDir() string {
gitDir := GitDir
if gw.Workdir != "" {
gitDir = gw.Workdir + "/" + GitDir
}
return gitDir
}
// -------------------------------------------------
// config the git command
// -------------------------------------------------
// PrintCmdline on exec command
func (gw *GitWrap) PrintCmdline() *GitWrap {
gw.BeforeExec = PrintCmdline
return gw
}
// WithDryRun on exec command
func (gw *GitWrap) WithDryRun(dryRun bool) *GitWrap {
gw.DryRun = dryRun
return gw
}
// OnBeforeExec add hook
func (gw *GitWrap) OnBeforeExec(fn func(gw *GitWrap)) *GitWrap {
gw.BeforeExec = fn
return gw
}
// WithWorkDir returns the current object
func (gw *GitWrap) WithWorkDir(dir string) *GitWrap {
gw.Workdir = dir
return gw
}
// WithStdin returns the current argument
func (gw *GitWrap) WithStdin(in io.Reader) *GitWrap {
gw.Stdin = in
return gw
}
// WithOutput returns the current argument
func (gw *GitWrap) WithOutput(out, errOut io.Writer) *GitWrap {
gw.Stdout = out
if errOut != nil {
gw.Stderr = errOut
}
return gw
}
// WithArg add args and returns the current object. alias of the WithArg()
func (gw *GitWrap) WithArg(args ...string) *GitWrap {
gw.Args = append(gw.Args, args...)
return gw
}
// AddArg add args and returns the current object
func (gw *GitWrap) AddArg(args ...string) *GitWrap {
return gw.WithArg(args...)
}
// Argf add arg and returns the current object.
func (gw *GitWrap) Argf(format string, args ...interface{}) *GitWrap {
gw.Args = append(gw.Args, fmt.Sprintf(format, args...))
return gw
}
// WithArgf add arg and returns the current object. alias of the Argf()
func (gw *GitWrap) WithArgf(format string, args ...interface{}) *GitWrap {
return gw.Argf(format, args...)
}
// ArgIf add arg and returns the current object
func (gw *GitWrap) ArgIf(arg string, exprOk bool) *GitWrap {
if exprOk {
gw.Args = append(gw.Args, arg)
}
return gw
}
// WithArgIf add arg and returns the current object
func (gw *GitWrap) WithArgIf(arg string, exprOk bool) *GitWrap {
return gw.ArgIf(arg, exprOk)
}
// AddArgs for the git. alias of WithArgs()
func (gw *GitWrap) AddArgs(args []string) *GitWrap {
return gw.WithArgs(args)
}
// WithArgs for the git
func (gw *GitWrap) WithArgs(args []string) *GitWrap {
if len(args) > 0 {
gw.Args = append(gw.Args, args...)
}
return gw
}
// WithArgsIf add arg and returns the current object
func (gw *GitWrap) WithArgsIf(args []string, exprOk bool) *GitWrap {
if exprOk && len(args) > 0 {
gw.Args = append(gw.Args, args...)
}
return gw
}
// ResetArgs for git
func (gw *GitWrap) ResetArgs() {
gw.Args = make([]string, 0)
}
// -------------------------------------------------
// run git command
// -------------------------------------------------
// NewExecCmd create exec.Cmd from current cmd
func (gw *GitWrap) NewExecCmd() *exec.Cmd {
c := exec.Command(gw.Bin, gw.Args...)
c.Dir = gw.Workdir
c.Stdin = gw.Stdin
c.Stdout = gw.Stdout
c.Stderr = gw.Stderr
return c
}
// Success run and return whether success
func (gw *GitWrap) Success() bool {
if gw.BeforeExec != nil {
gw.BeforeExec(gw)
}
if gw.DryRun {
return true
}
return gw.NewExecCmd().Run() == nil
}
// SafeLines run and return output as lines
func (gw *GitWrap) SafeLines() []string {
ss, _ := gw.OutputLines()
return ss
}
// OutputLines run and return output as lines
func (gw *GitWrap) OutputLines() ([]string, error) {
out, err := gw.Output()
if err != nil {
return nil, err
}
return cmdr.OutputLines(out), err
}
// SafeOutput run and return output
func (gw *GitWrap) SafeOutput() string {
gw.Stderr = nil // ignore stderr
out, err := gw.Output()
if err != nil {
return ""
}
return out
}
// Output run and return output
func (gw *GitWrap) Output() (string, error) {
c := exec.Command(gw.Bin, gw.Args...)
c.Dir = gw.Workdir
c.Stderr = gw.Stderr
if gw.BeforeExec != nil {
gw.BeforeExec(gw)
}
if gw.DryRun {
return "DIY-RUN: OK", nil
}
output, err := c.Output()
return string(output), err
}
// CombinedOutput run and return output, will combine stderr and stdout output
func (gw *GitWrap) CombinedOutput() (string, error) {
c := exec.Command(gw.Bin, gw.Args...)
c.Dir = gw.Workdir
if gw.BeforeExec != nil {
gw.BeforeExec(gw)
}
if gw.DryRun {
return "DIY-RUN: OK", nil
}
output, err := c.CombinedOutput()
return string(output), err
}
// MustRun a command. will panic on error
func (gw *GitWrap) MustRun() {
if err := gw.Run(); err != nil {
panic(err)
}
}
// Run runs command with `Exec` on platforms except Windows
// which only supports `Spawn`
func (gw *GitWrap) Run() error {
if gw.BeforeExec != nil {
gw.BeforeExec(gw)
}
if gw.DryRun {
fmt.Println("DIY-RUN: OK")
return nil
}
return gw.NewExecCmd().Run()
// if envutil.IsWindows() {
// return gw.Spawn()
// }
// return gw.Exec()
}
// Spawn runs command with spawn(3)
func (gw *GitWrap) Spawn() error {
if gw.DryRun {
return nil
}
return gw.NewExecCmd().Run()
}
// Exec runs command with exec(3)
// Note that Windows doesn't support exec(3): http://golang.org/src/pkg/syscall/exec_windows.go#L339
func (gw *GitWrap) Exec() error {
binary, err := exec.LookPath(gw.Bin)
if err != nil {
return &exec.Error{
Name: gw.Bin,
Err: errorx.Newf("%s not found in the system", gw.Bin),
}
}
args := []string{binary}
args = append(args, gw.Args...)
if gw.BeforeExec != nil {
gw.BeforeExec(gw)
}
if gw.DryRun {
fmt.Println("DIY-RUN: OK")
return nil
}
return syscall.Exec(binary, args, os.Environ())
}
// -------------------------------------------------
// commands of git
// -------------------------------------------------
// Add command for git
func (gw *GitWrap) Add(args ...string) *GitWrap {
return gw.Cmd("add", args...)
}
// Annotate command for git
func (gw *GitWrap) Annotate(args ...string) *GitWrap {
return gw.Cmd("annotate", args...)
}
// Apply command for git
func (gw *GitWrap) Apply(args ...string) *GitWrap {
return gw.Cmd("apply", args...)
}
// Bisect command for git
func (gw *GitWrap) Bisect(args ...string) *GitWrap {
return gw.Cmd("bisect", args...)
}
// Blame command for git
func (gw *GitWrap) Blame(args ...string) *GitWrap {
return gw.Cmd("blame", args...)
}
// Branch command for git
func (gw *GitWrap) Branch(args ...string) *GitWrap {
return gw.Cmd("branch", args...)
}
// Checkout command for git
func (gw *GitWrap) Checkout(args ...string) *GitWrap {
return gw.Cmd("checkout", args...)
}
// CherryPick command for git
func (gw *GitWrap) CherryPick(args ...string) *GitWrap {
return gw.Cmd("cherry-pick", args...)
}
// Clean command for git
func (gw *GitWrap) Clean(args ...string) *GitWrap {
return gw.Cmd("clean", args...)
}
// Clone command for git
func (gw *GitWrap) Clone(args ...string) *GitWrap {
return gw.Cmd("clone", args...)
}
// Commit command for git
func (gw *GitWrap) Commit(args ...string) *GitWrap {
return gw.Cmd("commit", args...)
}
// Config command for git
func (gw *GitWrap) Config(args ...string) *GitWrap {
return gw.Cmd("config", args...)
}
// Describe command for git
func (gw *GitWrap) Describe(args ...string) *GitWrap {
return gw.Cmd("describe", args...)
}
// Diff command for git
func (gw *GitWrap) Diff(args ...string) *GitWrap {
return gw.Cmd("diff", args...)
}
// Fetch command for git
func (gw *GitWrap) Fetch(args ...string) *GitWrap {
return gw.Cmd("fetch", args...)
}
// Grep command for git
func (gw *GitWrap) Grep(args ...string) *GitWrap {
return gw.Cmd("grep", args...)
}
// Init command for git
func (gw *GitWrap) Init(args ...string) *GitWrap {
return gw.Cmd("init", args...)
}
// Log command for git
func (gw *GitWrap) Log(args ...string) *GitWrap {
return gw.Cmd("log", args...)
}
// Merge command for git
func (gw *GitWrap) Merge(args ...string) *GitWrap {
return gw.Cmd("merge", args...)
}
// Mv command for git
func (gw *GitWrap) Mv(args ...string) *GitWrap {
return gw.Cmd("mv", args...)
}
// Pull command for git
func (gw *GitWrap) Pull(args ...string) *GitWrap {
return gw.Cmd("pull", args...)
}
// Push command for git
func (gw *GitWrap) Push(args ...string) *GitWrap {
return gw.Cmd("push", args...)
}
// Rebase command for git
func (gw *GitWrap) Rebase(args ...string) *GitWrap {
return gw.Cmd("rebase", args...)
}
// Reflog command for git
func (gw *GitWrap) Reflog(args ...string) *GitWrap {
return gw.Cmd("reflog", args...)
}
// Remote command for git
func (gw *GitWrap) Remote(args ...string) *GitWrap {
return gw.Cmd("remote", args...)
}
// Reset command for git
func (gw *GitWrap) Reset(args ...string) *GitWrap {
return gw.Cmd("reset", args...)
}
// Restore command for git
func (gw *GitWrap) Restore(args ...string) *GitWrap {
return gw.Cmd("restore", args...)
}
// Revert command for git
func (gw *GitWrap) Revert(args ...string) *GitWrap {
return gw.Cmd("revert", args...)
}
// RevList command for git
func (gw *GitWrap) RevList(args ...string) *GitWrap {
return gw.Cmd("rev-list", args...)
}
// RevParse command for git
//
// rev-parse usage:
//
// git rev-parse --show-toplevel // get git workdir, repo dir.
// git rev-parse -q --git-dir // get git data dir name. eg: .git
func (gw *GitWrap) RevParse(args ...string) *GitWrap {
return gw.Cmd("rev-parse", args...)
}
// Rm command for git
func (gw *GitWrap) Rm(args ...string) *GitWrap {
return gw.Cmd("rm", args...)
}
// ShortLog command for git
func (gw *GitWrap) ShortLog(args ...string) *GitWrap {
return gw.Cmd("shortlog", args...)
}
// Show command for git
func (gw *GitWrap) Show(args ...string) *GitWrap {
return gw.Cmd("show", args...)
}
// Stash command for git
func (gw *GitWrap) Stash(args ...string) *GitWrap {
return gw.Cmd("stash", args...)
}
// Status command for git
func (gw *GitWrap) Status(args ...string) *GitWrap {
return gw.Cmd("status", args...)
}
// Switch command for git
func (gw *GitWrap) Switch(args ...string) *GitWrap {
return gw.Cmd("switch", args...)
}
// Tag command for git
func (gw *GitWrap) Tag(args ...string) *GitWrap {
return gw.Cmd("tag", args...)
}
// Var command for git
func (gw *GitWrap) Var(args ...string) *GitWrap {
return gw.Cmd("var", args...)
}
// Worktree command for git
func (gw *GitWrap) Worktree(args ...string) *GitWrap {
return gw.Cmd("worktree", args...)
}