-
Notifications
You must be signed in to change notification settings - Fork 33
/
git.go
581 lines (499 loc) · 15.3 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
Copyright 2021 The Flux 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 stash
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/plumbing/object"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/fluxcd/go-git-providers/gitprovider"
)
var licenseURLs = map[gitprovider.LicenseTemplate]string{
gitprovider.LicenseTemplate("apache-2.0"): "https://www.apache.org/licenses/LICENSE-2.0.txt",
gitprovider.LicenseTemplate("gpl-3.0"): "https://www.gnu.org/licenses/gpl-3.0-standalone.html",
}
// Git interface defines the methods that can be used to
// communicate with the git protocol.
type Git interface {
CleanCloner
CleanIniter
Committer
Brancher
Pusher
}
// CleanCloner interface defines the methods that can be used to Clone a repository
// and clean it up afterwards.
type CleanCloner interface {
CloneRepository(ctx context.Context, URL string) (r *git.Repository, dir string, err error)
Cleaner
}
// CleanIniter interface defines the methods that can be used to initialize a repository
// and clean it up afterwards.
type CleanIniter interface {
InitRepository(c *CreateCommit, createRemote bool) (r *git.Repository, dir string, err error)
Cleaner
}
// Cleaner interface defines the methods that can be used to clean up a directory
type Cleaner interface {
Cleanup(dir string) error
}
// Committer interface defines the methods that can be used to commit to a repository
type Committer interface {
CreateCommit(rPath string, r *git.Repository, branchName string, c *CreateCommit) (*Commit, error)
}
// Brancher interface defines the methods that can be used to create a new branch
type Brancher interface {
CreateBranch(branchName string, r *git.Repository, commitID string) error
}
// Pusher interface defines the methods that can be used to push to a repository
type Pusher interface {
Push(ctx context.Context, r *git.Repository) error
}
// GitService is a client for communicating with stash users endpoint
type GitService service
// Commit is a version of the repository
type Commit struct {
// SHA of the commit.
SHA string `json:"sha,omitempty"`
// Author is the author of the commit.
Author *CommitAuthor `json:"author,omitempty"`
// Committer is the committer of the commit.
Committer *CommitAuthor `json:"committer,omitempty"`
// Message is the commit message.
Message string `json:"message,omitempty"`
// Tree is the three of git objects that this commit points to.
Tree string `json:"tree,omitempty"`
// Parents is the list of parents commit.
Parents []string `json:"parents,omitempty"`
// PGPSignature is the PGP signature of the commit.
PGPSignature string `json:"pgp_signature,omitempty"`
}
// CommitAuthor represents the author or committer of a commit. The commit
// author may not correspond to a GitHub User.
type CommitAuthor struct {
// Date is the date of the commit
Date int64 `json:"date,omitempty"`
// Name is the name of the author
Name string `json:"name,omitempty"`
// Email is the email of the author
Email string `json:"email,omitempty"`
}
// CreateCommit creates a new commit in the repository.
type CreateCommit struct {
// Author is the author of the commit.
Author *CommitAuthor `json:"author,omitempty"`
// Committer is the committer of the commit.
Committer *CommitAuthor `json:"committer,omitempty"`
// Message is the commit message.
Message string `json:"message,omitempty"`
// Parents is the list of parents commit.
Parents []string `json:"parents,omitempty"`
// URL is the URL of the commit.
URL string `json:"url,omitempty"`
// Files is the list of files to commit.
Files []CommitFile `json:"files,omitempty"`
// SigningKey denotes a key to sign the commit with. If not nil this key will
// be used to sign the commit. The private key must be present and already
// decrypted.
SignKey *openpgp.Entity `json:"-"`
}
// CommitFile is a file to commit
type CommitFile struct {
// The path of the file relative to the repository root.
Path *string `json:"path"`
// The contents of the file.
Content *string `json:"content"`
}
// GitCommitOptionsFunc is a function that returns an error if the commit options are invalid
type GitCommitOptionsFunc func(c *CreateCommit) error
// WithURL is a currying function for the URL field
func WithURL(url string) GitCommitOptionsFunc {
return func(c *CreateCommit) error {
if url == "" {
return errors.New("url is required")
}
c.URL = url
return nil
}
}
// WithAuthor is a currying function for the Author field
func WithAuthor(author *CommitAuthor) GitCommitOptionsFunc {
return func(c *CreateCommit) error {
if author == nil {
return errors.New("Author is required")
}
c.Author = author
return nil
}
}
// WithCommitter is a currying function for the Committer field
func WithCommitter(committer *CommitAuthor) GitCommitOptionsFunc {
return func(c *CreateCommit) error {
if committer == nil {
return errors.New("Committer is required")
}
c.Committer = committer
return nil
}
}
// WithMessage is a currying function for the message field
func WithMessage(message string) GitCommitOptionsFunc {
return func(c *CreateCommit) error {
if message == "" {
return errors.New("message is required")
}
c.Message = message
return nil
}
}
// WithFiles is a currying function for the files field
func WithFiles(files []CommitFile) GitCommitOptionsFunc {
return func(c *CreateCommit) error {
if files != nil && len(files) != 0 {
c.Files = files
return nil
}
return errors.New("files are required")
}
}
// WithSignature is a currying function for the signKey field
func WithSignature(signKey *openpgp.Entity) GitCommitOptionsFunc {
return func(c *CreateCommit) error {
if signKey != nil {
c.SignKey = signKey
return nil
}
return errors.New("SignKey required")
}
}
// NewCommit is a helper function to create a CreateCommit object
// Use the currying functions provided to pass in the commit options
func NewCommit(opts ...GitCommitOptionsFunc) (*CreateCommit, error) {
c := &CreateCommit{}
for _, opt := range opts {
err := opt(c)
if err != nil {
return nil, err
}
}
if c.Author == nil {
return nil, errors.New("commit author: invalid parameters")
}
if c.Message == "" {
return nil, errors.New("commit message: invalid parameters")
}
if c.URL == "" {
return nil, errors.New("commit url: invalid parameters")
}
return c, nil
}
// CreateCommit creates a commit for the given CommitFiles. The commit is not pushed.
// The commit is signed with the given SignKey when provided.
// When committer is nil, author is used as the committer.
// An optional branch name can be provided to checkout the branch before committing.
func (s *GitService) CreateCommit(rPath string, r *git.Repository, branchName string, c *CreateCommit) (*Commit, error) {
if c == nil {
return nil, errors.New("commit must be provided")
}
w, err := r.Worktree()
if err != nil {
return nil, err
}
err = s.addCommitFiles(w, rPath, c.Files)
if err != nil {
return nil, err
}
// Set the committer & author DATE
now := time.Now().Unix()
c.Author.Date = now
if c.Committer != nil {
c.Committer.Date = now
}
if branchName != "" {
err := s.CreateBranch(branchName, r, "")
if err != nil {
return nil, err
}
}
obj, err := s.commit(w, r, c)
if err != nil {
return nil, err
}
var parents []string
if obj.ParentHashes == nil {
for _, parent := range obj.ParentHashes {
parents = append(parents, parent.String())
}
}
commit := &Commit{
SHA: obj.Hash.String(),
Author: &CommitAuthor{
Date: obj.Author.When.Unix(),
Name: obj.Author.Name,
Email: obj.Author.Email,
},
Committer: &CommitAuthor{
Date: obj.Committer.When.Unix(),
Name: obj.Committer.Name,
Email: obj.Committer.Email,
},
Message: obj.Message,
Tree: obj.TreeHash.String(),
Parents: parents,
PGPSignature: obj.PGPSignature,
}
return commit, nil
}
// CloneRepository clones the repository at the given URL to the given path.
// The repository will be cloned into a temporary directory which shall be clean up by the caller.
func (s *GitService) CloneRepository(ctx context.Context, URL string) (r *git.Repository, dir string, err error) {
dir, err = os.MkdirTemp("", "repo-*")
if err != nil {
return nil, "", err
}
r, err = git.PlainCloneContext(ctx, dir, false, &git.CloneOptions{
URL: URL,
Auth: &githttp.BasicAuth{Username: s.Client.username, Password: s.Client.token},
CABundle: s.Client.caBundle,
})
if err != nil {
return nil, "", fmt.Errorf("failed to clone repository: %v", err)
}
err = r.Fetch(&git.FetchOptions{
RefSpecs: []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"},
Auth: &githttp.BasicAuth{Username: s.Client.username, Password: s.Client.token},
CABundle: s.Client.caBundle,
})
if err != nil {
return nil, "", fmt.Errorf("failed to fetch repository: %v", err)
}
return r, dir, nil
}
func (s *GitService) addCommitFiles(w *git.Worktree, dir string, files []CommitFile) error {
for _, file := range files {
err := writeCommitFile(file, dir)
if err != nil {
return err
}
// Adds the new file to the staging area.
_, err = w.Add(*file.Path)
if err != nil {
return err
}
}
return nil
}
func writeCommitFile(file CommitFile, dir string) error {
filename := filepath.Join(dir, *file.Path)
filePath := strings.Split(*file.Path, "/")
if len(filePath) > 1 {
fullPath := append([]string{dir}, filePath[0:len(filePath)-1]...)
err := os.MkdirAll(strings.Join(fullPath, "/"), 0777)
if err != nil {
return err
}
}
err := os.WriteFile(filename, []byte(*file.Content), 0644)
if err != nil {
return err
}
return nil
}
// Cleanup removes the temporary directory created for the repository.
func (s *GitService) Cleanup(dir string) error {
err := os.RemoveAll(dir)
if err != nil {
return err
}
return nil
}
// CreateBranch creates a new branch with the given name and checkout the branch.
// An optional commit id can be provided to checkout the branch at the given commit.
func (s *GitService) CreateBranch(branchName string, r *git.Repository, commitID string) error {
w, err := r.Worktree()
if err != nil {
return err
}
branch := fmt.Sprintf("refs/heads/%s", branchName)
b := plumbing.ReferenceName(branch)
if commitID != "" {
commitHash := plumbing.NewHash(commitID)
err = w.Checkout(&git.CheckoutOptions{
Hash: commitHash,
})
if err != nil {
return fmt.Errorf("failed to checkout commit: %v", err)
}
// make we are on the correct Head
head, err := r.Head()
if err != nil {
return fmt.Errorf("failed to get head: %v", err)
}
if head.Hash() != commitHash {
return fmt.Errorf("commit %s not found", commitID)
}
ref := plumbing.NewHashReference(b, plumbing.NewHash(commitID))
err = w.Checkout(&git.CheckoutOptions{Create: true, Force: false, Branch: plumbing.ReferenceName(ref.Name().String())})
if err != nil {
return err
}
return nil
}
// First try to checkout branch
err = w.Checkout(&git.CheckoutOptions{Create: false, Force: false, Branch: b})
if err != nil {
// got an error - try to create it
err := w.Checkout(&git.CheckoutOptions{Create: true, Force: false, Branch: b})
if err != nil {
return err
}
}
return nil
}
// InitRepository is a function to create a new repository.
// The caller must clean up the directory after the function returns.
func (s *GitService) InitRepository(c *CreateCommit, createRemote bool) (r *git.Repository, dir string, err error) {
dir, err = os.MkdirTemp("", "repo-*")
if err != nil {
return nil, "", err
}
gitDir := osfs.New(dir + "/.git")
fs := osfs.New(dir)
r, err = git.Init(filesystem.NewStorage(gitDir, cache.NewObjectLRUDefault()), fs)
if err != nil {
return nil, "", err
}
w, err := r.Worktree()
if err != nil {
return nil, "", err
}
err = s.addCommitFiles(w, dir, c.Files)
if err != nil {
return nil, "", err
}
if createRemote {
rc := &config.RemoteConfig{Name: "origin", URLs: []string{c.URL}}
_, err = r.CreateRemote(rc)
if err != nil {
return nil, "", err
}
}
_, err = s.commit(w, r, c)
if err != nil {
return nil, "", err
}
return r, dir, nil
}
// commit creates a new commit with the given commit object.
// The commit is pushed to the remote repository.
func (s *GitService) commit(w *git.Worktree, r *git.Repository, c *CreateCommit) (*object.Commit, error) {
// Commits the current staging area to the repository, with the new file
// just created. We should provide the object.Signature of Author of the
// gitClient Since version 5.0.1, we can omit the Author signature, being read
// from the git config files.
var p []plumbing.Hash
if c.Parents != nil && len(c.Parents) > 0 {
p = make([]plumbing.Hash, len(c.Parents))
}
if p != nil && len(p) > 0 {
for i, parent := range c.Parents {
copy(p[i][:], parent)
}
}
// calculate time.Time from unix Time
authorDate := time.Unix(c.Author.Date, 0)
var committer *object.Signature
if c.Committer != nil {
committerDate := time.Unix(c.Committer.Date, 0)
committer = &object.Signature{
Name: c.Committer.Name,
Email: c.Committer.Email,
When: committerDate,
}
} else {
committer = &object.Signature{
Name: c.Author.Name,
Email: c.Author.Email,
When: authorDate,
}
}
commitHash, err := w.Commit(c.Message, &git.CommitOptions{
Author: &object.Signature{
Name: c.Author.Name,
Email: c.Author.Email,
When: authorDate,
},
Committer: committer,
Parents: p,
SignKey: c.SignKey,
All: true,
})
if err != nil {
return nil, err
}
obj, err := r.CommitObject(commitHash)
if err != nil {
return nil, err
}
return obj, nil
}
// Push commits the current changes to the remote repository.
func (s *GitService) Push(ctx context.Context, r *git.Repository) error {
options := &git.PushOptions{
RemoteName: "origin",
Auth: &githttp.BasicAuth{Username: s.Client.username, Password: s.Client.token},
CABundle: s.Client.caBundle,
}
err := r.PushContext(ctx, options)
if err != nil {
return fmt.Errorf("failed to push to remote: %w", err)
}
return nil
}
func getLicense(license gitprovider.LicenseTemplate) (string, error) {
licenseURL, ok := licenseURLs[license]
if !ok {
return "", fmt.Errorf("license: %s, not supported", license)
}
return downloadFile(licenseURL)
}
// downloadFile will download a url to a string.
func downloadFile(url string) (string, error) {
// Get the data
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
out := new(strings.Builder)
// Write the body to the string builder
_, err = io.Copy(out, resp.Body)
if err != nil {
return "", err
}
return out.String(), nil
}