@@ -69,8 +69,9 @@ type settings struct {
69
69
var flagValues settings
70
70
71
71
var (
72
- sshGitURLRegEx = regexp .MustCompile (`^(git@|ssh:\/\/).+$` )
73
- commitShaRegEx = regexp .MustCompile (`^[0-9a-f]{7,40}$` )
72
+ sshGitURLRegEx = regexp .MustCompile (`^(git@|ssh:\/\/).+$` )
73
+ fullCommitShaRegex = regexp .MustCompile (`^[0-9a-f]{40}$` )
74
+ shortCommitShaRegEx = regexp .MustCompile (`^[0-9a-f]{7,39}$` )
74
75
)
75
76
76
77
func init () {
@@ -273,10 +274,19 @@ func clone(ctx context.Context) error {
273
274
cloneArgs = append (cloneArgs , "--no-tags" )
274
275
}
275
276
276
- var commitSha string
277
+ var shortCommitSha string
277
278
switch {
278
- case commitShaRegEx .MatchString (flagValues .revision ):
279
- commitSha = flagValues .revision
279
+ case fullCommitShaRegex .MatchString (flagValues .revision ):
280
+ // we can pass the commit SHA directly to `git clone --revision` and can honor the depth.
281
+ cloneArgs = append (cloneArgs , "--revision" , flagValues .revision )
282
+
283
+ if flagValues .depth > 0 {
284
+ cloneArgs = append (cloneArgs , "--depth" , fmt .Sprintf ("%d" , flagValues .depth ))
285
+ }
286
+
287
+ case shortCommitShaRegEx .MatchString (flagValues .revision ):
288
+ // for a short commit SHA we must do a full `git clone --no-checkout` and later a checkout. For this, we cannot honor depth.
289
+ shortCommitSha = flagValues .revision
280
290
cloneArgs = append (cloneArgs , "--no-checkout" )
281
291
282
292
default :
@@ -417,8 +427,8 @@ func clone(ctx context.Context) error {
417
427
return err
418
428
}
419
429
420
- if commitSha != "" {
421
- if _ , err := git (ctx , "-C" , flagValues .target , "checkout" , commitSha ); err != nil {
430
+ if shortCommitSha != "" {
431
+ if _ , err := git (ctx , "-C" , flagValues .target , "checkout" , shortCommitSha ); err != nil {
422
432
return err
423
433
}
424
434
}
0 commit comments