Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-26842] Handle regex branch spec which does not contain '*'
Apply code changes via patch ignoring mismatched whitespace
  • Loading branch information
Joshua Johnston authored and MarkEWaite committed Feb 10, 2015
1 parent 1c397db commit 94dff14
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main/java/hudson/plugins/git/util/DefaultBuildChooser.java
Expand Up @@ -49,7 +49,7 @@ public Collection<Revision> getCandidateRevisions(boolean isPollCall, String bra

// if the branch name contains more wildcards then the simple usecase
// does not apply and we need to skip to the advanced usecase
if (branchSpec == null || branchSpec.contains("*"))
if (isAdvancedSpec(branchSpec))
return getAdvancedCandidateRevisions(isPollCall,listener,new GitUtils(listener,git),data, context);

// check if we're trying to build a specific commit
Expand Down Expand Up @@ -307,4 +307,17 @@ public String getLegacyId() {
return "Default";
}
}

/**
* Helper to determine if the branchSpec requires advanced matching
*
* - if the branch name contains more wildcards then the simple usecase
* - if the branch name should be treated as regexp
* @param branchSpec
* @return
*/
boolean isAdvancedSpec(String branchSpec) {
// null or wildcards or regexp
return (branchSpec == null || branchSpec.contains("*") || branchSpec.startsWith(":"));
}
}
9 changes: 9 additions & 0 deletions src/test/java/hudson/plugins/git/TestBranchSpec.java
Expand Up @@ -147,4 +147,13 @@ public void testUsesJavaPatternDirectlyIfPrefixedWithColon() {
assertFalse(m.matches("origin/prefix"));
assertFalse(m.matches("origin/prefix-abc"));
}

public void testUsesJavaPatternWithRepetition() {
// match pattern from JENKINS-26842
BranchSpec m = new BranchSpec(":origin/release-\\d{8}");
assertTrue(m.matches("origin/release-20150101"));
assertFalse(m.matches("origin/release-2015010"));
assertFalse(m.matches("origin/release-201501011"));
assertFalse(m.matches("origin/release-20150101-something"));
}
}
14 changes: 14 additions & 0 deletions src/test/java/hudson/plugins/git/util/DefaultBuildChooserTest.java
Expand Up @@ -27,4 +27,18 @@ public void testChooseGitRevisionToBuildByShaHash() throws Exception {
candidateRevisions = buildChooser.getCandidateRevisions(false, "aaa" + shaHashCommit1.substring(3), git, null, null, null);
assertTrue(candidateRevisions.isEmpty());
}
/**
* RegExp patterns prefixed with : should pass through to DefaultBuildChooser.getAdvancedCandidateRevisions
* @throws Exception
*/
public void testIsAdvancedSpec() throws Exception {
DefaultBuildChooser buildChooser = (DefaultBuildChooser) new GitSCM("foo").getBuildChooser();

assertFalse(buildChooser.isAdvancedSpec("origin/master"));
assertTrue(buildChooser.isAdvancedSpec("origin/master-*"));
assertTrue(buildChooser.isAdvancedSpec("origin**"));
// regexp use case
assertTrue(buildChooser.isAdvancedSpec(":origin/master"));
assertTrue(buildChooser.isAdvancedSpec(":origin/master-\\d{*}"));
}
}

0 comments on commit 94dff14

Please sign in to comment.