Skip to content

Commit ac3eb58

Browse files
committed
git: export gitutil helper for identifying commit shas
Signed-off-by: Justin Chadwell <[email protected]>
1 parent f835dd4 commit ac3eb58

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

source/git/source.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
"google.golang.org/grpc/status"
3737
)
3838

39-
var validHex = regexp.MustCompile(`^[a-f0-9]{40}$`)
4039
var defaultBranch = regexp.MustCompile(`refs/heads/(\S+)`)
4140

4241
type Opt struct {
@@ -341,7 +340,7 @@ func (gs *gitSourceHandler) CacheKey(ctx context.Context, g session.Group, index
341340
gs.locker.Lock(remote)
342341
defer gs.locker.Unlock(remote)
343342

344-
if ref := gs.src.Ref; ref != "" && isCommitSHA(ref) {
343+
if ref := gs.src.Ref; ref != "" && gitutil.IsCommitSHA(ref) {
345344
cacheKey := gs.shaToCacheKey(ref)
346345
gs.cacheKey = cacheKey
347346
return cacheKey, ref, nil, true, nil
@@ -400,7 +399,7 @@ func (gs *gitSourceHandler) CacheKey(ctx context.Context, g session.Group, index
400399
if sha == "" {
401400
return "", "", nil, false, errors.Errorf("repository does not contain ref %s, output: %q", ref, string(buf))
402401
}
403-
if !isCommitSHA(sha) {
402+
if !gitutil.IsCommitSHA(sha) {
404403
return "", "", nil, false, errors.Errorf("invalid commit sha %q", sha)
405404
}
406405

@@ -455,7 +454,7 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
455454
}
456455

457456
doFetch := true
458-
if isCommitSHA(ref) {
457+
if gitutil.IsCommitSHA(ref) {
459458
// skip fetch if commit already exists
460459
if _, err := git.Run(ctx, "cat-file", "-e", ref+"^{commit}"); err == nil {
461460
doFetch = false
@@ -467,7 +466,7 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
467466
os.RemoveAll(filepath.Join(gitDir, "shallow.lock"))
468467

469468
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?
471470
args = append(args, "--depth=1", "--no-tags")
472471
} else {
473472
args = append(args, "--tags")
@@ -476,11 +475,11 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
476475
}
477476
}
478477
args = append(args, "origin")
479-
if !isCommitSHA(ref) {
480-
args = append(args, "--force", ref+":tags/"+ref)
478+
if !gitutil.IsCommitSHA(ref) {
481479
// local refs are needed so they would be advertised on next fetches. Force is used
482480
// in case the ref is a branch and it now points to a different commit sha
483481
// TODO: is there a better way to do this?
482+
args = append(args, "--force", ref+":tags/"+ref)
484483
}
485484
if _, err := git.Run(ctx, args...); err != nil {
486485
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
549548
pullref := ref
550549
if isAnnotatedTag {
551550
pullref += ":refs/tags/" + pullref
552-
} else if isCommitSHA(ref) {
551+
} else if gitutil.IsCommitSHA(ref) {
553552
pullref = "refs/buildkit/" + identity.NewID()
554553
_, err = git.Run(ctx, "update-ref", pullref, ref)
555554
if err != nil {
@@ -710,10 +709,6 @@ func (gs *gitSourceHandler) gitCli(ctx context.Context, g session.Group, opts ..
710709
return gitCLI(opts...), cleanup, err
711710
}
712711

713-
func isCommitSHA(str string) bool {
714-
return validHex.MatchString(str)
715-
}
716-
717712
func tokenScope(remote string) string {
718713
// generally we can only use the token for fetching main remote but in case of github.com we do best effort
719714
// to try reuse same token for all github.com remotes. This is the same behavior actions/checkout uses

util/gitutil/git_commit.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package gitutil
2+
3+
import "regexp"
4+
5+
var validHex = regexp.MustCompile(`^[a-f0-9]{40}$`)
6+
7+
func IsCommitSHA(str string) bool {
8+
return validHex.MatchString(str)
9+
}

0 commit comments

Comments
 (0)