From f85b230a253329c55be933115102f48595fb5c9b Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Sat, 26 Nov 2022 15:17:54 -0700 Subject: [PATCH 1/2] Duplicate null pointer exception in a test --- .../jenkins/plugins/git/GitSCMFileSystemTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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==';'; From 67dcdce97c7f7d8bb36b548a897474e74310317a Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Sat, 26 Nov 2022 15:19:51 -0700 Subject: [PATCH 2/2] [JENKINS-70158] Fix null pointer exception in GitSCMFileSystem When rev was not null and env was null, a null pointer exception was thrown as the code tried to use the null env to expand environment variables in the rev argument. Test duplicates the null pointer exception and this commit resolves the null pointer exception. --- src/main/java/jenkins/plugins/git/GitSCMFileSystem.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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)) {