-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
git.go
305 lines (275 loc) · 9.97 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
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package git
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
homedir "github.com/mitchellh/go-homedir"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"go.uber.org/zap"
)
const (
sshKnownHostsUserPath = ".ssh/known_hosts"
sshMissingKnownHostsSSHCommand = "ssh -o StrictHostKeyChecking=accept-new"
)
var (
// sshURLRegexFormat matches the url of SSH git repository
sshURLRegexFormat = regexp.MustCompile(`(ssh://[\w\d\.]+|.+@?.+\..+:)(:[\d]+){0,1}/*(.*)`)
)
func run(logger *zap.SugaredLogger, dir string, args ...string) (string, error) {
c := exec.Command("git", args...)
var output bytes.Buffer
c.Stderr = &output
c.Stdout = &output
// This is the optional working directory. If not set, it defaults to the current
// working directory of the process.
if dir != "" {
c.Dir = dir
}
if err := c.Run(); err != nil {
logger.Errorf("Error running git %v: %v\n%v", args, err, output.String())
return "", err
}
return output.String(), nil
}
// FetchSpec describes how to initialize and fetch from a Git repository.
type FetchSpec struct {
URL string
Revision string
Refspec string
Path string
Depth uint
Submodules bool
SSLVerify bool
HTTPProxy string
HTTPSProxy string
NOProxy string
SparseCheckoutDirectories string
}
// Fetch fetches the specified git repository at the revision into path, using the refspec to fetch if provided.
func Fetch(logger *zap.SugaredLogger, spec FetchSpec) error {
homepath, err := homedir.Dir()
if err != nil {
logger.Errorf("Unexpected error getting the user home directory: %v", err)
return err
}
if os.Geteuid() == 0 {
homepath = "/root"
}
ensureHomeEnv(logger, homepath)
validateGitAuth(logger, pipeline.CredsDir, spec.URL)
if spec.Path != "" {
if _, err := run(logger, "", "init", spec.Path); err != nil {
return err
}
if err := os.Chdir(spec.Path); err != nil {
return fmt.Errorf("failed to change directory with path %s; err: %w", spec.Path, err)
}
} else if _, err := run(logger, "", "init"); err != nil {
return err
}
if err := configSparseCheckout(logger, spec); err != nil {
return err
}
trimmedURL := strings.TrimSpace(spec.URL)
if _, err := run(logger, "", "remote", "add", "origin", trimmedURL); err != nil {
return err
}
hasKnownHosts, err := userHasKnownHostsFile(homepath)
if err != nil {
return fmt.Errorf("error checking for known_hosts file: %w", err)
}
if !hasKnownHosts {
if _, err := run(logger, "", "config", "core.sshCommand", sshMissingKnownHostsSSHCommand); err != nil {
err = fmt.Errorf("error disabling strict host key checking: %w", err)
logger.Warnf(err.Error())
return err
}
}
if _, err := run(logger, "", "config", "http.sslVerify", strconv.FormatBool(spec.SSLVerify)); err != nil {
logger.Warnf("Failed to set http.sslVerify in git config: %s", err)
return err
}
fetchArgs := []string{"fetch"}
if spec.Submodules {
fetchArgs = append(fetchArgs, "--recurse-submodules=yes")
}
if spec.Depth > 0 {
fetchArgs = append(fetchArgs, fmt.Sprintf("--depth=%d", spec.Depth))
}
// Fetch the revision and verify with FETCH_HEAD
fetchParam := []string{spec.Revision}
checkoutParam := "FETCH_HEAD"
if spec.Refspec != "" {
// if refspec is specified, fetch the refspec and verify with provided revision
fetchParam = strings.Split(spec.Refspec, " ")
checkoutParam = spec.Revision
}
// git-init always creates and checks out an empty master branch. When the user requests
// "master" as the revision, git-fetch will refuse to update the HEAD of the branch it is
// currently on. The --update-head-ok parameter tells git-fetch that it is ok to update
// the current (empty) HEAD on initial fetch.
// The --force parameter tells git-fetch that its ok to update an existing HEAD in a
// non-fast-forward manner (though this cannot be possible on initial fetch, it can help
// when the refspec specifies the same destination twice)
fetchArgs = append(fetchArgs, "origin", "--update-head-ok", "--force")
fetchArgs = append(fetchArgs, fetchParam...)
if _, err := run(logger, spec.Path, fetchArgs...); err != nil {
return fmt.Errorf("failed to fetch %v: %v", fetchParam, err)
}
// After performing a fetch, verify that the item to checkout is actually valid
if _, err := ShowCommit(logger, checkoutParam, spec.Path); err != nil {
return fmt.Errorf("error parsing %s after fetching refspec %s", checkoutParam, spec.Refspec)
}
if _, err := run(logger, "", "checkout", "-f", checkoutParam); err != nil {
return err
}
commit, err := ShowCommit(logger, "HEAD", spec.Path)
if err != nil {
return err
}
ref, err := showRef(logger, "HEAD", spec.Path)
if err != nil {
return err
}
logger.Infof("Successfully cloned %s @ %s (%s) in path %s", trimmedURL, commit, ref, spec.Path)
if spec.Submodules {
if err := submoduleFetch(logger, spec); err != nil {
return err
}
}
return nil
}
// ShowCommit calls "git show ..." to get the commit SHA for the given revision
func ShowCommit(logger *zap.SugaredLogger, revision, path string) (string, error) {
output, err := run(logger, path, "show", "-q", "--pretty=format:%H", revision)
if err != nil {
return "", err
}
return strings.TrimSuffix(output, "\n"), nil
}
func showRef(logger *zap.SugaredLogger, revision, path string) (string, error) {
output, err := run(logger, path, "show", "-q", "--pretty=format:%D", revision)
if err != nil {
return "", err
}
return strings.TrimSuffix(output, "\n"), nil
}
func submoduleFetch(logger *zap.SugaredLogger, spec FetchSpec) error {
if spec.Path != "" {
if err := os.Chdir(spec.Path); err != nil {
return fmt.Errorf("failed to change directory with path %s; err: %w", spec.Path, err)
}
}
updateArgs := []string{"submodule", "update", "--recursive", "--init"}
if spec.Depth > 0 {
updateArgs = append(updateArgs, fmt.Sprintf("--depth=%d", spec.Depth))
}
if _, err := run(logger, "", updateArgs...); err != nil {
return err
}
logger.Infof("Successfully initialized and updated submodules in path %s", spec.Path)
return nil
}
// ensureHomeEnv works around an issue where ssh doesn't respect the HOME env variable. If HOME is set and
// different from the user's detected home directory then symlink .ssh from the home directory to the HOME env
// var. This way ssh will see the .ssh directory in the user's home directory even though it ignores
// the HOME env var.
func ensureHomeEnv(logger *zap.SugaredLogger, homepath string) {
homeenv := os.Getenv("HOME")
if _, err := os.Stat(filepath.Join(homeenv, ".ssh")); err != nil {
// There's no $HOME/.ssh directory to access or the user doesn't have permissions
// to read it, or something else; in any event there's no need to try creating a
// symlink to it.
return
}
if homeenv != "" {
ensureHomeEnvSSHLinkedFromPath(logger, homeenv, homepath)
}
}
func ensureHomeEnvSSHLinkedFromPath(logger *zap.SugaredLogger, homeenv string, homepath string) {
if filepath.Clean(homeenv) != filepath.Clean(homepath) {
homeEnvSSH := filepath.Join(homeenv, ".ssh")
homePathSSH := filepath.Join(homepath, ".ssh")
if _, err := os.Stat(homePathSSH); os.IsNotExist(err) {
if err := os.Symlink(homeEnvSSH, homePathSSH); err != nil {
// Only do a warning, in case we don't have a real home
// directory writable in our image
logger.Warnf("Unexpected error: creating symlink: %v", err)
}
}
}
}
func userHasKnownHostsFile(homepath string) (bool, error) {
f, err := os.Open(filepath.Join(homepath, sshKnownHostsUserPath))
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
defer f.Close()
return true, nil
}
func validateGitAuth(logger *zap.SugaredLogger, credsDir, url string) {
sshCred := true
if _, err := os.Stat(filepath.Join(credsDir, ".ssh")); os.IsNotExist(err) {
sshCred = false
}
urlSSHFormat := validateGitSSHURLFormat(url)
if sshCred && !urlSSHFormat {
logger.Warnf("SSH credentials have been provided but the URL(%q) is not a valid SSH URL. This warning can be safely ignored if the URL is for a public repo or you are using basic auth", url)
} else if !sshCred && urlSSHFormat {
logger.Warnf("URL(%q) appears to need SSH authentication but no SSH credentials have been provided", url)
}
}
// validateGitSSHURLFormat validates the given URL format is SSH or not
func validateGitSSHURLFormat(url string) bool {
return sshURLRegexFormat.MatchString(url)
}
func configSparseCheckout(logger *zap.SugaredLogger, spec FetchSpec) error {
if spec.SparseCheckoutDirectories != "" {
if _, err := run(logger, "", "config", "core.sparsecheckout", "true"); err != nil {
return err
}
dirPatterns := strings.Split(spec.SparseCheckoutDirectories, ",")
cwd, err := os.Getwd()
if err != nil {
logger.Errorf("failed to get current directory: %v", err)
return err
}
file, err := os.OpenFile(filepath.Join(cwd, ".git/info/sparse-checkout"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
logger.Errorf("failed to open sparse-checkout file: %v", err)
return err
}
for _, pattern := range dirPatterns {
if _, err := file.WriteString(pattern + "\n"); err != nil {
defer file.Close()
logger.Errorf("failed to write to sparse-checkout file: %v", err)
return err
}
}
if err := file.Close(); err != nil {
logger.Errorf("failed to close sparse-checkout file: %v", err)
return err
}
}
return nil
}