-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcommand.go
More file actions
49 lines (43 loc) · 1.29 KB
/
command.go
File metadata and controls
49 lines (43 loc) · 1.29 KB
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
package git
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
)
const BranchTemplate = "当前项目: %s\n当前分支: %s\n项目路径: %s\n"
func GetCurrentBranchName(projectName string, projectPath string, abortOnFailure bool) string {
executeCommand := "git symbolic-ref --short HEAD"
branchCmd := exec.Command("bash", "-c", executeCommand)
branchCmd.Dir = projectPath
output, err := branchCmd.Output()
if err != nil {
log.Println("获取当前项目分支名失败:", err)
if abortOnFailure {
fmt.Printf(BranchTemplate, projectName, "获取分支名称失败", projectPath)
os.Exit(1)
}
return "获取分支名称失败"
}
return strings.Split(string(output), "\n")[0]
}
func PushBranchToOrigin(branchName, path string) {
pushBranch(branchName, "origin", path)
}
func PushBranchToUpstream(branchName, path string) {
pushBranch(branchName, "upstream", path)
}
func pushBranch(branchName, repositoryAlias, path string) {
pushOriginCmd := exec.Command("bash", "-c", "git push "+repositoryAlias+" "+branchName)
pushOriginCmd.Stdout = os.Stdout
pushOriginCmd.Stderr = os.Stderr
pushOriginCmd.Dir = path
err := pushOriginCmd.Run()
if err != nil {
fmt.Println("执行失败:", err)
os.Exit(1)
} else {
fmt.Println(branchName + ",已推到" + repositoryAlias + "仓库")
}
}