Skip to content

Commit

Permalink
Merge pull request #914 from MarkEWaite/avoid-NPE-on-wildcard-branch-…
Browse files Browse the repository at this point in the history
…name

[JENKINS-57587] Prevent GitSCMFileSystem NPE on '*' branch name
  • Loading branch information
MarkEWaite authored Jun 22, 2020
2 parents 0247327 + 39e2962 commit fee2130
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/jenkins/plugins/git/GitSCMFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ public boolean supports(SCM source) {
return source instanceof GitSCM
&& ((GitSCM) source).getUserRemoteConfigs().size() == 1
&& ((GitSCM) source).getBranches().size() == 1
&& !((GitSCM) source).getBranches().get(0).getName().equals("*") // JENKINS-57587
&& (
((GitSCM) source).getBranches().get(0).getName().matches(
"^((\\Q" + Constants.R_HEADS + "\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$"
Expand All @@ -263,7 +264,7 @@ public boolean supports(SCM source) {
"^((\\Q" + Constants.R_TAGS + "\\E.*)|([^/]+)|(\\*/[^/*]+(/[^/*]+)*))$"
)
);
// we only support where the branch spec is obvious
// we only support where the branch spec is obvious and not a wildcard
}

@Override
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -197,6 +198,29 @@ public void slashyBranches() throws Exception {
assertThat(file.contentAsString(), is("modified"));
}

@Issue("JENKINS-57587")
@Test
public void wildcardBranchNameCausesNPE() throws Exception {
sampleRepo.init();
sampleRepo.write("file", "contents-for-npe-when-branch-name-is-asterisk");
sampleRepo.git("commit", "--all", "--message=npe-when-branch-name-is-asterisk");
/* Non-existent branch names like 'not-a-branch', will fail
* the build early with a message that the remote ref cannot
* be found. Branch names that are valid portions of a
* refspec like '*' do not fail the build early but generate a
* null pointer exception when trying to resolve the branch
* name in the GitSCMFileSystem constructor.
*/
SCMFileSystem fs = SCMFileSystem.of(r.createFreeStyleProject(),
new GitSCM(GitSCM.createRepoList(sampleRepo.toString(), null),
Collections.singletonList(new BranchSpec("*")), // JENKINS-57587 issue here
false,
Collections.<SubmoduleConfig>emptyList(),
null, null,
Collections.<GitSCMExtension>emptyList()));
assertThat("Wildcard branch name '*' resolved to a specific checkout unexpectedly", fs, is(nullValue()));
}

@Test
@Deprecated // Testing deprecated GitSCMSource constructor
public void lastModified_Smokes() throws Exception {
Expand Down

0 comments on commit fee2130

Please sign in to comment.