diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index fcbb9bdcb7..a51d915b05 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -200,7 +200,6 @@ public class CliGitAPIImpl extends LegacyCompatibleGitAPIImpl { EnvVars environment; private Map credentials = new HashMap<>(); private StandardCredentials defaultCredentials; - private StandardCredentials lfsCredentials; private final String encoding; /* If we fail some helper tool (e.g. SELinux chcon) do not make noise @@ -680,10 +679,7 @@ public void fetch(String remoteName, RefSpec... refspec) throws GitException, In } } - StandardCredentials cred = credentials.get(url); - if (cred == null) { - cred = defaultCredentials; - } + StandardCredentials cred = getCredentials(url); launchCommandWithCredentials(args, workspace, cred, url); } @@ -3177,23 +3173,49 @@ public void execute() throws GitException, InterruptedException { args.add("-f"); } args.add(ref); - launchCommandIn(args, workspace, checkoutEnv, timeout); + + StandardCredentials cred = null; + String checkoutUrl = null; + if (isAtLeastVersion(2, 27, 0, 0)) { + String result = firstLine(launchCommand( + "config", "--get", "--default", "''", "remote.origin.partialclonefilter")); + if (result != null && !result.isBlank()) { + checkoutUrl = launchCommand("config", "--get", "--default", "''", "remote.origin.url") + .trim(); // TODO: how to get the url correctly (and compatible with the + // unit tests)? + // checkoutUrl = getRemoteUrl("origin"); // fails with unit tests + if (checkoutUrl.isBlank()) { + checkoutUrl = null; + } else { + cred = getCredentials(checkoutUrl); + } + } + } + + if (cred != null) { + try { + // credentials are needed for instance for blobless clone and are simply not used in + // "standard" cases + launchCommandWithCredentials(args, workspace, cred, new URIish(checkoutUrl), timeout); + } catch (URISyntaxException e) { + throw new GitException("Invalid URL " + checkoutUrl, e); + } + } else { + launchCommandIn(args, workspace, checkoutEnv, timeout); + } if (lfsRemote != null) { final String url = getRemoteUrl(lfsRemote); - StandardCredentials cred = lfsCredentials; - if (cred == null) { - cred = credentials.get(url); - } - if (cred == null) { - cred = defaultCredentials; + StandardCredentials lfsCred = lfsCredentials; + if (lfsCred == null) { + lfsCred = getCredentials(url); } ArgumentListBuilder lfsArgs = new ArgumentListBuilder(); lfsArgs.add("lfs"); lfsArgs.add("pull"); lfsArgs.add(lfsRemote); try { - launchCommandWithCredentials(lfsArgs, workspace, cred, new URIish(url), timeout); + launchCommandWithCredentials(lfsArgs, workspace, lfsCred, new URIish(url), timeout); } catch (URISyntaxException e) { throw new GitException("Invalid URL " + url, e); } @@ -3740,10 +3762,7 @@ public Map getHeadRev(String url) throws GitException, Interru args.add("-h"); addCheckedRemoteUrl(args, url); - StandardCredentials cred = credentials.get(url); - if (cred == null) { - cred = defaultCredentials; - } + StandardCredentials cred = getCredentials(url); String result = launchCommandWithCredentials(args, null, cred, url); @@ -3768,10 +3787,7 @@ public ObjectId getHeadRev(String url, String branchSpec) throws GitException, I args.add("-h"); } - StandardCredentials cred = credentials.get(url); - if (cred == null) { - cred = defaultCredentials; - } + StandardCredentials cred = getCredentials(url); addCheckedRemoteUrl(args, url); @@ -3800,10 +3816,7 @@ public Map getRemoteReferences(String url, String pattern, boo args.add(pattern); } - StandardCredentials cred = credentials.get(url); - if (cred == null) { - cred = defaultCredentials; - } + StandardCredentials cred = getCredentials(url); String result = launchCommandWithCredentials(args, null, cred, url); @@ -3843,10 +3856,7 @@ public Map getRemoteSymbolicReferences(String url, String patter args.add(pattern); } - StandardCredentials cred = credentials.get(url); - if (cred == null) { - cred = defaultCredentials; - } + StandardCredentials cred = getCredentials(url); String result = launchCommandWithCredentials(args, null, cred, url); @@ -3886,10 +3896,7 @@ public void push(RemoteConfig repository, String refspec) throws GitException, I ArgumentListBuilder args = new ArgumentListBuilder(); URIish uri = repository.getURIs().get(0); String url = uri.toPrivateString(); - StandardCredentials cred = credentials.get(url); - if (cred == null) { - cred = defaultCredentials; - } + StandardCredentials cred = getCredentials(url); args.add("push"); addCheckedRemoteUrl(args, url); @@ -3992,6 +3999,14 @@ private static boolean setsidExists() { return false; } + private StandardCredentials getCredentials(String url) { + StandardCredentials cred = credentials.get(url); + if (cred == null) { + cred = defaultCredentials; + } + return cred; + } + /** {@inheritDoc} */ @Override public Set getTags() throws GitException, InterruptedException {