From 5bd9173c0774af2cc92f57f4a756291c32bff52c Mon Sep 17 00:00:00 2001 From: Thierry Date: Mon, 14 Oct 2019 11:59:09 +0200 Subject: [PATCH 1/8] JENKINS-48431 Support both lightweight checkout AND build parameters --- pom.xml | 2 + .../jenkins/plugins/git/GitSCMFileSystem.java | 86 +++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/pom.xml b/pom.xml index c479f0724c..96f8822929 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,7 @@ true 1C false + 2.6.4-SNAPSHOT @@ -98,6 +99,7 @@ org.jenkins-ci.plugins scm-api + ${scm-api-plugin.version} org.jenkins-ci.plugins.workflow diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index b436d6646a..c3e720d874 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -34,6 +34,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import hudson.EnvVars; import hudson.Extension; +import hudson.model.Run; import hudson.model.Item; import hudson.model.TaskListener; import hudson.plugins.git.BranchSpec; @@ -286,6 +287,91 @@ public boolean supportsDescriptor(SCMSourceDescriptor descriptor) { return AbstractGitSCMSource.class.isAssignableFrom(descriptor.clazz); } + @Override + public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + throws IOException, InterruptedException { + if (rev != null && !(rev instanceof AbstractGitSCMSource.SCMRevisionImpl)) { + return null; + } + Item owner = build.getParent(); + TaskListener listener = new LogTaskListener(LOGGER, Level.FINE); + GitSCM gitSCM = (GitSCM) scm; + UserRemoteConfig config = gitSCM.getUserRemoteConfigs().get(0); + BranchSpec branchSpec = gitSCM.getBranches().get(0); + EnvVars environment = build.getEnvironment(listener); + String remote = environment.expand(config.getUrl()); + String cacheEntry = AbstractGitSCMSource.getCacheEntry(remote); + Lock cacheLock = AbstractGitSCMSource.getCacheLock(cacheEntry); + cacheLock.lock(); + try { + File cacheDir = AbstractGitSCMSource.getCacheDir(cacheEntry); + Git git = Git.with(listener, environment).in(cacheDir); + GitTool tool = gitSCM.resolveGitTool(listener); + if (tool != null) { + git.using(tool.getGitExe()); + } + GitClient client = git.getClient(); + String credentialsId = config.getCredentialsId(); + if (credentialsId != null) { + StandardCredentials credential = CredentialsMatchers.firstOrNull( + CredentialsProvider.lookupCredentials( + StandardUsernameCredentials.class, + owner, + ACL.SYSTEM, + URIRequirementBuilder.fromUri(remote).build() + ), + CredentialsMatchers.allOf( + CredentialsMatchers.withId(credentialsId), + GitClient.CREDENTIALS_MATCHER + ) + ); + client.addDefaultCredentials(credential); + CredentialsProvider.track(owner, credential); + } + + if (!client.hasGitRepo()) { + listener.getLogger().println("Creating git repository in " + cacheDir); + client.init(); + } + String remoteName = StringUtils.defaultIfBlank(environment.expand(config.getName()), Constants.DEFAULT_REMOTE_NAME); + listener.getLogger().println("Setting " + remoteName + " to " + remote); + client.setRemoteUrl(remoteName, remote); + listener.getLogger().println("Fetching & pruning " + remoteName + "..."); + URIish remoteURI = null; + try { + remoteURI = new URIish(remoteName); + } catch (URISyntaxException ex) { + listener.getLogger().println("URI syntax exception for '" + remoteName + "' " + ex); + } + String headName; + if (rev != null) { + headName = environment.expand(rev.getHead().getName()); + } else { + String branch = environment.expand(branchSpec.getName()); + if (branch.startsWith(Constants.R_HEADS)) { + headName = branch.substring(Constants.R_HEADS.length()); + } else if (branch.startsWith("*/")) { + headName = branch.substring(2); + } else { + headName = branch; + } + } + + String refspec = environment.expand(config.getRefspec()); + String head = headName; + if (refspec == null) { + refspec = "+" + Constants.R_HEADS + headName + ":" + Constants.R_REMOTES + remoteName + "/" + headName; + head = Constants.R_REMOTES + remoteName + "/" +headName; + } + client.fetch_().prune().from(remoteURI, Arrays + .asList(new RefSpec (refspec))).execute(); + listener.getLogger().println("Done."); + return new GitSCMFileSystem(client, remote, head, (AbstractGitSCMSource.SCMRevisionImpl) rev); + } finally { + cacheLock.unlock(); + } + } + @Override public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev) throws IOException, InterruptedException { From bf791e285e8f069894d923caab9651ae11ac8d24 Mon Sep 17 00:00:00 2001 From: Thierry Date: Mon, 14 Oct 2019 19:22:19 +0200 Subject: [PATCH 2/8] Bugfix for tests --- src/main/java/jenkins/plugins/git/GitSCMTelescope.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/jenkins/plugins/git/GitSCMTelescope.java b/src/main/java/jenkins/plugins/git/GitSCMTelescope.java index 886e5cae95..8537e3d6be 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMTelescope.java +++ b/src/main/java/jenkins/plugins/git/GitSCMTelescope.java @@ -35,6 +35,7 @@ import hudson.ExtensionList; import hudson.model.Item; import hudson.model.Queue; +import hudson.model.Run; import hudson.model.queue.Tasks; import hudson.plugins.git.BranchSpec; import hudson.plugins.git.GitSCM; @@ -230,6 +231,12 @@ public final SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, SCMRevis return null; } + @Override + public final SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + throws IOException, InterruptedException { + return null; + } + /** * Given a {@link SCM} this should try to build a corresponding {@link SCMFileSystem} instance that * reflects the content at the specified {@link SCMRevision}. If the {@link SCM} is supported but not From 96f458d6a67c5be6febf1f102077094bb392c01a Mon Sep 17 00:00:00 2001 From: Thierry Gatineau Date: Sat, 19 Oct 2019 20:32:34 +0200 Subject: [PATCH 3/8] Trim whitespaces --- .../java/jenkins/plugins/git/GitSCMFileSystem.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index c3e720d874..0a61a2490f 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -299,7 +299,7 @@ public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull S UserRemoteConfig config = gitSCM.getUserRemoteConfigs().get(0); BranchSpec branchSpec = gitSCM.getBranches().get(0); EnvVars environment = build.getEnvironment(listener); - String remote = environment.expand(config.getUrl()); + String remote = environment.expand(config.getUrl()).trim(); String cacheEntry = AbstractGitSCMSource.getCacheEntry(remote); Lock cacheLock = AbstractGitSCMSource.getCacheLock(cacheEntry); cacheLock.lock(); @@ -333,7 +333,7 @@ public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull S listener.getLogger().println("Creating git repository in " + cacheDir); client.init(); } - String remoteName = StringUtils.defaultIfBlank(environment.expand(config.getName()), Constants.DEFAULT_REMOTE_NAME); + String remoteName = StringUtils.defaultIfBlank(environment.expand(config.getName()).trim(), Constants.DEFAULT_REMOTE_NAME); listener.getLogger().println("Setting " + remoteName + " to " + remote); client.setRemoteUrl(remoteName, remote); listener.getLogger().println("Fetching & pruning " + remoteName + "..."); @@ -345,9 +345,9 @@ public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull S } String headName; if (rev != null) { - headName = environment.expand(rev.getHead().getName()); + headName = environment.expand(rev.getHead().getName()).trim(); } else { - String branch = environment.expand(branchSpec.getName()); + String branch = environment.expand(branchSpec.getName()).trim(); if (branch.startsWith(Constants.R_HEADS)) { headName = branch.substring(Constants.R_HEADS.length()); } else if (branch.startsWith("*/")) { @@ -357,7 +357,7 @@ public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull S } } - String refspec = environment.expand(config.getRefspec()); + String refspec = environment.expand(config.getRefspec()).trim(); String head = headName; if (refspec == null) { refspec = "+" + Constants.R_HEADS + headName + ":" + Constants.R_REMOTES + remoteName + "/" + headName; From cb533202ba71bc5dabe4d6fb04fccf10f48cecf6 Mon Sep 17 00:00:00 2001 From: Thierry Date: Thu, 28 Nov 2019 11:51:54 +0100 Subject: [PATCH 4/8] Bugfix spotbugs --- src/main/java/jenkins/plugins/git/GitSCMFileSystem.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index 0a61a2490f..a54805ae89 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -357,12 +357,15 @@ public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull S } } - String refspec = environment.expand(config.getRefspec()).trim(); + String refspec = environment.expand(config.getRefspec()); String head = headName; if (refspec == null) { refspec = "+" + Constants.R_HEADS + headName + ":" + Constants.R_REMOTES + remoteName + "/" + headName; head = Constants.R_REMOTES + remoteName + "/" +headName; } + else { + refspec = refspec.trim(); + } client.fetch_().prune().from(remoteURI, Arrays .asList(new RefSpec (refspec))).execute(); listener.getLogger().println("Done."); From 58258072ac742b4dcdd587f6dc867546b8647f54 Mon Sep 17 00:00:00 2001 From: Joseph Petersen Date: Tue, 26 May 2020 00:34:06 +0200 Subject: [PATCH 5/8] deduplicate and implement both GitSCMTelescope and GitSCMFileSystem --- pom.xml | 2 +- .../hudson/plugins/git/util/GitUtils.java | 4 + .../jenkins/plugins/git/GitSCMFileSystem.java | 108 +++--------------- .../jenkins/plugins/git/GitSCMTelescope.java | 33 ++++-- 4 files changed, 47 insertions(+), 100 deletions(-) diff --git a/pom.xml b/pom.xml index 7a00a948d6..59d5cf0a41 100644 --- a/pom.xml +++ b/pom.xml @@ -34,7 +34,7 @@ false true false - 2.6.4-SNAPSHOT + 2.6.4-20200525.215035-2 1.36 1.7.26 Max diff --git a/src/main/java/hudson/plugins/git/util/GitUtils.java b/src/main/java/hudson/plugins/git/util/GitUtils.java index 58be3ae6a8..0ec262f623 100644 --- a/src/main/java/hudson/plugins/git/util/GitUtils.java +++ b/src/main/java/hudson/plugins/git/util/GitUtils.java @@ -408,6 +408,10 @@ public static String[] fixupNames(String[] names, String[] urls) { return returnNames; } + public static String expand(String str, EnvVars env) { + return env == null ? str : env.expand(str); + } + private static final Logger LOGGER = Logger.getLogger(GitUtils.class.getName()); private static final long serialVersionUID = 1L; diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index c6730644af..7a1af73ea6 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -76,6 +76,8 @@ import org.jenkinsci.plugins.gitclient.Git; import org.jenkinsci.plugins.gitclient.GitClient; +import static hudson.plugins.git.util.GitUtils.expand; + /** * Base implementation of {@link SCMFileSystem}. * @@ -283,96 +285,20 @@ public boolean supportsDescriptor(SCMSourceDescriptor descriptor) { } @Override - public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) + public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev) throws IOException, InterruptedException { - if (rev != null && !(rev instanceof AbstractGitSCMSource.SCMRevisionImpl)) { - return null; - } - Item owner = build.getParent(); - TaskListener listener = new LogTaskListener(LOGGER, Level.FINE); - GitSCM gitSCM = (GitSCM) scm; - UserRemoteConfig config = gitSCM.getUserRemoteConfigs().get(0); - BranchSpec branchSpec = gitSCM.getBranches().get(0); - EnvVars environment = build.getEnvironment(listener); - String remote = environment.expand(config.getUrl()).trim(); - String cacheEntry = AbstractGitSCMSource.getCacheEntry(remote); - Lock cacheLock = AbstractGitSCMSource.getCacheLock(cacheEntry); - cacheLock.lock(); - try { - File cacheDir = AbstractGitSCMSource.getCacheDir(cacheEntry); - Git git = Git.with(listener, environment).in(cacheDir); - GitTool tool = gitSCM.resolveGitTool(listener); - if (tool != null) { - git.using(tool.getGitExe()); - } - GitClient client = git.getClient(); - String credentialsId = config.getCredentialsId(); - if (credentialsId != null) { - StandardCredentials credential = CredentialsMatchers.firstOrNull( - CredentialsProvider.lookupCredentials( - StandardUsernameCredentials.class, - owner, - ACL.SYSTEM, - URIRequirementBuilder.fromUri(remote).build() - ), - CredentialsMatchers.allOf( - CredentialsMatchers.withId(credentialsId), - GitClient.CREDENTIALS_MATCHER - ) - ); - client.addDefaultCredentials(credential); - CredentialsProvider.track(owner, credential); - } - - if (!client.hasGitRepo()) { - listener.getLogger().println("Creating git repository in " + cacheDir); - client.init(); - } - String remoteName = StringUtils.defaultIfBlank(environment.expand(config.getName()).trim(), Constants.DEFAULT_REMOTE_NAME); - listener.getLogger().println("Setting " + remoteName + " to " + remote); - client.setRemoteUrl(remoteName, remote); - listener.getLogger().println("Fetching & pruning " + remoteName + "..."); - URIish remoteURI = null; - try { - remoteURI = new URIish(remoteName); - } catch (URISyntaxException ex) { - listener.getLogger().println("URI syntax exception for '" + remoteName + "' " + ex); - } - String headName; - if (rev != null) { - headName = environment.expand(rev.getHead().getName()).trim(); - } else { - String branch = environment.expand(branchSpec.getName()).trim(); - if (branch.startsWith(Constants.R_HEADS)) { - headName = branch.substring(Constants.R_HEADS.length()); - } else if (branch.startsWith("*/")) { - headName = branch.substring(2); - } else { - headName = branch; - } - } - - String refspec = environment.expand(config.getRefspec()); - String head = headName; - if (refspec == null) { - refspec = "+" + Constants.R_HEADS + headName + ":" + Constants.R_REMOTES + remoteName + "/" + headName; - head = Constants.R_REMOTES + remoteName + "/" +headName; - } - else { - refspec = refspec.trim(); - } - client.fetch_().prune().from(remoteURI, Arrays - .asList(new RefSpec (refspec))).execute(); - listener.getLogger().println("Done."); - return new GitSCMFileSystem(client, remote, head, (AbstractGitSCMSource.SCMRevisionImpl) rev); - } finally { - cacheLock.unlock(); - } + return getScmFileSystem(scm, rev, owner, null); } @Override - public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev) - throws IOException, InterruptedException { + public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, SCMRevision rev) + throws IOException, InterruptedException { + Item owner = build.getParent(); + return getScmFileSystem(scm, rev, owner, build); + } + + private SCMFileSystem getScmFileSystem(@NonNull SCM scm, + SCMRevision rev, Item owner, @CheckForNull Run build) throws IOException, InterruptedException { if (rev != null && !(rev instanceof AbstractGitSCMSource.SCMRevisionImpl)) { return null; } @@ -380,10 +306,11 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull return null; // Spotbugs warns about unchecked cast without this check } GitSCM gitSCM = (GitSCM) scm; + TaskListener listener = new LogTaskListener(LOGGER, Level.FINE); UserRemoteConfig config = gitSCM.getUserRemoteConfigs().get(0); BranchSpec branchSpec = gitSCM.getBranches().get(0); - String remote = config.getUrl(); - TaskListener listener = new LogTaskListener(LOGGER, Level.FINE); + EnvVars env = build == null ? null : build.getEnvironment(listener); + String remote = expand(config.getUrl(), env); if (remote == null) { listener.getLogger().println("Git remote url is null"); return null; @@ -421,7 +348,7 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull listener.getLogger().println("Creating git repository in " + cacheDir); client.init(); } - String remoteName = StringUtils.defaultIfBlank(config.getName(), Constants.DEFAULT_REMOTE_NAME); + String remoteName = StringUtils.defaultIfBlank(expand(config.getName(), env), Constants.DEFAULT_REMOTE_NAME); listener.getLogger().println("Setting " + remoteName + " to " + remote); client.setRemoteUrl(remoteName, remote); listener.getLogger().println("Fetching & pruning " + remoteName + "..."); @@ -447,6 +374,9 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull headName = branchSpec.getName(); } } + if (build != null) { + headName = expand(headName, env); + } client.fetch_().prune(true).from(remoteURI, Arrays .asList(new RefSpec( "+" + prefix + headName + ":" + Constants.R_REMOTES + remoteName + "/" diff --git a/src/main/java/jenkins/plugins/git/GitSCMTelescope.java b/src/main/java/jenkins/plugins/git/GitSCMTelescope.java index 5e95996d8f..375422012e 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMTelescope.java +++ b/src/main/java/jenkins/plugins/git/GitSCMTelescope.java @@ -32,10 +32,12 @@ import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder; import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; +import hudson.EnvVars; import hudson.ExtensionList; import hudson.model.Item; import hudson.model.Queue; import hudson.model.Run; +import hudson.model.TaskListener; import hudson.model.queue.Tasks; import hudson.plugins.git.BranchSpec; import hudson.plugins.git.GitSCM; @@ -55,6 +57,8 @@ import org.eclipse.jgit.lib.Constants; import org.jenkinsci.plugins.gitclient.GitClient; +import static hudson.plugins.git.util.GitUtils.expand; + /** * An implementation of this extension point allows {@link AbstractGitSCMSource} to examine a repository from a distance * without requiring a local checkout. @@ -182,14 +186,27 @@ public final SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead hea @Override public final SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, SCMRevision rev) throws IOException, InterruptedException { + return build(scm, rev, owner, null); + } + + @Override + public SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, SCMRevision rev) + throws IOException, InterruptedException { + Item owner = build.getParent(); + return build(scm, rev, owner, build); + } + + private SCMFileSystem build(@NonNull SCM scm, SCMRevision rev, Item owner, @CheckForNull Run build) + throws IOException, InterruptedException { if (scm instanceof GitSCM) { // we only support the GitSCM if the branch is completely unambiguous GitSCM git = (GitSCM) scm; List configs = git.getUserRemoteConfigs(); List branches = git.getBranches(); + EnvVars env = build == null ? null : build.getEnvironment(TaskListener.NULL); if (configs.size() == 1) { UserRemoteConfig config = configs.get(0); - String remote = config.getUrl(); + String remote = expand(config.getUrl(), env); if (remote != null && supports(remote) && branches.size() == 1 && !branches.get(0).getName().contains("*")) { StandardCredentials credentials; @@ -211,7 +228,7 @@ public final SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, SCMRevis validate(remote, credentials); SCMHead head; if (rev == null) { - String name = branches.get(0).getName(); + String name = expand(branches.get(0).getName(), env); if (name.startsWith(Constants.R_TAGS)) { head = new GitTagSCMHead( name.substring(Constants.R_TAGS.length()), @@ -220,8 +237,10 @@ public final SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, SCMRevis } else if (name.startsWith(Constants.R_HEADS)) { head = new GitBranchSCMHead(name.substring(Constants.R_HEADS.length())); } else { - if (name.startsWith(config.getName() + "/")) { - head = new GitBranchSCMHead(name.substring(config.getName().length() + 1)); + String remoteName = expand(config.getName(), env); + if (name.startsWith(remoteName + "/")) { + head = new GitBranchSCMHead(name.substring( + remoteName.length() + 1)); } else { head = new GitBranchSCMHead(name); } @@ -236,12 +255,6 @@ public final SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, SCMRevis return null; } - @Override - public final SCMFileSystem build(@NonNull Run build, @NonNull SCM scm, @CheckForNull SCMRevision rev) - throws IOException, InterruptedException { - return null; - } - /** * Given a {@link SCM} this should try to build a corresponding {@link SCMFileSystem} instance that * reflects the content at the specified {@link SCMRevision}. If the {@link SCM} is supported but not From 274e66b592406ccd2327b003d35c770b6dbe1051 Mon Sep 17 00:00:00 2001 From: Joseph Petersen Date: Tue, 26 May 2020 00:46:42 +0200 Subject: [PATCH 6/8] remove null check for build --- src/main/java/jenkins/plugins/git/GitSCMFileSystem.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index 7a1af73ea6..f04d8d9e76 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -374,9 +374,7 @@ private SCMFileSystem getScmFileSystem(@NonNull SCM scm, headName = branchSpec.getName(); } } - if (build != null) { - headName = expand(headName, env); - } + headName = expand(headName, env); client.fetch_().prune(true).from(remoteURI, Arrays .asList(new RefSpec( "+" + prefix + headName + ":" + Constants.R_REMOTES + remoteName + "/" From 3cc15677f739e7f53b5e811febf5314e292d1501 Mon Sep 17 00:00:00 2001 From: Joseph Petersen Date: Sat, 6 Jun 2020 14:47:07 +0200 Subject: [PATCH 7/8] apply suggestions from @tgatinea --- .../jenkins/plugins/git/GitSCMFileSystem.java | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index f04d8d9e76..f5e5a29dfd 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -54,6 +54,7 @@ import java.io.Writer; import java.net.URISyntaxException; import java.util.Arrays; +import java.util.Collections; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.logging.Level; @@ -310,11 +311,7 @@ private SCMFileSystem getScmFileSystem(@NonNull SCM scm, UserRemoteConfig config = gitSCM.getUserRemoteConfigs().get(0); BranchSpec branchSpec = gitSCM.getBranches().get(0); EnvVars env = build == null ? null : build.getEnvironment(listener); - String remote = expand(config.getUrl(), env); - if (remote == null) { - listener.getLogger().println("Git remote url is null"); - return null; - } + String remote = expand(config.getUrl(), env).trim(); String cacheEntry = AbstractGitSCMSource.getCacheEntry(remote); Lock cacheLock = AbstractGitSCMSource.getCacheLock(cacheEntry); cacheLock.lock(); @@ -348,7 +345,7 @@ private SCMFileSystem getScmFileSystem(@NonNull SCM scm, listener.getLogger().println("Creating git repository in " + cacheDir); client.init(); } - String remoteName = StringUtils.defaultIfBlank(expand(config.getName(), env), Constants.DEFAULT_REMOTE_NAME); + String remoteName = StringUtils.defaultIfBlank(expand(config.getName(), env), Constants.DEFAULT_REMOTE_NAME).trim(); listener.getLogger().println("Setting " + remoteName + " to " + remote); client.setRemoteUrl(remoteName, remote); listener.getLogger().println("Fetching & pruning " + remoteName + "..."); @@ -358,29 +355,37 @@ private SCMFileSystem getScmFileSystem(@NonNull SCM scm, } catch (URISyntaxException ex) { listener.getLogger().println("URI syntax exception for '" + remoteName + "' " + ex); } - String prefix = Constants.R_HEADS; + String prefix = Constants.R_HEADS; if(branchSpec.getName().startsWith(Constants.R_TAGS)){ - prefix = Constants.R_TAGS; + prefix = Constants.R_TAGS; } String headName; if (rev != null) { headName = rev.getHead().getName(); } else { if (branchSpec.getName().startsWith(prefix)){ - headName = branchSpec.getName().substring(prefix.length()); + headName = branchSpec.getName().substring(prefix.length()); } else if (branchSpec.getName().startsWith("*/")) { headName = branchSpec.getName().substring(2); } else { headName = branchSpec.getName(); } } - headName = expand(headName, env); - client.fetch_().prune(true).from(remoteURI, Arrays - .asList(new RefSpec( - "+" + prefix + headName + ":" + Constants.R_REMOTES + remoteName + "/" - + headName))).execute(); + headName = expand(headName, env).trim(); + + String refspec = expand(config.getRefspec(), env); + String head = headName; + if (refspec == null) { + refspec = "+" + Constants.R_HEADS + headName + ":" + Constants.R_REMOTES + remoteName + "/" + headName; + head = Constants.R_REMOTES + remoteName + "/" +headName; + } + else { + refspec = refspec.trim(); + } + client.fetch_().prune(true).from(remoteURI, + Collections.singletonList(new RefSpec(refspec))).execute(); listener.getLogger().println("Done."); - return new GitSCMFileSystem(client, remote, Constants.R_REMOTES + remoteName + "/" +headName, (AbstractGitSCMSource.SCMRevisionImpl) rev); + return new GitSCMFileSystem(client, remote, head, (AbstractGitSCMSource.SCMRevisionImpl) rev); } finally { cacheLock.unlock(); } From c424a69eebe93c921bb12c2dfe44494770469613 Mon Sep 17 00:00:00 2001 From: Joseph Petersen Date: Sun, 7 Jun 2020 06:41:28 +0200 Subject: [PATCH 8/8] remote can potentially be null --- src/main/java/jenkins/plugins/git/GitSCMFileSystem.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index f5e5a29dfd..3cafe70019 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -311,7 +311,12 @@ private SCMFileSystem getScmFileSystem(@NonNull SCM scm, UserRemoteConfig config = gitSCM.getUserRemoteConfigs().get(0); BranchSpec branchSpec = gitSCM.getBranches().get(0); EnvVars env = build == null ? null : build.getEnvironment(listener); - String remote = expand(config.getUrl(), env).trim(); + String remote = expand(config.getUrl(), env); + if (remote == null) { + listener.getLogger().println("Git remote url is null"); + return null; + } + remote = remote.trim(); String cacheEntry = AbstractGitSCMSource.getCacheEntry(remote); Lock cacheLock = AbstractGitSCMSource.getCacheLock(cacheEntry); cacheLock.lock();