Skip to content

Commit dfcd032

Browse files
authored
Merge pull request #867 from schollz:schollz/issue866
fix: --exclude should exclude recursive files
2 parents 42bde22 + ec7b86d commit dfcd032

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

src/croc/croc.go

+57-14
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ func isChild(parentPath, childPath string) bool {
313313
return !strings.HasPrefix(relPath, "..")
314314
}
315315

316+
func recursiveFiles(path string) (paths []string, err error) {
317+
paths = []string{strings.ToLower(path)}
318+
err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
319+
paths = append(paths, strings.ToLower(path))
320+
return nil
321+
})
322+
return
323+
}
324+
316325
// This function retrieves the important file information
317326
// for every file that will be transferred
318327
func GetFilesInfo(fnames []string, zipfolder bool, ignoreGit bool, exclusions []string) (filesInfo []FileInfo, emptyFolders []FileInfo, totalNumberFolders int, err error) {
@@ -380,18 +389,6 @@ func GetFilesInfo(fnames []string, zipfolder bool, ignoreGit bool, exclusions []
380389
}
381390

382391
absPath, errAbs := filepath.Abs(fpath)
383-
absPathLower := strings.ToLower(absPath)
384-
ignorePath := false
385-
for _, exclusion := range exclusions {
386-
if strings.Contains(absPathLower, exclusion) {
387-
ignorePath = true
388-
break
389-
}
390-
}
391-
if ignorePath {
392-
log.Debugf("Ignoring %s", absPath)
393-
continue
394-
}
395392

396393
if errAbs != nil {
397394
err = errAbs
@@ -426,6 +423,21 @@ func GetFilesInfo(fnames []string, zipfolder bool, ignoreGit bool, exclusions []
426423
TempFile: true,
427424
IsIgnored: ignoredPaths[absPath],
428425
}
426+
// check if exclusions apply to this file
427+
if len(exclusions) > 0 && !fInfo.IsIgnored {
428+
allFiles, _ := recursiveFiles(absPath)
429+
for _, exclusion := range exclusions {
430+
for _, file := range allFiles {
431+
if strings.Contains(strings.ToLower(file), strings.ToLower(exclusion)) {
432+
fInfo.IsIgnored = true
433+
break
434+
}
435+
}
436+
if fInfo.IsIgnored {
437+
break
438+
}
439+
}
440+
}
429441
if fInfo.IsIgnored {
430442
continue
431443
}
@@ -458,7 +470,23 @@ func GetFilesInfo(fnames []string, zipfolder bool, ignoreGit bool, exclusions []
458470
TempFile: false,
459471
IsIgnored: ignoredPaths[pathName],
460472
}
461-
if fInfo.IsIgnored && ignoreGit {
473+
// check if exclusions apply to this file
474+
if len(exclusions) > 0 && !fInfo.IsIgnored {
475+
allFiles, _ := recursiveFiles(pathName)
476+
for _, exclusion := range exclusions {
477+
for _, file := range allFiles {
478+
log.Tracef("ignoring test: %s %s %v", strings.ToLower(file), strings.ToLower(exclusion), strings.Contains(strings.ToLower(file), strings.ToLower(exclusion)))
479+
if strings.Contains(strings.ToLower(file), strings.ToLower(exclusion)) {
480+
fInfo.IsIgnored = true
481+
break
482+
}
483+
}
484+
if fInfo.IsIgnored {
485+
break
486+
}
487+
}
488+
}
489+
if fInfo.IsIgnored && (ignoreGit || len(exclusions) > 0) {
462490
return nil
463491
} else {
464492
filesInfo = append(filesInfo, fInfo)
@@ -494,7 +522,22 @@ func GetFilesInfo(fnames []string, zipfolder bool, ignoreGit bool, exclusions []
494522
TempFile: false,
495523
IsIgnored: ignoredPaths[absPath],
496524
}
497-
if fInfo.IsIgnored && ignoreGit {
525+
if len(exclusions) > 0 && !fInfo.IsIgnored {
526+
allFiles, _ := recursiveFiles(absPath)
527+
for _, exclusion := range exclusions {
528+
for _, file := range allFiles {
529+
log.Tracef("ignoring test: %s %s %v", strings.ToLower(file), strings.ToLower(exclusion), strings.Contains(strings.ToLower(file), strings.ToLower(exclusion)))
530+
if strings.Contains(strings.ToLower(file), strings.ToLower(exclusion)) {
531+
fInfo.IsIgnored = true
532+
break
533+
}
534+
}
535+
if fInfo.IsIgnored {
536+
break
537+
}
538+
}
539+
}
540+
if fInfo.IsIgnored && (ignoreGit || len(exclusions) > 0) {
498541
continue
499542
} else {
500543
filesInfo = append(filesInfo, fInfo)

0 commit comments

Comments
 (0)