-
Notifications
You must be signed in to change notification settings - Fork 8
/
git.go
235 lines (199 loc) · 5.89 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
package resource
import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
// Git interface for testing purposes.
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o fakes/fake_git.go . Git
type Git interface {
Init(string) error
Pull(string, string, int) error
Clone(string, string, int) error
RevParse(string) (string, error)
Fetch(int, int) error
Checkout(string, string) error
Merge(string) error
Rebase(string, string) error
GitCryptUnlock(string) error
}
// NewGitClient ...
func NewGitClient(source *Source, dir string, output io.Writer) (*GitClient, error) {
if source.SkipSSLVerification {
os.Setenv("GIT_SSL_NO_VERIFY", "true")
}
return &GitClient{
AccessToken: source.AccessToken,
Directory: dir,
Output: output,
}, nil
}
// GitClient ...
type GitClient struct {
AccessToken string
Directory string
Output io.Writer
}
func (g *GitClient) command(name string, arg ...string) *exec.Cmd {
cmd := exec.Command(name, arg...)
cmd.Dir = g.Directory
cmd.Stdout = g.Output
cmd.Stderr = g.Output
return cmd
}
// Init ...
func (g *GitClient) Init(branch string) error {
if err := g.command("git", "init").Run(); err != nil {
return fmt.Errorf("init failed: %s", err)
}
if err := g.command("git", "checkout", "-b", branch).Run(); err != nil {
return fmt.Errorf("checkout to '%s' failed: %s", branch, err)
}
log.Println("initialized repository:", branch)
return g.Config()
}
// Pull ...
func (g *GitClient) Pull(uri, branch string, depth int) error {
endpoint, err := g.Endpoint(uri)
if err != nil {
return err
}
if err := g.command("git", "remote", "add", "origin", endpoint+".git").Run(); err != nil {
return fmt.Errorf("failed to set remote origin: %s", err)
}
args := []string{"pull", "origin", branch}
args = appendDepth(args, depth)
cmd := g.command("git", args...)
// Discard output to have zero chance of logging the access token.
cmd.Stdout = ioutil.Discard
cmd.Stderr = ioutil.Discard
log.Println("pulling baseref:", args)
if err := cmd.Run(); err != nil {
return fmt.Errorf("clone failed: %s", err)
}
return nil
}
// Config ...
func (g *GitClient) Config() error {
if err := g.command("git", "config", "user.name", "concourse-ci").Run(); err != nil {
return fmt.Errorf("failed to configure git user: %s", err)
}
if err := g.command("git", "config", "user.email", "concourse@local").Run(); err != nil {
return fmt.Errorf("failed to configure git email: %s", err)
}
return nil
}
// Clone ...
func (g *GitClient) Clone(uri, branch string, depth int) error {
endpoint, err := g.Endpoint(uri)
if err != nil {
return err
}
args := []string{"clone", endpoint + ".git", "-b", branch, "."}
args = appendDepth(args, depth)
cmd := g.command("git", args...)
// Discard output to have zero chance of logging the access token.
cmd.Stdout = ioutil.Discard
cmd.Stderr = ioutil.Discard
log.Println("cloning baseref:", args)
if err := cmd.Run(); err != nil {
return fmt.Errorf("clone failed: %s", err)
}
return g.Config()
}
// RevParse retrieves the SHA of the given branch.
func (g *GitClient) RevParse(branch string) (string, error) {
cmd := exec.Command("git", "rev-parse", "--verify", branch)
cmd.Dir = g.Directory
sha, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("rev-parse '%s' failed: %s: %s", branch, err, string(sha))
}
return strings.TrimSpace(string(sha)), nil
}
// Fetch ...
func (g *GitClient) Fetch(prNumber, depth int) error {
args := []string{
"fetch",
"origin",
"-q",
fmt.Sprintf("pull/%s/head", strconv.Itoa(prNumber)),
}
args = appendDepth(args, depth)
cmd := g.command("git", args...)
// Discard output to have zero chance of logging the access token.
cmd.Stdout = ioutil.Discard
cmd.Stderr = ioutil.Discard
log.Println("fetching headref:", args)
if err := cmd.Run(); err != nil {
return fmt.Errorf("fetch failed: %s", err)
}
return nil
}
// Checkout ...
func (g *GitClient) Checkout(branch, sha string) error {
log.Println("checkout:", branch, sha)
if err := g.command("git", "checkout", "-b", "pr-"+branch, sha).Run(); err != nil {
return fmt.Errorf("checkout failed: %s", err)
}
return nil
}
// Merge ...
func (g *GitClient) Merge(sha string) error {
log.Println("merging sha:", sha)
if err := g.command("git", "merge", sha, "--no-stat").Run(); err != nil {
return fmt.Errorf("merge failed: %s", err)
}
return nil
}
// Rebase ...
func (g *GitClient) Rebase(baseRef string, headSha string) error {
log.Println("rebasing:", baseRef, headSha)
if err := g.command("git", "rebase", baseRef, headSha).Run(); err != nil {
return fmt.Errorf("rebase failed: %s", err)
}
return nil
}
// GitCryptUnlock unlocks the repository using git-crypt
func (g *GitClient) GitCryptUnlock(base64key string) error {
keyDir, err := ioutil.TempDir("", "")
if err != nil {
return fmt.Errorf("failed to create temporary directory")
}
defer os.RemoveAll(keyDir)
decodedKey, err := base64.StdEncoding.DecodeString(base64key)
if err != nil {
return fmt.Errorf("failed to decode git-crypt key")
}
keyPath := filepath.Join(keyDir, "git-crypt-key")
if err := ioutil.WriteFile(keyPath, decodedKey, 600); err != nil {
return fmt.Errorf("failed to write git-crypt key to file: %s", err)
}
if err := g.command("git-crypt", "unlock", keyPath).Run(); err != nil {
return fmt.Errorf("git-crypt unlock failed: %s", err)
}
return nil
}
// Endpoint takes an uri and produces an endpoint with the login information baked in.
func (g *GitClient) Endpoint(uri string) (string, error) {
endpoint, err := url.Parse(uri)
if err != nil {
return "", fmt.Errorf("failed to parse commit url: %s", err)
}
endpoint.User = url.UserPassword("x-oauth-basic", g.AccessToken)
return endpoint.String(), nil
}
func appendDepth(args []string, depth int) []string {
if depth > 0 {
args = append(args, []string{"--depth", strconv.Itoa(depth)}...)
}
return args
}