-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
client.go
1020 lines (918 loc) · 32.3 KB
/
client.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package git
import (
"crypto/tls"
"errors"
"fmt"
"math"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"syscall"
"time"
"github.com/Masterminds/semver/v3"
argoexec "github.com/argoproj/pkg/exec"
"github.com/bmatcuk/doublestar/v4"
"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/transport"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
apierrors "k8s.io/apimachinery/pkg/api/errors"
utilnet "k8s.io/apimachinery/pkg/util/net"
"github.com/argoproj/argo-cd/v2/common"
certutil "github.com/argoproj/argo-cd/v2/util/cert"
"github.com/argoproj/argo-cd/v2/util/env"
executil "github.com/argoproj/argo-cd/v2/util/exec"
"github.com/argoproj/argo-cd/v2/util/proxy"
)
var ErrInvalidRepoURL = fmt.Errorf("repo URL is invalid")
type RevisionMetadata struct {
Author string
Date time.Time
Tags []string
Message string
}
// this should match reposerver/repository/repository.proto/RefsList
type Refs struct {
Branches []string
Tags []string
// heads and remotes are also refs, but are not needed at this time.
}
type gitRefCache interface {
SetGitReferences(repo string, references []*plumbing.Reference) error
GetOrLockGitReferences(repo string, lockId string, references *[]*plumbing.Reference) (string, error)
UnlockGitReferences(repo string, lockId string) error
}
// Client is a generic git client interface
type Client interface {
Root() string
Init() error
Fetch(revision string) error
Submodule() error
Checkout(revision string, submoduleEnabled bool) (string, error)
LsRefs() (*Refs, error)
LsRemote(revision string) (string, error)
LsFiles(path string, enableNewGitFileGlobbing bool) ([]string, error)
LsLargeFiles() ([]string, error)
CommitSHA() (string, error)
RevisionMetadata(revision string) (*RevisionMetadata, error)
VerifyCommitSignature(string) (string, error)
IsAnnotatedTag(string) bool
ChangedFiles(revision string, targetRevision string) ([]string, error)
IsRevisionPresent(revision string) bool
// SetAuthor sets the author name and email in the git configuration.
SetAuthor(name, email string) (string, error)
// CheckoutOrOrphan checks out the branch. If the branch does not exist, it creates an orphan branch.
CheckoutOrOrphan(branch string, submoduleEnabled bool) (string, error)
// CheckoutOrNew checks out the given branch. If the branch does not exist, it creates an empty branch based on
// the base branch.
CheckoutOrNew(branch, base string, submoduleEnabled bool) (string, error)
// RemoveContents removes all files from the git repository.
RemoveContents() (string, error)
// CommitAndPush commits and pushes changes to the target branch.
CommitAndPush(branch, message string) (string, error)
}
type EventHandlers struct {
OnLsRemote func(repo string) func()
OnFetch func(repo string) func()
OnPush func(repo string) func()
}
// nativeGitClient implements Client interface using git CLI
type nativeGitClient struct {
EventHandlers
// URL of the repository
repoURL string
// Root path of repository
root string
// Authenticator credentials for private repositories
creds Creds
// Whether to connect insecurely to repository, e.g. don't verify certificate
insecure bool
// Whether the repository is LFS enabled
enableLfs bool
// gitRefCache knows how to cache git refs
gitRefCache gitRefCache
// indicates if client allowed to load refs from cache
loadRefFromCache bool
// HTTP/HTTPS proxy used to access repository
proxy string
// list of targets that shouldn't use the proxy, applies only if the proxy is set
noProxy string
}
type runOpts struct {
SkipErrorLogging bool
CaptureStderr bool
}
var (
maxAttemptsCount = 1
maxRetryDuration time.Duration
retryDuration time.Duration
factor int64
)
func init() {
if countStr := os.Getenv(common.EnvGitAttemptsCount); countStr != "" {
if cnt, err := strconv.Atoi(countStr); err != nil {
panic(fmt.Sprintf("Invalid value in %s env variable: %v", common.EnvGitAttemptsCount, err))
} else {
maxAttemptsCount = int(math.Max(float64(cnt), 1))
}
}
maxRetryDuration = env.ParseDurationFromEnv(common.EnvGitRetryMaxDuration, common.DefaultGitRetryMaxDuration, 0, math.MaxInt64)
retryDuration = env.ParseDurationFromEnv(common.EnvGitRetryDuration, common.DefaultGitRetryDuration, 0, math.MaxInt64)
factor = env.ParseInt64FromEnv(common.EnvGitRetryFactor, common.DefaultGitRetryFactor, 0, math.MaxInt64)
}
type ClientOpts func(c *nativeGitClient)
// WithCache sets git revisions cacher as well as specifies if client should tries to use cached resolved revision
func WithCache(cache gitRefCache, loadRefFromCache bool) ClientOpts {
return func(c *nativeGitClient) {
c.gitRefCache = cache
c.loadRefFromCache = loadRefFromCache
}
}
// WithEventHandlers sets the git client event handlers
func WithEventHandlers(handlers EventHandlers) ClientOpts {
return func(c *nativeGitClient) {
c.EventHandlers = handlers
}
}
func NewClient(rawRepoURL string, creds Creds, insecure bool, enableLfs bool, proxy string, noProxy string, opts ...ClientOpts) (Client, error) {
r := regexp.MustCompile("(/|:)")
normalizedGitURL := NormalizeGitURL(rawRepoURL)
if normalizedGitURL == "" {
return nil, fmt.Errorf("repository %q cannot be initialized: %w", rawRepoURL, ErrInvalidRepoURL)
}
root := filepath.Join(os.TempDir(), r.ReplaceAllString(normalizedGitURL, "_"))
if root == os.TempDir() {
return nil, fmt.Errorf("repository %q cannot be initialized, because its root would be system temp at %s", rawRepoURL, root)
}
return NewClientExt(rawRepoURL, root, creds, insecure, enableLfs, proxy, noProxy, opts...)
}
func NewClientExt(rawRepoURL string, root string, creds Creds, insecure bool, enableLfs bool, proxy string, noProxy string, opts ...ClientOpts) (Client, error) {
client := &nativeGitClient{
repoURL: rawRepoURL,
root: root,
creds: creds,
insecure: insecure,
enableLfs: enableLfs,
proxy: proxy,
noProxy: noProxy,
}
for i := range opts {
opts[i](client)
}
return client, nil
}
var gitClientTimeout = env.ParseDurationFromEnv("ARGOCD_GIT_REQUEST_TIMEOUT", 15*time.Second, 0, math.MaxInt64)
// Returns a HTTP client object suitable for go-git to use using the following
// pattern:
// - If insecure is true, always returns a client with certificate verification
// turned off.
// - If one or more custom certificates are stored for the repository, returns
// a client with those certificates in the list of root CAs used to verify
// the server's certificate.
// - Otherwise (and on non-fatal errors), a default HTTP client is returned.
func GetRepoHTTPClient(repoURL string, insecure bool, creds Creds, proxyURL string, noProxy string) *http.Client {
// Default HTTP client
customHTTPClient := &http.Client{
// 15 second timeout by default
Timeout: gitClientTimeout,
// don't follow redirect
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
proxyFunc := proxy.GetCallback(proxyURL, noProxy)
// Callback function to return any configured client certificate
// We never return err, but an empty cert instead.
clientCertFunc := func(req *tls.CertificateRequestInfo) (*tls.Certificate, error) {
var err error
cert := tls.Certificate{}
// If we aren't called with GenericHTTPSCreds, then we just return an empty cert
httpsCreds, ok := creds.(GenericHTTPSCreds)
if !ok {
return &cert, nil
}
// If the creds contain client certificate data, we return a TLS.Certificate
// populated with the cert and its key.
if httpsCreds.HasClientCert() {
cert, err = tls.X509KeyPair([]byte(httpsCreds.GetClientCertData()), []byte(httpsCreds.GetClientCertKey()))
if err != nil {
log.Errorf("Could not load Client Certificate: %v", err)
return &cert, nil
}
}
return &cert, nil
}
transport := &http.Transport{
Proxy: proxyFunc,
TLSClientConfig: &tls.Config{
GetClientCertificate: clientCertFunc,
},
DisableKeepAlives: true,
}
customHTTPClient.Transport = transport
if insecure {
transport.TLSClientConfig.InsecureSkipVerify = true
return customHTTPClient
}
parsedURL, err := url.Parse(repoURL)
if err != nil {
return customHTTPClient
}
serverCertificatePem, err := certutil.GetCertificateForConnect(parsedURL.Host)
if err != nil {
return customHTTPClient
}
if len(serverCertificatePem) > 0 {
certPool := certutil.GetCertPoolFromPEMData(serverCertificatePem)
transport.TLSClientConfig.RootCAs = certPool
}
return customHTTPClient
}
func newAuth(repoURL string, creds Creds) (transport.AuthMethod, error) {
switch creds := creds.(type) {
case SSHCreds:
var sshUser string
if isSSH, user := IsSSHURL(repoURL); isSSH {
sshUser = user
}
signer, err := ssh.ParsePrivateKey([]byte(creds.sshPrivateKey))
if err != nil {
return nil, err
}
auth := &PublicKeysWithOptions{}
auth.User = sshUser
auth.Signer = signer
if creds.insecure {
auth.HostKeyCallback = ssh.InsecureIgnoreHostKey()
} else {
// Set up validation of SSH known hosts for using our ssh_known_hosts
// file.
auth.HostKeyCallback, err = knownhosts.New(certutil.GetSSHKnownHostsDataPath())
if err != nil {
log.Errorf("Could not set-up SSH known hosts callback: %v", err)
}
}
return auth, nil
case HTTPSCreds:
auth := githttp.BasicAuth{Username: creds.username, Password: creds.password}
if auth.Username == "" {
auth.Username = "x-access-token"
}
return &auth, nil
case GitHubAppCreds:
token, err := creds.getAccessToken()
if err != nil {
return nil, err
}
auth := githttp.BasicAuth{Username: "x-access-token", Password: token}
return &auth, nil
case GoogleCloudCreds:
username, err := creds.getUsername()
if err != nil {
return nil, fmt.Errorf("failed to get username from creds: %w", err)
}
token, err := creds.getAccessToken()
if err != nil {
return nil, fmt.Errorf("failed to get access token from creds: %w", err)
}
auth := githttp.BasicAuth{Username: username, Password: token}
return &auth, nil
}
return nil, nil
}
func (m *nativeGitClient) Root() string {
return m.root
}
// Init initializes a local git repository and sets the remote origin
func (m *nativeGitClient) Init() error {
_, err := git.PlainOpen(m.root)
if err == nil {
return nil
}
if !errors.Is(err, git.ErrRepositoryNotExists) {
return err
}
log.Infof("Initializing %s to %s", m.repoURL, m.root)
err = os.RemoveAll(m.root)
if err != nil {
return fmt.Errorf("unable to clean repo at %s: %w", m.root, err)
}
err = os.MkdirAll(m.root, 0o755)
if err != nil {
return err
}
repo, err := git.PlainInit(m.root, false)
if err != nil {
return err
}
_, err = repo.CreateRemote(&config.RemoteConfig{
Name: git.DefaultRemoteName,
URLs: []string{m.repoURL},
})
return err
}
// Returns true if the repository is LFS enabled
func (m *nativeGitClient) IsLFSEnabled() bool {
return m.enableLfs
}
func (m *nativeGitClient) fetch(revision string) error {
var err error
if revision != "" {
err = m.runCredentialedCmd("fetch", "origin", revision, "--tags", "--force", "--prune")
} else {
err = m.runCredentialedCmd("fetch", "origin", "--tags", "--force", "--prune")
}
return err
}
// IsRevisionPresent checks to see if the given revision already exists locally.
func (m *nativeGitClient) IsRevisionPresent(revision string) bool {
if revision == "" {
return false
}
cmd := exec.Command("git", "cat-file", "-t", revision)
out, err := m.runCmdOutput(cmd, runOpts{SkipErrorLogging: true})
if out == "commit" && err == nil {
return true
} else {
return false
}
}
// Fetch fetches latest updates from origin
func (m *nativeGitClient) Fetch(revision string) error {
if m.OnFetch != nil {
done := m.OnFetch(m.repoURL)
defer done()
}
err := m.fetch(revision)
// When we have LFS support enabled, check for large files and fetch them too.
if err == nil && m.IsLFSEnabled() {
largeFiles, err := m.LsLargeFiles()
if err == nil && len(largeFiles) > 0 {
err = m.runCredentialedCmd("lfs", "fetch", "--all")
if err != nil {
return err
}
}
}
return err
}
// LsFiles lists the local working tree, including only files that are under source control
func (m *nativeGitClient) LsFiles(path string, enableNewGitFileGlobbing bool) ([]string, error) {
if enableNewGitFileGlobbing {
// This is the new way with safer globbing
err := os.Chdir(m.root)
if err != nil {
return nil, err
}
all_files, err := doublestar.FilepathGlob(path)
if err != nil {
return nil, err
}
var files []string
for _, file := range all_files {
link, err := filepath.EvalSymlinks(file)
if err != nil {
return nil, err
}
absPath, err := filepath.Abs(link)
if err != nil {
return nil, err
}
if strings.HasPrefix(absPath, m.root) {
files = append(files, file)
} else {
log.Warnf("Absolute path for %s is outside of repository, removing it", file)
}
}
return files, nil
} else {
// This is the old and default way
out, err := m.runCmd("ls-files", "--full-name", "-z", "--", path)
if err != nil {
return nil, err
}
// remove last element, which is blank regardless of whether we're using nullbyte or newline
ss := strings.Split(out, "\000")
return ss[:len(ss)-1], nil
}
}
// LsLargeFiles lists all files that have references to LFS storage
func (m *nativeGitClient) LsLargeFiles() ([]string, error) {
out, err := m.runCmd("lfs", "ls-files", "-n")
if err != nil {
return nil, err
}
ss := strings.Split(out, "\n")
return ss, nil
}
// Submodule embed other repositories into this repository
func (m *nativeGitClient) Submodule() error {
if err := m.runCredentialedCmd("submodule", "sync", "--recursive"); err != nil {
return err
}
if err := m.runCredentialedCmd("submodule", "update", "--init", "--recursive"); err != nil {
return err
}
return nil
}
// Checkout checks out the specified revision
func (m *nativeGitClient) Checkout(revision string, submoduleEnabled bool) (string, error) {
if revision == "" || revision == "HEAD" {
revision = "origin/HEAD"
}
if out, err := m.runCmd("checkout", "--force", revision); err != nil {
return out, fmt.Errorf("failed to checkout %s: %w", revision, err)
}
// We must populate LFS content by using lfs checkout, if we have at least
// one LFS reference in the current revision.
if m.IsLFSEnabled() {
if largeFiles, err := m.LsLargeFiles(); err == nil {
if len(largeFiles) > 0 {
if out, err := m.runCmd("lfs", "checkout"); err != nil {
return out, fmt.Errorf("failed to checkout LFS files: %w", err)
}
}
} else {
return "", fmt.Errorf("failed to list LFS files: %w", err)
}
}
if _, err := os.Stat(m.root + "/.gitmodules"); !os.IsNotExist(err) {
if submoduleEnabled {
if err := m.Submodule(); err != nil {
return "", fmt.Errorf("failed to update submodules: %w", err)
}
}
}
// NOTE
// The double “f” in the arguments is not a typo: the first “f” tells
// `git clean` to delete untracked files and directories, and the second “f”
// tells it to clean untracked nested Git repositories (for example a
// submodule which has since been removed).
if out, err := m.runCmd("clean", "-ffdx"); err != nil {
return out, fmt.Errorf("failed to clean: %w", err)
}
return "", nil
}
func (m *nativeGitClient) getRefs() ([]*plumbing.Reference, error) {
myLockUUID, err := uuid.NewRandom()
myLockId := ""
if err != nil {
log.Debug("Error generating git references cache lock id: ", err)
} else {
myLockId = myLockUUID.String()
}
// Prevent an additional get call to cache if we know our state isn't stale
needsUnlock := true
if m.gitRefCache != nil && m.loadRefFromCache {
var res []*plumbing.Reference
foundLockId, err := m.gitRefCache.GetOrLockGitReferences(m.repoURL, myLockId, &res)
isLockOwner := myLockId == foundLockId
if !isLockOwner && err == nil {
// Valid value already in cache
return res, nil
} else if !isLockOwner && err != nil {
// Error getting value from cache
log.Debugf("Error getting git references from cache: %v", err)
return nil, err
}
// Defer a soft reset of the cache lock, if the value is set this call will be ignored
defer func() {
if needsUnlock {
err := m.gitRefCache.UnlockGitReferences(m.repoURL, myLockId)
if err != nil {
log.Debugf("Error unlocking git references from cache: %v", err)
}
}
}()
}
if m.OnLsRemote != nil {
done := m.OnLsRemote(m.repoURL)
defer done()
}
repo, err := git.Init(memory.NewStorage(), nil)
if err != nil {
return nil, err
}
remote, err := repo.CreateRemote(&config.RemoteConfig{
Name: git.DefaultRemoteName,
URLs: []string{m.repoURL},
})
if err != nil {
return nil, err
}
auth, err := newAuth(m.repoURL, m.creds)
if err != nil {
return nil, err
}
res, err := listRemote(remote, &git.ListOptions{Auth: auth}, m.insecure, m.creds, m.proxy, m.noProxy)
if err == nil && m.gitRefCache != nil {
if err := m.gitRefCache.SetGitReferences(m.repoURL, res); err != nil {
log.Warnf("Failed to store git references to cache: %v", err)
} else {
// Since we successfully overwrote the lock with valid data, we don't need to unlock
needsUnlock = false
}
return res, nil
}
return res, err
}
func (m *nativeGitClient) LsRefs() (*Refs, error) {
refs, err := m.getRefs()
if err != nil {
return nil, err
}
sortedRefs := &Refs{
Branches: []string{},
Tags: []string{},
}
for _, revision := range refs {
if revision.Name().IsBranch() {
sortedRefs.Branches = append(sortedRefs.Branches, revision.Name().Short())
} else if revision.Name().IsTag() {
sortedRefs.Tags = append(sortedRefs.Tags, revision.Name().Short())
}
}
log.Debugf("LsRefs resolved %d branches and %d tags on repository", len(sortedRefs.Branches), len(sortedRefs.Tags))
// Would prefer to sort by last modified date but that info does not appear to be available without resolving each ref
sort.Strings(sortedRefs.Branches)
sort.Strings(sortedRefs.Tags)
return sortedRefs, nil
}
// LsRemote resolves the commit SHA of a specific branch, tag (with semantic versioning or not),
// or HEAD. If the supplied revision does not resolve, and "looks" like a 7+ hexadecimal commit SHA,
// it will return the revision string. Otherwise, it returns an error indicating that the revision could
// not be resolved. This method runs with in-memory storage and is safe to run concurrently,
// or to be run without a git repository locally cloned.
func (m *nativeGitClient) LsRemote(revision string) (res string, err error) {
for attempt := 0; attempt < maxAttemptsCount; attempt++ {
res, err = m.lsRemote(revision)
if err == nil {
return
} else if apierrors.IsInternalError(err) || apierrors.IsTimeout(err) || apierrors.IsServerTimeout(err) ||
apierrors.IsTooManyRequests(err) || utilnet.IsProbableEOF(err) || utilnet.IsConnectionReset(err) {
// Formula: timeToWait = duration * factor^retry_number
// Note that timeToWait should equal to duration for the first retry attempt.
// When timeToWait is more than maxDuration retry should be performed at maxDuration.
timeToWait := float64(retryDuration) * (math.Pow(float64(factor), float64(attempt)))
if maxRetryDuration > 0 {
timeToWait = math.Min(float64(maxRetryDuration), timeToWait)
}
time.Sleep(time.Duration(timeToWait))
}
}
return
}
func (m *nativeGitClient) lsRemote(revision string) (string, error) {
if IsCommitSHA(revision) {
return revision, nil
}
refs, err := m.getRefs()
if err != nil {
return "", fmt.Errorf("failed to list refs: %w", err)
}
if revision == "" {
revision = "HEAD"
}
semverSha := m.resolveSemverRevision(revision, refs)
if semverSha != "" {
return semverSha, nil
}
// refToHash keeps a maps of remote refs to their hash
// (e.g. refs/heads/master -> a67038ae2e9cb9b9b16423702f98b41e36601001)
refToHash := make(map[string]string)
// refToResolve remembers ref name of the supplied revision if we determine the revision is a
// symbolic reference (like HEAD), in which case we will resolve it from the refToHash map
refToResolve := ""
for _, ref := range refs {
refName := ref.Name().String()
hash := ref.Hash().String()
if ref.Type() == plumbing.HashReference {
refToHash[refName] = hash
}
// log.Debugf("%s\t%s", hash, refName)
if ref.Name().Short() == revision || refName == revision {
if ref.Type() == plumbing.HashReference {
log.Debugf("revision '%s' resolved to '%s'", revision, hash)
return hash, nil
}
if ref.Type() == plumbing.SymbolicReference {
refToResolve = ref.Target().String()
}
}
}
if refToResolve != "" {
// If refToResolve is non-empty, we are resolving symbolic reference (e.g. HEAD).
// It should exist in our refToHash map
if hash, ok := refToHash[refToResolve]; ok {
log.Debugf("symbolic reference '%s' (%s) resolved to '%s'", revision, refToResolve, hash)
return hash, nil
}
}
// We support the ability to use a truncated commit-SHA (e.g. first 7 characters of a SHA)
if IsTruncatedCommitSHA(revision) {
log.Debugf("revision '%s' assumed to be commit sha", revision)
return revision, nil
}
// If we get here, revision string had non hexadecimal characters (indicating its a branch, tag,
// or symbolic ref) and we were unable to resolve it to a commit SHA.
return "", fmt.Errorf("unable to resolve '%s' to a commit SHA", revision)
}
// resolveSemverRevision is a part of the lsRemote method workflow.
// When the user correctly configures the Git repository revision, and that revision is a valid semver constraint, we
// use this logic path rather than the standard lsRemote revision resolution loop.
// Some examples to illustrate the actual behavior - if the revision is:
// * "v0.1.2"/"0.1.2" or "v0.1"/"0.1", then this is not a constraint, it's a pinned version - so we fall back to the standard tag matching in the lsRemote loop.
// * "v0.1.*"/"0.1.*", and there's a tag matching that constraint, then we find the latest matching version and return its commit hash.
// * "v0.1.*"/"0.1.*", and there is *no* tag matching that constraint, then we fall back to the standard tag matching in the lsRemote loop.
// * "custom-tag", only the lsRemote loop will run - because that revision is an invalid semver;
// * "master-branch", only the lsRemote loop will run because that revision is an invalid semver;
func (m *nativeGitClient) resolveSemverRevision(revision string, refs []*plumbing.Reference) string {
if _, err := semver.NewVersion(revision); err == nil {
// If the revision is a valid version, then we know it isn't a constraint; it's just a pin.
// In which case, we should use standard tag resolution mechanisms.
return ""
}
constraint, err := semver.NewConstraint(revision)
if err != nil {
log.Debugf("Revision '%s' is not a valid semver constraint, skipping semver resolution.", revision)
return ""
}
maxVersion := semver.New(0, 0, 0, "", "")
maxVersionHash := plumbing.ZeroHash
for _, ref := range refs {
if !ref.Name().IsTag() {
continue
}
tag := ref.Name().Short()
version, err := semver.NewVersion(tag)
if err != nil {
log.Debugf("Error parsing version for tag: '%s': %v", tag, err)
// Skip this tag and continue to the next one
continue
}
if constraint.Check(version) {
if version.GreaterThan(maxVersion) {
maxVersion = version
maxVersionHash = ref.Hash()
}
}
}
if maxVersionHash.IsZero() {
return ""
}
log.Debugf("Semver constraint '%s' resolved to tag '%s', at reference '%s'", revision, maxVersion.Original(), maxVersionHash.String())
return maxVersionHash.String()
}
// CommitSHA returns current commit sha from `git rev-parse HEAD`
func (m *nativeGitClient) CommitSHA() (string, error) {
out, err := m.runCmd("rev-parse", "HEAD")
if err != nil {
return "", err
}
return strings.TrimSpace(out), nil
}
// returns the meta-data for the commit
func (m *nativeGitClient) RevisionMetadata(revision string) (*RevisionMetadata, error) {
out, err := m.runCmd("show", "-s", "--format=%an <%ae>%n%at%n%B", revision)
if err != nil {
return nil, err
}
segments := strings.SplitN(out, "\n", 3)
if len(segments) != 3 {
return nil, fmt.Errorf("expected 3 segments, got %v", segments)
}
author := segments[0]
authorDateUnixTimestamp, _ := strconv.ParseInt(segments[1], 10, 64)
message := strings.TrimSpace(segments[2])
out, err = m.runCmd("tag", "--points-at", revision)
if err != nil {
return nil, err
}
tags := strings.Fields(out)
return &RevisionMetadata{author, time.Unix(authorDateUnixTimestamp, 0), tags, message}, nil
}
// VerifyCommitSignature Runs verify-commit on a given revision and returns the output
func (m *nativeGitClient) VerifyCommitSignature(revision string) (string, error) {
out, err := m.runGnuPGWrapper("git-verify-wrapper.sh", revision)
if err != nil {
log.Errorf("error verifying commit signature: %v", err)
return "", fmt.Errorf("permission denied")
}
return out, nil
}
// IsAnnotatedTag returns true if the revision points to an annotated tag
func (m *nativeGitClient) IsAnnotatedTag(revision string) bool {
cmd := exec.Command("git", "describe", "--exact-match", revision)
out, err := m.runCmdOutput(cmd, runOpts{SkipErrorLogging: true})
if out != "" && err == nil {
return true
} else {
return false
}
}
// ChangedFiles returns a list of files changed between two revisions
func (m *nativeGitClient) ChangedFiles(revision string, targetRevision string) ([]string, error) {
if revision == targetRevision {
return []string{}, nil
}
if !IsCommitSHA(revision) || !IsCommitSHA(targetRevision) {
return []string{}, fmt.Errorf("invalid revision provided, must be SHA")
}
out, err := m.runCmd("diff", "--name-only", fmt.Sprintf("%s..%s", revision, targetRevision))
if err != nil {
return nil, fmt.Errorf("failed to diff %s..%s: %w", revision, targetRevision, err)
}
if out == "" {
return []string{}, nil
}
files := strings.Split(out, "\n")
return files, nil
}
// config runs a git config command.
func (m *nativeGitClient) config(args ...string) (string, error) {
args = append([]string{"config"}, args...)
out, err := m.runCmd(args...)
if err != nil {
return out, fmt.Errorf("failed to run git config: %w", err)
}
return out, nil
}
// SetAuthor sets the author name and email in the git configuration.
func (m *nativeGitClient) SetAuthor(name, email string) (string, error) {
if name != "" {
out, err := m.config("--local", "user.name", name)
if err != nil {
return out, err
}
}
if email != "" {
out, err := m.config("--local", "user.email", email)
if err != nil {
return out, err
}
}
return "", nil
}
// CheckoutOrOrphan checks out the branch. If the branch does not exist, it creates an orphan branch.
func (m *nativeGitClient) CheckoutOrOrphan(branch string, submoduleEnabled bool) (string, error) {
out, err := m.Checkout(branch, submoduleEnabled)
if err != nil {
// If the branch doesn't exist, create it as an orphan branch.
if strings.Contains(err.Error(), "did not match any file(s) known to git") {
out, err = m.runCmd("switch", "--orphan", branch)
if err != nil {
return out, fmt.Errorf("failed to create orphan branch: %w", err)
}
} else {
return out, fmt.Errorf("failed to checkout branch: %w", err)
}
// Make an empty initial commit.
out, err = m.runCmd("commit", "--allow-empty", "-m", "Initial commit")
if err != nil {
return out, fmt.Errorf("failed to commit initial commit: %w", err)
}
// Push the commit.
err = m.runCredentialedCmd("push", "origin", branch)
if err != nil {
return "", fmt.Errorf("failed to push to branch: %w", err)
}
}
return "", nil
}
// CheckoutOrNew checks out the given branch. If the branch does not exist, it creates an empty branch based on
// the base branch.
func (m *nativeGitClient) CheckoutOrNew(branch, base string, submoduleEnabled bool) (string, error) {
out, err := m.Checkout(branch, submoduleEnabled)
if err != nil {
if strings.Contains(err.Error(), "did not match any file(s) known to git") {
// If the branch does not exist, create any empty branch based on the sync branch
// First, checkout the sync branch.
out, err = m.Checkout(base, submoduleEnabled)
if err != nil {
return out, fmt.Errorf("failed to checkout sync branch: %w", err)
}
out, err = m.runCmd("checkout", "-b", branch)
if err != nil {
return out, fmt.Errorf("failed to create branch: %w", err)
}
} else {
return out, fmt.Errorf("failed to checkout branch: %w", err)
}
}
return "", nil
}
// RemoveContents removes all files from the git repository.
func (m *nativeGitClient) RemoveContents() (string, error) {
out, err := m.runCmd("rm", "-r", "--ignore-unmatch", ".")
if err != nil {
return out, fmt.Errorf("failed to clear repo contents: %w", err)
}
return "", nil
}
// CommitAndPush commits and pushes changes to the target branch.
func (m *nativeGitClient) CommitAndPush(branch, message string) (string, error) {
out, err := m.runCmd("add", ".")
if err != nil {
return out, fmt.Errorf("failed to add files: %w", err)
}
out, err = m.runCmd("commit", "-m", message)
if err != nil {
if strings.Contains(out, "nothing to commit, working tree clean") {
return out, nil
}
return out, fmt.Errorf("failed to commit: %w", err)
}
if m.OnPush != nil {
done := m.OnPush(m.repoURL)
defer done()
}
err = m.runCredentialedCmd("push", "origin", branch)
if err != nil {
return "", fmt.Errorf("failed to push: %w", err)
}
return "", nil
}
// runWrapper runs a custom command with all the semantics of running the Git client
func (m *nativeGitClient) runGnuPGWrapper(wrapper string, args ...string) (string, error) {
cmd := exec.Command(wrapper, args...)
cmd.Env = append(cmd.Env, fmt.Sprintf("GNUPGHOME=%s", common.GetGnuPGHomePath()), "LANG=C")
return m.runCmdOutput(cmd, runOpts{})
}
// runCmd is a convenience function to run a command in a given directory and return its output
func (m *nativeGitClient) runCmd(args ...string) (string, error) {
cmd := exec.Command("git", args...)
return m.runCmdOutput(cmd, runOpts{})
}
// runCredentialedCmd is a convenience function to run a git command with username/password credentials
// nolint:unparam
func (m *nativeGitClient) runCredentialedCmd(args ...string) error {
closer, environ, err := m.creds.Environ()
if err != nil {
return err
}
defer func() { _ = closer.Close() }()
// If a basic auth header is explicitly set, tell Git to send it to the
// server to force use of basic auth instead of negotiating the auth scheme
for _, e := range environ {
if strings.HasPrefix(e, fmt.Sprintf("%s=", forceBasicAuthHeaderEnv)) {
args = append([]string{"--config-env", fmt.Sprintf("http.extraHeader=%s", forceBasicAuthHeaderEnv)}, args...)
}
}
cmd := exec.Command("git", args...)
cmd.Env = append(cmd.Env, environ...)
_, err = m.runCmdOutput(cmd, runOpts{})
return err
}
func (m *nativeGitClient) runCmdOutput(cmd *exec.Cmd, ropts runOpts) (string, error) {
cmd.Dir = m.root
cmd.Env = append(os.Environ(), cmd.Env...)
// Set $HOME to nowhere, so we can execute Git regardless of any external
// authentication keys (e.g. in ~/.ssh) -- this is especially important for
// running tests on local machines and/or CircleCI.
cmd.Env = append(cmd.Env, "HOME=/dev/null")
// Skip LFS for most Git operations except when explicitly requested
cmd.Env = append(cmd.Env, "GIT_LFS_SKIP_SMUDGE=1")
// Disable Git terminal prompts in case we're running with a tty
cmd.Env = append(cmd.Env, "GIT_TERMINAL_PROMPT=false")
// For HTTPS repositories, we need to consider insecure repositories as well
// as custom CA bundles from the cert database.
if IsHTTPSURL(m.repoURL) {
if m.insecure {
cmd.Env = append(cmd.Env, "GIT_SSL_NO_VERIFY=true")
} else {
parsedURL, err := url.Parse(m.repoURL)
// We don't fail if we cannot parse the URL, but log a warning in that
// case. And we execute the command in a verbatim way.
if err != nil {