Skip to content

Commit 7386b7d

Browse files
committed
Add support for Git submodules with go-git
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
1 parent 5486321 commit 7386b7d

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

api/v1beta1/gitrepository_types.go

+6
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ type GitRepositoryRef struct {
101101
// The Git commit SHA to checkout, if specified Tag filters will be ignored.
102102
// +optional
103103
Commit string `json:"commit,omitempty"`
104+
105+
// When enabled, after the clone is created, initializes all submodules within,
106+
// using their default settings.
107+
// This option is available only when using the 'go-git' GitImplementation.
108+
// +optional
109+
RecurseSubmodules bool `json:"recurseSubmodules,omitempty"`
104110
}
105111

106112
// GitRepositoryVerification defines the OpenPGP signature verification process.

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

+5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ spec:
7878
description: The Git commit SHA to checkout, if specified Tag
7979
filters will be ignored.
8080
type: string
81+
recurseSubmodules:
82+
description: When enabled, after the clone is created, initializes
83+
all submodules within, using their default settings. This option
84+
is available only when using the 'go-git' GitImplementation.
85+
type: boolean
8186
semver:
8287
description: The Git tag semver expression, takes precedence over
8388
Tag.

docs/api/source.md

+14
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,20 @@ string
11011101
<p>The Git commit SHA to checkout, if specified Tag filters will be ignored.</p>
11021102
</td>
11031103
</tr>
1104+
<tr>
1105+
<td>
1106+
<code>recurseSubmodules</code><br>
1107+
<em>
1108+
bool
1109+
</em>
1110+
</td>
1111+
<td>
1112+
<em>(Optional)</em>
1113+
<p>When enabled, after the clone is created, initializes all submodules within,
1114+
using their default settings.
1115+
This option is available only when using the &lsquo;go-git&rsquo; GitImplementation.</p>
1116+
</td>
1117+
</tr>
11041118
</tbody>
11051119
</table>
11061120
</div>

docs/spec/v1beta1/gitrepositories.md

+23
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ type GitRepositoryRef struct {
8080
// The Git commit SHA to checkout, if specified Tag filters will be ignored.
8181
// +optional
8282
Commit string `json:"commit,omitempty"`
83+
84+
// When enabled, after the clone is created, initializes all submodules within,
85+
// using their default settings.
86+
// This option is available only when using the 'go-git' GitImplementation.
87+
// +optional
88+
RecurseSubmodules bool `json:"recurseSubmodules,omitempty"`
8389
}
8490
```
8591

@@ -245,6 +251,22 @@ spec:
245251
branch: v3.x
246252
```
247253

254+
Pull a specific branch and including Git submodules:
255+
256+
```yaml
257+
apiVersion: source.toolkit.fluxcd.io/v1beta1
258+
kind: GitRepository
259+
metadata:
260+
name: podinfo
261+
namespace: default
262+
spec:
263+
interval: 1m
264+
url: https://github.com/stefanprodan/podinfo
265+
ref:
266+
branch: main
267+
recurseSubmodules: true
268+
```
269+
248270
Checkout a specific commit from a branch:
249271

250272
```yaml
@@ -261,6 +283,7 @@ spec:
261283
commit: 363a6a8fe6a7f13e05d34c163b0ef02a777da20a
262284
```
263285

286+
264287
Pull a specific tag:
265288

266289
```yaml

pkg/git/gogit/checkout.go

+24-13
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,25 @@ func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef) git.CheckoutStrategy
3838
case ref == nil:
3939
return &CheckoutBranch{branch: git.DefaultBranch}
4040
case ref.SemVer != "":
41-
return &CheckoutSemVer{semVer: ref.SemVer}
41+
return &CheckoutSemVer{semVer: ref.SemVer, recurseSubmodules: ref.RecurseSubmodules}
4242
case ref.Tag != "":
43-
return &CheckoutTag{tag: ref.Tag}
43+
return &CheckoutTag{tag: ref.Tag, recurseSubmodules: ref.RecurseSubmodules}
4444
case ref.Commit != "":
45-
strategy := &CheckoutCommit{branch: ref.Branch, commit: ref.Commit}
45+
strategy := &CheckoutCommit{branch: ref.Branch, commit: ref.Commit, recurseSubmodules: ref.RecurseSubmodules}
4646
if strategy.branch == "" {
4747
strategy.branch = git.DefaultBranch
4848
}
4949
return strategy
5050
case ref.Branch != "":
51-
return &CheckoutBranch{branch: ref.Branch}
51+
return &CheckoutBranch{branch: ref.Branch, recurseSubmodules: ref.RecurseSubmodules}
5252
default:
5353
return &CheckoutBranch{branch: git.DefaultBranch}
5454
}
5555
}
5656

5757
type CheckoutBranch struct {
58-
branch string
58+
branch string
59+
recurseSubmodules bool
5960
}
6061

6162
func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
@@ -67,7 +68,7 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *g
6768
SingleBranch: true,
6869
NoCheckout: false,
6970
Depth: 1,
70-
RecurseSubmodules: extgogit.DefaultSubmoduleRecursionDepth,
71+
RecurseSubmodules: recurseSubmodules(c.recurseSubmodules),
7172
Progress: nil,
7273
Tags: extgogit.NoTags,
7374
CABundle: auth.CABundle,
@@ -87,7 +88,8 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *g
8788
}
8889

8990
type CheckoutTag struct {
90-
tag string
91+
tag string
92+
recurseSubmodules bool
9193
}
9294

9395
func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
@@ -99,7 +101,7 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git.
99101
SingleBranch: true,
100102
NoCheckout: false,
101103
Depth: 1,
102-
RecurseSubmodules: extgogit.DefaultSubmoduleRecursionDepth,
104+
RecurseSubmodules: recurseSubmodules(c.recurseSubmodules),
103105
Progress: nil,
104106
Tags: extgogit.NoTags,
105107
CABundle: auth.CABundle,
@@ -119,8 +121,9 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git.
119121
}
120122

121123
type CheckoutCommit struct {
122-
branch string
123-
commit string
124+
branch string
125+
commit string
126+
recurseSubmodules bool
124127
}
125128

126129
func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
@@ -131,7 +134,7 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *g
131134
ReferenceName: plumbing.NewBranchReferenceName(c.branch),
132135
SingleBranch: true,
133136
NoCheckout: false,
134-
RecurseSubmodules: extgogit.DefaultSubmoduleRecursionDepth,
137+
RecurseSubmodules: recurseSubmodules(c.recurseSubmodules),
135138
Progress: nil,
136139
Tags: extgogit.NoTags,
137140
CABundle: auth.CABundle,
@@ -158,7 +161,8 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *g
158161
}
159162

160163
type CheckoutSemVer struct {
161-
semVer string
164+
semVer string
165+
recurseSubmodules bool
162166
}
163167

164168
func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
@@ -173,7 +177,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *g
173177
RemoteName: git.DefaultOrigin,
174178
NoCheckout: false,
175179
Depth: 1,
176-
RecurseSubmodules: extgogit.DefaultSubmoduleRecursionDepth,
180+
RecurseSubmodules: recurseSubmodules(c.recurseSubmodules),
177181
Progress: nil,
178182
Tags: extgogit.AllTags,
179183
CABundle: auth.CABundle,
@@ -262,3 +266,10 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *g
262266

263267
return &Commit{commit}, fmt.Sprintf("%s/%s", t, head.Hash().String()), nil
264268
}
269+
270+
func recurseSubmodules(recurse bool) extgogit.SubmoduleRescursivity {
271+
if recurse {
272+
return extgogit.DefaultSubmoduleRecursionDepth
273+
}
274+
return extgogit.NoRecurseSubmodules
275+
}

0 commit comments

Comments
 (0)