Skip to content

Commit

Permalink
Add option to excludePathPattern only from relative path (#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas authored Nov 1, 2023
1 parent 6bb09fe commit 422f205
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
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

0 comments on commit 422f205

Please sign in to comment.