Skip to content

Commit c4765ab

Browse files
author
magu
committed
JENKINS-64844 allow checkout of partially cloned repo
1 parent 20c4928 commit c4765ab

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ public class CliGitAPIImpl extends LegacyCompatibleGitAPIImpl {
195195
EnvVars environment;
196196
private Map<String, StandardCredentials> credentials = new HashMap<>();
197197
private StandardCredentials defaultCredentials;
198-
private StandardCredentials lfsCredentials;
199198
private final String encoding;
200199

201200
/* If we fail some helper tool (e.g. SELinux chcon) do not make noise
@@ -672,10 +671,7 @@ public void fetch(String remoteName, RefSpec... refspec) throws GitException, In
672671
}
673672
}
674673

675-
StandardCredentials cred = credentials.get(url);
676-
if (cred == null) {
677-
cred = defaultCredentials;
678-
}
674+
StandardCredentials cred = getCredentials(url);
679675
launchCommandWithCredentials(args, workspace, cred, url);
680676
}
681677

@@ -3171,23 +3167,49 @@ public void execute() throws GitException, InterruptedException {
31713167
args.add("-f");
31723168
}
31733169
args.add(ref);
3174-
launchCommandIn(args, workspace, checkoutEnv, timeout);
3170+
3171+
StandardCredentials cred = null;
3172+
String checkoutUrl = null;
3173+
if (isAtLeastVersion(2, 27, 0, 0)) {
3174+
String result = firstLine(launchCommand(
3175+
"config", "--get", "--default", "''", "remote.origin.partialclonefilter"));
3176+
if (result != null && !result.isBlank()) {
3177+
checkoutUrl = launchCommand("config", "--get", "--default", "''", "remote.origin.url")
3178+
.trim(); // TODO: how to get the url correctly (and compatible with the
3179+
// unit tests)?
3180+
// checkoutUrl = getRemoteUrl("origin"); // fails with unit tests
3181+
if (checkoutUrl.isBlank()) {
3182+
checkoutUrl = null;
3183+
} else {
3184+
cred = getCredentials(checkoutUrl);
3185+
}
3186+
}
3187+
}
3188+
3189+
if (checkoutUrl != null) {
3190+
try {
3191+
// credentials are needed for instance for blobless clone and are simply not used in
3192+
// "standard" cases
3193+
launchCommandWithCredentials(args, workspace, cred, new URIish(checkoutUrl), timeout);
3194+
} catch (URISyntaxException e) {
3195+
throw new GitException("Invalid URL " + checkoutUrl, e);
3196+
}
3197+
} else {
3198+
launchCommandIn(args, workspace, checkoutEnv, timeout);
3199+
}
31753200

31763201
if (lfsRemote != null) {
31773202
final String url = getRemoteUrl(lfsRemote);
3178-
StandardCredentials cred = lfsCredentials;
3179-
if (cred == null) {
3180-
cred = credentials.get(url);
3181-
}
3182-
if (cred == null) {
3183-
cred = defaultCredentials;
3203+
StandardCredentials lfsCred = lfsCredentials;
3204+
if (lfsCred == null) {
3205+
lfsCred = getCredentials(url);
31843206
}
31853207
ArgumentListBuilder lfsArgs = new ArgumentListBuilder();
31863208
lfsArgs.add("lfs");
31873209
lfsArgs.add("pull");
31883210
lfsArgs.add(lfsRemote);
31893211
try {
3190-
launchCommandWithCredentials(lfsArgs, workspace, cred, new URIish(url), timeout);
3212+
launchCommandWithCredentials(lfsArgs, workspace, lfsCred, new URIish(url), timeout);
31913213
} catch (URISyntaxException e) {
31923214
throw new GitException("Invalid URL " + url, e);
31933215
}
@@ -3731,10 +3753,7 @@ public Map<String, ObjectId> getHeadRev(String url) throws GitException, Interru
37313753
args.add("-h");
37323754
addCheckedRemoteUrl(args, url);
37333755

3734-
StandardCredentials cred = credentials.get(url);
3735-
if (cred == null) {
3736-
cred = defaultCredentials;
3737-
}
3756+
StandardCredentials cred = getCredentials(url);
37383757

37393758
String result = launchCommandWithCredentials(args, null, cred, url);
37403759

@@ -3759,10 +3778,7 @@ public ObjectId getHeadRev(String url, String branchSpec) throws GitException, I
37593778
args.add("-h");
37603779
}
37613780

3762-
StandardCredentials cred = credentials.get(url);
3763-
if (cred == null) {
3764-
cred = defaultCredentials;
3765-
}
3781+
StandardCredentials cred = getCredentials(url);
37663782

37673783
addCheckedRemoteUrl(args, url);
37683784

@@ -3791,10 +3807,7 @@ public Map<String, ObjectId> getRemoteReferences(String url, String pattern, boo
37913807
args.add(pattern);
37923808
}
37933809

3794-
StandardCredentials cred = credentials.get(url);
3795-
if (cred == null) {
3796-
cred = defaultCredentials;
3797-
}
3810+
StandardCredentials cred = getCredentials(url);
37983811

37993812
String result = launchCommandWithCredentials(args, null, cred, url);
38003813

@@ -3834,10 +3847,7 @@ public Map<String, String> getRemoteSymbolicReferences(String url, String patter
38343847
args.add(pattern);
38353848
}
38363849

3837-
StandardCredentials cred = credentials.get(url);
3838-
if (cred == null) {
3839-
cred = defaultCredentials;
3840-
}
3850+
StandardCredentials cred = getCredentials(url);
38413851

38423852
String result = launchCommandWithCredentials(args, null, cred, url);
38433853

@@ -3877,10 +3887,7 @@ public void push(RemoteConfig repository, String refspec) throws GitException, I
38773887
ArgumentListBuilder args = new ArgumentListBuilder();
38783888
URIish uri = repository.getURIs().get(0);
38793889
String url = uri.toPrivateString();
3880-
StandardCredentials cred = credentials.get(url);
3881-
if (cred == null) {
3882-
cred = defaultCredentials;
3883-
}
3890+
StandardCredentials cred = getCredentials(url);
38843891

38853892
args.add("push");
38863893
addCheckedRemoteUrl(args, url);
@@ -3983,6 +3990,14 @@ private static boolean setsidExists() {
39833990
return false;
39843991
}
39853992

3993+
private StandardCredentials getCredentials(String url) {
3994+
StandardCredentials cred = credentials.get(url);
3995+
if (cred == null) {
3996+
cred = defaultCredentials;
3997+
}
3998+
return cred;
3999+
}
4000+
39864001
/** {@inheritDoc} */
39874002
@Override
39884003
public Set<GitObject> getTags() throws GitException, InterruptedException {

0 commit comments

Comments
 (0)