Navigation Menu

Skip to content

Commit

Permalink
[FIXED JENKINS-13147] Added case sensitivity option for exclusion of …
Browse files Browse the repository at this point in the history
…files during polling
  • Loading branch information
rpetti committed Mar 20, 2012
1 parent 7c0c24c commit d825526
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 26 deletions.
42 changes: 33 additions & 9 deletions src/main/java/hudson/plugins/perforce/PerforceSCM.java
Expand Up @@ -194,6 +194,11 @@ public class PerforceSCM extends SCM {
*/
String excludedFiles;

/**
* Use Case sensitive matching on excludedFiles.
*/
Boolean excludedFilesCaseSensitivity;

/**
* If a ticket was issued we can use it instead of the password in the environment.
*/
Expand Down Expand Up @@ -255,7 +260,7 @@ public PerforceSCM(
String p4Charset,
String p4CommandCharset,
boolean updateCounterValue,
boolean forceSync,
boolean forceSync,
boolean dontUpdateServer,
boolean alwaysForceSync,
boolean createWorkspace,
Expand All @@ -271,8 +276,8 @@ public PerforceSCM(
int firstChange,
PerforceRepositoryBrowser browser,
String excludedUsers,
String excludedFiles
) {
String excludedFiles,
boolean excludedFilesCaseSensitivity) {

this.configVersion = 0L;

Expand Down Expand Up @@ -353,6 +358,7 @@ public PerforceSCM(
this.p4CommandCharset = Util.fixEmptyAndTrim(p4CommandCharset);
this.excludedUsers = Util.fixEmptyAndTrim(excludedUsers);
this.excludedFiles = Util.fixEmptyAndTrim(excludedFiles);
this.excludedFilesCaseSensitivity = excludedFilesCaseSensitivity;
}

/**
Expand Down Expand Up @@ -509,6 +515,10 @@ public Object readResolve() {
p4Tool = p4Exe;
}

if(excludedFilesCaseSensitivity == null) {
excludedFilesCaseSensitivity = Boolean.TRUE;
}

if(configVersion == null) {
configVersion = 0L;
}
Expand Down Expand Up @@ -1206,7 +1216,7 @@ private boolean isChangelistExcluded(Changelist changelist, AbstractProject proj
if (files.size() > 0 && changelist.getFiles().size() > 0)
{
for (FileEntry f : changelist.getFiles()) {
if (!doesFilenameMatchAnyP4Pattern(f.getFilename(),files)) {
if (!doesFilenameMatchAnyP4Pattern(f.getFilename(),files,excludedFilesCaseSensitivity)) {
return false;
}

Expand All @@ -1224,22 +1234,27 @@ private boolean isChangelistExcluded(Changelist changelist, AbstractProject proj
return false;
}

private static boolean doesFilenameMatchAnyP4Pattern(String filename, List<String> patternStrings){
private static boolean doesFilenameMatchAnyP4Pattern(String filename, List<String> patternStrings, boolean caseSensitive){
for(String patternString : patternStrings){
if(patternString.trim().equals("")) continue;
if(doesFilenameMatchP4Pattern(filename, patternString)){
if(doesFilenameMatchP4Pattern(filename, patternString, caseSensitive)){
return true;
}
}
return false;
}

public static boolean doesFilenameMatchP4Pattern(String filename, String patternString) throws PatternSyntaxException {
public static boolean doesFilenameMatchP4Pattern(String filename, String patternString, boolean caseSensitive) throws PatternSyntaxException {
patternString = patternString.trim();
filename = filename.trim();
patternString = patternString.replaceAll("\\*", "[^/]*");
patternString = patternString.replaceAll("\\.\\.\\.", ".*");
Pattern pattern = Pattern.compile(patternString);
Pattern pattern;
if(!caseSensitive){
pattern = Pattern.compile(patternString,Pattern.CASE_INSENSITIVE);
} else {
pattern = Pattern.compile(patternString);
}
Matcher matcher = pattern.matcher(filename);
if(matcher.matches()){
return true;
Expand Down Expand Up @@ -1868,6 +1883,7 @@ public FormValidation doValidateExcludedUsers(StaplerRequest req) {
*/
public FormValidation doValidateExcludedFiles(StaplerRequest req) {
String excludedFiles = fixNull(req.getParameter("excludedFiles")).trim();
Boolean excludedFilesCaseSensitivity = Boolean.valueOf(fixNull(req.getParameter("excludedFilesCaseSensitivity")).trim());
List<String> files = Arrays.asList(excludedFiles.split("\n"));
for (String file : files) {
// splitting with \n can still leave \r on some OS/browsers
Expand All @@ -1883,7 +1899,7 @@ public FormValidation doValidateExcludedFiles(StaplerRequest req) {
// (ie, in case there are special characters that the user hasn't escaped properly)
try {
file = file.replaceAll("\\$\\{[^\\}]*\\}","SOMEVARIABLE");
doesFilenameMatchP4Pattern("somefile", file);
doesFilenameMatchP4Pattern("somefile", file, excludedFilesCaseSensitivity);
}
catch (PatternSyntaxException pse) {
return FormValidation.error("Invalid file spec ["+file+"]: " + pse.getMessage());
Expand Down Expand Up @@ -2601,6 +2617,14 @@ public void setDontUpdateServer(boolean dontUpdateServer) {
this.dontUpdateServer = dontUpdateServer;
}

public boolean getExcludedFilesCaseSensitivity() {
return excludedFilesCaseSensitivity;
}

public void setExcludedFilesCaseSensitivity(boolean excludedFilesCaseSensitivity) {
this.excludedFilesCaseSensitivity = excludedFilesCaseSensitivity;
}

public List<String> getAllLineEndChoices(){
List<String> allChoices = ((PerforceSCMDescriptor)this.getDescriptor()).getAllLineEndChoices();
ArrayList<String> choices = new ArrayList<String>();
Expand Down
Expand Up @@ -184,6 +184,11 @@
<f:entry title="Poll Exclude File(s)" help="/plugin/perforce/help/excludeFiles.html">
<f:textarea field="excludedFiles" id="excludedFiles"
checkUrl="'${rootURL}/scm/PerforceSCM/validateExcludedFiles?excludedFiles='+escape(this.value)" />
<table width="100%">
<f:entry title="Case Sensitive File Matching">
<f:checkbox field="excludedFilesCaseSensitivity" id="excludedFilesCaseSensitivity" default="true"/>
</f:entry>
</table>
</f:entry>

<f:optionalBlock name="p4.useViewMask" title="Use View Mask" help="/plugin/perforce/help/useViewMask.html" checked="${instance.useViewMask}">
Expand Down
37 changes: 20 additions & 17 deletions src/test/java/hudson/plugins/perforce/PerforceSCMTest.java
Expand Up @@ -27,7 +27,7 @@ public void testConfigRoundtrip() throws Exception {
PerforceSCM scm = new PerforceSCM(
"user", "pass", "client", "port", "", "exe", "sysRoot",
"sysDrive", "label", "counter", "shared", "charset", "charset2", false, true, true, true, true, true, false,
false, true, false, false, false, false, "${basename}", 0, browser, "exclude_user", "exclude_file");
false, true, false, false, false, false, "${basename}", 0, browser, "exclude_user", "exclude_file", true);
scm.setProjectPath("path");
project.setScm(scm);

Expand All @@ -48,7 +48,7 @@ public void testConfigRoundtripWithStream() throws Exception {
PerforceSCM scm = new PerforceSCM(
"user", "pass", "client", "port", "", "exe", "sysRoot",
"sysDrive", "label", "counter", "shared", "charset", "charset2", false, true, true, true, true, true, false,
false, true, false, false, false, false, "${basename}", 0, browser, "exclude_user", "exclude_file");
false, true, false, false, false, false, "${basename}", 0, browser, "exclude_user", "exclude_file", true);
scm.setP4Stream("stream");
scm.setUseStreamDepot(true);
project.setScm(scm);
Expand All @@ -75,7 +75,7 @@ public void testConfigPasswordEnctyptionAndDecription() throws Exception {
PerforceSCM scm = new PerforceSCM(
"user", password, "client", "port", "", "test_installation", "sysRoot",
"sysDrive", "label", "counter", "shared", "charset", "charset2", false, true, true, true, true, true, false,
false, true, false, false, false, false, "${basename}", 0, browser, "exclude_user", "exclude_file");
false, true, false, false, false, false, "${basename}", 0, browser, "exclude_user", "exclude_file", true);
scm.setProjectPath("path");
project.setScm(scm);

Expand Down Expand Up @@ -104,7 +104,7 @@ public void testDepotContainsUnencryptedPassword() throws Exception {
PerforceSCM scm = new PerforceSCM(
"user", password, "client", "port", "", "test_installation", "sysRoot",
"sysDrive", "label", "counter", "shared", "charset", "charset2", false, true, true, true, true, true, false,
false, true, false, false, false, false, "${basename}", 0, browser, "exclude_user", "exclude_file");
false, true, false, false, false, false, "${basename}", 0, browser, "exclude_user", "exclude_file", true);
scm.setProjectPath("path");
project.setScm(scm);

Expand All @@ -122,7 +122,7 @@ public void testConfigSaveReloadAndSaveDoesNotDoubleEncryptThePassword() throws
PerforceSCM scm = new PerforceSCM(
"user", password, "client", "port", "", "test_installation", "sysRoot",
"sysDrive", "label", "counter", "shared", "charset", "charset2", false, true, true, true, true, true, false,
false, true, false, false, false, false, "${basename}", 0, browser, "exclude_user", "exclude_file");
false, true, false, false, false, false, "${basename}", 0, browser, "exclude_user", "exclude_file", true);
scm.setProjectPath("path");
project.setScm(scm);

Expand Down Expand Up @@ -205,40 +205,43 @@ public void testWindowsPathName() throws Exception {
public void testFilenameP4PatternMatcher() throws Exception {
assertEquals(true, PerforceSCM.doesFilenameMatchP4Pattern(
"//depot/somefile/testfile",
"//depot/..."));
"//depot/...",true));
assertEquals(false, PerforceSCM.doesFilenameMatchP4Pattern(
"//depot3/somefile/testfile",
"//depot/..."));
"//depot/...",true));
assertEquals(true, PerforceSCM.doesFilenameMatchP4Pattern(
"//depot/somefile/testfile",
"//depot/.../testfile"));
"//depot/.../testfile",true));
assertEquals(true, PerforceSCM.doesFilenameMatchP4Pattern(
"//depot/somefile/testfile",
"//depot/*/testfile"));
"//depot/*/testfile",true));
assertEquals(true, PerforceSCM.doesFilenameMatchP4Pattern(
"//depot/somefile/testfile",
"//depot/some*/..."));
"//depot/some*/...",true));
assertEquals(true, PerforceSCM.doesFilenameMatchP4Pattern(
"//depot/somefile/testfile",
"//depot/*file..."));
"//depot/*file...",true));
assertEquals(true, PerforceSCM.doesFilenameMatchP4Pattern(
"//depot/somefile/testfile",
"//depot/.../*"));
"//depot/.../*",true));
assertEquals(false, PerforceSCM.doesFilenameMatchP4Pattern(
"//depot/somefile/testfile",
"//depot/somefile/test"));
"//depot/somefile/test",true));
assertEquals(true, PerforceSCM.doesFilenameMatchP4Pattern(
"//depot/somefile/testfile",
"//depot/somefile/testfile"));
"//depot/somefile/testfile",true));
assertEquals(false, PerforceSCM.doesFilenameMatchP4Pattern(
"//depot/somefile/testfile",
"//depot/.../test"));
"//depot/.../test",true));
assertEquals(false, PerforceSCM.doesFilenameMatchP4Pattern(
"//depot/somefile/testfile",
"//depot/.../*test"));
"//depot/.../*test",true));
assertEquals(false, PerforceSCM.doesFilenameMatchP4Pattern(
"//depot/somefile/testfile",
"//depot/.../file*"));
"//depot/.../file*",true));
assertEquals(true, PerforceSCM.doesFilenameMatchP4Pattern(
"//depot/SomeFile/testFile",
"//depot/s.../testfile", false));
}

/** Test migration from "p4Exe" field to tool installation.
Expand Down

0 comments on commit d825526

Please sign in to comment.