forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
104 lines (96 loc) · 3.27 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
/*
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"
"os"
"os/exec"
"strings"
homedir "github.com/mitchellh/go-homedir"
"go.uber.org/zap"
"golang.org/x/xerrors"
)
func run(logger *zap.SugaredLogger, cmd string, args ...string) error {
c := exec.Command(cmd, args...)
var output bytes.Buffer
c.Stderr = &output
c.Stdout = &output
if err := c.Run(); err != nil {
logger.Errorf("Error running %v %v: %v\n%v", cmd, args, err, output.String())
return err
}
return nil
}
// Fetch fetches the specified git repository at the revision into path.
func Fetch(logger *zap.SugaredLogger, revision, path, url string) error {
// HACK: This is to get git+ssh to work since ssh doesn't respect the HOME
// env variable.
homepath, err := homedir.Dir()
if err != nil {
logger.Errorf("Unexpected error: getting the user home directory: %v", err)
return err
}
homeenv := os.Getenv("HOME")
euid := os.Geteuid()
// Special case the root user/directory
if euid == 0 {
if err := os.Symlink(homeenv+"/.ssh", "/root/.ssh"); 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)
}
} else if homeenv != "" && homeenv != homepath {
if _, err := os.Stat(homepath + "/.ssh"); os.IsNotExist(err) {
if err := os.Symlink(homeenv+"/.ssh", homepath+"/.ssh"); 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)
}
}
}
if revision == "" {
revision = "master"
}
if path != "" {
if err := run(logger, "git", "init", path); err != nil {
return err
}
if err := os.Chdir(path); err != nil {
return xerrors.Errorf("Failed to change directory with path %s; err: %w", path, err)
}
} else {
if err := run(logger, "git", "init"); err != nil {
return err
}
}
trimmedURL := strings.TrimSpace(url)
if err := run(logger, "git", "remote", "add", "origin", trimmedURL); err != nil {
return err
}
if err := run(logger, "git", "fetch", "--depth=1", "--recurse-submodules=yes", "origin", revision); err != nil {
// Fetch can fail if an old commitid was used so try git pull, performing regardless of error
// as no guarantee that the same error is returned by all git servers gitlab, github etc...
if err := run(logger, "git", "pull", "--recurse-submodules=yes", "origin"); err != nil {
logger.Warnf("Failed to pull origin : %s", err)
}
if err := run(logger, "git", "checkout", revision); err != nil {
return err
}
} else {
if err := run(logger, "git", "reset", "--hard", "FETCH_HEAD"); err != nil {
return err
}
}
logger.Infof("Successfully cloned %s @ %s in path %s", trimmedURL, revision, path)
return nil
}