This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
git.go
176 lines (153 loc) · 3.45 KB
/
git.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strings"
)
type Git struct{}
var git Git
func (self *Git) push(dir, mirror, remote string) {
oldDir := self.dirSwap(nil, fmt.Sprintf("%s/src/%s", getGoPath(), dir))
cmd := exec.Command(
"git",
"push",
"--all",
fmt.Sprintf("git@%s:%s", mirror, remote),
)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(out))
stderrAndExit(err)
}
self.dirSwap(nil, oldDir)
}
func (self *Git) clone(pkg *Package) error {
cmd := exec.Command(
"git",
"clone",
"git@"+pkg.GitUrl+".git",
getGoPath()+"/src/"+pkg.Name,
)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("git clone:\n%s\n%s\n", err.Error(), out)
}
return nil
}
func (self *Git) checkout(pkg *Package, branch string) error {
if branch == "" {
branch = self.checkBranch(pkg.Branch)
}
oldDir := self.dirSwap(pkg, "")
cmd := exec.Command(
"git",
"checkout",
branch,
)
out, err := cmd.CombinedOutput()
self.dirSwap(pkg, oldDir)
if err != nil {
return fmt.Errorf("Could not switch branch:\n\n%s\n"+err.Error(), out)
}
return nil
}
func (self *Git) pull(pkg *Package) error {
err := self.checkout(pkg, "master")
if err != nil {
return fmt.Errorf("git pull: %s", err.Error())
}
oldDir := self.dirSwap(pkg, "")
cmd := exec.Command(
"git",
"pull",
)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf(
"Could not update source:%s\n%s\n%s\n",
pkg.Name,
err.Error(),
out,
)
}
self.dirSwap(pkg, oldDir)
return nil
}
func (self *Git) log(pkg *Package, format string) ([]string, error) {
if format == "" {
format = "%H"
}
self.checkout(pkg, "master")
oldDir := self.dirSwap(pkg, "")
cmd := exec.Command(
"/usr/bin/git",
"log",
"--decorate",
"--pretty=format:"+format,
)
out, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("while trying to set out pipe: %s\n", err.Error())
}
if err = cmd.Start(); err != nil {
return nil, fmt.Errorf("while trying to get git log: %s \n", err.Error())
}
data, err := ioutil.ReadAll(out)
if err != nil {
return nil, fmt.Errorf("while trying to read from out pipe: %s\n", err.Error())
}
self.dirSwap(pkg, oldDir)
self.checkout(pkg, "")
return strings.Split(string(data), "\n"), nil
}
func (self *Git) refNameToCommit(pkg *Package) (string, error) {
gitlog, err := self.log(pkg, "%H %d")
if err != nil {
return "", fmt.Errorf("refNameToCommit: %s", err.Error())
}
regex := regexp.MustCompile(self.checkBranch(pkg.Branch))
for _, line := range gitlog {
if regex.MatchString(line) {
/*
t1 := strings.Split(line, " ")
t2 := strings.Replace(t1[1], "(", "", -1)
t2 = strings.Replace(t2, ")", "", -1)
t3 = strings.Split(t2, ",")
t4 := len(t3)
name := t3[t4-1]
name = string.TrimSpace(name)
*/
if pkg.Branch[0] == '<' || pkg.Branch[0] == '>' {
return pkg.Branch[0:1] + strings.Split(line, " ")[0], nil
}
return strings.Split(line, " ")[0], nil
}
}
return "", nil
}
func (self *Git) dirSwap(pkg *Package, dir string) string {
oldDir, err := os.Getwd()
if err != nil {
fmt.Println("Could not switch dir: %s\n", err.Error())
}
if dir == "" {
dir = getGoPath() + "/src/" + pkg.Name
}
err = os.Chdir(dir)
if err != nil {
stderrAndExit(err)
}
return oldDir
}
func (self *Git) checkBranch(branch string) string {
if branch == "" {
return branch
}
if branch[0] == '>' || branch[0] == '<' {
return branch[1:]
}
return branch
}