Skip to content

Commit

Permalink
[JENKINS-20258] prune branches during fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeloof committed Oct 29, 2013
1 parent 41e5962 commit f6d42c4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -196,7 +196,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git-client</artifactId>
<version>1.4.4</version>
<version>1.4.7-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/hudson/plugins/git/GitSCM.java
Expand Up @@ -47,6 +47,7 @@
import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.plugins.gitclient.ChangelogCommand;
import org.jenkinsci.plugins.gitclient.CloneCommand;
import org.jenkinsci.plugins.gitclient.FetchCommand;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.gitclient.JGitTool;
Expand Down Expand Up @@ -603,11 +604,15 @@ private BuildData fixNull(BuildData bd) {
*/
private void fetchFrom(GitClient git,
TaskListener listener,
RemoteConfig remoteRepository) throws InterruptedException {
RemoteConfig remoteRepository) throws InterruptedException, IOException {

for (URIish url : remoteRepository.getURIs()) {
try {
git.fetch(url, remoteRepository.getFetchRefSpecs());
FetchCommand fetch = git.fetch_().from(url, remoteRepository.getFetchRefSpecs());
for (GitSCMExtension extension : extensions) {
extension.decorateFetchCommand(this, git, listener, fetch);
}
fetch.execute();
} catch (GitException ex) {
throw new GitException("Failed to fetch from "+url.toString(), ex);
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
import hudson.plugins.git.util.BuildData;
import hudson.scm.SCM;
import org.jenkinsci.plugins.gitclient.CloneCommand;
import org.jenkinsci.plugins.gitclient.FetchCommand;
import org.jenkinsci.plugins.gitclient.MergeCommand;
import org.jenkinsci.plugins.gitclient.GitClient;

Expand Down Expand Up @@ -131,6 +132,12 @@ public GitClient decorate(GitSCM scm, GitClient git) throws IOException, Interru
public void decorateCloneCommand(GitSCM scm, AbstractBuild<?, ?> build, GitClient git, BuildListener listener, CloneCommand cmd) throws IOException, InterruptedException, GitException {
}

/**
* Called before a {@link FetchCommand} is executed to allow extensions to alter its behaviour.
*/
public void decorateFetchCommand(GitSCM scm, GitClient git, TaskListener listener, FetchCommand cmd) throws IOException, InterruptedException, GitException {
}

/**
* Called before a {@link MergeCommand} is executed to allow extensions to alter its behaviour.
*/
Expand Down
@@ -1,13 +1,13 @@
package hudson.plugins.git.extensions.impl;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.TaskListener;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;
import org.eclipse.jgit.transport.RemoteConfig;
import org.jenkinsci.plugins.gitclient.FetchCommand;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.kohsuke.stapler.DataBoundConstructor;

Expand All @@ -25,11 +25,9 @@ public PruneStaleBranch() {
}

@Override
public void beforeCheckout(GitSCM scm, AbstractBuild<?, ?> build, GitClient git, BuildListener listener) throws IOException, InterruptedException, GitException {
public void decorateFetchCommand(GitSCM scm, GitClient git, TaskListener listener, FetchCommand cmd) throws IOException, InterruptedException, GitException {
listener.getLogger().println("Pruning obsolete local branches");
for (RemoteConfig remoteRepository : scm.getRepositories()) {
git.prune(remoteRepository);
}
cmd.prune();
}

@Extension
Expand Down

0 comments on commit f6d42c4

Please sign in to comment.