@@ -19,6 +19,7 @@ package controllers
1919import (
2020 "archive/tar"
2121 "bufio"
22+ "bytes"
2223 "compress/gzip"
2324 "crypto/sha1"
2425 "fmt"
@@ -39,7 +40,7 @@ import (
3940const (
4041 excludeFile = ".sourceignore"
4142 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"
4344)
4445
4546// Storage manages artifacts
@@ -108,7 +109,7 @@ func (s *Storage) RemoveAllButCurrent(artifact sourcev1.Artifact) error {
108109 })
109110
110111 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 , " " ))
112113 }
113114 return nil
114115}
@@ -123,15 +124,17 @@ func (s *Storage) ArtifactExist(artifact sourcev1.Artifact) bool {
123124
124125// Archive creates a tar.gz to the artifact path from the given dir excluding any VCS specific
125126// 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 {
127129 if _ , err := os .Stat (dir ); err != nil {
128130 return err
129131 }
130132
131- ps , err := loadExcludePatterns (dir )
133+ ps , err := loadExcludePatterns (dir , spec )
132134 if err != nil {
133135 return err
134136 }
137+
135138 matcher := gitignore .NewMatcher (ps )
136139
137140 gzFile , err := os .Create (artifact .Path )
@@ -241,27 +244,44 @@ func (s *Storage) Lock(artifact sourcev1.Artifact) (unlock func(), err error) {
241244 return mutex .Lock ()
242245}
243246
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 ) {
245264 path := strings .Split (dir , "/" )
265+
246266 var ps []gitignore.Pattern
247267 for _ , p := range strings .Split (excludeVCS , "," ) {
248268 ps = append (ps , gitignore .ParsePattern (p , path ))
249269 }
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 ))
262274 }
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 )... )
265284 }
285+
266286 return ps , nil
267287}
0 commit comments