From 62b403af0498464046822c41f9bde39066fbeb92 Mon Sep 17 00:00:00 2001 From: Jagruti Date: Sun, 14 Jan 2024 17:39:09 +0530 Subject: [PATCH 01/16] Removed leading and trailing spaces from refspec --- .../plugins/gitclient/CliGitAPIImpl.java | 64 +++++++++++-------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index f27474cb78..1121f29cbb 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -1,29 +1,15 @@ package org.jenkinsci.plugins.gitclient; -import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey; -import com.cloudbees.plugins.credentials.common.StandardCredentials; -import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; -import edu.umd.cs.findbugs.annotations.CheckForNull; -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import hudson.EnvVars; -import hudson.FilePath; -import hudson.Launcher; -import hudson.Launcher.LocalLauncher; -import hudson.Proc; -import hudson.Util; -import hudson.console.HyperlinkNote; -import hudson.model.TaskListener; -import hudson.plugins.git.Branch; -import hudson.plugins.git.GitException; -import hudson.plugins.git.GitLockFailedException; -import hudson.plugins.git.GitObject; -import hudson.plugins.git.IGitAPI; -import hudson.plugins.git.IndexEntry; -import hudson.plugins.git.Revision; -import hudson.util.ArgumentListBuilder; -import hudson.util.Secret; -import java.io.*; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.Charset; @@ -59,6 +45,30 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; + +import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey; +import com.cloudbees.plugins.credentials.common.StandardCredentials; +import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; +import edu.umd.cs.findbugs.annotations.CheckForNull; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import hudson.EnvVars; +import hudson.FilePath; +import hudson.Launcher; +import hudson.Launcher.LocalLauncher; +import hudson.Proc; +import hudson.Util; +import hudson.console.HyperlinkNote; +import hudson.model.TaskListener; +import hudson.plugins.git.Branch; +import hudson.plugins.git.GitException; +import hudson.plugins.git.GitLockFailedException; +import hudson.plugins.git.GitObject; +import hudson.plugins.git.IGitAPI; +import hudson.plugins.git.IndexEntry; +import hudson.plugins.git.Revision; +import hudson.util.ArgumentListBuilder; +import hudson.util.Secret; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; @@ -612,7 +622,7 @@ public void execute() throws GitException, InterruptedException { if (refspecs != null) { for (RefSpec rs : refspecs) { if (rs != null) { - args.add(rs.toString()); + args.add(rs.toString().trim()); } } } @@ -667,7 +677,7 @@ public void fetch(String remoteName, RefSpec... refspec) throws GitException, In if (refspec != null && refspec.length > 0) { for (RefSpec rs : refspec) { if (rs != null) { - args.add(rs.toString()); + args.add(rs.toString().trim()); } } } @@ -861,7 +871,7 @@ public void execute() throws GitException, InterruptedException { } if (refspecs == null) { - refspecs = Collections.singletonList(new RefSpec("+refs/heads/*:refs/remotes/" + origin + "/*")); + refspecs = Collections.singletonList(new RefSpec("+refs/heads/*:refs/remotes/" + origin + "/*".trim())); } fetch_().from(urIish, refspecs) .shallow(shallow) From cb655393b5f8d5bbe255e9346bc16a4d313bf8e2 Mon Sep 17 00:00:00 2001 From: Jagruti Date: Sun, 14 Jan 2024 18:02:43 +0530 Subject: [PATCH 02/16] Executed spotless #JENKINS-70303 --- .../plugins/gitclient/CliGitAPIImpl.java | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index 1121f29cbb..036ec44cf2 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -1,5 +1,28 @@ package org.jenkinsci.plugins.gitclient; +import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey; +import com.cloudbees.plugins.credentials.common.StandardCredentials; +import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; +import edu.umd.cs.findbugs.annotations.CheckForNull; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import hudson.EnvVars; +import hudson.FilePath; +import hudson.Launcher; +import hudson.Launcher.LocalLauncher; +import hudson.Proc; +import hudson.Util; +import hudson.console.HyperlinkNote; +import hudson.model.TaskListener; +import hudson.plugins.git.Branch; +import hudson.plugins.git.GitException; +import hudson.plugins.git.GitLockFailedException; +import hudson.plugins.git.GitObject; +import hudson.plugins.git.IGitAPI; +import hudson.plugins.git.IndexEntry; +import hudson.plugins.git.Revision; +import hudson.util.ArgumentListBuilder; +import hudson.util.Secret; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; @@ -45,30 +68,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; - -import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey; -import com.cloudbees.plugins.credentials.common.StandardCredentials; -import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; -import edu.umd.cs.findbugs.annotations.CheckForNull; -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import hudson.EnvVars; -import hudson.FilePath; -import hudson.Launcher; -import hudson.Launcher.LocalLauncher; -import hudson.Proc; -import hudson.Util; -import hudson.console.HyperlinkNote; -import hudson.model.TaskListener; -import hudson.plugins.git.Branch; -import hudson.plugins.git.GitException; -import hudson.plugins.git.GitLockFailedException; -import hudson.plugins.git.GitObject; -import hudson.plugins.git.IGitAPI; -import hudson.plugins.git.IndexEntry; -import hudson.plugins.git.Revision; -import hudson.util.ArgumentListBuilder; -import hudson.util.Secret; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; @@ -871,7 +870,8 @@ public void execute() throws GitException, InterruptedException { } if (refspecs == null) { - refspecs = Collections.singletonList(new RefSpec("+refs/heads/*:refs/remotes/" + origin + "/*".trim())); + refspecs = Collections.singletonList( + new RefSpec("+refs/heads/*:refs/remotes/" + origin + "/*".trim())); } fetch_().from(urIish, refspecs) .shallow(shallow) From 2b04ed4bc052ed17545022ff373a9fb99c59077c Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Sun, 14 Jan 2024 06:00:12 -0700 Subject: [PATCH 03/16] Test leading and trailing space RefSpec in clone --- .../plugins/gitclient/GitClientCloneTest.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/jenkinsci/plugins/gitclient/GitClientCloneTest.java b/src/test/java/org/jenkinsci/plugins/gitclient/GitClientCloneTest.java index c81b9aa7ad..15e0e4d0c0 100644 --- a/src/test/java/org/jenkinsci/plugins/gitclient/GitClientCloneTest.java +++ b/src/test/java/org/jenkinsci/plugins/gitclient/GitClientCloneTest.java @@ -40,6 +40,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; +import org.jvnet.hudson.test.Issue; @RunWith(Parameterized.class) public class GitClientCloneTest { @@ -328,11 +329,16 @@ public void test_clone_refspec() throws Exception { })); } + private String randomSpace() { + return random.nextBoolean() ? " " : ""; + } + @Test + @Issue("JENKINS-70303") // spaces at start or end of refspec should be ignored public void test_clone_refspecs() throws Exception { List refspecs = Arrays.asList( - new RefSpec("+refs/heads/master:refs/remotes/origin/master"), - new RefSpec("+refs/heads/1.4.x:refs/remotes/origin/1.4.x")); + new RefSpec(randomSpace() + "+refs/heads/master:refs/remotes/origin/master" + randomSpace()), + new RefSpec(randomSpace() + "+refs/heads/1.4.x:refs/remotes/origin/1.4.x" + randomSpace())); testGitClient .clone_() .url(workspace.localMirror()) From bce6f5882655a52a1fba19b32e5fb89f35a9c84f Mon Sep 17 00:00:00 2001 From: Jagruti Tiwari Date: Sun, 14 Jan 2024 18:43:18 +0530 Subject: [PATCH 04/16] Update src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java Co-authored-by: Mark Waite --- .../java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index 036ec44cf2..6f65f01d33 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -871,7 +871,7 @@ public void execute() throws GitException, InterruptedException { if (refspecs == null) { refspecs = Collections.singletonList( - new RefSpec("+refs/heads/*:refs/remotes/" + origin + "/*".trim())); + new RefSpec("+refs/heads/*:refs/remotes/" + origin.trim() + "/*")); } fetch_().from(urIish, refspecs) .shallow(shallow) From 31ecb75f53a9b222d0145b774572a87714dd5403 Mon Sep 17 00:00:00 2001 From: Jagruti Date: Sun, 14 Jan 2024 20:42:17 +0530 Subject: [PATCH 05/16] Fixed test case for git #70303 --- .../plugins/gitclient/CliGitAPIImpl.java | 58 ++++++++++--------- .../plugins/gitclient/JGitAPIImpl.java | 27 ++++----- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index 6f65f01d33..240167d849 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -1,28 +1,5 @@ package org.jenkinsci.plugins.gitclient; -import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey; -import com.cloudbees.plugins.credentials.common.StandardCredentials; -import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; -import edu.umd.cs.findbugs.annotations.CheckForNull; -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import hudson.EnvVars; -import hudson.FilePath; -import hudson.Launcher; -import hudson.Launcher.LocalLauncher; -import hudson.Proc; -import hudson.Util; -import hudson.console.HyperlinkNote; -import hudson.model.TaskListener; -import hudson.plugins.git.Branch; -import hudson.plugins.git.GitException; -import hudson.plugins.git.GitLockFailedException; -import hudson.plugins.git.GitObject; -import hudson.plugins.git.IGitAPI; -import hudson.plugins.git.IndexEntry; -import hudson.plugins.git.Revision; -import hudson.util.ArgumentListBuilder; -import hudson.util.Secret; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; @@ -68,6 +45,30 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; + +import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey; +import com.cloudbees.plugins.credentials.common.StandardCredentials; +import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; +import edu.umd.cs.findbugs.annotations.CheckForNull; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import hudson.EnvVars; +import hudson.FilePath; +import hudson.Launcher; +import hudson.Launcher.LocalLauncher; +import hudson.Proc; +import hudson.Util; +import hudson.console.HyperlinkNote; +import hudson.model.TaskListener; +import hudson.plugins.git.Branch; +import hudson.plugins.git.GitException; +import hudson.plugins.git.GitLockFailedException; +import hudson.plugins.git.GitObject; +import hudson.plugins.git.IGitAPI; +import hudson.plugins.git.IndexEntry; +import hudson.plugins.git.Revision; +import hudson.util.ArgumentListBuilder; +import hudson.util.Secret; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; @@ -798,7 +799,11 @@ public CloneCommand depth(Integer depth) { @Override public CloneCommand refspecs(List refspecs) { - this.refspecs = new ArrayList<>(refspecs); + List refSpecsList = new ArrayList(); + for (RefSpec ref: refspecs) { + refSpecsList.add(new RefSpec(ref.toString().trim())); + } + this.refspecs = refSpecsList; return this; } @@ -870,8 +875,7 @@ public void execute() throws GitException, InterruptedException { } if (refspecs == null) { - refspecs = Collections.singletonList( - new RefSpec("+refs/heads/*:refs/remotes/" + origin.trim() + "/*")); + refspecs = Collections.singletonList(new RefSpec("+refs/heads/*:refs/remotes/" + origin + "/*".trim())); } fetch_().from(urIish, refspecs) .shallow(shallow) @@ -881,7 +885,7 @@ public void execute() throws GitException, InterruptedException { .execute(); setRemoteUrl(origin, url); for (RefSpec refSpec : refspecs) { - launchCommand("config", "--add", "remote." + origin + ".fetch", refSpec.toString()); + launchCommand("config", "--add", "remote." + origin + ".fetch", refSpec.toString().trim()); } } }; diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java index 07612df4f6..0c9d18cd9e 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java @@ -14,18 +14,6 @@ import static org.jenkinsci.plugins.gitclient.CliGitAPIImpl.TIMEOUT; import static org.jenkinsci.plugins.gitclient.CliGitAPIImpl.TIMEOUT_LOG_PREFIX; -import com.cloudbees.plugins.credentials.common.StandardCredentials; -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import hudson.FilePath; -import hudson.Util; -import hudson.model.TaskListener; -import hudson.plugins.git.Branch; -import hudson.plugins.git.GitException; -import hudson.plugins.git.GitLockFailedException; -import hudson.plugins.git.GitObject; -import hudson.plugins.git.IndexEntry; -import hudson.plugins.git.Revision; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -49,6 +37,19 @@ import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; + +import com.cloudbees.plugins.credentials.common.StandardCredentials; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import hudson.FilePath; +import hudson.Util; +import hudson.model.TaskListener; +import hudson.plugins.git.Branch; +import hudson.plugins.git.GitException; +import hudson.plugins.git.GitLockFailedException; +import hudson.plugins.git.GitObject; +import hudson.plugins.git.IndexEntry; +import hudson.plugins.git.Revision; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.time.FastDateFormat; import org.eclipse.jgit.api.AddNoteCommand; @@ -1550,7 +1551,7 @@ public void execute() throws GitException { FetchCommand fetch = new Git(repository) .fetch() .setProgressMonitor(new JGitProgressMonitor(listener)) - .setRemote(url) + .setRemote(url.trim()) .setCredentialsProvider(getProvider()) .setTagOpt(tags ? TagOpt.FETCH_TAGS : TagOpt.NO_TAGS) .setRefSpecs(refspecs); From 01afbe3e505d2896e3a1766abf2a026e7ec8e4e3 Mon Sep 17 00:00:00 2001 From: Jagruti Date: Sun, 14 Jan 2024 21:47:40 +0530 Subject: [PATCH 06/16] Fixed test cases #70303 --- .../plugins/gitclient/CliGitAPIImpl.java | 53 +++++++++---------- .../plugins/gitclient/JGitAPIImpl.java | 31 ++++++----- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index 240167d849..9066be5e37 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -1,5 +1,28 @@ package org.jenkinsci.plugins.gitclient; +import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey; +import com.cloudbees.plugins.credentials.common.StandardCredentials; +import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; +import edu.umd.cs.findbugs.annotations.CheckForNull; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import hudson.EnvVars; +import hudson.FilePath; +import hudson.Launcher; +import hudson.Launcher.LocalLauncher; +import hudson.Proc; +import hudson.Util; +import hudson.console.HyperlinkNote; +import hudson.model.TaskListener; +import hudson.plugins.git.Branch; +import hudson.plugins.git.GitException; +import hudson.plugins.git.GitLockFailedException; +import hudson.plugins.git.GitObject; +import hudson.plugins.git.IGitAPI; +import hudson.plugins.git.IndexEntry; +import hudson.plugins.git.Revision; +import hudson.util.ArgumentListBuilder; +import hudson.util.Secret; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; @@ -45,30 +68,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; - -import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey; -import com.cloudbees.plugins.credentials.common.StandardCredentials; -import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; -import edu.umd.cs.findbugs.annotations.CheckForNull; -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import hudson.EnvVars; -import hudson.FilePath; -import hudson.Launcher; -import hudson.Launcher.LocalLauncher; -import hudson.Proc; -import hudson.Util; -import hudson.console.HyperlinkNote; -import hudson.model.TaskListener; -import hudson.plugins.git.Branch; -import hudson.plugins.git.GitException; -import hudson.plugins.git.GitLockFailedException; -import hudson.plugins.git.GitObject; -import hudson.plugins.git.IGitAPI; -import hudson.plugins.git.IndexEntry; -import hudson.plugins.git.Revision; -import hudson.util.ArgumentListBuilder; -import hudson.util.Secret; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; @@ -800,7 +799,7 @@ public CloneCommand depth(Integer depth) { @Override public CloneCommand refspecs(List refspecs) { List refSpecsList = new ArrayList(); - for (RefSpec ref: refspecs) { + for (RefSpec ref : refspecs) { refSpecsList.add(new RefSpec(ref.toString().trim())); } this.refspecs = refSpecsList; @@ -875,7 +874,7 @@ public void execute() throws GitException, InterruptedException { } if (refspecs == null) { - refspecs = Collections.singletonList(new RefSpec("+refs/heads/*:refs/remotes/" + origin + "/*".trim())); + refspecs = Collections.singletonList(new RefSpec("+refs/heads/*:refs/remotes/" + origin + "/*")); } fetch_().from(urIish, refspecs) .shallow(shallow) @@ -885,7 +884,7 @@ public void execute() throws GitException, InterruptedException { .execute(); setRemoteUrl(origin, url); for (RefSpec refSpec : refspecs) { - launchCommand("config", "--add", "remote." + origin + ".fetch", refSpec.toString().trim()); + launchCommand("config", "--add", "remote." + origin + ".fetch", refSpec.toString()); } } }; diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java index 0c9d18cd9e..62d35076aa 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java @@ -14,6 +14,18 @@ import static org.jenkinsci.plugins.gitclient.CliGitAPIImpl.TIMEOUT; import static org.jenkinsci.plugins.gitclient.CliGitAPIImpl.TIMEOUT_LOG_PREFIX; +import com.cloudbees.plugins.credentials.common.StandardCredentials; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import hudson.FilePath; +import hudson.Util; +import hudson.model.TaskListener; +import hudson.plugins.git.Branch; +import hudson.plugins.git.GitException; +import hudson.plugins.git.GitLockFailedException; +import hudson.plugins.git.GitObject; +import hudson.plugins.git.IndexEntry; +import hudson.plugins.git.Revision; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -37,19 +49,6 @@ import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; - -import com.cloudbees.plugins.credentials.common.StandardCredentials; -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import hudson.FilePath; -import hudson.Util; -import hudson.model.TaskListener; -import hudson.plugins.git.Branch; -import hudson.plugins.git.GitException; -import hudson.plugins.git.GitLockFailedException; -import hudson.plugins.git.GitObject; -import hudson.plugins.git.IndexEntry; -import hudson.plugins.git.Revision; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.time.FastDateFormat; import org.eclipse.jgit.api.AddNoteCommand; @@ -1433,7 +1432,11 @@ public CloneCommand reference(String reference) { @Override public CloneCommand refspecs(List refspecs) { - this.refspecs = new ArrayList<>(refspecs); + List refSpecsList = new ArrayList(); + for (RefSpec ref : refspecs) { + refSpecsList.add(new RefSpec(ref.toString().trim())); + } + this.refspecs = refSpecsList; return this; } From 4dabf5e5cff0df8fdcece4f275d06de19968f207 Mon Sep 17 00:00:00 2001 From: Jagruti Date: Sun, 14 Jan 2024 22:12:54 +0530 Subject: [PATCH 07/16] Added space in GitClientFetchTest case to test the code changes --- .../plugins/gitclient/CliGitCommand.java | 18 ++++++++++------- .../plugins/gitclient/GitClientFetchTest.java | 20 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/test/java/org/jenkinsci/plugins/gitclient/CliGitCommand.java b/src/test/java/org/jenkinsci/plugins/gitclient/CliGitCommand.java index a0ace530e4..06465f22e6 100644 --- a/src/test/java/org/jenkinsci/plugins/gitclient/CliGitCommand.java +++ b/src/test/java/org/jenkinsci/plugins/gitclient/CliGitCommand.java @@ -1,12 +1,9 @@ package org.jenkinsci.plugins.gitclient; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; -import hudson.EnvVars; -import hudson.Launcher; -import hudson.model.TaskListener; -import hudson.util.ArgumentListBuilder; -import hudson.util.StreamTaskListener; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -16,13 +13,20 @@ import java.util.List; import java.util.concurrent.TimeUnit; +import hudson.EnvVars; +import hudson.Launcher; +import hudson.model.TaskListener; +import hudson.util.ArgumentListBuilder; +import hudson.util.StreamTaskListener; + /** * Run a command line git command, return output as array of String, optionally * assert on contents of command output. * * @author Mark Waite */ -class CliGitCommand { +class +CliGitCommand { private final TaskListener listener; private final transient Launcher launcher; diff --git a/src/test/java/org/jenkinsci/plugins/gitclient/GitClientFetchTest.java b/src/test/java/org/jenkinsci/plugins/gitclient/GitClientFetchTest.java index 40098a1215..69fead54e6 100644 --- a/src/test/java/org/jenkinsci/plugins/gitclient/GitClientFetchTest.java +++ b/src/test/java/org/jenkinsci/plugins/gitclient/GitClientFetchTest.java @@ -12,14 +12,9 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; -import static org.hamcrest.io.FileMatchers.*; +import static org.hamcrest.io.FileMatchers.anExistingFile; import static org.junit.Assert.assertThrows; -import hudson.Util; -import hudson.model.TaskListener; -import hudson.plugins.git.Branch; -import hudson.plugins.git.GitException; -import hudson.util.StreamTaskListener; import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -33,6 +28,12 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; + +import hudson.Util; +import hudson.model.TaskListener; +import hudson.plugins.git.Branch; +import hudson.plugins.git.GitException; +import hudson.util.StreamTaskListener; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.transport.RefSpec; @@ -109,6 +110,7 @@ public static void computeDefaultBranchName() throws Exception { .in(configDir) .using("git") .getClient()); + String[] output = getDefaultBranchNameCmd.runWithoutAssert("config", "--get", "init.defaultBranch"); for (String s : output) { String result = s.trim(); @@ -153,6 +155,10 @@ public void setUpRepositories() throws Exception { cliGitCommand.run("config", "user.email", "email.by.git.client.test@example.com"); } + private String randomSpace() { + return random.nextBoolean() ? " " : ""; + } + /* Workspace -> original repo, bareWorkspace -> bare repo and newAreaWorkspace -> newArea repo */ @Test public void test_fetch() throws Exception { @@ -215,7 +221,7 @@ public void test_fetch() throws Exception { is(commit2)); /* Fetch new change into newArea repo */ - RefSpec defaultRefSpec = new RefSpec("+refs/heads/*:refs/remotes/origin/*"); + RefSpec defaultRefSpec = new RefSpec(randomSpace() + "+refs/heads/*:refs/remotes/origin/*" + randomSpace()); List refSpecs = new ArrayList<>(); refSpecs.add(defaultRefSpec); newAreaWorkspace.launchCommand("git", "config", "fetch.prune", "false"); From 03f01ffe8710d24b37a00f984e3822d55676cc4a Mon Sep 17 00:00:00 2001 From: Jagruti Date: Sun, 14 Jan 2024 22:14:13 +0530 Subject: [PATCH 08/16] Fixed styling --- .../jenkinsci/plugins/gitclient/CliGitCommand.java | 14 ++++++-------- .../plugins/gitclient/GitClientFetchTest.java | 11 +++++------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/test/java/org/jenkinsci/plugins/gitclient/CliGitCommand.java b/src/test/java/org/jenkinsci/plugins/gitclient/CliGitCommand.java index 06465f22e6..799c92b26e 100644 --- a/src/test/java/org/jenkinsci/plugins/gitclient/CliGitCommand.java +++ b/src/test/java/org/jenkinsci/plugins/gitclient/CliGitCommand.java @@ -4,6 +4,11 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import hudson.EnvVars; +import hudson.Launcher; +import hudson.model.TaskListener; +import hudson.util.ArgumentListBuilder; +import hudson.util.StreamTaskListener; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -13,20 +18,13 @@ import java.util.List; import java.util.concurrent.TimeUnit; -import hudson.EnvVars; -import hudson.Launcher; -import hudson.model.TaskListener; -import hudson.util.ArgumentListBuilder; -import hudson.util.StreamTaskListener; - /** * Run a command line git command, return output as array of String, optionally * assert on contents of command output. * * @author Mark Waite */ -class -CliGitCommand { +class CliGitCommand { private final TaskListener listener; private final transient Launcher launcher; diff --git a/src/test/java/org/jenkinsci/plugins/gitclient/GitClientFetchTest.java b/src/test/java/org/jenkinsci/plugins/gitclient/GitClientFetchTest.java index 69fead54e6..52ed8f0f23 100644 --- a/src/test/java/org/jenkinsci/plugins/gitclient/GitClientFetchTest.java +++ b/src/test/java/org/jenkinsci/plugins/gitclient/GitClientFetchTest.java @@ -15,6 +15,11 @@ import static org.hamcrest.io.FileMatchers.anExistingFile; import static org.junit.Assert.assertThrows; +import hudson.Util; +import hudson.model.TaskListener; +import hudson.plugins.git.Branch; +import hudson.plugins.git.GitException; +import hudson.util.StreamTaskListener; import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -28,12 +33,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; - -import hudson.Util; -import hudson.model.TaskListener; -import hudson.plugins.git.Branch; -import hudson.plugins.git.GitException; -import hudson.util.StreamTaskListener; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.transport.RefSpec; From b9f95b0459cfc49f050209cab52ae732f2dbbfc5 Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Sun, 14 Jan 2024 14:08:58 -0700 Subject: [PATCH 09/16] Simplify the ArrayList declaration --- .../java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index 9066be5e37..61c51a2132 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -798,7 +798,7 @@ public CloneCommand depth(Integer depth) { @Override public CloneCommand refspecs(List refspecs) { - List refSpecsList = new ArrayList(); + List refSpecsList = new ArrayList<>(); for (RefSpec ref : refspecs) { refSpecsList.add(new RefSpec(ref.toString().trim())); } From 3fb3ae280a9e2de6ff031b97658ee4768a7e6630 Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Sun, 14 Jan 2024 15:58:33 -0700 Subject: [PATCH 10/16] Do not trim remote name --- src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java index 62d35076aa..b30e5aa0d7 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java @@ -1554,7 +1554,7 @@ public void execute() throws GitException { FetchCommand fetch = new Git(repository) .fetch() .setProgressMonitor(new JGitProgressMonitor(listener)) - .setRemote(url.trim()) + .setRemote(url) .setCredentialsProvider(getProvider()) .setTagOpt(tags ? TagOpt.FETCH_TAGS : TagOpt.NO_TAGS) .setRefSpecs(refspecs); From 08cb1ebbd632c6b18714e571565a9dcb0e27eb70 Mon Sep 17 00:00:00 2001 From: Jagruti Date: Fri, 26 Jan 2024 19:00:53 +0530 Subject: [PATCH 11/16] Trimmed refpsecs at different implementation points --- src/main/java/hudson/plugins/git/GitAPI.java | 23 +++++--- .../plugins/gitclient/CliGitAPIImpl.java | 57 +++++++++++-------- .../plugins/gitclient/JGitAPIImpl.java | 39 +++++++------ .../gitclient/LegacyCompatibleGitAPIImpl.java | 19 ++++--- .../plugins/gitclient/RemoteGitImpl.java | 47 +++++++++------ 5 files changed, 111 insertions(+), 74 deletions(-) diff --git a/src/main/java/hudson/plugins/git/GitAPI.java b/src/main/java/hudson/plugins/git/GitAPI.java index e5c44ff8ea..b3ecb0d1b9 100644 --- a/src/main/java/hudson/plugins/git/GitAPI.java +++ b/src/main/java/hudson/plugins/git/GitAPI.java @@ -1,12 +1,15 @@ package hudson.plugins.git; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + import edu.umd.cs.findbugs.annotations.NonNull; import hudson.EnvVars; import hudson.FilePath; import hudson.model.TaskListener; -import java.io.*; -import java.util.List; -import java.util.Set; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.Repository; @@ -113,9 +116,9 @@ public String getRemoteUrl(String name) throws GitException, InterruptedExceptio @Override public void push(String remoteName, String refspec) throws GitException, InterruptedException { if (Git.USE_CLI) { - super.push(remoteName, refspec); + super.push(remoteName, refspec.trim()); } else { - jgit.push(remoteName, refspec); + jgit.push(remoteName, refspec.trim()); } } @@ -368,10 +371,16 @@ public List showRevision(ObjectId r) throws GitException { @SuppressWarnings("deprecation") public void fetch(URIish url, List refspecs) throws GitException, InterruptedException { /* Intentionally using the deprecated method because the replacement method is not serializable. */ + List trimmedRefSpecs = new ArrayList<>(); + for (RefSpec rs : refspecs) { + if (rs != null) { + trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); + } + } if (Git.USE_CLI) { - super.fetch(url, refspecs); + super.fetch(url, trimmedRefSpecs); } else { - jgit.fetch(url, refspecs); + jgit.fetch(url, trimmedRefSpecs); } } diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index 61c51a2132..c67c177ca1 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -1,28 +1,5 @@ package org.jenkinsci.plugins.gitclient; -import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey; -import com.cloudbees.plugins.credentials.common.StandardCredentials; -import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; -import edu.umd.cs.findbugs.annotations.CheckForNull; -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import hudson.EnvVars; -import hudson.FilePath; -import hudson.Launcher; -import hudson.Launcher.LocalLauncher; -import hudson.Proc; -import hudson.Util; -import hudson.console.HyperlinkNote; -import hudson.model.TaskListener; -import hudson.plugins.git.Branch; -import hudson.plugins.git.GitException; -import hudson.plugins.git.GitLockFailedException; -import hudson.plugins.git.GitObject; -import hudson.plugins.git.IGitAPI; -import hudson.plugins.git.IndexEntry; -import hudson.plugins.git.Revision; -import hudson.util.ArgumentListBuilder; -import hudson.util.Secret; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; @@ -68,6 +45,30 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; + +import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey; +import com.cloudbees.plugins.credentials.common.StandardCredentials; +import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; +import edu.umd.cs.findbugs.annotations.CheckForNull; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import hudson.EnvVars; +import hudson.FilePath; +import hudson.Launcher; +import hudson.Launcher.LocalLauncher; +import hudson.Proc; +import hudson.Util; +import hudson.console.HyperlinkNote; +import hudson.model.TaskListener; +import hudson.plugins.git.Branch; +import hudson.plugins.git.GitException; +import hudson.plugins.git.GitLockFailedException; +import hudson.plugins.git.GitObject; +import hudson.plugins.git.IGitAPI; +import hudson.plugins.git.IndexEntry; +import hudson.plugins.git.Revision; +import hudson.util.ArgumentListBuilder; +import hudson.util.Secret; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; @@ -533,7 +534,13 @@ public FetchCommand fetch_() { @Override public FetchCommand from(URIish remote, List refspecs) { this.url = remote; - this.refspecs = refspecs; + List trimmedRefSpecs = new ArrayList<>(); + for (RefSpec rs : refspecs) { + if (rs != null) { + trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); + } + } + this.refspecs = trimmedRefSpecs; return this; } @@ -2894,7 +2901,7 @@ public PushCommand to(URIish remote) { @Override public PushCommand ref(String refspec) { - this.refspec = refspec; + this.refspec = refspec.trim(); return this; } diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java index b30e5aa0d7..3d13dc911e 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java @@ -14,18 +14,6 @@ import static org.jenkinsci.plugins.gitclient.CliGitAPIImpl.TIMEOUT; import static org.jenkinsci.plugins.gitclient.CliGitAPIImpl.TIMEOUT_LOG_PREFIX; -import com.cloudbees.plugins.credentials.common.StandardCredentials; -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import hudson.FilePath; -import hudson.Util; -import hudson.model.TaskListener; -import hudson.plugins.git.Branch; -import hudson.plugins.git.GitException; -import hudson.plugins.git.GitLockFailedException; -import hudson.plugins.git.GitObject; -import hudson.plugins.git.IndexEntry; -import hudson.plugins.git.Revision; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -49,6 +37,19 @@ import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; + +import com.cloudbees.plugins.credentials.common.StandardCredentials; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import hudson.FilePath; +import hudson.Util; +import hudson.model.TaskListener; +import hudson.plugins.git.Branch; +import hudson.plugins.git.GitException; +import hudson.plugins.git.GitLockFailedException; +import hudson.plugins.git.GitObject; +import hudson.plugins.git.IndexEntry; +import hudson.plugins.git.Revision; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.time.FastDateFormat; import org.eclipse.jgit.api.AddNoteCommand; @@ -624,7 +625,13 @@ public org.jenkinsci.plugins.gitclient.FetchCommand fetch_() { @Override public org.jenkinsci.plugins.gitclient.FetchCommand from(URIish remote, List refspecs) { this.url = remote; - this.refspecs = refspecs; + List trimmedRefSpecs = new ArrayList(); + for (RefSpec rs : refspecs) { + if (rs != null) { + trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); + } + } + this.refspecs = trimmedRefSpecs; return this; } @@ -1996,7 +2003,7 @@ public PushCommand to(URIish remote) { @Override public PushCommand ref(String refspec) { - this.refspec = refspec; + this.refspec = refspec.trim(); return this; } @@ -2027,8 +2034,8 @@ public PushCommand timeout(Integer timeout) { public void execute() throws GitException { try (Repository repo = getRepository()) { RefSpec ref = - (refspec != null) ? new RefSpec(fixRefSpec(refspec, repo)) : Transport.REFSPEC_PUSH_ALL; - listener.getLogger().println("RefSpec is \"" + ref + "\"."); + (refspec != null) ? new RefSpec(fixRefSpec(refspec.trim(), repo)) : Transport.REFSPEC_PUSH_ALL; + listener.getLogger().println("RefSpec is \"" + ref.toString().trim() + "\"."); Git g = git(repo); Config config = g.getRepository().getConfig(); if (remote == null) { diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/LegacyCompatibleGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/LegacyCompatibleGitAPIImpl.java index 3d6b46d049..2168400062 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/LegacyCompatibleGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/LegacyCompatibleGitAPIImpl.java @@ -3,6 +3,13 @@ import static java.util.Arrays.copyOfRange; import static org.apache.commons.lang.StringUtils.join; +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + import hudson.model.TaskListener; import hudson.plugins.git.GitException; import hudson.plugins.git.IGitAPI; @@ -10,12 +17,6 @@ import hudson.plugins.git.Revision; import hudson.plugins.git.Tag; import hudson.remoting.Channel; -import java.io.File; -import java.io.IOException; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; @@ -107,7 +108,7 @@ public void setupSubmoduleUrls(String remote, TaskListener listener) throws GitE @Override @Deprecated public void fetch(String repository, String refspec) throws GitException, InterruptedException { - fetch(repository, new RefSpec(refspec)); + fetch(repository, new RefSpec(refspec.trim())); } /** {@inheritDoc} */ @@ -146,7 +147,7 @@ public void reset() throws GitException, InterruptedException { @Override @Deprecated public void push(URIish url, String refspec) throws GitException, InterruptedException { - push().ref(refspec).to(url).execute(); + push().ref(refspec.trim()).to(url).execute(); } /** {@inheritDoc} */ @@ -159,7 +160,7 @@ public void push(String remoteName, String refspec) throws GitException, Interru } try { - push(new URIish(url), refspec); + push(new URIish(url), refspec.trim()); } catch (URISyntaxException e) { throw new GitException("bad repository URL", e); } diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java index 8b00cb2924..e9faad3db5 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java @@ -1,5 +1,18 @@ package org.jenkinsci.plugins.gitclient; +import java.io.IOException; +import java.io.OutputStream; +import java.io.Serializable; +import java.io.Writer; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; + import com.cloudbees.plugins.credentials.CredentialsProvider; import com.cloudbees.plugins.credentials.common.StandardCredentials; import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials; @@ -16,18 +29,6 @@ import hudson.remoting.Channel; import hudson.remoting.RemoteOutputStream; import hudson.remoting.RemoteWriter; -import java.io.IOException; -import java.io.OutputStream; -import java.io.Serializable; -import java.io.Writer; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.Repository; @@ -480,31 +481,43 @@ public PushCommand push() { @Override public void fetch(URIish url, List refspecs) throws GitException, InterruptedException { /* Intentionally using the deprecated method because the replacement method is not serializable. */ - proxy.fetch(url, refspecs); + List trimmedRefSpecs = new ArrayList<>(); + for (RefSpec rs : refspecs) { + if (rs != null) { + trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); + } + } + proxy.fetch(url, trimmedRefSpecs); } /** {@inheritDoc} */ @Override public void fetch(String remoteName, RefSpec... refspec) throws GitException, InterruptedException { - proxy.fetch(remoteName, refspec); + List trimmedRefSpecs = new ArrayList<>(); + for (RefSpec rs : refspec) { + if (rs != null) { + trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); + } + } + proxy.fetch(remoteName, (RefSpec) trimmedRefSpecs); } /** {@inheritDoc} */ @Override public void fetch(String remoteName, RefSpec refspec) throws GitException, InterruptedException { - fetch(remoteName, new RefSpec[] {refspec}); + fetch(remoteName, new RefSpec[] { new RefSpec(refspec.toString().trim())}); } /** {@inheritDoc} */ @Override public void push(String remoteName, String refspec) throws GitException, InterruptedException { - proxy.push(remoteName, refspec); + proxy.push(remoteName, refspec.trim()); } /** {@inheritDoc} */ @Override public void push(URIish url, String refspec) throws GitException, InterruptedException { - proxy.push(url, refspec); + proxy.push(url, refspec.trim()); } /** {@inheritDoc} */ From 69e52692e5d4e93fddc5cbea48e005f15884cb72 Mon Sep 17 00:00:00 2001 From: Jagruti Date: Fri, 26 Jan 2024 19:03:10 +0530 Subject: [PATCH 12/16] Simpliying ArrayList declaration --- .../java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java index 3d13dc911e..8f60204eda 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java @@ -625,7 +625,7 @@ public org.jenkinsci.plugins.gitclient.FetchCommand fetch_() { @Override public org.jenkinsci.plugins.gitclient.FetchCommand from(URIish remote, List refspecs) { this.url = remote; - List trimmedRefSpecs = new ArrayList(); + List trimmedRefSpecs = new ArrayList<>(); for (RefSpec rs : refspecs) { if (rs != null) { trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); @@ -1439,7 +1439,7 @@ public CloneCommand reference(String reference) { @Override public CloneCommand refspecs(List refspecs) { - List refSpecsList = new ArrayList(); + List refSpecsList = new ArrayList<>(); for (RefSpec ref : refspecs) { refSpecsList.add(new RefSpec(ref.toString().trim())); } From ef865e4aa127309d7ac50fbb6c31763a3850622d Mon Sep 17 00:00:00 2001 From: Jagruti Date: Fri, 26 Jan 2024 19:22:02 +0530 Subject: [PATCH 13/16] Null check added for refspecs --- .../org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java | 8 +++----- .../java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java | 8 +++----- .../org/jenkinsci/plugins/gitclient/RemoteGitImpl.java | 4 ++-- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index c67c177ca1..3d4f9b130f 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -535,8 +535,8 @@ public FetchCommand fetch_() { public FetchCommand from(URIish remote, List refspecs) { this.url = remote; List trimmedRefSpecs = new ArrayList<>(); - for (RefSpec rs : refspecs) { - if (rs != null) { + if (refspecs != null) { + for (RefSpec rs : refspecs) { trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); } } @@ -627,9 +627,7 @@ public void execute() throws GitException, InterruptedException { if (refspecs != null) { for (RefSpec rs : refspecs) { - if (rs != null) { - args.add(rs.toString().trim()); - } + args.add(rs.toString().trim()); } } diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java index 8f60204eda..93806036db 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java @@ -626,8 +626,8 @@ public org.jenkinsci.plugins.gitclient.FetchCommand fetch_() { public org.jenkinsci.plugins.gitclient.FetchCommand from(URIish remote, List refspecs) { this.url = remote; List trimmedRefSpecs = new ArrayList<>(); - for (RefSpec rs : refspecs) { - if (rs != null) { + if (!refspecs.isEmpty() && refspecs != null) { + for (RefSpec rs : refspecs) { trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); } } @@ -678,9 +678,7 @@ public void execute() throws GitException { List allRefSpecs = new ArrayList<>(); if (refspecs != null) { for (RefSpec rs : refspecs) { - if (rs != null) { - allRefSpecs.add(rs); - } + allRefSpecs.add(rs); } } diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java index e9faad3db5..17b56ab418 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java @@ -482,8 +482,8 @@ public PushCommand push() { public void fetch(URIish url, List refspecs) throws GitException, InterruptedException { /* Intentionally using the deprecated method because the replacement method is not serializable. */ List trimmedRefSpecs = new ArrayList<>(); - for (RefSpec rs : refspecs) { - if (rs != null) { + if (refspecs != null) { + for (RefSpec rs : refspecs) { trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); } } From 76b416c8856dce4da0268aaa6f70f9850d2d48f0 Mon Sep 17 00:00:00 2001 From: Jagruti Date: Fri, 26 Jan 2024 19:24:44 +0530 Subject: [PATCH 14/16] Updated the null check syntax --- .../java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java | 4 ++-- .../java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java | 2 +- .../java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index 3d4f9b130f..4bf11ac698 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -535,7 +535,7 @@ public FetchCommand fetch_() { public FetchCommand from(URIish remote, List refspecs) { this.url = remote; List trimmedRefSpecs = new ArrayList<>(); - if (refspecs != null) { + if (!refspecs.isEmpty() && refspecs != null) { for (RefSpec rs : refspecs) { trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); } @@ -625,7 +625,7 @@ public void execute() throws GitException, InterruptedException { addCheckedRemoteUrl(args, url.toString()); } - if (refspecs != null) { + if (!refspecs.isEmpty() && refspecs != null) { for (RefSpec rs : refspecs) { args.add(rs.toString().trim()); } diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java index 93806036db..9ee6df6fe8 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java @@ -676,7 +676,7 @@ public void execute() throws GitException { Git git = git(repo); List allRefSpecs = new ArrayList<>(); - if (refspecs != null) { + if (!refspecs.isEmpty() && refspecs != null) { for (RefSpec rs : refspecs) { allRefSpecs.add(rs); } diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java index 17b56ab418..3248bc6d15 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java @@ -482,7 +482,7 @@ public PushCommand push() { public void fetch(URIish url, List refspecs) throws GitException, InterruptedException { /* Intentionally using the deprecated method because the replacement method is not serializable. */ List trimmedRefSpecs = new ArrayList<>(); - if (refspecs != null) { + if (!refspecs.isEmpty() && refspecs != null) { for (RefSpec rs : refspecs) { trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); } From 021801af5f94d86420ed072a5a99651ee87f310a Mon Sep 17 00:00:00 2001 From: Jagruti Date: Fri, 26 Jan 2024 19:39:16 +0530 Subject: [PATCH 15/16] Replacing the null check order for refspecs --- .../java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java | 4 ++-- .../java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java | 4 ++-- .../java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index 4bf11ac698..3463860d42 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -535,7 +535,7 @@ public FetchCommand fetch_() { public FetchCommand from(URIish remote, List refspecs) { this.url = remote; List trimmedRefSpecs = new ArrayList<>(); - if (!refspecs.isEmpty() && refspecs != null) { + if (refspecs != null && !refspecs.isEmpty()) { for (RefSpec rs : refspecs) { trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); } @@ -625,7 +625,7 @@ public void execute() throws GitException, InterruptedException { addCheckedRemoteUrl(args, url.toString()); } - if (!refspecs.isEmpty() && refspecs != null) { + if (refspecs != null && !refspecs.isEmpty()) { for (RefSpec rs : refspecs) { args.add(rs.toString().trim()); } diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java index 9ee6df6fe8..56d1d365f4 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java @@ -626,7 +626,7 @@ public org.jenkinsci.plugins.gitclient.FetchCommand fetch_() { public org.jenkinsci.plugins.gitclient.FetchCommand from(URIish remote, List refspecs) { this.url = remote; List trimmedRefSpecs = new ArrayList<>(); - if (!refspecs.isEmpty() && refspecs != null) { + if (refspecs != null && !refspecs.isEmpty()) { for (RefSpec rs : refspecs) { trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); } @@ -676,7 +676,7 @@ public void execute() throws GitException { Git git = git(repo); List allRefSpecs = new ArrayList<>(); - if (!refspecs.isEmpty() && refspecs != null) { + if (refspecs != null && !refspecs.isEmpty()) { for (RefSpec rs : refspecs) { allRefSpecs.add(rs); } diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java index 3248bc6d15..67f2e6c724 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java @@ -482,7 +482,7 @@ public PushCommand push() { public void fetch(URIish url, List refspecs) throws GitException, InterruptedException { /* Intentionally using the deprecated method because the replacement method is not serializable. */ List trimmedRefSpecs = new ArrayList<>(); - if (!refspecs.isEmpty() && refspecs != null) { + if (refspecs != null && !refspecs.isEmpty()) { for (RefSpec rs : refspecs) { trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); } From 034c027415cea5fb3c8b04fcc811e2ffda59a128 Mon Sep 17 00:00:00 2001 From: Jagruti Date: Fri, 26 Jan 2024 20:52:48 +0530 Subject: [PATCH 16/16] Removed incorrect parsing highlighted in spotbug --- .../org/jenkinsci/plugins/gitclient/RemoteGitImpl.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java index 67f2e6c724..66226ea8c7 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java @@ -493,13 +493,7 @@ public void fetch(URIish url, List refspecs) throws GitException, Inter /** {@inheritDoc} */ @Override public void fetch(String remoteName, RefSpec... refspec) throws GitException, InterruptedException { - List trimmedRefSpecs = new ArrayList<>(); - for (RefSpec rs : refspec) { - if (rs != null) { - trimmedRefSpecs.add(new RefSpec(rs.toString().trim())); - } - } - proxy.fetch(remoteName, (RefSpec) trimmedRefSpecs); + proxy.fetch(remoteName, refspec); } /** {@inheritDoc} */