Skip to content

Commit 378acc3

Browse files
magumagu
authored andcommitted
JENKINS-64844 allow checkout of partially cloned repo
1 parent e10df03 commit 378acc3

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
@@ -200,7 +200,6 @@ public class CliGitAPIImpl extends LegacyCompatibleGitAPIImpl {
200200
EnvVars environment;
201201
private Map<String, StandardCredentials> credentials = new HashMap<>();
202202
private StandardCredentials defaultCredentials;
203-
private StandardCredentials lfsCredentials;
204203
private final String encoding;
205204

206205
/* 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
680679
}
681680
}
682681

683-
StandardCredentials cred = credentials.get(url);
684-
if (cred == null) {
685-
cred = defaultCredentials;
686-
}
682+
StandardCredentials cred = getCredentials(url);
687683
launchCommandWithCredentials(args, workspace, cred, url);
688684
}
689685

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

31823207
if (lfsRemote != null) {
31833208
final String url = getRemoteUrl(lfsRemote);
3184-
StandardCredentials cred = lfsCredentials;
3185-
if (cred == null) {
3186-
cred = credentials.get(url);
3187-
}
3188-
if (cred == null) {
3189-
cred = defaultCredentials;
3209+
StandardCredentials lfsCred = lfsCredentials;
3210+
if (lfsCred == null) {
3211+
lfsCred = getCredentials(url);
31903212
}
31913213
ArgumentListBuilder lfsArgs = new ArgumentListBuilder();
31923214
lfsArgs.add("lfs");
31933215
lfsArgs.add("pull");
31943216
lfsArgs.add(lfsRemote);
31953217
try {
3196-
launchCommandWithCredentials(lfsArgs, workspace, cred, new URIish(url), timeout);
3218+
launchCommandWithCredentials(lfsArgs, workspace, lfsCred, new URIish(url), timeout);
31973219
} catch (URISyntaxException e) {
31983220
throw new GitException("Invalid URL " + url, e);
31993221
}
@@ -3740,10 +3762,7 @@ public Map<String, ObjectId> getHeadRev(String url) throws GitException, Interru
37403762
args.add("-h");
37413763
addCheckedRemoteUrl(args, url);
37423764

3743-
StandardCredentials cred = credentials.get(url);
3744-
if (cred == null) {
3745-
cred = defaultCredentials;
3746-
}
3765+
StandardCredentials cred = getCredentials(url);
37473766

37483767
String result = launchCommandWithCredentials(args, null, cred, url);
37493768

@@ -3768,10 +3787,7 @@ public ObjectId getHeadRev(String url, String branchSpec) throws GitException, I
37683787
args.add("-h");
37693788
}
37703789

3771-
StandardCredentials cred = credentials.get(url);
3772-
if (cred == null) {
3773-
cred = defaultCredentials;
3774-
}
3790+
StandardCredentials cred = getCredentials(url);
37753791

37763792
addCheckedRemoteUrl(args, url);
37773793

@@ -3800,10 +3816,7 @@ public Map<String, ObjectId> getRemoteReferences(String url, String pattern, boo
38003816
args.add(pattern);
38013817
}
38023818

3803-
StandardCredentials cred = credentials.get(url);
3804-
if (cred == null) {
3805-
cred = defaultCredentials;
3806-
}
3819+
StandardCredentials cred = getCredentials(url);
38073820

38083821
String result = launchCommandWithCredentials(args, null, cred, url);
38093822

@@ -3843,10 +3856,7 @@ public Map<String, String> getRemoteSymbolicReferences(String url, String patter
38433856
args.add(pattern);
38443857
}
38453858

3846-
StandardCredentials cred = credentials.get(url);
3847-
if (cred == null) {
3848-
cred = defaultCredentials;
3849-
}
3859+
StandardCredentials cred = getCredentials(url);
38503860

38513861
String result = launchCommandWithCredentials(args, null, cred, url);
38523862

@@ -3886,10 +3896,7 @@ public void push(RemoteConfig repository, String refspec) throws GitException, I
38863896
ArgumentListBuilder args = new ArgumentListBuilder();
38873897
URIish uri = repository.getURIs().get(0);
38883898
String url = uri.toPrivateString();
3889-
StandardCredentials cred = credentials.get(url);
3890-
if (cred == null) {
3891-
cred = defaultCredentials;
3892-
}
3899+
StandardCredentials cred = getCredentials(url);
38933900

38943901
args.add("push");
38953902
addCheckedRemoteUrl(args, url);
@@ -3992,6 +3999,14 @@ private static boolean setsidExists() {
39923999
return false;
39934000
}
39944001

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

0 commit comments

Comments
 (0)