-
Notifications
You must be signed in to change notification settings - Fork 7
/
git.go
338 lines (309 loc) · 9.5 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
package git
import (
"context"
"fmt"
"io"
"os"
"path"
"regexp"
"runtime"
"time"
"github.com/lunarway/release-manager/internal/log"
"github.com/pkg/errors"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/format/config"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
)
var (
ErrNothingToCommit = errors.New("nothing to commit")
ErrReleaseNotFound = errors.New("release not found")
ErrArtifactNotFound = errors.New("artifact not found")
)
func CloneDepth(ctx context.Context, repoURL, destination, sshPrivateKeyPath string, depth int) (*git.Repository, error) {
return clone(ctx, repoURL, destination, sshPrivateKeyPath, depth)
}
func Clone(ctx context.Context, repoURL, destination, sshPrivateKeyPath string) (*git.Repository, error) {
return clone(ctx, repoURL, destination, sshPrivateKeyPath, 0)
}
func clone(ctx context.Context, repoURL, destination, sshPrivateKeyPath string, depth int) (*git.Repository, error) {
authSSH, err := ssh.NewPublicKeysFromFile("git", sshPrivateKeyPath, "")
if err != nil {
return nil, errors.WithMessage(err, "public keys from file")
}
err = os.RemoveAll(destination)
if err != nil {
return nil, errors.WithMessage(err, "remove existing destination")
}
r, err := git.PlainCloneContext(ctx, destination, false, &git.CloneOptions{
URL: repoURL,
Auth: authSSH,
Depth: depth,
})
if err != nil {
return nil, errors.WithMessage(err, "clone repo")
}
return r, nil
}
func Checkout(r *git.Repository, hash plumbing.Hash) error {
workTree, err := r.Worktree()
if err != nil {
return errors.WithMessage(err, "get worktree")
}
err = workTree.Checkout(&git.CheckoutOptions{
Hash: hash,
})
if err != nil {
return errors.WithMessage(err, "checkout hash")
}
return nil
}
// LocateRelease traverses the git log to find a release commit with id
// artifactID.
//
// It expects the commit to have a commit messages as the one returned by
// ReleaseCommitMessage.
func LocateRelease(r *git.Repository, artifactID string) (plumbing.Hash, error) {
return locate(r, locateReleaseCondition(artifactID), ErrReleaseNotFound)
}
func locateReleaseCondition(artifactID string) conditionFunc {
r := regexp.MustCompile(fmt.Sprintf(`(?i)release %s$`, regexp.QuoteMeta(artifactID)))
return func(commitMsg string) bool {
if artifactID == "" {
return false
}
return r.MatchString(commitMsg)
}
}
// LocateServiceRelease traverses the git log to find a release
// commit for a specified service and environment.
//
// It expects the commit to have a commit messages as the one returned by
// ReleaseCommitMessage.
func LocateServiceRelease(r *git.Repository, env, service string) (plumbing.Hash, error) {
return locate(r, locateServiceReleaseCondition(env, service), ErrReleaseNotFound)
}
func locateServiceReleaseCondition(env, service string) conditionFunc {
r := regexp.MustCompile(fmt.Sprintf(`(?i)\[%s/%s] release`, regexp.QuoteMeta(env), regexp.QuoteMeta(service)))
return func(commitMsg string) bool {
if env == "" {
return false
}
if service == "" {
return false
}
return r.MatchString(commitMsg)
}
}
// LocateEnvRelease traverses the git log to find a release
// commit for a specified environment and artifactID.
//
// It expects the commit to have a commit messages as the one returned by
// ReleaseCommitMessage.
func LocateEnvRelease(r *git.Repository, env, artifactID string) (plumbing.Hash, error) {
return locate(r, locateEnvReleaseCondition(env, artifactID), ErrReleaseNotFound)
}
func locateEnvReleaseCondition(env, artifactId string) conditionFunc {
r := regexp.MustCompile(fmt.Sprintf(`(?i)\[%s/.*] release %s$`, regexp.QuoteMeta(env), regexp.QuoteMeta(artifactId)))
return func(commitMsg string) bool {
if env == "" {
return false
}
if artifactId == "" {
return false
}
return r.MatchString(commitMsg)
}
}
// LocateServiceReleaseRollbackSkip traverses the git log to find a release or
// rollback commit for a specified service and environment.
//
// It expects the commit to have a commit messages as the one returned by
// ReleaseCommitMessage or RollbackCommitMessage.
func LocateServiceReleaseRollbackSkip(r *git.Repository, env, service string, n uint) (plumbing.Hash, error) {
return locate(r, locateServiceReleaseRollbackSkipCondition(env, service, n), ErrReleaseNotFound)
}
func locateServiceReleaseRollbackSkipCondition(env, service string, n uint) conditionFunc {
return func(commitMsg string) bool {
releaseOK := locateServiceReleaseCondition(env, service)(commitMsg)
rollbackOK := locateServiceRollbackCondition(env, service)(commitMsg)
ok := releaseOK || rollbackOK
if !ok {
return false
}
if n == 0 {
return true
}
n--
return false
}
}
func locateServiceRollbackCondition(env, service string) conditionFunc {
r := regexp.MustCompile(fmt.Sprintf(`(?i)\[%s/%s] rollback `, regexp.QuoteMeta(env), regexp.QuoteMeta(service)))
return func(commitMsg string) bool {
if env == "" {
return false
}
if service == "" {
return false
}
return r.MatchString(commitMsg)
}
}
// LocateArtifact traverses the git log to find an artifact commit with id
// artifactID.
//
// It expects the commit to have a commit messages as the one returned by
// ArtifactCommitMessage.
func LocateArtifact(r *git.Repository, artifactID string) (plumbing.Hash, error) {
return locate(r, locateArtifactCondition(artifactID), ErrArtifactNotFound)
}
func locateArtifactCondition(artifactID string) conditionFunc {
artifactRegex := regexp.MustCompile(fmt.Sprintf(`(?i)artifact %s `, regexp.QuoteMeta(artifactID)))
return func(commitMsg string) bool {
if artifactID == "" {
return false
}
return artifactRegex.MatchString(commitMsg)
}
}
type conditionFunc func(commitMsg string) bool
func locate(r *git.Repository, condition conditionFunc, notFoundErr error) (plumbing.Hash, error) {
ref, err := r.Head()
if err != nil {
return plumbing.ZeroHash, errors.WithMessage(err, "retrieve HEAD branch")
}
cIter, err := r.Log(&git.LogOptions{
From: ref.Hash(),
})
if err != nil {
return plumbing.ZeroHash, errors.WithMessage(err, "retrieve commit history")
}
for {
commit, err := cIter.Next()
if err != nil {
if err == io.EOF {
return plumbing.ZeroHash, notFoundErr
}
return plumbing.ZeroHash, errors.WithMessage(err, "retrieve commit")
}
if condition(commit.Message) {
return commit.Hash, nil
}
}
}
func Commit(ctx context.Context, repo *git.Repository, changesPath, authorName, authorEmail, committerName, committerEmail, msg, sshPrivateKeyPath string) error {
w, err := repo.Worktree()
if err != nil {
return errors.WithMessage(err, "get worktree")
}
err = w.AddGlob(changesPath)
if err != nil {
return errors.WithMessage(err, "add changes")
}
status, err := w.Status()
if err != nil {
return errors.WithMessage(err, "status")
}
log.Infof("internal/git: Commit status:\n%s", status)
// if commit is empty
if status.IsClean() {
log.Debugf("internal/git: Commit: message '%s': nothing to commit", msg)
return ErrNothingToCommit
}
_, err = w.Commit(msg, &git.CommitOptions{
All: true,
Author: &object.Signature{
Name: authorName,
Email: authorEmail,
When: time.Now(),
},
Committer: &object.Signature{
Name: committerName,
Email: committerEmail,
When: time.Now(),
},
})
if err != nil {
return errors.WithMessage(err, "commit")
}
authSSH, err := ssh.NewPublicKeysFromFile("git", sshPrivateKeyPath, "")
if err != nil {
return errors.WithMessage(err, "public keys from file")
}
// TODO: this could be made optional if needed
err = repo.PushContext(ctx, &git.PushOptions{Auth: authSSH})
if err != nil {
return errors.WithMessage(err, "push")
}
return nil
}
// userHomeDir returns the home directory of the current user.
//
// It handles windows, linux and darwin operating systems by inspecting
// runtime.GOOS.
func userHomeDir() string {
switch runtime.GOOS {
case "windows":
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
case "linux":
home := os.Getenv("XDG_CONFIG_HOME")
if home != "" {
return home
}
fallthrough
default:
return os.Getenv("HOME")
}
}
// CommitterDetails returns name and email read for a Git configuration file.
//
// Configuration files are read first in the local git repository (if available)
// and then read the global Git configuration.
func CommitterDetails() (string, string, error) {
var paths []string
pwd, err := os.Getwd()
if err == nil {
paths = append(paths, path.Join(pwd, ".git", "config"))
}
paths = append(paths, path.Join(userHomeDir(), ".gitconfig"))
return credentials(paths...)
}
// credentials will try to read user name and email from provided paths.
func credentials(paths ...string) (string, string, error) {
for _, path := range paths {
c, err := parseConfig(path)
if err != nil {
continue
}
committerName := c.Section("user").Option("name")
committerEmail := c.Section("user").Option("email")
if committerEmail == "" {
continue
}
if committerName == "" {
continue
}
return committerName, committerEmail, nil
}
return "", "", errors.Errorf("failed to read Git credentials from paths: %v", paths)
}
// parseConfig returns the Git configuration parsed from provided path.
func parseConfig(path string) (config.Config, error) {
file, err := os.Open(path)
if err != nil {
return config.Config{}, err
}
decoder := config.NewDecoder(file)
var c config.Config
err = decoder.Decode(&c)
if err != nil {
return config.Config{}, err
}
return c, nil
}