forked from telia-oss/github-pr-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
236 lines (209 loc) · 6.64 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
package resource
import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"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, bool, bool) error
RevParse(string) (string, error)
Fetch(string, int, int, bool) error
Checkout(string, string, bool) error
Merge(string, bool) error
Rebase(string, string, bool) 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")
}
if source.DisableGitLFS {
os.Setenv("GIT_LFS_SKIP_SMUDGE", "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
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env,
"X_OAUTH_BASIC_TOKEN="+g.AccessToken,
"GIT_ASKPASS=/usr/local/bin/askpass.sh")
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)
}
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)
}
if err := g.command("git", "config", "url.https://x-oauth-basic@github.com/.insteadOf", "git@github.com:").Run(); err != nil {
return fmt.Errorf("failed to configure github url: %s", err)
}
if err := g.command("git", "config", "url.https://.insteadOf", "git://").Run(); err != nil {
return fmt.Errorf("failed to configure github url: %s", err)
}
return nil
}
// Pull ...
func (g *GitClient) Pull(uri, branch string, depth int, submodules bool, fetchTags bool) error {
endpoint, err := g.Endpoint(uri)
if err != nil {
return err
}
if err := g.command("git", "remote", "add", "origin", endpoint).Run(); err != nil {
return fmt.Errorf("setting 'origin' remote to '%s' failed: %s", endpoint, err)
}
args := []string{"pull", "origin", branch}
if depth > 0 {
args = append(args, "--depth", strconv.Itoa(depth))
}
if fetchTags {
args = append(args, "--tags")
}
if submodules {
args = append(args, "--recurse-submodules")
}
cmd := g.command("git", args...)
// Discard output to have zero chance of logging the access token.
cmd.Stdout = ioutil.Discard
cmd.Stderr = ioutil.Discard
if err := cmd.Run(); err != nil {
return fmt.Errorf("pull failed: %s", cmd)
}
if submodules {
submodulesGet := g.command("git", "submodule", "update", "--init", "--recursive")
if err := submodulesGet.Run(); err != nil {
return fmt.Errorf("submodule update failed: %s", err)
}
}
return nil
}
// 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(uri string, prNumber int, depth int, submodules bool) error {
endpoint, err := g.Endpoint(uri)
if err != nil {
return err
}
args := []string{"fetch", endpoint, fmt.Sprintf("pull/%s/head", strconv.Itoa(prNumber))}
if depth > 0 {
args = append(args, "--depth", strconv.Itoa(depth))
}
if submodules {
args = append(args, "--recurse-submodules")
}
cmd := g.command("git", args...)
// Discard output to have zero chance of logging the access token.
cmd.Stdout = ioutil.Discard
cmd.Stderr = ioutil.Discard
if err := cmd.Run(); err != nil {
return fmt.Errorf("fetch failed: %s", err)
}
return nil
}
// CheckOut
func (g *GitClient) Checkout(branch, sha string, submodules bool) error {
if err := g.command("git", "checkout", "-b", branch, sha).Run(); err != nil {
return fmt.Errorf("checkout failed: %s", err)
}
if submodules {
if err := g.command("git", "submodule", "update", "--init", "--recursive", "--checkout").Run(); err != nil {
return fmt.Errorf("submodule update failed: %s", err)
}
}
return nil
}
// Merge ...
func (g *GitClient) Merge(sha string, submodules bool) error {
if err := g.command("git", "merge", sha, "--no-stat").Run(); err != nil {
return fmt.Errorf("merge failed: %s", err)
}
if submodules {
if err := g.command("git", "submodule", "update", "--init", "--recursive", "--merge").Run(); err != nil {
return fmt.Errorf("submodule update failed: %s", err)
}
}
return nil
}
// Rebase ...
func (g *GitClient) Rebase(baseRef string, headSha string, submodules bool) error {
if err := g.command("git", "rebase", baseRef, headSha).Run(); err != nil {
return fmt.Errorf("rebase failed: %s", err)
}
if submodules {
if err := g.command("git", "submodule", "update", "--init", "--recursive", "--rebase").Run(); err != nil {
return fmt.Errorf("submodule update 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, os.FileMode(0600)); 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
}