@@ -36,7 +36,6 @@ import (
36
36
"google.golang.org/grpc/status"
37
37
)
38
38
39
- var validHex = regexp .MustCompile (`^[a-f0-9]{40}$` )
40
39
var defaultBranch = regexp .MustCompile (`refs/heads/(\S+)` )
41
40
42
41
type Opt struct {
@@ -341,7 +340,7 @@ func (gs *gitSourceHandler) CacheKey(ctx context.Context, g session.Group, index
341
340
gs .locker .Lock (remote )
342
341
defer gs .locker .Unlock (remote )
343
342
344
- if ref := gs .src .Ref ; ref != "" && isCommitSHA (ref ) {
343
+ if ref := gs .src .Ref ; ref != "" && gitutil . IsCommitSHA (ref ) {
345
344
cacheKey := gs .shaToCacheKey (ref )
346
345
gs .cacheKey = cacheKey
347
346
return cacheKey , ref , nil , true , nil
@@ -400,7 +399,7 @@ func (gs *gitSourceHandler) CacheKey(ctx context.Context, g session.Group, index
400
399
if sha == "" {
401
400
return "" , "" , nil , false , errors .Errorf ("repository does not contain ref %s, output: %q" , ref , string (buf ))
402
401
}
403
- if ! isCommitSHA (sha ) {
402
+ if ! gitutil . IsCommitSHA (sha ) {
404
403
return "" , "" , nil , false , errors .Errorf ("invalid commit sha %q" , sha )
405
404
}
406
405
@@ -455,7 +454,7 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
455
454
}
456
455
457
456
doFetch := true
458
- if isCommitSHA (ref ) {
457
+ if gitutil . IsCommitSHA (ref ) {
459
458
// skip fetch if commit already exists
460
459
if _ , err := git .Run (ctx , "cat-file" , "-e" , ref + "^{commit}" ); err == nil {
461
460
doFetch = false
@@ -467,7 +466,7 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
467
466
os .RemoveAll (filepath .Join (gitDir , "shallow.lock" ))
468
467
469
468
args := []string {"fetch" }
470
- if ! isCommitSHA (ref ) { // TODO: find a branch from ls-remote?
469
+ if ! gitutil . IsCommitSHA (ref ) { // TODO: find a branch from ls-remote?
471
470
args = append (args , "--depth=1" , "--no-tags" )
472
471
} else {
473
472
args = append (args , "--tags" )
@@ -476,11 +475,11 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
476
475
}
477
476
}
478
477
args = append (args , "origin" )
479
- if ! isCommitSHA (ref ) {
480
- args = append (args , "--force" , ref + ":tags/" + ref )
478
+ if ! gitutil .IsCommitSHA (ref ) {
481
479
// local refs are needed so they would be advertised on next fetches. Force is used
482
480
// in case the ref is a branch and it now points to a different commit sha
483
481
// TODO: is there a better way to do this?
482
+ args = append (args , "--force" , ref + ":tags/" + ref )
484
483
}
485
484
if _ , err := git .Run (ctx , args ... ); err != nil {
486
485
return nil , errors .Wrapf (err , "failed to fetch remote %s" , urlutil .RedactCredentials (gs .src .Remote ))
@@ -549,7 +548,7 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
549
548
pullref := ref
550
549
if isAnnotatedTag {
551
550
pullref += ":refs/tags/" + pullref
552
- } else if isCommitSHA (ref ) {
551
+ } else if gitutil . IsCommitSHA (ref ) {
553
552
pullref = "refs/buildkit/" + identity .NewID ()
554
553
_ , err = git .Run (ctx , "update-ref" , pullref , ref )
555
554
if err != nil {
@@ -710,10 +709,6 @@ func (gs *gitSourceHandler) gitCli(ctx context.Context, g session.Group, opts ..
710
709
return gitCLI (opts ... ), cleanup , err
711
710
}
712
711
713
- func isCommitSHA (str string ) bool {
714
- return validHex .MatchString (str )
715
- }
716
-
717
712
func tokenScope (remote string ) string {
718
713
// generally we can only use the token for fetching main remote but in case of github.com we do best effort
719
714
// to try reuse same token for all github.com remotes. This is the same behavior actions/checkout uses
0 commit comments