Skip to content

Support ignored files as a parameter #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions api/v1alpha1/gitrepository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ type GitRepositorySpec struct {
// Verify OpenPGP signature for the commit that HEAD points to.
// +optional
Verification *GitRepositoryVerification `json:"verify,omitempty"`

// SourceIgnore overrides the set of excluded patterns in the .sourceignore
// format (which is the same as .gitignore). If not provided, a default will
// be used, consult the documentation for your version to find out what those
// are.
// +optional
SourceIgnore *string `json:"sourceIgnore,omitempty"`
}

// GitRepositoryRef defines the git ref used for pull and checkout operations.
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions config/crd/bases/source.fluxcd.io_gitrepositories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ spec:
TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
type: object
sourceIgnore:
description: SourceIgnore overrides the set of excluded patterns in
the .sourceignore format (which is the same as .gitignore). If not
provided, a default will be used, consult the documentation for your
version to find out what those are.
type: string
timeout:
description: The timeout for remote git operations like cloning, default
to 20s.
Expand Down
3 changes: 1 addition & 2 deletions controllers/gitrepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ func (r *GitRepositoryReconciler) sync(ctx context.Context, repository sourcev1.
defer unlock()

// archive artifact and check integrity
err = r.Storage.Archive(artifact, tmpGit)
if err != nil {
if err := r.Storage.Archive(artifact, tmpGit, repository.Spec); err != nil {
err = fmt.Errorf("storage archive error: %w", err)
return sourcev1.GitRepositoryNotReady(repository, sourcev1.StorageOperationFailedReason, err.Error()), err
}
Expand Down
58 changes: 39 additions & 19 deletions controllers/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controllers
import (
"archive/tar"
"bufio"
"bytes"
"compress/gzip"
"crypto/sha1"
"fmt"
Expand All @@ -39,7 +40,7 @@ import (
const (
excludeFile = ".sourceignore"
excludeVCS = ".git/,.gitignore,.gitmodules,.gitattributes"
excludeExt = "*.jpg,*.jpeg,*.gif,*.png,*.wmv,.*flv,.*tar.gz,*.zip"
excludeExt = "*.jpg,*.jpeg,*.gif,*.png,*.wmv,*.flv,*.tar.gz,*.zip"
)

// Storage manages artifacts
Expand Down Expand Up @@ -108,7 +109,7 @@ func (s *Storage) RemoveAllButCurrent(artifact sourcev1.Artifact) error {
})

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

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

ps, err := loadExcludePatterns(dir)
ps, err := loadExcludePatterns(dir, spec)
if err != nil {
return err
}

matcher := gitignore.NewMatcher(ps)

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

func loadExcludePatterns(dir string) ([]gitignore.Pattern, error) {
func getPatterns(reader io.Reader, path []string) []gitignore.Pattern {
ps := []gitignore.Pattern{}
scanner := bufio.NewScanner(reader)

for scanner.Scan() {
s := scanner.Text()
if !strings.HasPrefix(s, "#") && len(strings.TrimSpace(s)) > 0 {
ps = append(ps, gitignore.ParsePattern(s, path))
}
}

return ps
}

// loadExcludePatterns loads the excluded patterns from sourceignore or other
// sources.
func loadExcludePatterns(dir string, spec sourcev1.GitRepositorySpec) ([]gitignore.Pattern, error) {
path := strings.Split(dir, "/")

var ps []gitignore.Pattern
for _, p := range strings.Split(excludeVCS, ",") {
ps = append(ps, gitignore.ParsePattern(p, path))
}
for _, p := range strings.Split(excludeExt, ",") {
ps = append(ps, gitignore.ParsePattern(p, path))
}
if f, err := os.Open(filepath.Join(dir, excludeFile)); err == nil {
defer f.Close()

scanner := bufio.NewScanner(f)
for scanner.Scan() {
s := scanner.Text()
if !strings.HasPrefix(s, "#") && len(strings.TrimSpace(s)) > 0 {
ps = append(ps, gitignore.ParsePattern(s, path))
}

if spec.SourceIgnore == nil {
for _, p := range strings.Split(excludeExt, ",") {
ps = append(ps, gitignore.ParsePattern(p, path))
}
} else if !os.IsNotExist(err) {
return nil, err

if f, err := os.Open(filepath.Join(dir, excludeFile)); err == nil {
defer f.Close()
ps = append(ps, getPatterns(f, path)...)
} else if !os.IsNotExist(err) {
return nil, err
}
} else {
ps = append(ps, getPatterns(bytes.NewBufferString(*spec.SourceIgnore), path)...)
}

return ps, nil
}
Loading