@@ -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
3743type 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