Skip to content

Commit 9e00da9

Browse files
When a full commit SHA is provided as revision and when git clone supports --revision, provide it with the git clone command and honor the depth
Signed-off-by: Sascha Schwarze <[email protected]>
1 parent b8c72f7 commit 9e00da9

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

cmd/git/main.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ const (
2929
typeUsernamePassword
3030
)
3131

32-
var useNoTagsFlag = false
33-
var useDepthForSubmodule = false
32+
var (
33+
useNoTagsFlag = false
34+
useRevisionFlag = false
35+
useDepthForSubmodule = false
3436

35-
var displayURL string
37+
displayURL string
38+
)
3639

3740
// ExitError is an error which has an exit code to be used in os.Exit() to
3841
// return both an exit code and an error message
@@ -69,8 +72,9 @@ type settings struct {
6972
var flagValues settings
7073

7174
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}$`)
7478
)
7579

7680
func init() {
@@ -141,9 +145,10 @@ func Execute(ctx context.Context) error {
141145
return err
142146
}
143147

144-
// Check if Git CLI supports --no-tags for clone
148+
// Check if Git CLI supports --no-tags and --revision for clone
145149
out, _ := git(ctx, "clone", "-h")
146150
useNoTagsFlag = strings.Contains(out, "--no-tags")
151+
useRevisionFlag = strings.Contains(out, "--revision")
147152

148153
// Check if Git CLI support --single-branch and therefore shallow clones using --depth
149154
out, _ = git(ctx, "submodule", "-h")
@@ -275,7 +280,16 @@ func clone(ctx context.Context) error {
275280

276281
var commitSha string
277282
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+
278291
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.
279293
commitSha = flagValues.revision
280294
cloneArgs = append(cloneArgs, "--no-checkout")
281295

0 commit comments

Comments
 (0)