diff --git a/cmd/git/main.go b/cmd/git/main.go index c1d0e6851b..ed93210442 100644 --- a/cmd/git/main.go +++ b/cmd/git/main.go @@ -93,7 +93,8 @@ func init() { // Optional flag to be able to override the default shallow clone depth, // which should be fine for almost all use cases we use the Git source step // for (in the context of Shipwright build). - pflag.UintVar(&flagValues.depth, "depth", 1, "Create a shallow clone based on the given depth") + // Setting depth to 0 means no shallow clone (full clone) + pflag.UintVar(&flagValues.depth, "depth", 0, "Create a shallow clone with the given depth. 0 means full clone (no shallow).") // Mostly internal flag pflag.BoolVar(&flagValues.skipValidation, "skip-validation", false, "skip pre-requisite validation") @@ -120,7 +121,7 @@ func main() { // Execute performs flag parsing, input validation and the Git clone func Execute(ctx context.Context) error { - flagValues = settings{depth: 1} + flagValues = settings{depth: 0} pflag.Parse() if flagValues.help { @@ -266,6 +267,7 @@ func clone(ctx context.Context) error { cloneArgs = append(cloneArgs, "--branch", flagValues.revision) } + // Only add depth if it's greater than 0 (meaning shallow clone is requested) if flagValues.depth > 0 { cloneArgs = append(cloneArgs, "--depth", fmt.Sprintf("%d", flagValues.depth)) } diff --git a/deploy/crds/shipwright.io_builds.yaml b/deploy/crds/shipwright.io_builds.yaml index 4d06c45769..2fbbc131e0 100644 --- a/deploy/crds/shipwright.io_builds.yaml +++ b/deploy/crds/shipwright.io_builds.yaml @@ -469,6 +469,13 @@ spec: url: description: URL describes the URL of the Git repository. type: string + shallowCloneDepth: + description: |- + The depth of the shallow clone. If not specified or set to 0, + a full clone will be performed. Values greater than 0 will + create a shallow clone with the specified depth. + type: integer + minimum: 0 type: object sources: description: |- @@ -2734,6 +2741,13 @@ spec: url: description: URL describes the URL of the Git repository. type: string + shallowCloneDepth: + description: |- + The depth of the shallow clone. If not specified or set to 0, + a full clone will be performed. Values greater than 0 will + create a shallow clone with the specified depth. + type: integer + minimum: 0 required: - url type: object diff --git a/docs/build.md b/docs/build.md index c0d685584c..7a4646e49b 100644 --- a/docs/build.md +++ b/docs/build.md @@ -135,6 +135,7 @@ A `Build` resource can specify a source type, such as a Git repository or an OCI - `source.git.url` - Specify the source location using a Git repository. - `source.git.cloneSecret` - For private repositories or registries, the name references a secret in the namespace that contains the SSH private key or Docker access credentials, respectively. - `source.git.revision` - A specific revision to select from the source repository, this can be a commit, tag or branch name. If not defined, it will fall back to the Git repository default branch. +- `source.git.shallowCloneDepth` - The depth of the shallow clone. If not specified or set to 0, a full clone will be performed. Values greater than 0 will create a shallow clone with the specified depth. - `source.contextDir` - For repositories where the source code is not located at the root folder, you can specify this path here. By default, the Build controller does not validate that the Git repository exists. If the validation is desired, users can explicitly define the `build.shipwright.io/verify.repository` annotation with `true`. For example: diff --git a/pkg/apis/build/v1beta1/build_conversion.go b/pkg/apis/build/v1beta1/build_conversion.go index 612923942a..3e0da9b3a9 100644 --- a/pkg/apis/build/v1beta1/build_conversion.go +++ b/pkg/apis/build/v1beta1/build_conversion.go @@ -446,7 +446,6 @@ func getAlphaBuildSource(src BuildSpec) v1alpha1.Source { source.URL = &src.Source.Git.URL revision = src.Source.Git.Revision } - } if credentials.Name != "" { diff --git a/pkg/apis/build/v1beta1/source.go b/pkg/apis/build/v1beta1/source.go index 8187d532a9..5358824e3c 100644 --- a/pkg/apis/build/v1beta1/source.go +++ b/pkg/apis/build/v1beta1/source.go @@ -59,6 +59,13 @@ type Git struct { // // +optional CloneSecret *string `json:"cloneSecret,omitempty"` + + // ShallowCloneDepth specifies the depth of the shallow clone. + // If not specified or set to 0, a full clone will be performed. + // Values greater than 0 will create a shallow clone with the specified depth. + // + // +optional + ShallowCloneDepth *int `json:"shallowCloneDepth,omitempty"` } // OCIArtifact describes the source code bundle container to pull diff --git a/pkg/reconciler/buildrun/resources/sources/git.go b/pkg/reconciler/buildrun/resources/sources/git.go index 67c91b7ccc..ef7940fd59 100644 --- a/pkg/reconciler/buildrun/resources/sources/git.go +++ b/pkg/reconciler/buildrun/resources/sources/git.go @@ -79,6 +79,15 @@ func AppendGitStep( ) } + // Check if shallow clone is requested + if source.ShallowCloneDepth != nil && *source.ShallowCloneDepth > 0 { + gitStep.Args = append( + gitStep.Args, + "--depth", + fmt.Sprintf("%d", *source.ShallowCloneDepth), + ) + } + // If configure, use Git URL rewrite flag if cfg.GitRewriteRule { gitStep.Args = append(gitStep.Args, "--git-url-rewrite")