Skip to content

Commit af080b9

Browse files
author
magu
committed
JENKINS-64844 allow checkout of partially cloned repo
1 parent 3443c0e commit af080b9

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
@@ -194,7 +194,6 @@ public class CliGitAPIImpl extends LegacyCompatibleGitAPIImpl {
194194
EnvVars environment;
195195
private Map<String, StandardCredentials> credentials = new HashMap<>();
196196
private StandardCredentials defaultCredentials;
197-
private StandardCredentials lfsCredentials;
198197
private final String encoding;
199198

200199
/* If we fail some helper tool (e.g. SELinux chcon) do not make noise
@@ -674,10 +673,7 @@ public void fetch(String remoteName, RefSpec... refspec) throws GitException, In
674673
}
675674
}
676675

677-
StandardCredentials cred = credentials.get(url);
678-
if (cred == null) {
679-
cred = defaultCredentials;
680-
}
676+
StandardCredentials cred = getCredentials(url);
681677
launchCommandWithCredentials(args, workspace, cred, url);
682678
}
683679

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

31803205
if (lfsRemote != null) {
31813206
final String url = getRemoteUrl(lfsRemote);
3182-
StandardCredentials cred = lfsCredentials;
3183-
if (cred == null) {
3184-
cred = credentials.get(url);
3185-
}
3186-
if (cred == null) {
3187-
cred = defaultCredentials;
3207+
StandardCredentials lfsCred = lfsCredentials;
3208+
if (lfsCred == null) {
3209+
lfsCred = getCredentials(url);
31883210
}
31893211
ArgumentListBuilder lfsArgs = new ArgumentListBuilder();
31903212
lfsArgs.add("lfs");
31913213
lfsArgs.add("pull");
31923214
lfsArgs.add(lfsRemote);
31933215
try {
3194-
launchCommandWithCredentials(lfsArgs, workspace, cred, new URIish(url), timeout);
3216+
launchCommandWithCredentials(lfsArgs, workspace, lfsCred, new URIish(url), timeout);
31953217
} catch (URISyntaxException e) {
31963218
throw new GitException("Invalid URL " + url, e);
31973219
}
@@ -3738,10 +3760,7 @@ public Map<String, ObjectId> getHeadRev(String url) throws GitException, Interru
37383760
args.add("-h");
37393761
addCheckedRemoteUrl(args, url);
37403762

3741-
StandardCredentials cred = credentials.get(url);
3742-
if (cred == null) {
3743-
cred = defaultCredentials;
3744-
}
3763+
StandardCredentials cred = getCredentials(url);
37453764

37463765
String result = launchCommandWithCredentials(args, null, cred, url);
37473766

@@ -3766,10 +3785,7 @@ public ObjectId getHeadRev(String url, String branchSpec) throws GitException, I
37663785
args.add("-h");
37673786
}
37683787

3769-
StandardCredentials cred = credentials.get(url);
3770-
if (cred == null) {
3771-
cred = defaultCredentials;
3772-
}
3788+
StandardCredentials cred = getCredentials(url);
37733789

37743790
addCheckedRemoteUrl(args, url);
37753791

@@ -3798,10 +3814,7 @@ public Map<String, ObjectId> getRemoteReferences(String url, String pattern, boo
37983814
args.add(pattern);
37993815
}
38003816

3801-
StandardCredentials cred = credentials.get(url);
3802-
if (cred == null) {
3803-
cred = defaultCredentials;
3804-
}
3817+
StandardCredentials cred = getCredentials(url);
38053818

38063819
String result = launchCommandWithCredentials(args, null, cred, url);
38073820

@@ -3841,10 +3854,7 @@ public Map<String, String> getRemoteSymbolicReferences(String url, String patter
38413854
args.add(pattern);
38423855
}
38433856

3844-
StandardCredentials cred = credentials.get(url);
3845-
if (cred == null) {
3846-
cred = defaultCredentials;
3847-
}
3857+
StandardCredentials cred = getCredentials(url);
38483858

38493859
String result = launchCommandWithCredentials(args, null, cred, url);
38503860

@@ -3884,10 +3894,7 @@ public void push(RemoteConfig repository, String refspec) throws GitException, I
38843894
ArgumentListBuilder args = new ArgumentListBuilder();
38853895
URIish uri = repository.getURIs().get(0);
38863896
String url = uri.toPrivateString();
3887-
StandardCredentials cred = credentials.get(url);
3888-
if (cred == null) {
3889-
cred = defaultCredentials;
3890-
}
3897+
StandardCredentials cred = getCredentials(url);
38913898

38923899
args.add("push");
38933900
addCheckedRemoteUrl(args, url);
@@ -3990,6 +3997,14 @@ private static boolean setsidExists() {
39903997
return false;
39913998
}
39923999

4000+
private StandardCredentials getCredentials(String url) {
4001+
StandardCredentials cred = credentials.get(url);
4002+
if (cred == null) {
4003+
cred = defaultCredentials;
4004+
}
4005+
return cred;
4006+
}
4007+
39934008
/** {@inheritDoc} */
39944009
@Override
39954010
public Set<GitObject> getTags() throws GitException, InterruptedException {

0 commit comments

Comments
 (0)