Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an "opt-out" global switch to retain second fetch #927

Merged
merged 11 commits into from
Jul 4, 2020
Merged
5 changes: 5 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ Show the entire commit summary in changes::
With the release of git plugin 4.0, the default was changed to show the complete change summary.
Administrators that want to restore the old behavior may disable this setting.

[[preserve-second-fetch-during-checkout]]
Preserve second fetch during checkout::

If checked the checkout step will not avoid the second fetch.
rishabhBudhouliya marked this conversation as resolved.
Show resolved Hide resolved

[#repository-browser]
=== Repository Browser

Expand Down
17 changes: 15 additions & 2 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ public boolean isUseExistingAccountWithSameEmail() {
return (gitDescriptor != null && gitDescriptor.isUseExistingAccountWithSameEmail());
}

public boolean isRedundantFetchAllowed() {
DescriptorImpl gitDescriptor = getDescriptor();
return (gitDescriptor != null && gitDescriptor.isRedundantFetchAllowed());
}

@Whitelisted
public BuildChooser getBuildChooser() {
BuildChooser bc;
Expand Down Expand Up @@ -835,7 +840,6 @@ public GitClient createClient(TaskListener listener, EnvVars environment, Run<?,

String gitExe = getGitExe(n, listener);
Git git = Git.with(listener, environment).in(ws).using(gitExe);

rishabhBudhouliya marked this conversation as resolved.
Show resolved Hide resolved
GitClient c = git.getClient();
for (GitSCMExtension ext : extensions) {
c = ext.decorate(this,c);
Expand Down Expand Up @@ -1133,7 +1137,9 @@ private void retrieveChanges(Run build, GitClient git, TaskListener listener) th
cmd.execute();
// determine if second fetch is required
CloneOption option = extensions.get(CloneOption.class);
removeRedundantFetch = determineRedundantFetch(option, rc);
if (!isRedundantFetchAllowed()) {
removeRedundantFetch = determineRedundantFetch(option, rc);
}
} catch (GitException ex) {
ex.printStackTrace(listener.error("Error cloning remote repo '" + rc.getName() + "'"));
throw new AbortException("Error cloning remote repo '" + rc.getName() + "'");
Expand Down Expand Up @@ -1519,6 +1525,7 @@ public static final class DescriptorImpl extends SCMDescriptor<GitSCM> {
private boolean useExistingAccountWithSameEmail;
// private GitClientType defaultClientType = GitClientType.GITCLI;
private boolean showEntireCommitSummaryInChanges;
private boolean allowSecondFetch;

public DescriptorImpl() {
super(GitSCM.class, GitRepositoryBrowser.class);
Expand Down Expand Up @@ -1642,6 +1649,12 @@ public void setUseExistingAccountWithSameEmail(boolean useExistingAccountWithSam
this.useExistingAccountWithSameEmail = useExistingAccountWithSameEmail;
}

public boolean isRedundantFetchAllowed() { return allowSecondFetch; }

public void setAllowSecondFetch(boolean allowSecondFetch) {
rishabhBudhouliya marked this conversation as resolved.
Show resolved Hide resolved
this.allowSecondFetch = allowSecondFetch;
}

/**
* Old configuration of git executable - exposed so that we can
* migrate this setting to GitTool without deprecation warnings.
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/hudson/plugins/git/GitSCM/global.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<f:entry field="showEntireCommitSummaryInChanges">
<f:checkbox title="${%Show the entire commit summary in changes}" name="showEntireCommitSummaryInChanges" checked="${descriptor.showEntireCommitSummaryInChanges}"/>
</f:entry>
<f:entry field="allowSecondFetch">
<f:checkbox title="${%Preserve second fetch during checkout}" name="allowSecondFetch" checked="${descriptor.allowSecondFetch}"/>
</f:entry>
<!--
<f:entry title="${%Default git client implementation}" field="defaultClientType">
<select>
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/hudson/plugins/git/GitSCMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,42 @@ public void testRetainRedundantFetch() throws Exception {
assertRedundantFetchIsUsed(build, refspec);
}

/*
* When "Preserve second fetch during checkout" is checked in during configuring Jenkins,
* the second fetch should be retained
*/
@Test
@Issue("JENKINS-49757")
public void testRetainRedundantFetchIfSecondFetchIsAllowed() throws Exception {
String refspec = "+refs/heads/*:refs/remotes/*";
List<UserRemoteConfig> repos = new ArrayList<>();
repos.add(new UserRemoteConfig(testRepo.gitDir.getAbsolutePath(), "origin", refspec, null));

/* Without honor refspec on initial clone */
FreeStyleProject projectWithMaster = setupProject(repos, Collections.singletonList(new BranchSpec("master")), null, false, null);

GitSCM scm = (GitSCM) projectWithMaster.getScm();
final DescriptorImpl descriptor = (DescriptorImpl) scm.getDescriptor();
assertThat("Redundant fetch is skipped by default", scm.isRedundantFetchAllowed(), is(false));
descriptor.setAllowSecondFetch(true);
assertThat("Redundant fetch should be allowed", scm.isRedundantFetchAllowed(), is(true));

if (random.nextBoolean()) {
/* Randomly enable shallow clone, should not alter test assertions */
CloneOption cloneOptionMaster = new CloneOption(false, null, null);
cloneOptionMaster.setDepth(1);
((GitSCM) projectWithMaster.getScm()).getExtensions().add(cloneOptionMaster);
}

// create initial commit
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit in master");

FreeStyleBuild build = build(projectWithMaster, Result.SUCCESS);

assertRedundantFetchIsUsed(build, refspec);
}

// Checks if the second fetch is being avoided
private void assertRedundantFetchIsSkipped(FreeStyleBuild build, String refSpec) throws IOException {
assertRedundantFetchCount(build, refSpec, 1);
Expand Down