Skip to content

Commit 3f53e14

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

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

cmd/git/main.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ type settings struct {
6969
var flagValues settings
7070

7171
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}$`)
7475
)
7576

7677
func init() {
@@ -273,10 +274,19 @@ func clone(ctx context.Context) error {
273274
cloneArgs = append(cloneArgs, "--no-tags")
274275
}
275276

276-
var commitSha string
277+
var shortCommitSha string
277278
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
280290
cloneArgs = append(cloneArgs, "--no-checkout")
281291

282292
default:
@@ -417,8 +427,8 @@ func clone(ctx context.Context) error {
417427
return err
418428
}
419429

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 {
422432
return err
423433
}
424434
}

0 commit comments

Comments
 (0)