@@ -19,6 +19,7 @@ package controllers
19
19
import (
20
20
"archive/tar"
21
21
"bufio"
22
+ "bytes"
22
23
"compress/gzip"
23
24
"crypto/sha1"
24
25
"fmt"
@@ -39,7 +40,7 @@ import (
39
40
const (
40
41
excludeFile = ".sourceignore"
41
42
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"
43
44
)
44
45
45
46
// Storage manages artifacts
@@ -108,7 +109,7 @@ func (s *Storage) RemoveAllButCurrent(artifact sourcev1.Artifact) error {
108
109
})
109
110
110
111
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 , " " ))
112
113
}
113
114
return nil
114
115
}
@@ -123,15 +124,17 @@ func (s *Storage) ArtifactExist(artifact sourcev1.Artifact) bool {
123
124
124
125
// Archive creates a tar.gz to the artifact path from the given dir excluding any VCS specific
125
126
// 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 {
127
129
if _ , err := os .Stat (dir ); err != nil {
128
130
return err
129
131
}
130
132
131
- ps , err := loadExcludePatterns (dir )
133
+ ps , err := loadExcludePatterns (dir , spec )
132
134
if err != nil {
133
135
return err
134
136
}
137
+
135
138
matcher := gitignore .NewMatcher (ps )
136
139
137
140
gzFile , err := os .Create (artifact .Path )
@@ -241,27 +244,44 @@ func (s *Storage) Lock(artifact sourcev1.Artifact) (unlock func(), err error) {
241
244
return mutex .Lock ()
242
245
}
243
246
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 ) {
245
264
path := strings .Split (dir , "/" )
265
+
246
266
var ps []gitignore.Pattern
247
267
for _ , p := range strings .Split (excludeVCS , "," ) {
248
268
ps = append (ps , gitignore .ParsePattern (p , path ))
249
269
}
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 ))
262
274
}
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 )... )
265
284
}
285
+
266
286
return ps , nil
267
287
}
0 commit comments