Skip to content

Commit 74c97ad

Browse files
authored
Merge pull request #77 from erikh/ignore-lists
Support ignored files as a parameter
2 parents 9a92164 + a723b9e commit 74c97ad

File tree

7 files changed

+340
-21
lines changed

7 files changed

+340
-21
lines changed

api/v1alpha1/gitrepository_types.go

+7
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

+5
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

+6
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

+1-2
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

+39-19
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"
@@ -39,7 +40,7 @@ import (
3940
const (
4041
excludeFile = ".sourceignore"
4142
excludeVCS = ".git/,.gitignore,.gitmodules,.gitattributes"
42-
excludeExt = "*.jpg,*.jpeg,*.gif,*.png,*.wmv,.*flv,.*tar.gz,*.zip"
43+
excludeExt = "*.jpg,*.jpeg,*.gif,*.png,*.wmv,*.flv,*.tar.gz,*.zip"
4344
)
4445

4546
// Storage manages artifacts
@@ -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
}

0 commit comments

Comments
 (0)