From 04e810f6f34a839a719f11f110a7a23daae057b0 Mon Sep 17 00:00:00 2001 From: attiasas Date: Wed, 25 Oct 2023 15:12:26 +0300 Subject: [PATCH] Add option to excludePathPattern only from relative path --- artifactory/services/fspatterns/utils.go | 12 ++++++++---- artifactory/services/fspatterns/utils_test.go | 11 +++++++++-- artifactory/services/upload.go | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/artifactory/services/fspatterns/utils.go b/artifactory/services/fspatterns/utils.go index df1a3e8f5..d87698b86 100644 --- a/artifactory/services/fspatterns/utils.go +++ b/artifactory/services/fspatterns/utils.go @@ -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 { @@ -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. @@ -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 } diff --git a/artifactory/services/fspatterns/utils_test.go b/artifactory/services/fspatterns/utils_test.go index 66f598ff3..fbfb1bea9 100644 --- a/artifactory/services/fspatterns/utils_test.go +++ b/artifactory/services/fspatterns/utils_test.go @@ -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]) diff --git a/artifactory/services/upload.go b/artifactory/services/upload.go index 0abf34585..10efdb304 100644 --- a/artifactory/services/upload.go +++ b/artifactory/services/upload.go @@ -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 }