-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
209 lines (185 loc) · 4.79 KB
/
main.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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"github.com/kballard/go-shellquote"
"github.com/spf13/cobra"
)
const DEFAULT_BRANCH_PREFIX = "tree"
const GITEA_BRANCH_PREFIX = "src/branch"
const GITEA_DOMAIN = "gitea.com"
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
var rootCmd = &cobra.Command{
Use: "git-open",
Run: openCurrentRepo,
}
func openCurrentRepo(cmd *cobra.Command, args []string) {
var remote string
if len(args) > 0 {
remote = args[0]
}
_, err := CurrentGitRepo()
if err != nil {
Err("not a git repository")
}
branch := CurrentBranch()
if remote == "" {
remote = CurrentRemote(branch)
}
gitURL := RemoteURL(remote)
url := TransferToURL(gitURL)
url = TransferWithBranch(url, branch)
_ = OpenBrowser(url)
}
// TransferWithBranch 添加分支名
func TransferWithBranch(url string, branch string) string {
if strings.Contains(url, GITEA_DOMAIN) {
url = fmt.Sprintf("%s/%s/%s", url, GITEA_BRANCH_PREFIX, branch)
} else {
url = fmt.Sprintf("%s/%s/%s", url, DEFAULT_BRANCH_PREFIX, branch)
}
return url
}
func GitCommand(args ...string) (string, error) {
cmd := exec.Command("git", args...)
cmd.Stderr = os.Stderr
output, err := cmd.Output()
return string(output), err
}
// CurrentGitRepo 获取当前git项目路径
func CurrentGitRepo() (string, error) {
output, err := GitCommand("rev-parse", "-q", "--show-toplevel")
return output, err
}
// CurrentBranch 获取当前分支,如果不存在则使用master
func CurrentBranch() string {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
output, err := cmd.Output()
if err != nil {
fmt.Println("generate branch failed")
}
outputStr := strings.TrimSuffix(string(output), "\n")
branchName := strings.TrimSpace(outputStr)
return branchName
}
// CurrentRemote 通过分支获取当前remote,默认为origin
func CurrentRemote(branch string) string {
remote, err := GitCommand("config", fmt.Sprintf("branch.%s.remote", branch))
remote = firstLine(remote)
if remote == "" || err != nil {
remote = "origin"
}
return remote
}
// RemoteURL 通过remote获取remote-url,如果使用错误的remote,则报错退出
func RemoteURL(remote string) string {
gitURL, err := GitCommand("ls-remote", "--get-url", remote)
if err != nil {
Err("git remote is not set for", remote)
}
gitURL = firstLine(gitURL)
if gitURL == remote {
Err(remote, "is a wrong remote")
}
return gitURL
}
// 移除末尾换行符
func firstLine(output string) string {
if i := strings.Index(output, "\n"); i >= 0 {
return output[0:i]
}
return output
}
// TransferToURL remote url转换为web url
func TransferToURL(gitURL string) string {
var url string
if strings.HasPrefix(gitURL, "https://") || strings.HasPrefix(gitURL, "http://") {
url = gitURL[:len(gitURL)-4]
}
//处理一下 https://oauth2:4be21afa355573d342c17a4517537bde7c4c8c8d78dc018df89bd6a73700f5ee@git.dfy.definesys.cn/apaas/xdap-app.git 格式的URL
if strings.HasPrefix(gitURL, "git@") {
url = gitURL[:len(gitURL)-4]
url = strings.Replace(url, ":", "/", 1)
url = strings.Replace(url, "git@", "https://", 1)
}
re := regexp.MustCompile(`https://.*@`)
match := re.FindString(url)
if match != "" {
// 将匹配到的部分替换为https://
url = strings.Replace(url, match, "https://", 1)
}
return url
}
// Err 输出报错,结束程序
func Err(msg ...interface{}) {
fmt.Println(msg...)
os.Exit(1)
}
// OpenBrowser 唤起浏览器,打开url
func OpenBrowser(url string) error {
launcher, err := browserLauncher()
if err != nil {
return err
}
args := append(launcher, url)
// fmt.Printf("使用浏览器:%s, 打开链接:%s", launcher, url)
cmd := exec.Command(args[0], args[1:]...)
return cmd.Run()
}
// 切分命令为数组,方便exec.Command执行
func browserLauncher() ([]string, error) {
browser := os.Getenv("BROWSER")
if browser == "" {
browser = searchBrowserLauncher()
} else {
browser = os.ExpandEnv(browser)
}
if browser == "" {
return nil, errors.New("please set $BROWSER to a web launcher")
}
return shellquote.Split(browser)
}
// 根据操作系统,返回默认browser command
func searchBrowserLauncher() (browser string) {
// 单独处理一下wsl的情况
if isWSL() {
browser = "powershell.exe Start-Process"
return browser
}
switch runtime.GOOS {
case "darwin":
browser = "open"
case "windows":
browser = "powershell.exe Start-Process"
case "linux":
browser = "xdg-open"
default:
browser = ""
}
return browser
}
func isWSL() bool {
file, err := os.Open("/proc/version")
if err != nil {
return false
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "Microsoft") || strings.Contains(line, "WSL") {
return true
}
}
return false
}