package hudson.plugins.scm_sync_configuration.strategies.model;

import hudson.model.Hudson;
import hudson.model.Saveable;
import hudson.plugins.scm_sync_configuration.JenkinsFilesHelper;
import org.apache.tools.ant.DirectoryScanner;
import org.springframework.util.AntPathMatcher;

import java.io.File;
import java.util.Arrays;
import java.util.List;

public class PatternsEntityMatcher implements ConfigurationEntityMatcher {

	private String[] includesPatterns;

    public PatternsEntityMatcher(String[] includesPatterns){
        this.includesPatterns = includesPatterns;
    }

	public boolean matches(Saveable saveable, File file) {
	    if (file == null) {
	    	return false;
            }
            
            // in case **/jobs/ directory is a link to a mounted volume so nextBuildNumber stays in sync with buildHistory and symlinks
            // such cases are usual when running Jenkins in Docker as a Phoenix Docker Container
            // adding this check avoids unnecessary Exceptions on Jenkins logs
            if (file.getAbsolutePath() == null ||
                !file.getAbsolutePath().startsWith(Hudson.getInstance().getRootDir().getAbsolutePath())) {
              	return false;
            }
            
            String filePathRelativeToHudsonRoot = JenkinsFilesHelper.buildPathRelativeToHudsonRoot(file);
            AntPathMatcher matcher = new AntPathMatcher();
            for(String pattern : includesPatterns) {
              if(matcher.match(pattern, filePathRelativeToHudsonRoot)){
                return true;
              }
            }
            return false;
	}

    public List<String> getIncludes(){
        return Arrays.asList(includesPatterns);
    }

    public String[] matchingFilesFrom(File rootDirectory) {
        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setIncludes(includesPatterns);
        scanner.setBasedir(rootDirectory);
        scanner.scan();
        return scanner.getIncludedFiles();
    }
}

@@ -634,20 +634,17 @@
 ile) %7B%0A%09
-    
+%09
 if (file
@@ -656,20 +656,17 @@
 ull) %7B%0A%09
-    
+%09
 %09return 
@@ -676,39 +676,13 @@
 se;%0A
-            %7D%0A            %0A    
+%09%09%7D%0A%0A
     
@@ -700,16 +700,12 @@
 ase 
-**/
 jobs
-/
  dir
@@ -813,20 +813,16 @@
         
-   
- 
 // such 
@@ -906,20 +906,16 @@
         
-    
 // addin
@@ -981,20 +981,16 @@
         
- 
-   
 if (file
@@ -1019,20 +1019,16 @@
 null %7C%7C%0A
-    
         
@@ -1132,20 +1132,16 @@
         
-    
 %09return 
@@ -1159,31 +1159,15 @@
     
-    %7D%0A        
+%7D%0A
     %0A
-    
     
@@ -1272,20 +1272,16 @@
         
-    
 AntPathM
@@ -1327,20 +1327,16 @@
         
-   
- 
 for(Stri
@@ -1364,26 +1364,24 @@
 Patterns) %7B%0A
-  
             
@@ -1436,16 +1436,17 @@
 onRoot))
+ 
 %7B%0A      
@@ -1447,28 +1447,25 @@
             
-  
-  
+%09
 return true;
@@ -1477,31 +1477,26 @@
         
-  %7D%0A   
+%7D%0A
         
  %7D%0A     
@@ -1483,31 +1483,26 @@
   %7D%0A        
- 
 %7D%0A
-    
         retu

Update PatternsEntityMatcher.java