Skip to content

Commit c3511cc

Browse files
committed
add support for checking out to Git refs
Add a new field `.spec.ref.name` which points to a Git reference which enables checking out to a particular commit pointed to by the specified reference. Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent 1de3b13 commit c3511cc

File tree

5 files changed

+64
-25
lines changed

5 files changed

+64
-25
lines changed

api/v1beta2/gitrepository_types.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ type GitRepositorySpec struct {
106106

107107
// RecurseSubmodules enables the initialization of all submodules within
108108
// the GitRepository as cloned from the URL, using their default settings.
109-
// This option is available only when using the 'go-git' GitImplementation.
110109
// +optional
111110
RecurseSubmodules bool `json:"recurseSubmodules,omitempty"`
112111

@@ -156,9 +155,6 @@ func (in *GitRepositoryInclude) GetToPath() string {
156155
// GitRepositoryRef specifies the Git reference to resolve and checkout.
157156
type GitRepositoryRef struct {
158157
// Branch to check out, defaults to 'master' if no other field is defined.
159-
//
160-
// When GitRepositorySpec.GitImplementation is set to 'go-git', a shallow
161-
// clone of the specified branch is performed.
162158
// +optional
163159
Branch string `json:"branch,omitempty"`
164160

@@ -170,11 +166,17 @@ type GitRepositoryRef struct {
170166
// +optional
171167
SemVer string `json:"semver,omitempty"`
172168

169+
// Name of the reference to check out; takes precedence over Branch, Tag and SemVer.
170+
//
171+
// It must be a valid Git reference: https://git-scm.com/docs/git-check-ref-format#_description
172+
// Examples: "refs/heads/main", "refs/tags/v0.1.0", "refs/pull/420/head", "refs/merge-requests/1/head"
173+
// +optional
174+
Name string `json:"name,omitempty"`
175+
173176
// Commit SHA to check out, takes precedence over all reference fields.
174177
//
175-
// When GitRepositorySpec.GitImplementation is set to 'go-git', this can be
176-
// combined with Branch to shallow clone the branch, in which the commit is
177-
// expected to exist.
178+
// This can be combined with Branch to shallow clone the branch, in which
179+
// the commit is expected to exist.
178180
// +optional
179181
Commit string `json:"commit,omitempty"`
180182
}

config/crd/bases/source.toolkit.fluxcd.io_gitrepositories.yaml

+11-8
Original file line numberDiff line numberDiff line change
@@ -462,25 +462,28 @@ spec:
462462
recurseSubmodules:
463463
description: RecurseSubmodules enables the initialization of all submodules
464464
within the GitRepository as cloned from the URL, using their default
465-
settings. This option is available only when using the 'go-git'
466-
GitImplementation.
465+
settings.
467466
type: boolean
468467
ref:
469468
description: Reference specifies the Git reference to resolve and
470469
monitor for changes, defaults to the 'master' branch.
471470
properties:
472471
branch:
473-
description: "Branch to check out, defaults to 'master' if no
474-
other field is defined. \n When GitRepositorySpec.GitImplementation
475-
is set to 'go-git', a shallow clone of the specified branch
476-
is performed."
472+
description: Branch to check out, defaults to 'master' if no other
473+
field is defined.
477474
type: string
478475
commit:
479476
description: "Commit SHA to check out, takes precedence over all
480-
reference fields. \n When GitRepositorySpec.GitImplementation
481-
is set to 'go-git', this can be combined with Branch to shallow
477+
reference fields. \n This can be combined with Branch to shallow
482478
clone the branch, in which the commit is expected to exist."
483479
type: string
480+
name:
481+
description: "Name of the reference to check out; takes precedence
482+
over Branch, Tag and SemVer. \n It must be a valid Git reference:
483+
https://git-scm.com/docs/git-check-ref-format#_description Examples:
484+
\"refs/heads/main\", \"refs/tags/v0.1.0\", \"refs/pull/420/head\",
485+
\"refs/merge-requests/1/head\""
486+
type: string
484487
semver:
485488
description: SemVer tag expression to check out, takes precedence
486489
over Tag.

controllers/gitrepository_controller.go

+1
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ func (r *GitRepositoryReconciler) gitCheckout(ctx context.Context,
787787
cloneOpts.Commit = ref.Commit
788788
cloneOpts.Tag = ref.Tag
789789
cloneOpts.SemVer = ref.SemVer
790+
cloneOpts.RefName = ref.Name
790791
}
791792

792793
// Only if the object has an existing artifact in storage, attempt to

docs/api/source.md

+18-9
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,7 @@ bool
436436
<td>
437437
<em>(Optional)</em>
438438
<p>RecurseSubmodules enables the initialization of all submodules within
439-
the GitRepository as cloned from the URL, using their default settings.
440-
This option is available only when using the &lsquo;go-git&rsquo; GitImplementation.</p>
439+
the GitRepository as cloned from the URL, using their default settings.</p>
441440
</td>
442441
</tr>
443442
<tr>
@@ -1671,8 +1670,6 @@ string
16711670
<td>
16721671
<em>(Optional)</em>
16731672
<p>Branch to check out, defaults to &lsquo;master&rsquo; if no other field is defined.</p>
1674-
<p>When GitRepositorySpec.GitImplementation is set to &lsquo;go-git&rsquo;, a shallow
1675-
clone of the specified branch is performed.</p>
16761673
</td>
16771674
</tr>
16781675
<tr>
@@ -1701,6 +1698,20 @@ string
17011698
</tr>
17021699
<tr>
17031700
<td>
1701+
<code>name</code><br>
1702+
<em>
1703+
string
1704+
</em>
1705+
</td>
1706+
<td>
1707+
<em>(Optional)</em>
1708+
<p>Name of the reference to check out; takes precedence over Branch, Tag and SemVer.</p>
1709+
<p>It must be a valid Git reference: <a href="https://git-scm.com/docs/git-check-ref-format#_description">https://git-scm.com/docs/git-check-ref-format#_description</a>
1710+
Examples: &ldquo;refs/heads/main&rdquo;, &ldquo;refs/tags/v0.1.0&rdquo;, &ldquo;refs/pull/420/head&rdquo;, &ldquo;refs/merge-requests/1/head&rdquo;</p>
1711+
</td>
1712+
</tr>
1713+
<tr>
1714+
<td>
17041715
<code>commit</code><br>
17051716
<em>
17061717
string
@@ -1709,9 +1720,8 @@ string
17091720
<td>
17101721
<em>(Optional)</em>
17111722
<p>Commit SHA to check out, takes precedence over all reference fields.</p>
1712-
<p>When GitRepositorySpec.GitImplementation is set to &lsquo;go-git&rsquo;, this can be
1713-
combined with Branch to shallow clone the branch, in which the commit is
1714-
expected to exist.</p>
1723+
<p>This can be combined with Branch to shallow clone the branch, in which
1724+
the commit is expected to exist.</p>
17151725
</td>
17161726
</tr>
17171727
</tbody>
@@ -1875,8 +1885,7 @@ bool
18751885
<td>
18761886
<em>(Optional)</em>
18771887
<p>RecurseSubmodules enables the initialization of all submodules within
1878-
the GitRepository as cloned from the URL, using their default settings.
1879-
This option is available only when using the &lsquo;go-git&rsquo; GitImplementation.</p>
1888+
the GitRepository as cloned from the URL, using their default settings.</p>
18801889
</td>
18811890
</tr>
18821891
<tr>

docs/spec/v1beta2/gitrepositories.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ is `60s`.
228228

229229
`.spec.ref` is an optional field to specify the Git reference to resolve and
230230
watch for changes. References are specified in one or more subfields
231-
(`.branch`, `.tag`, `.semver`, `.commit`), with latter listed fields taking
231+
(`.branch`, `.tag`, `.semver`, `.name`, `.commit`), with latter listed fields taking
232232
precedence over earlier ones. If not specified, it defaults to a `master`
233233
branch reference.
234234

@@ -287,6 +287,30 @@ spec:
287287
This field takes precedence over [`.branch`](#branch-example) and
288288
[`.tag`](#tag-example).
289289

290+
291+
#### Name example
292+
293+
To Git checkout a specfied [reference](https://git-scm.com/book/en/v2/Git-Internals-Git-References),
294+
use `.spec.ref.name`:
295+
296+
```yaml
297+
---
298+
apiVersion: source.toolkit.fluxcd.io/v1beta2
299+
kind: GitRepository
300+
metadata:
301+
name: <repository-name>
302+
spec:
303+
ref:
304+
# Ref name format reference: https://git-scm.com/docs/git-check-ref-format#_description
305+
name: <reference-name>
306+
```
307+
308+
Valid examples are: `refs/heads/main`, `refs/tags/v0.1.0`, `refs/pull/420/head`,
309+
`refs/merge-requests/1/head`.
310+
311+
This field takes precedence over [`.branch`](#branch-example),
312+
[`.tag`](#tag-example), and [`.semver`](#semver-example).
313+
290314
#### Commit example
291315

292316
To Git checkout a specified commit, use `.spec.ref.commit`:

0 commit comments

Comments
 (0)