Skip to content

Commit

Permalink
[Fix JENKINS-29482] Don't prune stale branches from BuildData
Browse files Browse the repository at this point in the history
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 336c178
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 49 deletions.
@@ -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;
Expand All @@ -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
Expand Down
45 changes: 4 additions & 41 deletions src/main/java/hudson/plugins/git/util/BuildData.java
Expand Up @@ -32,7 +32,7 @@ public class BuildData implements Action, Serializable, Cloneable {
*
* <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>();

Expand Down Expand Up @@ -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();
Expand All @@ -104,7 +104,7 @@ public Object readResolve() {

return this;
}

/**
* Return true if the history shows this SHA1 has been built.
* False otherwise.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

jglick Mar 22, 2016

Member
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.