Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-57587] Prevent GitSCMFileSystem NPE on '*' branch name #914

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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