@@ -33,6 +33,12 @@ import (
33
33
"github.com/fluxcd/source-controller/internal/lockedfile"
34
34
)
35
35
36
+ const (
37
+ excludeFile = ".sourceignore"
38
+ excludeVCS = ".git/,.gitignore,.gitmodules,.gitattributes"
39
+ defaultExcludes = "jpg,jpeg,gif,png,wmv,flv,tar.gz,zip"
40
+ )
41
+
36
42
// Storage manages artifacts
37
43
type Storage struct {
38
44
// BasePath is the local directory path where the source artifacts are stored.
@@ -112,17 +118,22 @@ func (s *Storage) ArtifactExist(artifact sourcev1.Artifact) bool {
112
118
return true
113
119
}
114
120
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 {
121
124
ctx , cancel := context .WithTimeout (context .Background (), s .Timeout )
122
125
defer cancel ()
123
126
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 )
126
137
command := exec .CommandContext (ctx , "/bin/sh" , "-c" , cmd )
127
138
128
139
err := command .Run ()
0 commit comments