Skip to content

Commit 31c243e

Browse files
committed
fix: handle includes correctly
Fixes #658 Update the TarDir function to match _any_ include pattern provided in kcl.mod. Otherwise an include with two exclusive patterns will never match any file and produce an empty tarball. Exclude the --target dir in the archive as well as ".". Signed-off-by: Aaron D Borden <[email protected]>
1 parent f97b4ff commit 31c243e

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

pkg/utils/utils.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func HashDir(dir string) (string, error) {
4848

4949
// files in the ".git "directory will cause the same repository, cloned at different times,
5050
// has different checksum.
51-
for _, ignore := range ignores {
51+
for _, ignore := range defaultIgnores {
5252
if strings.Contains(path, ignore) {
5353
return nil
5454
}
@@ -120,7 +120,7 @@ func Exists(path string) (bool, error) {
120120
}
121121

122122
// todo: Consider using the OCI tarball as the standard tar format.
123-
var ignores = []string{".git", ".tar"}
123+
var defaultIgnores = []string{".git", ".tar"}
124124

125125
func TarDir(srcDir string, tarPath string, include []string, exclude []string) error {
126126
fw, err := os.Create(tarPath)
@@ -132,11 +132,19 @@ func TarDir(srcDir string, tarPath string, include []string, exclude []string) e
132132
tw := tar.NewWriter(fw)
133133
defer tw.Close()
134134

135+
// In case the tarPath is within the current working directory, exclude it from the tar itself.
136+
ignores := append(defaultIgnores, filepath.Join(srcDir, filepath.Dir(tarPath)))
137+
135138
err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
136139
if err != nil {
137140
return err
138141
}
139142

143+
// Ignore the current directory root "."
144+
if path == srcDir {
145+
return nil
146+
}
147+
140148
for _, ignore := range ignores {
141149
if strings.Contains(path, ignore) {
142150
return nil
@@ -145,7 +153,8 @@ func TarDir(srcDir string, tarPath string, include []string, exclude []string) e
145153

146154
getNewPattern := func(ex string) string {
147155
newPath := ex
148-
if !strings.HasPrefix(ex, srcDir+string(filepath.Separator)) {
156+
prefix := srcDir + string(filepath.Separator)
157+
if !strings.HasPrefix(ex, prefix) {
149158
newPath = filepath.Join(srcDir, ex)
150159
}
151160
return newPath
@@ -157,12 +166,20 @@ func TarDir(srcDir string, tarPath string, include []string, exclude []string) e
157166
}
158167
}
159168

169+
// If the include list is empty, all files are included by default.
170+
matchedInclude := len(include) == 0
160171
for _, inc := range include {
161-
if matched, _ := filepath.Match(getNewPattern(inc), path); !matched {
162-
return nil
172+
if matched, _ := filepath.Match(getNewPattern(inc), path); matched {
173+
matchedInclude = true
174+
break
163175
}
164176
}
165177

178+
// As long as _one_ of the include patterns matches, the file will be included.
179+
if !matchedInclude {
180+
return nil
181+
}
182+
166183
relPath, _ := filepath.Rel(srcDir, path)
167184
relPath = filepath.ToSlash(relPath)
168185

0 commit comments

Comments
 (0)