1
0
mirror of https://github.com/moby/buildkit.git synced 2025-08-02 13:26:49 +03:00

git: export gitutil helper for identifying commit shas

Signed-off-by: Justin Chadwell <me@jedevc.com>
This commit is contained in:
Justin Chadwell
2024-10-21 14:19:54 +01:00
parent f835dd4f7a
commit ac3eb58262
2 changed files with 16 additions and 12 deletions

View File

@ -36,7 +36,6 @@ import (
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
var validHex = regexp.MustCompile(`^[a-f0-9]{40}$`)
var defaultBranch = regexp.MustCompile(`refs/heads/(\S+)`) var defaultBranch = regexp.MustCompile(`refs/heads/(\S+)`)
type Opt struct { type Opt struct {
@ -341,7 +340,7 @@ func (gs *gitSourceHandler) CacheKey(ctx context.Context, g session.Group, index
gs.locker.Lock(remote) gs.locker.Lock(remote)
defer gs.locker.Unlock(remote) defer gs.locker.Unlock(remote)
if ref := gs.src.Ref; ref != "" && isCommitSHA(ref) { if ref := gs.src.Ref; ref != "" && gitutil.IsCommitSHA(ref) {
cacheKey := gs.shaToCacheKey(ref) cacheKey := gs.shaToCacheKey(ref)
gs.cacheKey = cacheKey gs.cacheKey = cacheKey
return cacheKey, ref, nil, true, nil return cacheKey, ref, nil, true, nil
@ -400,7 +399,7 @@ func (gs *gitSourceHandler) CacheKey(ctx context.Context, g session.Group, index
if sha == "" { if sha == "" {
return "", "", nil, false, errors.Errorf("repository does not contain ref %s, output: %q", ref, string(buf)) return "", "", nil, false, errors.Errorf("repository does not contain ref %s, output: %q", ref, string(buf))
} }
if !isCommitSHA(sha) { if !gitutil.IsCommitSHA(sha) {
return "", "", nil, false, errors.Errorf("invalid commit sha %q", sha) return "", "", nil, false, errors.Errorf("invalid commit sha %q", sha)
} }
@ -455,7 +454,7 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
} }
doFetch := true doFetch := true
if isCommitSHA(ref) { if gitutil.IsCommitSHA(ref) {
// skip fetch if commit already exists // skip fetch if commit already exists
if _, err := git.Run(ctx, "cat-file", "-e", ref+"^{commit}"); err == nil { if _, err := git.Run(ctx, "cat-file", "-e", ref+"^{commit}"); err == nil {
doFetch = false doFetch = false
@ -467,7 +466,7 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
os.RemoveAll(filepath.Join(gitDir, "shallow.lock")) os.RemoveAll(filepath.Join(gitDir, "shallow.lock"))
args := []string{"fetch"} args := []string{"fetch"}
if !isCommitSHA(ref) { // TODO: find a branch from ls-remote? if !gitutil.IsCommitSHA(ref) { // TODO: find a branch from ls-remote?
args = append(args, "--depth=1", "--no-tags") args = append(args, "--depth=1", "--no-tags")
} else { } else {
args = append(args, "--tags") args = append(args, "--tags")
@ -476,11 +475,11 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
} }
} }
args = append(args, "origin") args = append(args, "origin")
if !isCommitSHA(ref) { if !gitutil.IsCommitSHA(ref) {
args = append(args, "--force", ref+":tags/"+ref)
// local refs are needed so they would be advertised on next fetches. Force is used // local refs are needed so they would be advertised on next fetches. Force is used
// in case the ref is a branch and it now points to a different commit sha // in case the ref is a branch and it now points to a different commit sha
// TODO: is there a better way to do this? // TODO: is there a better way to do this?
args = append(args, "--force", ref+":tags/"+ref)
} }
if _, err := git.Run(ctx, args...); err != nil { if _, err := git.Run(ctx, args...); err != nil {
return nil, errors.Wrapf(err, "failed to fetch remote %s", urlutil.RedactCredentials(gs.src.Remote)) 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
pullref := ref pullref := ref
if isAnnotatedTag { if isAnnotatedTag {
pullref += ":refs/tags/" + pullref pullref += ":refs/tags/" + pullref
} else if isCommitSHA(ref) { } else if gitutil.IsCommitSHA(ref) {
pullref = "refs/buildkit/" + identity.NewID() pullref = "refs/buildkit/" + identity.NewID()
_, err = git.Run(ctx, "update-ref", pullref, ref) _, err = git.Run(ctx, "update-ref", pullref, ref)
if err != nil { if err != nil {
@ -710,10 +709,6 @@ func (gs *gitSourceHandler) gitCli(ctx context.Context, g session.Group, opts ..
return gitCLI(opts...), cleanup, err return gitCLI(opts...), cleanup, err
} }
func isCommitSHA(str string) bool {
return validHex.MatchString(str)
}
func tokenScope(remote string) string { func tokenScope(remote string) string {
// generally we can only use the token for fetching main remote but in case of github.com we do best effort // generally we can only use the token for fetching main remote but in case of github.com we do best effort
// to try reuse same token for all github.com remotes. This is the same behavior actions/checkout uses // to try reuse same token for all github.com remotes. This is the same behavior actions/checkout uses

View File

@ -0,0 +1,9 @@
package gitutil
import "regexp"
var validHex = regexp.MustCompile(`^[a-f0-9]{40}$`)
func IsCommitSHA(str string) bool {
return validHex.MatchString(str)
}