-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitCmd.go
180 lines (159 loc) · 5.66 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
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
/*
Copyright © 2022 John, Sing Dao, Siu <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package helper
import (
"os/exec"
"strings"
)
// Run "git <optionsP>".
// - If <workPathP> is empty/nil, current directory is used.
// - Return a MyCmd pointer for execution information.
func Git(workPathP *string, optionsP *[]string) *MyCmd {
return MyCmdRun("git", optionsP, workPathP)
}
// Run "git clone <optionsP>".
// - If <workPathP> is empty/nil, current directory is used.
// - Return a MyCmd pointer for execution information.
func GitClone(workPathP *string, optionsP *[]string) *MyCmd {
args := []string{"clone"}
if optionsP != nil {
args = append(args, *optionsP...)
}
return Git(workPathP, &args)
}
// Check git executable exist.
func GitExecExist() bool {
return GitExecPath() != ""
}
// Get git executable path.
// - Return empty string if not found.
func GitExecPath() string {
path, err := exec.LookPath("git")
if err != nil {
return ""
}
return path
}
// Run "git init".
// - If <workPathP> is empty/nil, current directory is used.
// - Return a MyCmd pointer for execution information.
func GitInit(workPathP *string) *MyCmd {
return Git(workPathP, &[]string{"init"})
}
// Run "git branch --show-current".
// - If <workPathP> is empty/nil, current directory is used.
// - Return a MyCmd pointer for execution information.
func GitBranchCurrent(workPathP *string) *MyCmd {
return Git(workPathP, &[]string{"branch", "--show-current"})
}
// Run "git pull <optionsP>".
// - If <workPathP> is empty/nil, current directory is used.
// - Return a MyCmd pointer for execution information.
func GitPull(workPathP *string, optionsP *[]string) *MyCmd {
args := []string{"pull"}
if optionsP != nil {
args = append(args, *optionsP...)
}
return Git(workPathP, &args)
}
// Run "git push <optionsP>".
// - If <workPathP> is empty/nil, current directory is used.
// - Return a MyCmd pointer for execution information.
func GitPush(workPathP *string, optionsP *[]string) *MyCmd {
args := []string{"push"}
if optionsP != nil {
args = append(args, *optionsP...)
}
return Git(workPathP, &args)
}
// Run "git remote".
// - If <workPathP> is empty/nil, current directory is used.
// - Return remotes in string array.
func GitRemote(workPathP *string, v bool) *[]string {
var args []string
if v {
args = []string{"remote", "-v"}
} else {
args = []string{"remote"}
}
output := MyCmdRun("git", &args, workPathP).Stdout.String()
return StrPtrToArrayPtr(&output)
}
// Run "git remote add <name> <git>".
// - If <workPathP> is empty/nil, current directory is used.
// - Return a MyCmd pointer for execution information.
func GitRemoteAdd(workPathP *string, name string, git string) *MyCmd {
return Git(workPathP, &[]string{"remote", "add", name, git})
}
// Check if a git remote(by name) exist in workPath.
// - If <workPathP> is empty/nil, current directory is used.
func GitRemoteExist(workPathP *string, name string) bool {
r := GitRemote(workPathP, false)
return StrArrayPtrContain(r, &name)
}
// Run "git remote remove".
// - If <workPathP> is empty/nil, current directory is used.
// - If remote exist Return a MyCmd pointer for execution information.
// - If remote does not exit, return nil.(Nothing to remove)
func GitRemoteRemove(workPathP *string, name string) *MyCmd {
return Git(workPathP, &[]string{"remote", "remove", name})
}
// Run "git remote remove" all git remotes
// - If <workPathP> is empty/nil, current directory is used.
func GitRemoteRemoveAll(workPathP *string) {
gr := GitRemote(workPathP, false)
for _, r := range *gr {
GitRemoteRemove(workPathP, r)
}
}
// Get git root from current directory.
// - If <workPathP> is empty/nil, current directory is used.
// - Return empty string if not a git dir.
func GitRoot(workPathP *string) string {
if *workPathP == "" {
CurrentPath()
}
// Check submodule path repeatedly
var submodulePath string = *workPathP
var currentPath string = *workPathP
for submodulePath != "" {
submodulePath = GitRootSubmodule(&submodulePath)
if submodulePath != "" {
currentPath = submodulePath
}
}
// Check git root
var opts []string = []string{"rev-parse", "--show-toplevel"}
var myCmd *MyCmd = MyCmdRun("git", &opts, ¤tPath)
if myCmd.Err != nil {
return ""
}
return strings.TrimSpace(myCmd.Stdout.String())
}
// Get git submodule root from `workPath`.
// - If <workPathP> is empty/nil, current directory is used.
// - Return empty string if not a submodule dir.
func GitRootSubmodule(workPathP *string) string {
var opts []string = []string{"rev-parse", "--show-superproject-working-tree"}
var myCmd *MyCmd = MyCmdRun("git", &opts, workPathP)
if myCmd.Err != nil {
return ""
}
return strings.TrimSpace(myCmd.Stdout.String())
}