-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
git.go
204 lines (170 loc) · 5.1 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
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
package main
import (
"errors"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/motemen/go-gitconfig"
)
// GitConfig represents git config file
type GitConfig struct {
UserName string `gitconfig:"user.name"`
UserEmail string `gitconfig:"user.email"`
PullRebase bool `gitconfig:"pull.rebase"`
GithubUser string `gitconfig:"github.user"`
RemoteOriginURL string `gitconfig:"remote.origin.url"`
}
// RepositoryNotFoundError is for when repository not found
type RepositoryNotFoundError struct {
TargetPath string
}
func (f RepositoryNotFoundError) Error() string {
return "Repository not found or moved: " + f.TargetPath
}
// NoRemoteSpecifiedError is for when no remote specified
type NoRemoteSpecifiedError struct {
TargetPath string
}
func (f NoRemoteSpecifiedError) Error() string {
return "No remote repository specified: " + f.TargetPath
}
// NoCommitsError is for when no commits found
type NoCommitsError struct {
TargetPath string
}
func (f NoCommitsError) Error() string {
return "Does not have any commits yet: " + f.TargetPath
}
// GitConfigGet returns git config value by key
func GitConfigGet(targetPath string, key string) (string, error) {
var configFile gitconfig.Config
switch targetPath {
case "global":
configFile = gitconfig.Global
case "local":
configFile = gitconfig.Local
default:
configPath := filepath.Join(targetPath, ".git/config")
configFile = gitconfig.File(configPath)
}
result, err := configFile.GetString(key)
if err != nil {
return "", err
}
return result, nil
}
// GitConfigSet will set git config value by key
func GitConfigSet(targetPath string, key string, value string) (string, error) {
out, err := exec.Command("git", "config", "--file", targetPath, "--set", key, value).Output()
return string(out), err
}
// GitStatus returns git status of certain repository
func GitStatus(targetPath string) ([]string, error) {
if err := os.Chdir(targetPath); err != nil {
return nil, err
}
out, _ := exec.Command("git", "status", "--porcelain").Output()
if len(out) == 0 {
return nil, errors.New("No status changed")
}
statuses := strings.Split(string(out), "\n")
return statuses[:len(statuses)-1], nil
}
// GitLog is `git log --branches --not --remotes`
func GitLog(targetPath string) ([]string, error) {
if err := os.Chdir(targetPath); err != nil {
return nil, err
}
out, err := exec.Command("git", "log", "--branches", "--not", "--remotes", "--oneline").CombinedOutput()
if err != nil {
eout := string(out)
if strings.HasPrefix(eout, "does not have any commits yet") {
return nil, &NoCommitsError{targetPath}
}
return nil, err
}
if len(out) == 0 {
return nil, errors.New("No output")
}
statuses := strings.Split(strings.TrimSpace(string(out)), "\n")
return statuses, nil
}
// GitRemoteAdd run `git remote add`
func GitRemoteAdd(targetPath string, name string, url string) error {
if err := os.Chdir(targetPath); err != nil {
return err
}
_, err := exec.Command("git", "remote", "add", name, url).Output()
if err != nil {
return err
}
return nil
}
// GitRemoteSetURL run `git remote set-url`
func GitRemoteSetURL(targetPath string, name string, url string) error {
if err := os.Chdir(targetPath); err != nil {
return err
}
_, err := exec.Command("git", "remote", "set-url", name, url).Output()
if err != nil {
return err
}
return nil
}
// GitPull pulls remote branch
func GitPull(targetPath string) (string, error) {
if err := os.Chdir(targetPath); err != nil {
return "", err
}
out, err := exec.Command("git", "pull").CombinedOutput()
if err != nil {
eout := string(out)
if strings.HasPrefix(eout, "conq: repository does not exist.") {
return "", &RepositoryNotFoundError{targetPath}
} else if strings.HasPrefix(eout, "ERROR: Repository not found.") {
return "", &RepositoryNotFoundError{targetPath}
} else if strings.HasPrefix(eout, "fatal: No remote repository specified.") {
return "", &NoRemoteSpecifiedError{targetPath}
} else {
return "", err
}
}
return string(out), nil
}
// GitFetch update tags and remove old branches
func GitFetch(targetPath string) (string, error) {
if err := os.Chdir(targetPath); err != nil {
return "", err
}
out, err := exec.Command("git", "fetch", "--tags", "--prune").CombinedOutput()
if err != nil {
eout := string(out)
if strings.HasPrefix(eout, "conq: repository does not exist.") {
return "", &RepositoryNotFoundError{targetPath}
} else if strings.HasPrefix(eout, "ERROR: Repository not found.") {
return "", &RepositoryNotFoundError{targetPath}
} else if strings.HasPrefix(eout, "fatal: No remote repository specified.") {
return "", &NoRemoteSpecifiedError{targetPath}
} else {
return "", err
}
}
return string(out), nil
}
// GitRemoteLocation returns Location header of remote
func GitRemoteLocation(targetURL string) (string, error) {
resp, err := http.Head(targetURL)
if err != nil {
return "", err
}
if resp.StatusCode == 301 {
// Moved permanently
return resp.Header["Location"][0], nil
} else if resp.StatusCode == 404 {
// Not found
return "", &RepositoryNotFoundError{targetURL}
}
return "", nil
}