-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathgit_util.go
163 lines (135 loc) · 3.77 KB
/
git_util.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
package main
import (
"fmt"
"log"
"net/url"
"os"
"os/exec"
"path"
"strings"
"github.com/pkg/errors"
)
// ConfigGitHTTPProxy ...
func ConfigGitHTTPProxy(workDir string, global bool, newHTTPProxy string) (oldHTTPProxy, oldHTTPProxyAuthMethod string) {
var scope string
if global {
scope = "--global"
workDir = ""
} else {
scope = "--local"
}
oldHTTPProxy = ExecGit(false, workDir, []string{"config", scope, "--get", "http.https://git.luolix.top.proxy"})
if strings.Index(oldHTTPProxy, "exit") >= 0 {
oldHTTPProxy = ""
}
oldHTTPProxyAuthMethod = ExecGit(false, workDir, []string{"config", scope, "--get", "http.https://git.luolix.top.proxyAuthMethod"})
if strings.Index(oldHTTPProxyAuthMethod, "exit") >= 0 {
oldHTTPProxyAuthMethod = ""
}
ExecGit(false, workDir, []string{"config", scope, "http.https://git.luolix.top.proxy", newHTTPProxy})
ExecGit(false, workDir, []string{"config", scope, "http.https://git.luolix.top.proxyAuthMethod", "basic"})
return
}
// SetGitHTTPProxy ...
func SetGitHTTPProxy(workDir string, global bool, oldHTTPProxy, oldHTTPProxyAuthMethod string) {
var scope string
if global {
scope = "--global"
} else {
scope = "--local"
}
if len(oldHTTPProxy) > 0 {
ExecGit(false, workDir, []string{"config", scope, "http.https://git.luolix.top.proxy", oldHTTPProxy})
} else {
ExecGit(false, workDir, []string{"config", scope, "--unset-all", "http.https://git.luolix.top.proxy"})
}
if len(oldHTTPProxyAuthMethod) > 0 {
ExecGit(false, workDir, []string{"config", scope, "http.https://git.luolix.top.proxyAuthMethod", oldHTTPProxyAuthMethod})
} else {
ExecGit(false, workDir, []string{"config", scope, "--unset-all", "http.https://git.luolix.top.proxyAuthMethod"})
}
}
// ResolveGitURLText ...
func ResolveGitURLText(gitURLText string, remoteName string, isGitClone bool) string {
if len(gitURLText) == 0 {
if !isGitClone {
gitURLText = GetGitRemoteURL("", remoteName)
}
}
if len(gitURLText) == 0 {
panic(fmt.Sprintf("获取GIT URL失败: %s", gitURLText))
}
return gitURLText
}
// ResolveGitRemoteName ...
func ResolveGitRemoteName(workDir string) string {
r := ExecGit(false, workDir, []string{"remote"})
r = strings.Trim(r, "\n\r\t ")
posOfReturn := strings.Index(r, "\n")
if posOfReturn > 0 {
r = r[0:posOfReturn]
}
posOfReturn = strings.Index(r, "\r")
if posOfReturn > 0 {
r = r[0:posOfReturn]
}
return r
}
// ResolveGitURL ...
func ResolveGitURL(gitURLText string) *url.URL {
var err error
var r *url.URL
if r, err = url.Parse(gitURLText); err != nil {
panic(errors.Wrapf(err, "解析URL失败: %s", gitURLText))
}
return r
}
// GetGitRemoteURL ...
func GetGitRemoteURL(workDir string, remoteName string) string {
return ExecGit(false, workDir, []string{"remote", "get-url", remoteName})
}
// SetGitRemoteURL ...
func SetGitRemoteURL(workDir string, remoteName string, remoteURL string) {
ExecGit(false, workDir, []string{"remote", "set-url", remoteName, remoteURL})
}
// ExecGit ...
func ExecGit(redirect bool, workDir string, args []string) string {
if len(workDir) > 0 {
if DirExists(workDir) == false {
workDir = path.Join(GetWorkDir(), workDir)
if DirExists(workDir) == false {
workDir = GetWorkDir()
}
}
} else {
workDir = GetWorkDir()
}
if Debug {
log.Printf("[fgit] %s: git %s\n", workDir, strings.Join(args, " "))
}
var command = exec.Command("git", args...)
if len(workDir) > 0 {
command.Dir = workDir
}
if redirect {
command.Env = []string{"GIT_SSL_NO_VERIFY=1"}
command.Stdout = os.Stdout
command.Stderr = os.Stderr
var err = command.Start()
if err != nil {
return err.Error()
}
err = command.Wait()
if err != nil {
return err.Error()
}
return ""
}
t, _ := command.Output()
r := string(t)
r = strings.Trim(r, "\n\r\t ")
if Debug {
log.Printf("[fgit] return: %s\n", r)
}
return r
}