diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index f9535e372b..a8998fb229 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -308,7 +308,7 @@ static HeadNameResult calculate(@NonNull BranchSpec branchSpec, } String headName; - if (rev != null) { + if (rev != null && env != null) { headName = env.expand(rev.getHead().getName()); } else { if (branchSpecExpandedName.startsWith(prefix)) { diff --git a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java index 59b2bf2875..64eb3718d5 100644 --- a/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java +++ b/src/test/java/jenkins/plugins/git/GitSCMFileSystemTest.java @@ -462,6 +462,21 @@ public void calculate_head_name_with_env() throws Exception { assertEquals(Constants.R_HEADS, result6.prefix); } + /* GitSCMFileSystem in git plugin 4.14.0 reported a null pointer + * exception when the rev was non-null and the env was null. */ + @Issue("JENKINS-70158") + @Test + public void null_pointer_exception() throws Exception { + File gitDir = new File("."); + GitClient client = Git.with(TaskListener.NULL, new EnvVars()).in(gitDir).using("git").getClient(); + ObjectId git260 = client.revParse(GIT_2_6_0_TAG); + AbstractGitSCMSource.SCMRevisionImpl rev260 = + new AbstractGitSCMSource.SCMRevisionImpl(new SCMHead("origin"), git260.getName()); + GitSCMFileSystem.BuilderImpl.HeadNameResult result1 = GitSCMFileSystem.BuilderImpl.HeadNameResult.calculate(new BranchSpec("master-f"), rev260, null); + assertEquals("master-f", result1.headName); + assertEquals(Constants.R_HEADS, result1.prefix); + } + /** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */ private boolean isWindows() { return java.io.File.pathSeparatorChar==';';