Skip to content

Commit b1b1dbc

Browse files
author
Erik Hollensbe
committed
Support programming excluded patterns in gitrepository spec
-- More coming in this commit message soon Signed-off-by: Erik Hollensbe <[email protected]>
1 parent 9a92164 commit b1b1dbc

File tree

6 files changed

+87
-20
lines changed

6 files changed

+87
-20
lines changed

api/v1alpha1/gitrepository_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ type GitRepositorySpec struct {
5454
// Verify OpenPGP signature for the commit that HEAD points to.
5555
// +optional
5656
Verification *GitRepositoryVerification `json:"verify,omitempty"`
57+
58+
// SourceIgnore overrides the set of excluded patterns in the .sourceignore
59+
// format (which is the same as .gitignore). If not provided, a default will
60+
// be used, consult the documentation for your version to find out what those
61+
// are.
62+
// +optional
63+
SourceIgnore *string `json:"sourceIgnore,omitempty"`
5764
}
5865

5966
// GitRepositoryRef defines the git ref used for pull and checkout operations.

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ spec:
8282
TODO: Add other useful fields. apiVersion, kind, uid?'
8383
type: string
8484
type: object
85+
sourceIgnore:
86+
description: SourceIgnore overrides the set of excluded patterns in
87+
the .sourceignore format (which is the same as .gitignore). If not
88+
provided, a default will be used, consult the documentation for your
89+
version to find out what those are.
90+
type: string
8591
timeout:
8692
description: The timeout for remote git operations like cloning, default
8793
to 20s.

controllers/gitrepository_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ func (r *GitRepositoryReconciler) sync(ctx context.Context, repository sourcev1.
198198
defer unlock()
199199

200200
// archive artifact and check integrity
201-
err = r.Storage.Archive(artifact, tmpGit)
202-
if err != nil {
201+
if err := r.Storage.Archive(artifact, tmpGit, repository.Spec); err != nil {
203202
err = fmt.Errorf("storage archive error: %w", err)
204203
return sourcev1.GitRepositoryNotReady(repository, sourcev1.StorageOperationFailedReason, err.Error()), err
205204
}

controllers/storage.go

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"archive/tar"
2121
"bufio"
22+
"bytes"
2223
"compress/gzip"
2324
"crypto/sha1"
2425
"fmt"
@@ -108,7 +109,7 @@ func (s *Storage) RemoveAllButCurrent(artifact sourcev1.Artifact) error {
108109
})
109110

110111
if len(errors) > 0 {
111-
return fmt.Errorf("faild to remove files: %s", strings.Join(errors, " "))
112+
return fmt.Errorf("failed to remove files: %s", strings.Join(errors, " "))
112113
}
113114
return nil
114115
}
@@ -123,15 +124,17 @@ func (s *Storage) ArtifactExist(artifact sourcev1.Artifact) bool {
123124

124125
// Archive creates a tar.gz to the artifact path from the given dir excluding any VCS specific
125126
// files and directories, or any of the excludes defined in the excludeFiles.
126-
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string) error {
127+
// Returns a modified sourcev1.Artifact and any error.
128+
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, spec sourcev1.GitRepositorySpec) error {
127129
if _, err := os.Stat(dir); err != nil {
128130
return err
129131
}
130132

131-
ps, err := loadExcludePatterns(dir)
133+
ps, err := loadExcludePatterns(dir, spec)
132134
if err != nil {
133135
return err
134136
}
137+
135138
matcher := gitignore.NewMatcher(ps)
136139

137140
gzFile, err := os.Create(artifact.Path)
@@ -241,27 +244,44 @@ func (s *Storage) Lock(artifact sourcev1.Artifact) (unlock func(), err error) {
241244
return mutex.Lock()
242245
}
243246

244-
func loadExcludePatterns(dir string) ([]gitignore.Pattern, error) {
247+
func getPatterns(reader io.Reader, path []string) []gitignore.Pattern {
248+
ps := []gitignore.Pattern{}
249+
scanner := bufio.NewScanner(reader)
250+
251+
for scanner.Scan() {
252+
s := scanner.Text()
253+
if !strings.HasPrefix(s, "#") && len(strings.TrimSpace(s)) > 0 {
254+
ps = append(ps, gitignore.ParsePattern(s, path))
255+
}
256+
}
257+
258+
return ps
259+
}
260+
261+
// loadExcludePatterns loads the excluded patterns from sourceignore or other
262+
// sources.
263+
func loadExcludePatterns(dir string, spec sourcev1.GitRepositorySpec) ([]gitignore.Pattern, error) {
245264
path := strings.Split(dir, "/")
265+
246266
var ps []gitignore.Pattern
247267
for _, p := range strings.Split(excludeVCS, ",") {
248268
ps = append(ps, gitignore.ParsePattern(p, path))
249269
}
250-
for _, p := range strings.Split(excludeExt, ",") {
251-
ps = append(ps, gitignore.ParsePattern(p, path))
252-
}
253-
if f, err := os.Open(filepath.Join(dir, excludeFile)); err == nil {
254-
defer f.Close()
255-
256-
scanner := bufio.NewScanner(f)
257-
for scanner.Scan() {
258-
s := scanner.Text()
259-
if !strings.HasPrefix(s, "#") && len(strings.TrimSpace(s)) > 0 {
260-
ps = append(ps, gitignore.ParsePattern(s, path))
261-
}
270+
271+
if spec.SourceIgnore == nil {
272+
for _, p := range strings.Split(excludeExt, ",") {
273+
ps = append(ps, gitignore.ParsePattern(p, path))
262274
}
263-
} else if !os.IsNotExist(err) {
264-
return nil, err
275+
276+
if f, err := os.Open(filepath.Join(dir, excludeFile)); err == nil {
277+
defer f.Close()
278+
ps = append(ps, getPatterns(f, path)...)
279+
} else if !os.IsNotExist(err) {
280+
return nil, err
281+
}
282+
} else {
283+
ps = append(ps, getPatterns(bytes.NewBufferString(*spec.SourceIgnore), path)...)
265284
}
285+
266286
return ps, nil
267287
}

docs/api/source.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ GitRepositoryVerification
157157
<p>Verify OpenPGP signature for the commit that HEAD points to.</p>
158158
</td>
159159
</tr>
160+
<tr>
161+
<td>
162+
<code>sourceIgnore</code><br>
163+
<em>
164+
string
165+
</em>
166+
</td>
167+
<td>
168+
<em>(Optional)</em>
169+
<p>SourceIgnore overrides the set of excluded patterns in the .sourceignore
170+
format (which is the same as .gitignore). If not provided, a default will
171+
be used, consult the documentation for your version to find out what those
172+
are.</p>
173+
</td>
174+
</tr>
160175
</table>
161176
</td>
162177
</tr>
@@ -666,6 +681,21 @@ GitRepositoryVerification
666681
<p>Verify OpenPGP signature for the commit that HEAD points to.</p>
667682
</td>
668683
</tr>
684+
<tr>
685+
<td>
686+
<code>sourceIgnore</code><br>
687+
<em>
688+
string
689+
</em>
690+
</td>
691+
<td>
692+
<em>(Optional)</em>
693+
<p>SourceIgnore overrides the set of excluded patterns in the .sourceignore
694+
format (which is the same as .gitignore). If not provided, a default will
695+
be used, consult the documentation for your version to find out what those
696+
are.</p>
697+
</td>
698+
</tr>
669699
</tbody>
670700
</table>
671701
</div>

0 commit comments

Comments
 (0)