/
git.go
311 lines (258 loc) · 9.95 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package bootstrap
import (
"bufio"
"context"
"errors"
"fmt"
"net"
"net/url"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/buildkite/agent/v3/bootstrap/shell"
"github.com/buildkite/shellwords"
)
const (
gitErrorCheckout = iota
gitErrorCheckoutReferenceIsNotATree
gitErrorCheckoutRetryClean
gitErrorClone
gitErrorFetch
gitErrorFetchRetryClean
gitErrorClean
gitErrorCleanSubmodules
)
var errNoHostname = errors.New("no hostname found")
type gitError struct {
error
Type int
}
func (e *gitError) Unwrap() error {
return e.error
}
type shellRunner interface {
Run(context.Context, string, ...string) error
}
func gitCheckout(ctx context.Context, sh shellRunner, gitCheckoutFlags, reference string) error {
individualCheckoutFlags, err := shellwords.Split(gitCheckoutFlags)
if err != nil {
return err
}
if !gitCheckRefFormat(reference) {
return fmt.Errorf("%q is not a valid git ref format", reference)
}
commandArgs := []string{"checkout"}
commandArgs = append(commandArgs, individualCheckoutFlags...)
commandArgs = append(commandArgs, reference)
if err := sh.Run(ctx, "git", commandArgs...); err != nil {
if strings.HasPrefix(err.Error(), "fatal: reference is not a tree: ") {
return &gitError{error: err, Type: gitErrorCheckoutReferenceIsNotATree}
}
// 128 is extremely broad, but it seems permissions errors, network unreachable errors etc, don't result in it
if exitErr := new(exec.ExitError); errors.As(err, &exitErr) && exitErr.ExitCode() == 128 {
return &gitError{error: err, Type: gitErrorCheckoutRetryClean}
}
return &gitError{error: err, Type: gitErrorCheckout}
}
return nil
}
func gitClone(ctx context.Context, sh shellRunner, gitCloneFlags, repository, dir string) error {
individualCloneFlags, err := shellwords.Split(gitCloneFlags)
if err != nil {
return err
}
commandArgs := []string{"clone"}
commandArgs = append(commandArgs, individualCloneFlags...)
commandArgs = append(commandArgs, "--", repository, dir)
if err := sh.Run(ctx, "git", commandArgs...); err != nil {
return &gitError{error: err, Type: gitErrorClone}
}
return nil
}
func gitClean(ctx context.Context, sh shellRunner, gitCleanFlags string) error {
individualCleanFlags, err := shellwords.Split(gitCleanFlags)
if err != nil {
return err
}
commandArgs := []string{"clean"}
commandArgs = append(commandArgs, individualCleanFlags...)
if err := sh.Run(ctx, "git", commandArgs...); err != nil {
return &gitError{error: err, Type: gitErrorClean}
}
return nil
}
func gitCleanSubmodules(ctx context.Context, sh shellRunner, gitCleanFlags string) error {
individualCleanFlags, err := shellwords.Split(gitCleanFlags)
if err != nil {
return err
}
gitCleanCommand := strings.Join(append([]string{"git", "clean"}, individualCleanFlags...), " ")
commandArgs := append([]string{"submodule", "foreach", "--recursive"}, gitCleanCommand)
if err := sh.Run(ctx, "git", commandArgs...); err != nil {
return &gitError{error: err, Type: gitErrorCleanSubmodules}
}
return nil
}
func gitFetch(ctx context.Context, sh shellRunner, gitFetchFlags, repository string, refSpec ...string) error {
individualFetchFlags, err := shellwords.Split(gitFetchFlags)
if err != nil {
return err
}
commandArgs := []string{"fetch"}
commandArgs = append(commandArgs, individualFetchFlags...)
commandArgs = append(commandArgs, "--") // terminate arg parsing; only repository & refspecs may follow.
commandArgs = append(commandArgs, repository)
for _, r := range refSpec {
individualRefSpecs, err := shellwords.Split(r)
if err != nil {
return err
}
commandArgs = append(commandArgs, individualRefSpecs...)
}
if err := sh.Run(ctx, "git", commandArgs...); err != nil {
// 128 is extremely broad, but it seems permissions errors, network unreachable errors etc, don't result in it
if exitErr := new(exec.ExitError); errors.As(err, &exitErr) && exitErr.ExitCode() == 128 {
return &gitError{error: err, Type: gitErrorFetchRetryClean}
}
return &gitError{error: err, Type: gitErrorFetch}
}
return nil
}
func gitEnumerateSubmoduleURLs(ctx context.Context, sh *shell.Shell) ([]string, error) {
urls := []string{}
// The output of this command looks like:
// submodule.bitbucket-git-docker-example.url\ngit@bitbucket.org:lox24/docker-example.git\0
// submodule.bitbucket-https-docker-example.url\nhttps://lox24@bitbucket.org/lox24/docker-example.git\0
// submodule.github-git-docker-example.url\ngit@github.com:buildkite/docker-example.git\0
// submodule.github-https-docker-example.url\nhttps://github.com/buildkite/docker-example.git\0
output, err := sh.RunAndCapture(ctx, "git", "config", "--file", ".gitmodules", "--null", "--get-regexp", "submodule\\..+\\.url")
if err != nil {
return nil, err
}
// splits lines on null-bytes to gracefully handle line endings and repositories with newlines
lines := strings.Split(strings.TrimRight(output, "\x00"), "\x00")
// process each line
for _, line := range lines {
tokens := strings.SplitN(line, "\n", 2)
if len(tokens) != 2 {
return nil, fmt.Errorf("Failed to parse .gitmodules line %q", line)
}
urls = append(urls, tokens[1])
}
return urls, nil
}
func gitRevParseInWorkingDirectory(ctx context.Context, sh *shell.Shell, workingDirectory string, extraRevParseArgs ...string) (string, error) {
gitDirectory := filepath.Join(workingDirectory, ".git")
revParseArgs := []string{"--git-dir", gitDirectory, "--work-tree", workingDirectory, "rev-parse"}
revParseArgs = append(revParseArgs, extraRevParseArgs...)
return sh.RunAndCapture(ctx, "git", revParseArgs...)
}
var (
hasSchemePattern = regexp.MustCompile("^[^:]+://")
scpLikeURLPattern = regexp.MustCompile("^([^@]+@)?([^:]{2,}):/?(.+)$")
)
// parseGittableURL parses and converts a git repository url into a url.URL
func parseGittableURL(ref string) (*url.URL, error) {
if !hasSchemePattern.MatchString(ref) {
if scpLikeURLPattern.MatchString(ref) {
matched := scpLikeURLPattern.FindStringSubmatch(ref)
user := matched[1]
host := matched[2]
path := matched[3]
ref = fmt.Sprintf("ssh://%s%s/%s", user, host, path)
} else {
normalizedRef := strings.Replace(ref, "\\", "/", -1)
ref = fmt.Sprintf("file:///%s", strings.TrimPrefix(normalizedRef, "/"))
}
}
return url.Parse(ref)
}
// Clean up the SSH host and remove any key identifiers. See:
// git@github.com-custom-identifier:foo/bar.git
// https://buildkite.com/docs/agent/ssh-keys#creating-multiple-ssh-keys
var gitHostAliasRegexp = regexp.MustCompile(`-[a-z0-9\-]+$`)
func resolveGitHost(ctx context.Context, sh *shell.Shell, host string) string {
// ask SSH to print its configuration for this host, honouring .ssh/config
output, err := sh.RunAndCapture(ctx, "ssh", "-G", host)
if err != nil {
// fall back to the old behaviour of just replacing strings
return gitHostAliasRegexp.ReplaceAllString(host, "")
}
// if we got no error, let's process the output
h, err := hostFromSSHG(output)
if err != nil {
// so we fall back to the old behaviour of just replacing strings
return gitHostAliasRegexp.ReplaceAllString(host, "")
}
return h
}
func hostFromSSHG(sshconf string) (string, error) {
var hostname, port string
// split up the ssh -G output by lines
scanner := bufio.NewScanner(strings.NewReader(sshconf))
for scanner.Scan() {
line := scanner.Text()
// search the ssh -G output for "hostname" and "port" lines
tokens := strings.SplitN(line, " ", 2)
// skip any line which isn't a key-value pair
if len(tokens) != 2 {
continue
}
// grab the values we care about
switch tokens[0] {
case "hostname":
hostname = tokens[1]
case "port":
port = tokens[1]
}
// if we have both values, we're done here!
if hostname != "" && port != "" {
break
}
}
if hostname == "" {
// if we got here, either the `-G` flag was unsupported, or ssh -G
// didn't return a value for hostname (weird!)
return "", errNoHostname
}
// if we got out of that with a hostname, things worked
// if the port is the default, we can leave it off
if port == "" || port == "22" {
return hostname, nil
}
// otherwise, output it in hostname:port form
return net.JoinHostPort(hostname, port), nil
}
// gitCheckRefFormatDenyRegexp is a pattern used by gitCheckRefFormat().
// Numbered rules are from git 2.28.0's `git help check-ref-format`.
// Not covered by this implementation:
// 1. They can include slash / for hierarchical (directory) grouping, but
// no slash-separated component can begin with a dot . or end with the
// sequence .lock
// 2. They must contain at least one /. This enforces the presence of a
// category like heads/, tags/, and so on, but the actual names are not
// restricted. If the --allow-onelevel option is used, this rule is waived
// 5. They cannot have question-mark ?, asterisk *, or open bracket [
// anywhere. See the --refspec-pattern option below for an exception to
// this rule
// 6. They cannot begin or end with a slash / or contain multiple
// consecutive slashes (see the --normalize option below for an exception
// to this rule)
// 8. They cannot contain a sequence @{.
var gitCheckRefFormatDenyRegexp = regexp.MustCompile(strings.Join([]string{
`\.\.`, // 3. cannot have two consecutive dots .. anywhere
`[[:cntrl:]]`, // 4. cannot have ASCII control characters (In other words, bytes whose values are lower than \040, or \177 DEL) ...
`[ ~^:]`, // 4. cannot have ... space, tilde ~, caret ^, or colon : anywhere
`\.$`, // 7. cannot end with a dot .
`^@$`, // 9. cannot be the single character @.
`\\`, // 10. cannot contain a \
`^-`, // bonus: disallow refs that would be interpreted as command options/flags
}, "|"))
// gitCheckRefFormat is a simplified version of `git check-ref-format`.
// It mostly assumes --allow-onelevel. In other words, no need for refs/heads/… prefix.
// It is more permissive than the canonical implementation.
// https://git-scm.com/docs/git-check-ref-format
func gitCheckRefFormat(ref string) bool {
return !gitCheckRefFormatDenyRegexp.MatchString(ref)
}