Skip to content
Permalink
Browse files

[Fix JENKINS-29482] Don't prune stale branches from BuildData

This reverts commit c1872d0.

Git plugin 2.4.1 included c1872d0 which regressed the plugin and
introduced JENKINS-32218
  • Loading branch information
MarkEWaite committed Feb 2, 2016
1 parent c8d05b9 commit 336c1787c7b967e4220c4d459092397fda29892a
@@ -1,13 +1,11 @@
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;
@@ -31,12 +29,6 @@ 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
@@ -32,7 +32,7 @@
*
* <p>
* This map contains all the branches we've built in the past (including the build that this {@link BuildData}
* is attached to)
* is attached to)
*/
public Map<String, Build> buildsByBranchName = new HashMap<String, Build>();

@@ -90,7 +90,7 @@ public String getUrlName() {

public Object readResolve() {
Map<String,Build> newBuildsByBranchName = new HashMap<String,Build>();

for (Map.Entry<String, Build> buildByBranchName : buildsByBranchName.entrySet()) {
String branchName = fixNull(buildByBranchName.getKey());
Build build = buildByBranchName.getValue();
@@ -104,7 +104,7 @@ public Object readResolve() {

return this;
}

/**
* Return true if the history shows this SHA1 has been built.
* False otherwise.
@@ -147,7 +147,7 @@ public Build getLastBuildOfBranch(String branch) {

/**
* Gets revision of the previous build.
* @return revision of the last build.
* @return revision of the last build.
* May be null will be returned if nothing has been checked out (e.g. due to wrong repository or branch)
*/
@Exported
@@ -243,41 +243,4 @@ public String toString() {
",buildsByBranchName="+buildsByBranchName+
",lastBuild="+lastBuild+"]";
}

@Override
public boolean equals(Object o) {

This comment has been minimized.

Copy link
@jglick
if (!(o instanceof BuildData)) {
return false;
} else {
BuildData otherBuildData = (BuildData) o;

if (otherBuildData.remoteUrls.equals(this.remoteUrls)
&& otherBuildData.buildsByBranchName.equals(this.buildsByBranchName)
&& otherBuildData.lastBuild.equals(this.lastBuild)) {
return true;
} else {
return false;
}
}
}

/**
* 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 336c178

Please sign in to comment.