Skip to content

Commit

Permalink
JENKINS-64844 allow checkout of partially cloned repo
Browse files Browse the repository at this point in the history
  • Loading branch information
magu committed Jul 22, 2024
1 parent 2d59762 commit c8a729f
Showing 1 changed file with 48 additions and 33 deletions.
81 changes: 48 additions & 33 deletions src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ public class CliGitAPIImpl extends LegacyCompatibleGitAPIImpl {
EnvVars environment;
private Map<String, StandardCredentials> 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
Expand Down Expand Up @@ -672,10 +671,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);
}

Expand Down Expand Up @@ -3162,23 +3158,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)) {

Check warning on line 3164 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 3164 is only partially covered, one branch is missing
String result = firstLine(launchCommand(
"config", "--get", "--default", "''", "remote.origin.partialclonefilter"));
if (result != null && !result.isBlank()) {

Check warning on line 3167 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 3167 is only partially covered, 2 branches are missing
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()) {

Check warning on line 3172 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 3172 is only partially covered, one branch is missing
checkoutUrl = null;

Check warning on line 3173 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 3173 is not covered by tests
} else {
cred = getCredentials(checkoutUrl);
}
}
}

if (checkoutUrl != null) {

Check warning on line 3180 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 3180 is only partially covered, one branch is missing
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);

Check warning on line 3186 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 3185-3186 are not covered by tests
}
} 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);

Check warning on line 3203 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 3194-3203 are not covered by tests
} catch (URISyntaxException e) {
throw new GitException("Invalid URL " + url, e);
}
Expand Down Expand Up @@ -3722,10 +3744,7 @@ public Map<String, ObjectId> 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);

Expand All @@ -3750,10 +3769,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);

Expand Down Expand Up @@ -3782,10 +3798,7 @@ public Map<String, ObjectId> 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);

Expand Down Expand Up @@ -3825,10 +3838,7 @@ public Map<String, String> 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);

Expand Down Expand Up @@ -3868,10 +3878,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);
Expand Down Expand Up @@ -3974,6 +3981,14 @@ private static boolean setsidExists() {
return false;
}

private StandardCredentials getCredentials(String url) {
StandardCredentials cred = credentials.get(url);
if (cred == null) {

Check warning on line 3986 in src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 3986 is only partially covered, one branch is missing
cred = defaultCredentials;
}
return cred;
}

/** {@inheritDoc} */
@Override
public Set<GitObject> getTags() throws GitException, InterruptedException {
Expand Down

0 comments on commit c8a729f

Please sign in to comment.