Skip to content

Commit c8a729f

Browse files
author
magu
committed
JENKINS-64844 allow checkout of partially cloned repo
1 parent 2d59762 commit c8a729f

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

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

31673192
if (lfsRemote != null) {
31683193
final String url = getRemoteUrl(lfsRemote);
3169-
StandardCredentials cred = lfsCredentials;
3170-
if (cred == null) {
3171-
cred = credentials.get(url);
3172-
}
3173-
if (cred == null) {
3174-
cred = defaultCredentials;
3194+
StandardCredentials lfsCred = lfsCredentials;
3195+
if (lfsCred == null) {
3196+
lfsCred = getCredentials(url);
31753197
}
31763198
ArgumentListBuilder lfsArgs = new ArgumentListBuilder();
31773199
lfsArgs.add("lfs");
31783200
lfsArgs.add("pull");
31793201
lfsArgs.add(lfsRemote);
31803202
try {
3181-
launchCommandWithCredentials(lfsArgs, workspace, cred, new URIish(url), timeout);
3203+
launchCommandWithCredentials(lfsArgs, workspace, lfsCred, new URIish(url), timeout);
31823204
} catch (URISyntaxException e) {
31833205
throw new GitException("Invalid URL " + url, e);
31843206
}
@@ -3722,10 +3744,7 @@ public Map<String, ObjectId> getHeadRev(String url) throws GitException, Interru
37223744
args.add("-h");
37233745
addCheckedRemoteUrl(args, url);
37243746

3725-
StandardCredentials cred = credentials.get(url);
3726-
if (cred == null) {
3727-
cred = defaultCredentials;
3728-
}
3747+
StandardCredentials cred = getCredentials(url);
37293748

37303749
String result = launchCommandWithCredentials(args, null, cred, url);
37313750

@@ -3750,10 +3769,7 @@ public ObjectId getHeadRev(String url, String branchSpec) throws GitException, I
37503769
args.add("-h");
37513770
}
37523771

3753-
StandardCredentials cred = credentials.get(url);
3754-
if (cred == null) {
3755-
cred = defaultCredentials;
3756-
}
3772+
StandardCredentials cred = getCredentials(url);
37573773

37583774
addCheckedRemoteUrl(args, url);
37593775

@@ -3782,10 +3798,7 @@ public Map<String, ObjectId> getRemoteReferences(String url, String pattern, boo
37823798
args.add(pattern);
37833799
}
37843800

3785-
StandardCredentials cred = credentials.get(url);
3786-
if (cred == null) {
3787-
cred = defaultCredentials;
3788-
}
3801+
StandardCredentials cred = getCredentials(url);
37893802

37903803
String result = launchCommandWithCredentials(args, null, cred, url);
37913804

@@ -3825,10 +3838,7 @@ public Map<String, String> getRemoteSymbolicReferences(String url, String patter
38253838
args.add(pattern);
38263839
}
38273840

3828-
StandardCredentials cred = credentials.get(url);
3829-
if (cred == null) {
3830-
cred = defaultCredentials;
3831-
}
3841+
StandardCredentials cred = getCredentials(url);
38323842

38333843
String result = launchCommandWithCredentials(args, null, cred, url);
38343844

@@ -3868,10 +3878,7 @@ public void push(RemoteConfig repository, String refspec) throws GitException, I
38683878
ArgumentListBuilder args = new ArgumentListBuilder();
38693879
URIish uri = repository.getURIs().get(0);
38703880
String url = uri.toPrivateString();
3871-
StandardCredentials cred = credentials.get(url);
3872-
if (cred == null) {
3873-
cred = defaultCredentials;
3874-
}
3881+
StandardCredentials cred = getCredentials(url);
38753882

38763883
args.add("push");
38773884
addCheckedRemoteUrl(args, url);
@@ -3974,6 +3981,14 @@ private static boolean setsidExists() {
39743981
return false;
39753982
}
39763983

3984+
private StandardCredentials getCredentials(String url) {
3985+
StandardCredentials cred = credentials.get(url);
3986+
if (cred == null) {
3987+
cred = defaultCredentials;
3988+
}
3989+
return cred;
3990+
}
3991+
39773992
/** {@inheritDoc} */
39783993
@Override
39793994
public Set<GitObject> getTags() throws GitException, InterruptedException {

0 commit comments

Comments
 (0)