@@ -29,10 +29,13 @@ const (
29
29
typeUsernamePassword
30
30
)
31
31
32
- var useNoTagsFlag = false
33
- var useDepthForSubmodule = false
32
+ var (
33
+ useNoTagsFlag = false
34
+ useRevisionFlag = false
35
+ useDepthForSubmodule = false
34
36
35
- var displayURL string
37
+ displayURL string
38
+ )
36
39
37
40
// ExitError is an error which has an exit code to be used in os.Exit() to
38
41
// return both an exit code and an error message
@@ -69,8 +72,9 @@ type settings struct {
69
72
var flagValues settings
70
73
71
74
var (
72
- sshGitURLRegEx = regexp .MustCompile (`^(git@|ssh:\/\/).+$` )
73
- commitShaRegEx = regexp .MustCompile (`^[0-9a-f]{7,40}$` )
75
+ sshGitURLRegEx = regexp .MustCompile (`^(git@|ssh:\/\/).+$` )
76
+ fullCommitShaRegex = regexp .MustCompile (`^[0-9a-f]{40}$` )
77
+ commitShaRegEx = regexp .MustCompile (`^[0-9a-f]{7,40}$` )
74
78
)
75
79
76
80
func init () {
@@ -141,9 +145,10 @@ func Execute(ctx context.Context) error {
141
145
return err
142
146
}
143
147
144
- // Check if Git CLI supports --no-tags for clone
148
+ // Check if Git CLI supports --no-tags and --revision for clone
145
149
out , _ := git (ctx , "clone" , "-h" )
146
150
useNoTagsFlag = strings .Contains (out , "--no-tags" )
151
+ useRevisionFlag = strings .Contains (out , "--revision" )
147
152
148
153
// Check if Git CLI support --single-branch and therefore shallow clones using --depth
149
154
out , _ = git (ctx , "submodule" , "-h" )
@@ -275,7 +280,16 @@ func clone(ctx context.Context) error {
275
280
276
281
var commitSha string
277
282
switch {
283
+ case useRevisionFlag && fullCommitShaRegex .MatchString (flagValues .revision ):
284
+ // we can pass the commit SHA directly to `git clone --revision` and can honor the depth.
285
+ cloneArgs = append (cloneArgs , "--revision" , flagValues .revision )
286
+
287
+ if flagValues .depth > 0 {
288
+ cloneArgs = append (cloneArgs , "--depth" , fmt .Sprintf ("%d" , flagValues .depth ))
289
+ }
290
+
278
291
case commitShaRegEx .MatchString (flagValues .revision ):
292
+ // for a short commit SHA or when --revision is not supported, we must do a full `git clone --no-checkout` and later a checkout. For this, we cannot honor depth.
279
293
commitSha = flagValues .revision
280
294
cloneArgs = append (cloneArgs , "--no-checkout" )
281
295
0 commit comments