-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitcmd.go
145 lines (128 loc) · 3.41 KB
/
gitcmd.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
package gitcmd
import (
"fmt"
"os"
"os/exec"
"syscall"
"github.com/gookit/color/colorp"
"github.com/gookit/gcli/v3"
"github.com/gookit/gcli/v3/events"
"github.com/gookit/gcli/v3/gflag"
"github.com/gookit/gitw"
"github.com/gookit/goutil"
"github.com/gookit/goutil/cliutil"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/sysutil/cmdr"
"github.com/inhere/kite-go/internal/app"
"github.com/inhere/kite-go/internal/biz/cmdbiz"
"github.com/inhere/kite-go/pkg/gitx"
)
// GitOpts object
var GitOpts = struct {
AutoChDir
cmdbiz.CommonOpts
}{}
// GitCommands commands for use git
var GitCommands = &gcli.Command{
Name: "git",
Desc: "tools for quick use `git` and more extra commands",
Aliases: []string{"g", "gitx"},
Subs: []*gcli.Command{
RepoInfoCmd,
// StatusInfoCmd,
RemoteInfoCmd,
NewCloneCmd(configProvider),
NewAddCommitPush(),
NewAddCommitCmd(),
NewUpdateCmd(),
NewUpdatePushCmd(),
NewOpenRemoteCmd(configProvider),
NewInitFlowCmd(),
CreatePRLink,
NewCheckoutCmd(),
ShowLogCmd,
ChangelogCmd,
TagCmd,
BatchCmd,
NewBranchCmd(),
NewGitEmojisCmd(),
},
Config: func(c *gcli.Command) {
GitOpts.BindCommonFlags(c)
GitOpts.BindChdirFlags(c)
c.On(events.OnCmdSubNotFound, SubCmdNotFound)
c.On(events.OnCmdRunBefore, func(ctx *gcli.HookCtx) bool {
c.Infoln("[kite.GIT] Workdir:", c.WorkDir())
return false
})
},
}
func configProvider() *gitx.Config {
return app.Gitx()
}
// SubCmdNotFound handle
func SubCmdNotFound(ctx *gcli.HookCtx) (stop bool) {
pName := ctx.Cmd.Name
name := ctx.Str("name")
args := ctx.Strings("args")
if name[0] == '@' {
rawNa := name
name = name[1:]
cliutil.Warnf("%s: %s - start with @, will be direct call `git %s` on system\n", pName, rawNa, name)
} else {
cliutil.Warnf("%s: subcommand %q is not found, will call `git %s` on system\n", pName, name, name)
}
colorp.Infoln("[kite.GIT] Workdir:", ctx.Cmd.WorkDir())
stop = true
err := cmdr.NewGitCmd(name, args...).PrintCmdline().ToOSStdout().Run()
if err != nil {
ee, ok := err.(*exec.ExitError)
if ok && ee.ProcessState != nil {
st := ee.Sys().(syscall.WaitStatus)
if st.Signaled() {
fmt.Println("Quited.")
return
}
}
cliutil.Errorln("Exec Error:", err)
}
return
}
// RedirectToGitx handle
func RedirectToGitx(ctx *gcli.HookCtx) (stop bool) {
if ctx.App == nil {
return
}
pName := ctx.Cmd.Name
sName := ctx.Str("name")
args := ctx.Cmd.RawArgs()
cliutil.Warnf("%s: subcommand '%s' not found, redirect to run `kite git %s`, args: %v\n", pName, sName, sName, args[1:])
cmdbiz.ProxyCC.AutoSetByName(pName, sName, args[1:])
// dump.P(ctx.App.CommandNames(), ctx.App.HasCommand("git"))
err := ctx.App.RunCmd("git", args)
if err != nil {
colorp.Errorln(err)
}
return true
}
// AutoChDir auto change dir
type AutoChDir struct {
// AutoGit auto find .git dir in parent.
AutoGit bool
}
// BindChdirFlags for auto change dir
func (a *AutoChDir) BindChdirFlags(c *gcli.Command) {
wd := c.WorkDir()
c.BoolOpt2(&a.AutoGit, "auto-root, auto-git", "auto find .git dir in parent and chdir to it", gflag.WithValidator(func(val string) error {
if strutil.QuietBool(val) {
repoDir, changed := fsutil.SearchNameUpx(wd, gitw.GitDir)
if changed {
goutil.MustOK(os.Chdir(repoDir))
c.ChWorkDir(repoDir)
cliutil.Yellowf("NOTICE: auto founded git root and will chdir to: %s\n", repoDir)
}
}
return nil
}))
}