Skip to content

Commit

Permalink
[Fix JENKINS-29482] remove branches in BuildData history which doesn’…
Browse files Browse the repository at this point in the history
…t exist on remote repo (anymore)
  • Loading branch information
ndeloof committed Jul 20, 2015
1 parent 05c5b1a commit c1872d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
@@ -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;
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/hudson/plugins/git/util/BuildData.java
Expand Up @@ -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.