Skip to content
Permalink
Browse files

[Fix JENKINS-29482] remove branches in BuildData history which doesn’…

…t exist on remote repo (anymore)
  • Loading branch information
ndeloof committed Jul 20, 2015
1 parent 05c5b1a commit c1872d038cc2f584bafbc375744deb54a1dbe316
@@ -1,11 +1,13 @@
package hudson.plugins.git.extensions.impl;

import hudson.Extension;
import hudson.model.Run;
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 hudson.plugins.git.util.BuildData;
import org.jenkinsci.plugins.gitclient.FetchCommand;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.kohsuke.stapler.DataBoundConstructor;
@@ -29,6 +31,12 @@ public void decorateFetchCommand(GitSCM scm, GitClient git, TaskListener listene
cmd.prune();
}

@Override
public void onCheckoutCompleted(GitSCM scm, Run<?, ?> build, GitClient git, TaskListener listener) throws IOException, InterruptedException, GitException {
build.getAction(BuildData.class).purgeStaleBranches(git.getBranches());

}

@Extension
public static class DescriptorImpl extends GitSCMExtensionDescriptor {
@Override
@@ -244,4 +244,24 @@ public String toString() {
",buildsByBranchName="+buildsByBranchName+
",lastBuild="+lastBuild+"]";
}

/**
* Remove branches from BuildData that have been seen in the past but do not exist anymore
* @param keepBranches all branches available in current repository state
*/
public void purgeStaleBranches(Set<Branch> keepBranches) {
Set<String> names = new HashSet<String>(buildsByBranchName.keySet());
for (Branch branch : keepBranches) {
String name = branch.getName();
if (name.startsWith("refs/")) {
names.remove(name.substring(5));
}
if (name.startsWith("remotes/")) {
names.remove(name.substring(8));
}
}
for (String name : names) {
buildsByBranchName.remove(name);
}
}
}

0 comments on commit c1872d0

Please sign in to comment.