Skip to content

Commit 96a76c2

Browse files
authored
Merge pull request #55 from fluxcd/tar-ignore
2 parents a598f5a + e880a45 commit 96a76c2

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

controllers/gitrepository_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (r *GitRepositoryReconciler) sync(ctx context.Context, repository sourcev1.
186186
defer unlock()
187187

188188
// archive artifact and check integrity
189-
err = r.Storage.Archive(artifact, tmpGit, "", true)
189+
err = r.Storage.Archive(artifact, tmpGit, true)
190190
if err != nil {
191191
err = fmt.Errorf("storage archive error: %w", err)
192192
return sourcev1.GitRepositoryNotReady(repository, sourcev1.StorageOperationFailedReason, err.Error()), err

controllers/storage.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ import (
3333
"github.com/fluxcd/source-controller/internal/lockedfile"
3434
)
3535

36+
const (
37+
excludeFile = ".sourceignore"
38+
excludeVCS = ".git/,.gitignore,.gitmodules,.gitattributes"
39+
defaultExcludes = "jpg,jpeg,gif,png,wmv,flv,tar.gz,zip"
40+
)
41+
3642
// Storage manages artifacts
3743
type Storage struct {
3844
// BasePath is the local directory path where the source artifacts are stored.
@@ -112,17 +118,22 @@ func (s *Storage) ArtifactExist(artifact sourcev1.Artifact) bool {
112118
return true
113119
}
114120

115-
// Archive creates a tar.gz to the artifact path from the given dir excluding the provided file extensions
116-
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes string, integrityCheck bool) error {
117-
if excludes == "" {
118-
excludes = "jpg,jpeg,gif,png,wmv,flv,tar.gz,zip"
119-
}
120-
121+
// Archive creates a tar.gz to the artifact path from the given dir excluding any VCS specific
122+
// files and directories, or any of the excludes defined in the excludeFiles.
123+
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, integrityCheck bool) error {
121124
ctx, cancel := context.WithTimeout(context.Background(), s.Timeout)
122125
defer cancel()
123126

124-
tarExcludes := fmt.Sprintf("--exclude=\\*.{%s} --exclude .git", excludes)
125-
cmd := fmt.Sprintf("cd %s && tar -c %s -f - . | gzip > %s", dir, tarExcludes, artifact.Path)
127+
var tarExcludes []string
128+
if _, err := os.Stat(filepath.Join(dir, excludeFile)); !os.IsNotExist(err) {
129+
tarExcludes = append(tarExcludes, "--exclude-file="+excludeFile)
130+
} else {
131+
tarExcludes = append(tarExcludes, fmt.Sprintf("--exclude=\\*.{%s}", defaultExcludes))
132+
}
133+
for _, excl := range strings.Split(excludeVCS, ",") {
134+
tarExcludes = append(tarExcludes, "--exclude="+excl)
135+
}
136+
cmd := fmt.Sprintf("cd %s && tar -c %s -f - . | gzip > %s", dir, strings.Join(tarExcludes, " "), artifact.Path)
126137
command := exec.CommandContext(ctx, "/bin/sh", "-c", cmd)
127138

128139
err := command.Run()

0 commit comments

Comments
 (0)