From 00024dd5917cb01b02978fe57f477da3c6848fd3 Mon Sep 17 00:00:00 2001 From: Sascha Schwarze Date: Sun, 11 May 2025 11:17:21 +0200 Subject: [PATCH] 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 --- cmd/git/main.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/cmd/git/main.go b/cmd/git/main.go index 75195d3392..50038b10a3 100644 --- a/cmd/git/main.go +++ b/cmd/git/main.go @@ -29,10 +29,13 @@ const ( typeUsernamePassword ) -var useNoTagsFlag = false -var useDepthForSubmodule = false +var ( + useNoTagsFlag = false + useRevisionFlag = false + useDepthForSubmodule = false -var displayURL string + displayURL string +) // ExitError is an error which has an exit code to be used in os.Exit() to // return both an exit code and an error message @@ -69,8 +72,9 @@ type settings struct { var flagValues settings var ( - sshGitURLRegEx = regexp.MustCompile(`^(git@|ssh:\/\/).+$`) - commitShaRegEx = regexp.MustCompile(`^[0-9a-f]{7,40}$`) + sshGitURLRegEx = regexp.MustCompile(`^(git@|ssh:\/\/).+$`) + fullCommitShaRegex = regexp.MustCompile(`^[0-9a-f]{40}$`) + commitShaRegEx = regexp.MustCompile(`^[0-9a-f]{7,40}$`) ) func init() { @@ -141,9 +145,10 @@ func Execute(ctx context.Context) error { return err } - // Check if Git CLI supports --no-tags for clone + // Check if Git CLI supports --no-tags and --revision for clone out, _ := git(ctx, "clone", "-h") useNoTagsFlag = strings.Contains(out, "--no-tags") + useRevisionFlag = strings.Contains(out, "--revision") // Check if Git CLI support --single-branch and therefore shallow clones using --depth out, _ = git(ctx, "submodule", "-h") @@ -275,7 +280,16 @@ func clone(ctx context.Context) error { var commitSha string switch { + case useRevisionFlag && fullCommitShaRegex.MatchString(flagValues.revision): + // we can pass the commit SHA directly to `git clone --revision` and can honor the depth. + cloneArgs = append(cloneArgs, "--revision", flagValues.revision) + + if flagValues.depth > 0 { + cloneArgs = append(cloneArgs, "--depth", fmt.Sprintf("%d", flagValues.depth)) + } + case commitShaRegEx.MatchString(flagValues.revision): + // 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. commitSha = flagValues.revision cloneArgs = append(cloneArgs, "--no-checkout")