@@ -40,14 +40,6 @@ import (
40
40
"github.com/fluxcd/source-controller/pkg/sourceignore"
41
41
)
42
42
43
- const (
44
- excludeFile = ".sourceignore"
45
- excludeVCS = ".git/,.gitignore,.gitmodules,.gitattributes"
46
- excludeExt = "*.jpg,*.jpeg,*.gif,*.png,*.wmv,*.flv,*.tar.gz,*.zip"
47
- excludeCI = ".github/,.circleci/,.travis.yml,.gitlab-ci.yml,appveyor.yml,.drone.yml,cloudbuild.yaml,codeship-services.yml,codeship-steps.yml"
48
- excludeExtra = "**/.goreleaser.yml,**/.sops.yaml,**/.flux.yaml"
49
- )
50
-
51
43
// Storage manages artifacts
52
44
type Storage struct {
53
45
// BasePath is the local directory path where the source artifacts are stored.
@@ -150,19 +142,35 @@ func (s *Storage) ArtifactExist(artifact sourcev1.Artifact) bool {
150
142
return fi .Mode ().IsRegular ()
151
143
}
152
144
153
- // Archive atomically archives the given directory as a tarball to the given v1beta1.Artifact
154
- // path, excluding any VCS specific files and directories, or any of the excludes defined in
155
- // the excludeFiles. If successful, it sets the checksum and last update time on the artifact.
156
- func (s * Storage ) Archive (artifact * sourcev1.Artifact , dir string , ignore * string ) (err error ) {
157
- if f , err := os .Stat (dir ); os .IsNotExist (err ) || ! f .IsDir () {
158
- return fmt .Errorf ("invalid dir path: %s" , dir )
145
+ // ArchiveFileFilter must return true if a file should not be included
146
+ // in the archive after inspecting the given path and/or os.FileInfo.
147
+ type ArchiveFileFilter func (p string , fi os.FileInfo ) bool
148
+
149
+ // SourceIgnoreFilter returns an ArchiveFileFilter that filters out
150
+ // files matching sourceignore.VCSPatterns and any of the provided
151
+ // patterns. If an empty gitignore.Pattern slice is given, the matcher
152
+ // is set to sourceignore.NewDefaultMatcher.
153
+ func SourceIgnoreFilter (ps []gitignore.Pattern , domain []string ) ArchiveFileFilter {
154
+ matcher := sourceignore .NewDefaultMatcher (ps , domain )
155
+ if len (ps ) > 0 {
156
+ ps = append (sourceignore .VCSPatterns (domain ), ps ... )
157
+ matcher = sourceignore .NewMatcher (ps )
158
+ }
159
+ return func (p string , fi os.FileInfo ) bool {
160
+ // The directory is always false as the archiver does already skip
161
+ // directories.
162
+ return matcher .Match (strings .Split (p , string (filepath .Separator )), false )
159
163
}
164
+ }
160
165
161
- ps , err := sourceignore .LoadExcludePatterns (dir , ignore )
162
- if err != nil {
163
- return err
166
+ // Archive atomically archives the given directory as a tarball to the
167
+ // given v1beta1.Artifact path, excluding directories and any
168
+ // ArchiveFileFilter matches. If successful, it sets the checksum and
169
+ // last update time on the artifact.
170
+ func (s * Storage ) Archive (artifact * sourcev1.Artifact , dir string , filter ArchiveFileFilter ) (err error ) {
171
+ if f , err := os .Stat (dir ); os .IsNotExist (err ) || ! f .IsDir () {
172
+ return fmt .Errorf ("invalid dir path: %s" , dir )
164
173
}
165
- matcher := sourceignore .NewMatcher (ps )
166
174
167
175
localPath := s .LocalPath (* artifact )
168
176
tf , err := ioutil .TempFile (filepath .Split (localPath ))
@@ -181,43 +189,7 @@ func (s *Storage) Archive(artifact *sourcev1.Artifact, dir string, ignore *strin
181
189
182
190
gw := gzip .NewWriter (mw )
183
191
tw := tar .NewWriter (gw )
184
- if err := writeToArchiveExcludeMatches (dir , matcher , tw ); err != nil {
185
- tw .Close ()
186
- gw .Close ()
187
- tf .Close ()
188
- return err
189
- }
190
-
191
- if err := tw .Close (); err != nil {
192
- gw .Close ()
193
- tf .Close ()
194
- return err
195
- }
196
- if err := gw .Close (); err != nil {
197
- tf .Close ()
198
- return err
199
- }
200
- if err := tf .Close (); err != nil {
201
- return err
202
- }
203
-
204
- if err := os .Chmod (tmpName , 0644 ); err != nil {
205
- return err
206
- }
207
-
208
- if err := fs .RenameWithFallback (tmpName , localPath ); err != nil {
209
- return err
210
- }
211
-
212
- artifact .Checksum = fmt .Sprintf ("%x" , h .Sum (nil ))
213
- artifact .LastUpdateTime = metav1 .Now ()
214
- return nil
215
- }
216
-
217
- // writeToArchiveExcludeMatches walks over the given dir and writes any regular file that does
218
- // not match the given gitignore.Matcher.
219
- func writeToArchiveExcludeMatches (dir string , matcher gitignore.Matcher , writer * tar.Writer ) error {
220
- fn := func (p string , fi os.FileInfo , err error ) error {
192
+ if err := filepath .Walk (dir , func (p string , fi os.FileInfo , err error ) error {
221
193
if err != nil {
222
194
return err
223
195
}
@@ -227,8 +199,8 @@ func writeToArchiveExcludeMatches(dir string, matcher gitignore.Matcher, writer
227
199
return nil
228
200
}
229
201
230
- // Ignore excluded extensions and files
231
- if matcher . Match ( strings . Split ( p , "/" ), false ) {
202
+ // Skip filtered files
203
+ if filter != nil && filter ( p , fi ) {
232
204
return nil
233
205
}
234
206
@@ -248,7 +220,7 @@ func writeToArchiveExcludeMatches(dir string, matcher gitignore.Matcher, writer
248
220
}
249
221
header .Name = relFilePath
250
222
251
- if err := writer .WriteHeader (header ); err != nil {
223
+ if err := tw .WriteHeader (header ); err != nil {
252
224
return err
253
225
}
254
226
@@ -257,13 +229,42 @@ func writeToArchiveExcludeMatches(dir string, matcher gitignore.Matcher, writer
257
229
f .Close ()
258
230
return err
259
231
}
260
- if _ , err := io .Copy (writer , f ); err != nil {
232
+ if _ , err := io .Copy (tw , f ); err != nil {
261
233
f .Close ()
262
234
return err
263
235
}
264
236
return f .Close ()
237
+ }); err != nil {
238
+ tw .Close ()
239
+ gw .Close ()
240
+ tf .Close ()
241
+ return err
265
242
}
266
- return filepath .Walk (dir , fn )
243
+
244
+ if err := tw .Close (); err != nil {
245
+ gw .Close ()
246
+ tf .Close ()
247
+ return err
248
+ }
249
+ if err := gw .Close (); err != nil {
250
+ tf .Close ()
251
+ return err
252
+ }
253
+ if err := tf .Close (); err != nil {
254
+ return err
255
+ }
256
+
257
+ if err := os .Chmod (tmpName , 0644 ); err != nil {
258
+ return err
259
+ }
260
+
261
+ if err := fs .RenameWithFallback (tmpName , localPath ); err != nil {
262
+ return err
263
+ }
264
+
265
+ artifact .Checksum = fmt .Sprintf ("%x" , h .Sum (nil ))
266
+ artifact .LastUpdateTime = metav1 .Now ()
267
+ return nil
267
268
}
268
269
269
270
// AtomicWriteFile atomically writes the io.Reader contents to the v1beta1.Artifact path.
0 commit comments