/
gitCmd.go
142 lines (126 loc) · 4.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
/*
Copyright © 2022 John, Sing Dao, Siu <john.sd.siu@gmail.com>
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"
)
// 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 MyCmdRun("git", &[]string{"init"}, workpathP)
}
// Run "git push".
// 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 MyCmdRun("git", &args, workpathP)
}
// 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 MyCmdRun("git", &[]string{"remote", "add", name, git}, workpathP)
}
// 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.
// Return a MyCmd pointer for execution information.
func GitRemoteRemove(workpathP *string, name string) *MyCmd {
return MyCmdRun("git", &[]string{"remote", "remove", name}, workpathP)
}
// 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 repeatly
var submodulePath string = *workpathP
var currPath string = *workpathP
for submodulePath != "" {
submodulePath = GitRootSubmodule(&submodulePath)
if submodulePath != "" {
currPath = submodulePath
}
}
// Check git root
var opts []string = []string{"rev-parse", "--show-toplevel"}
var myCmd *MyCmd = MyCmdRun("git", &opts, &currPath)
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())
}