-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
git.go
476 lines (423 loc) · 15.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
Copyright 2017 The Kubernetes 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 provides a client to plugins that can do git operations.
package git
import (
"bufio"
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
prowgithub "k8s.io/test-infra/prow/github"
)
const github = "github.com"
// Client can clone repos. It keeps a local cache, so successive clones of the
// same repo should be quick. Create with NewClient. Be sure to clean it up.
type Client struct {
// logger will be used to log git operations and must be set.
logger *logrus.Entry
credLock sync.RWMutex
// user is used when pushing or pulling code if specified.
user string
// needed to generate the token.
tokenGenerator func() []byte
// dir is the location of the git cache.
dir string
// git is the path to the git binary.
git string
// base is the base path for git clone calls. For users it will be set to
// GitHub, but for tests set it to a directory with git repos.
base string
// host is the git host.
// TODO: use either base or host. the redundancy here is to help landing
// #14609 easier.
host string
// The mutex protects repoLocks which protect individual repos. This is
// necessary because Clone calls for the same repo are racy. Rather than
// one lock for all repos, use a lock per repo.
// Lock with Client.lockRepo, unlock with Client.unlockRepo.
rlm sync.Mutex
repoLocks map[string]*sync.Mutex
}
// Clean removes the local repo cache. The Client is unusable after calling.
func (c *Client) Clean() error {
return os.RemoveAll(c.dir)
}
// NewClient returns a client that talks to GitHub. It will fail if git is not
// in the PATH.
func NewClient() (*Client, error) {
return NewClientWithHost(github)
}
// NewClientWithHost creates a client with specified host.
func NewClientWithHost(host string) (*Client, error) {
g, err := exec.LookPath("git")
if err != nil {
return nil, err
}
t, err := ioutil.TempDir("", "git")
if err != nil {
return nil, err
}
return &Client{
logger: logrus.WithField("client", "git"),
dir: t,
git: g,
base: fmt.Sprintf("https://%s", host),
host: host,
repoLocks: make(map[string]*sync.Mutex),
}, nil
}
// SetRemote sets the remote for the client. This is not thread-safe, and is
// useful for testing. The client will clone from remote/org/repo, and Repo
// objects spun out of the client will also hit that path.
// TODO: c.host field needs to be updated accordingly.
func (c *Client) SetRemote(remote string) {
c.base = remote
}
// SetCredentials sets credentials in the client to be used for pushing to
// or pulling from remote repositories.
func (c *Client) SetCredentials(user string, tokenGenerator func() []byte) {
c.credLock.Lock()
defer c.credLock.Unlock()
c.user = user
c.tokenGenerator = tokenGenerator
}
func (c *Client) getCredentials() (string, string) {
c.credLock.RLock()
defer c.credLock.RUnlock()
return c.user, string(c.tokenGenerator())
}
func (c *Client) lockRepo(repo string) {
c.rlm.Lock()
if _, ok := c.repoLocks[repo]; !ok {
c.repoLocks[repo] = &sync.Mutex{}
}
m := c.repoLocks[repo]
c.rlm.Unlock()
m.Lock()
}
func (c *Client) unlockRepo(repo string) {
c.rlm.Lock()
defer c.rlm.Unlock()
c.repoLocks[repo].Unlock()
}
// Clone clones a repository. Pass the full repository name, such as
// "kubernetes/test-infra" as the repo.
// This function may take a long time if it is the first time cloning the repo.
// In that case, it must do a full git mirror clone. For large repos, this can
// take a while. Once that is done, it will do a git fetch instead of a clone,
// which will usually take at most a few seconds.
func (c *Client) Clone(organization, repository string) (*Repo, error) {
repo := organization + "/" + repository
c.lockRepo(repo)
defer c.unlockRepo(repo)
base := c.base
user, pass := c.getCredentials()
if user != "" && pass != "" {
base = fmt.Sprintf("https://%s:%s@%s", user, pass, c.host)
}
cache := filepath.Join(c.dir, repo) + ".git"
if _, err := os.Stat(cache); os.IsNotExist(err) {
// Cache miss, clone it now.
c.logger.Infof("Cloning %s for the first time.", repo)
if err := os.MkdirAll(filepath.Dir(cache), os.ModePerm); err != nil && !os.IsExist(err) {
return nil, err
}
remote := fmt.Sprintf("%s/%s", base, repo)
if b, err := retryCmd(c.logger, "", c.git, "clone", "--mirror", remote, cache); err != nil {
return nil, fmt.Errorf("git cache clone error: %v. output: %s", err, string(b))
}
} else if err != nil {
return nil, err
} else {
// Cache hit. Do a git fetch to keep updated.
c.logger.Infof("Fetching %s.", repo)
if b, err := retryCmd(c.logger, cache, c.git, "fetch"); err != nil {
return nil, fmt.Errorf("git fetch error: %v. output: %s", err, string(b))
}
}
t, err := ioutil.TempDir("", "git")
if err != nil {
return nil, err
}
if b, err := exec.Command(c.git, "clone", cache, t).CombinedOutput(); err != nil {
return nil, fmt.Errorf("git repo clone error: %v. output: %s", err, string(b))
}
return &Repo{
dir: t,
logger: c.logger,
git: c.git,
base: base,
org: organization,
repo: repository,
user: user,
pass: pass,
}, nil
}
// Repo is a clone of a git repository. Create with Client.Clone, and don't
// forget to clean it up after.
type Repo struct {
// dir is the location of the git repo.
dir string
// git is the path to the git binary.
git string
// base is the base path for remote git fetch calls.
base string
// org is the organization name: "org" in "org/repo".
org string
// repo is the repository name: "repo" in "org/repo".
repo string
// user is used for pushing to the remote repo.
user string
// pass is used for pushing to the remote repo.
pass string
logger *logrus.Entry
}
// Directory exposes the location of the git repo
func (r *Repo) Directory() string {
return r.dir
}
// SetLogger sets logger: Do not use except in unit tests
func (r *Repo) SetLogger(logger *logrus.Entry) {
r.logger = logger
}
// SetGit sets git: Do not use except in unit tests
func (r *Repo) SetGit(git string) {
r.git = git
}
// Clean deletes the repo. It is unusable after calling.
func (r *Repo) Clean() error {
return os.RemoveAll(r.dir)
}
func (r *Repo) gitCommand(arg ...string) *exec.Cmd {
cmd := exec.Command(r.git, arg...)
cmd.Dir = r.dir
r.logger.WithField("args", cmd.Args).WithField("dir", cmd.Dir).Debug("Constructed git command")
return cmd
}
// Checkout runs git checkout.
func (r *Repo) Checkout(commitlike string) error {
r.logger.Infof("Checkout %s.", commitlike)
co := r.gitCommand("checkout", commitlike)
if b, err := co.CombinedOutput(); err != nil {
return fmt.Errorf("error checking out %s: %v. output: %s", commitlike, err, string(b))
}
return nil
}
// RevParse runs git rev-parse.
func (r *Repo) RevParse(commitlike string) (string, error) {
r.logger.Infof("RevParse %s.", commitlike)
b, err := r.gitCommand("rev-parse", commitlike).CombinedOutput()
if err != nil {
return "", fmt.Errorf("error rev-parsing %s: %v. output: %s", commitlike, err, string(b))
}
return string(b), nil
}
// BranchExists returns true if branch exists in heads.
func (r *Repo) BranchExists(branch string) bool {
heads := "origin"
r.logger.Infof("Checking if branch %s exists in %s.", branch, heads)
co := r.gitCommand("ls-remote", "--exit-code", "--heads", heads, branch)
if co.Run() == nil {
return true
}
return false
}
// CheckoutNewBranch creates a new branch and checks it out.
func (r *Repo) CheckoutNewBranch(branch string) error {
r.logger.Infof("Create and checkout %s.", branch)
co := r.gitCommand("checkout", "-b", branch)
if b, err := co.CombinedOutput(); err != nil {
return fmt.Errorf("error checking out %s: %v. output: %s", branch, err, string(b))
}
return nil
}
// Merge attempts to merge commitlike into the current branch. It returns true
// if the merge completes. It returns an error if the abort fails.
func (r *Repo) Merge(commitlike string) (bool, error) {
return r.MergeWithStrategy(commitlike, prowgithub.MergeMerge)
}
// MergeWithStrategy attempts to merge commitlike into the current branch given the merge strategy.
// It returns true if the merge completes. It returns an error if the abort fails.
func (r *Repo) MergeWithStrategy(commitlike string, mergeStrategy prowgithub.PullRequestMergeType) (bool, error) {
r.logger.Infof("Merging %s.", commitlike)
switch mergeStrategy {
case prowgithub.MergeMerge:
return r.mergeWithMergeStrategyMerge(commitlike)
case prowgithub.MergeSquash:
return r.mergeWithMergeStrategySquash(commitlike)
default:
return false, fmt.Errorf("merge strategy %q is not supported", mergeStrategy)
}
}
func (r *Repo) mergeWithMergeStrategyMerge(commitlike string) (bool, error) {
co := r.gitCommand("merge", "--no-ff", "--no-stat", "-m merge", commitlike)
b, err := co.CombinedOutput()
if err == nil {
return true, nil
}
r.logger.WithError(err).Infof("Merge failed with output: %s", string(b))
if b, err := r.gitCommand("merge", "--abort").CombinedOutput(); err != nil {
return false, fmt.Errorf("error aborting merge for commitlike %s: %v. output: %s", commitlike, err, string(b))
}
return false, nil
}
func (r *Repo) mergeWithMergeStrategySquash(commitlike string) (bool, error) {
co := r.gitCommand("merge", "--squash", "--no-stat", commitlike)
b, err := co.CombinedOutput()
if err != nil {
r.logger.WithError(err).Infof("Merge failed with output: %s", string(b))
if b, err := r.gitCommand("reset", "--hard", "HEAD").CombinedOutput(); err != nil {
return false, fmt.Errorf("error resetting after failed squash for commitlike %s: %v. output: %s", commitlike, err, string(b))
}
return false, nil
}
b, err = r.gitCommand("commit", "--no-stat", "-m", "merge").CombinedOutput()
if err != nil {
r.logger.WithError(err).Infof("Commit after squash failed with output: %s", string(b))
return false, err
}
return true, nil
}
// MergeAndCheckout merges the provided headSHAs in order onto baseSHA using the provided strategy.
// If no headSHAs are provided, it will only checkout the baseSHA and return.
// Only the `merge` and `squash` strategies are supported.
func (r *Repo) MergeAndCheckout(baseSHA string, mergeStrategy prowgithub.PullRequestMergeType, headSHAs ...string) error {
if baseSHA == "" {
return errors.New("baseSHA must be set")
}
if err := r.Checkout(baseSHA); err != nil {
return err
}
if len(headSHAs) == 0 {
return nil
}
r.logger.Infof("Merging headSHAs %v onto base %s using strategy %s", headSHAs, baseSHA, mergeStrategy)
for _, headSHA := range headSHAs {
ok, err := r.MergeWithStrategy(headSHA, mergeStrategy)
if err != nil {
return err
} else if !ok {
return fmt.Errorf("failed to merge %q", headSHA)
}
}
return nil
}
// Am tries to apply the patch in the given path into the current branch
// by performing a three-way merge (similar to git cherry-pick). It returns
// an error if the patch cannot be applied.
func (r *Repo) Am(path string) error {
r.logger.Infof("Applying %s.", path)
co := r.gitCommand("am", "--3way", path)
b, err := co.CombinedOutput()
if err == nil {
return nil
}
output := string(b)
r.logger.WithError(err).Infof("Patch apply failed with output: %s", output)
if b, abortErr := r.gitCommand("am", "--abort").CombinedOutput(); abortErr != nil {
r.logger.WithError(abortErr).Warningf("Aborting patch apply failed with output: %s", string(b))
}
applyMsg := "The copy of the patch that failed is found in: .git/rebase-apply/patch"
if strings.Contains(output, applyMsg) {
i := strings.Index(output, applyMsg)
err = fmt.Errorf("%s", output[:i])
}
return err
}
// Push pushes over https to the provided owner/repo#branch using a password
// for basic auth.
func (r *Repo) Push(branch string) error {
if r.user == "" || r.pass == "" {
return errors.New("cannot push without credentials - configure your git client")
}
r.logger.Infof("Pushing to '%s/%s (branch: %s)'.", r.user, r.repo, branch)
remote := fmt.Sprintf("https://%s:%s@%s/%s/%s", r.user, r.pass, github, r.user, r.repo)
co := r.gitCommand("push", remote, branch)
out, err := co.CombinedOutput()
if err != nil {
r.logger.Errorf("Pushing failed with error: %v and output: %q", err, string(out))
return fmt.Errorf("pushing failed, output: %q, error: %v", string(out), err)
}
return nil
}
// CheckoutPullRequest does exactly that.
func (r *Repo) CheckoutPullRequest(number int) error {
r.logger.Infof("Fetching and checking out %s/%s#%d.", r.org, r.repo, number)
if b, err := retryCmd(r.logger, r.dir, r.git, "fetch", r.base+"/"+r.org+"/"+r.repo, fmt.Sprintf("pull/%d/head:pull%d", number, number)); err != nil {
return fmt.Errorf("git fetch failed for PR %d: %v. output: %s", number, err, string(b))
}
co := r.gitCommand("checkout", fmt.Sprintf("pull%d", number))
if b, err := co.CombinedOutput(); err != nil {
return fmt.Errorf("git checkout failed for PR %d: %v. output: %s", number, err, string(b))
}
return nil
}
// Config runs git config.
func (r *Repo) Config(key, value string) error {
r.logger.Infof("Running git config %s %s", key, value)
if b, err := r.gitCommand("config", key, value).CombinedOutput(); err != nil {
return fmt.Errorf("git config %s %s failed: %v. output: %s", key, value, err, string(b))
}
return nil
}
// retryCmd will retry the command a few times with backoff. Use this for any
// commands that will be talking to GitHub, such as clones or fetches.
func retryCmd(l *logrus.Entry, dir, cmd string, arg ...string) ([]byte, error) {
var b []byte
var err error
sleepyTime := time.Second
for i := 0; i < 3; i++ {
c := exec.Command(cmd, arg...)
c.Dir = dir
b, err = c.CombinedOutput()
if err != nil {
l.Warningf("Running %s %v returned error %v with output %s.", cmd, arg, err, string(b))
time.Sleep(sleepyTime)
sleepyTime *= 2
continue
}
break
}
return b, err
}
func (r *Repo) Diff(head, sha string) (changes []string, err error) {
r.logger.Infof("Diff head with %s'.", sha)
output, err := r.gitCommand("diff", head, sha, "--name-only").CombinedOutput()
if err != nil {
return nil, err
}
scan := bufio.NewScanner(bytes.NewReader(output))
scan.Split(bufio.ScanLines)
for scan.Scan() {
changes = append(changes, scan.Text())
}
return
}
// MergeCommitsExistBetween runs 'git log <target>..<head> --merged' to verify
// if merge commits exist between "target" and "head".
func (r *Repo) MergeCommitsExistBetween(target, head string) (bool, error) {
r.logger.Infof("Verifying if merge commits exist between %s and %s.", target, head)
b, err := r.gitCommand("log", fmt.Sprintf("%s..%s", target, head), "--oneline", "--merges").CombinedOutput()
if err != nil {
return false, fmt.Errorf("error verifying if merge commits exist between %s and %s: %v. output: %s", target, head, err, string(b))
}
return len(b) != 0, nil
}