Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional excludePathPattern only from relative path #850

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions artifactory/services/fspatterns/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// Return all the existing paths of the provided root path
func ListFiles(rootPath string, isRecursive, includeDirs, isSymlink bool, excludePathPattern string) ([]string, error) {
func ListFiles(rootPath string, isRecursive, includeDirs, excludeWithRelativePath, isSymlink bool, excludePathPattern string) ([]string, error) {
var paths []string
var err error
if isRecursive {
Expand All @@ -26,7 +26,11 @@ func ListFiles(rootPath string, isRecursive, includeDirs, isSymlink bool, exclud
if err != nil {
return paths, err
}
return filterFiles(paths, excludePathPattern)
var rootFilter string
if excludeWithRelativePath {
rootFilter = rootPath
}
return filterFiles(rootFilter, paths, excludePathPattern)
}

// Transform to regexp and prepare Exclude patterns to be used, exclusion patterns must be absolute paths.
Expand All @@ -49,13 +53,13 @@ func PrepareExcludePathPattern(exclusions []string, patternType utils.PatternTyp
return excludePathPattern
}

func filterFiles(files []string, excludePathPattern string) (filteredFiles []string, err error) {
func filterFiles(rootPath string, files []string, excludePathPattern string) (filteredFiles []string, err error) {
var excludedPath bool
for i := 0; i < len(files); i++ {
if files[i] == "." {
continue
}
excludedPath, err = isPathExcluded(files[i], excludePathPattern)
excludedPath, err = isPathExcluded(strings.TrimPrefix(files[i], rootPath), excludePathPattern)
if err != nil {
return
}
Expand Down
11 changes: 9 additions & 2 deletions artifactory/services/fspatterns/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ func TestFilterFiles(t *testing.T) {
data := []struct {
files []string
ExcludePattern string
root string
result []string
}{
{[]string{"file1", filepath.Join("dir", "file1"), "file2.zip"}, "^*.zip$", []string{"file1", filepath.Join("dir", "file1")}},
{[]string{"file1", filepath.Join("dir", "file1"), "file2.zip"}, "^*.zip$", "", []string{"file1", filepath.Join("dir", "file1")}},
{[]string{
"file1",
"test.zip",
filepath.Join("test", "file1"),
filepath.Join("dir", "test", "should-be-filter"),
}, "(^.*test.*$)", "test", []string{"file1", "test.zip", filepath.Join("test", "file1")}},
}
for _, d := range data {
got, err := filterFiles(d.files, d.ExcludePattern)
got, err := filterFiles(d.root, d.files, d.ExcludePattern)
assert.NoError(t, err)
assert.Len(t, got, len(d.result))
assert.Contains(t, got, d.files[0])
Expand Down
2 changes: 1 addition & 1 deletion artifactory/services/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func scanFilesByPattern(uploadParams UploadParams, rootPath string, progressMgr
if errorutils.CheckError(err) != nil {
return err
}
paths, err := fspatterns.ListFiles(rootPath, uploadParams.IsRecursive(), uploadParams.IsIncludeDirs(), uploadParams.IsSymlink(), excludePathPattern)
paths, err := fspatterns.ListFiles(rootPath, uploadParams.IsRecursive(), uploadParams.IsIncludeDirs(), false, uploadParams.IsSymlink(), excludePathPattern)
if err != nil {
return err
}
Expand Down
Loading