Skip to content

Commit 6237b27

Browse files
initializing shallow cloning logic and CRD change
1 parent b8c72f7 commit 6237b27

File tree

6 files changed

+35
-3
lines changed

6 files changed

+35
-3
lines changed

cmd/git/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ func init() {
9696
// Optional flag to be able to override the default shallow clone depth,
9797
// which should be fine for almost all use cases we use the Git source step
9898
// for (in the context of Shipwright build).
99-
pflag.UintVar(&flagValues.depth, "depth", 1, "Create a shallow clone based on the given depth")
99+
// Setting depth to 0 means no shallow clone (full clone)
100+
pflag.UintVar(&flagValues.depth, "depth", 0, "Create a shallow clone with the given depth. 0 means full clone (no shallow).")
100101

101102
// Mostly internal flag
102103
pflag.BoolVar(&flagValues.skipValidation, "skip-validation", false, "skip pre-requisite validation")
@@ -124,7 +125,7 @@ func main() {
124125

125126
// Execute performs flag parsing, input validation and the Git clone
126127
func Execute(ctx context.Context) error {
127-
flagValues = settings{depth: 1}
128+
flagValues = settings{depth: 0}
128129
pflag.Parse()
129130

130131
if val, ok := os.LookupEnv("GIT_SHOW_LISTING"); ok {
@@ -286,6 +287,7 @@ func clone(ctx context.Context) error {
286287
cloneArgs = append(cloneArgs, "--branch", flagValues.revision)
287288
}
288289

290+
// Only add depth if it's greater than 0 (meaning shallow clone is requested)
289291
if flagValues.depth > 0 {
290292
cloneArgs = append(cloneArgs, "--depth", fmt.Sprintf("%d", flagValues.depth))
291293
}

deploy/crds/shipwright.io_builds.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,13 @@ spec:
494494
url:
495495
description: URL describes the URL of the Git repository.
496496
type: string
497+
shallowCloneDepth:
498+
description: |-
499+
The depth of the shallow clone. If not specified or set to 0,
500+
a full clone will be performed. Values greater than 0 will
501+
create a shallow clone with the specified depth.
502+
type: integer
503+
minimum: 0
497504
type: object
498505
sources:
499506
description: |-
@@ -2853,6 +2860,13 @@ spec:
28532860
url:
28542861
description: URL describes the URL of the Git repository.
28552862
type: string
2863+
shallowCloneDepth:
2864+
description: |-
2865+
The depth of the shallow clone. If not specified or set to 0,
2866+
a full clone will be performed. Values greater than 0 will
2867+
create a shallow clone with the specified depth.
2868+
type: integer
2869+
minimum: 0
28562870
required:
28572871
- url
28582872
type: object

docs/build.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ A `Build` resource can specify a source type, such as a Git repository or an OCI
142142
- `source.git.url` - Specify the source location using a Git repository.
143143
- `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.
144144
- `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.
145+
- `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.
145146
- `source.contextDir` - For repositories where the source code is not located at the root folder, you can specify this path here.
146147

147148
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:

pkg/apis/build/v1beta1/build_conversion.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,6 @@ func getAlphaBuildSource(src BuildSpec) v1alpha1.Source {
446446
source.URL = &src.Source.Git.URL
447447
revision = src.Source.Git.Revision
448448
}
449-
450449
}
451450

452451
if credentials.Name != "" {

pkg/apis/build/v1beta1/source.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ type Git struct {
6262
//
6363
// +optional
6464
CloneSecret *string `json:"cloneSecret,omitempty"`
65+
66+
// ShallowCloneDepth specifies the depth of the shallow clone.
67+
// If not specified or set to 0, a full clone will be performed.
68+
// Values greater than 0 will create a shallow clone with the specified depth.
69+
//
70+
// +optional
71+
ShallowCloneDepth *int `json:"shallowCloneDepth,omitempty"`
6572
}
6673

6774
// OCIArtifact describes how to obtain source code from a container image, also known as an OCI

pkg/reconciler/buildrun/resources/sources/git.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ func AppendGitStep(
7979
)
8080
}
8181

82+
// Check if shallow clone is requested
83+
if source.ShallowCloneDepth != nil && *source.ShallowCloneDepth > 0 {
84+
gitStep.Args = append(
85+
gitStep.Args,
86+
"--depth",
87+
fmt.Sprintf("%d", *source.ShallowCloneDepth),
88+
)
89+
}
90+
8291
// If configure, use Git URL rewrite flag
8392
if cfg.GitRewriteRule {
8493
gitStep.Args = append(gitStep.Args, "--git-url-rewrite")

0 commit comments

Comments
 (0)