Skip to content

Commit

Permalink
[FIXED JENKINS-2344] filter duplicated (e.g. because of svn:externals…
Browse files Browse the repository at this point in the history
  • Loading branch information
kutzi committed Nov 6, 2011
1 parent e4b7fee commit 8fb6755
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 13 deletions.
86 changes: 73 additions & 13 deletions src/main/java/hudson/scm/SubversionChangeLogSet.java
Expand Up @@ -29,18 +29,21 @@
import hudson.scm.SubversionChangeLogSet.LogEntry;
import hudson.scm.SubversionSCM.ModuleLocation;

import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

import java.io.IOException;
import java.io.Serializable;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Comparator;
import java.util.Set;

import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

/**
* {@link ChangeLogSet} for Subversion.
Expand All @@ -57,15 +60,7 @@ public final class SubversionChangeLogSet extends ChangeLogSet<LogEntry> {

/*package*/ SubversionChangeLogSet(AbstractBuild<?,?> build, List<LogEntry> logs) {
super(build);
// we want recent changes first
Collections.sort(logs,new Comparator<LogEntry>() {
public int compare(LogEntry a, LogEntry b) {
return b.getRevision()-a.getRevision();
}
});
this.logs = Collections.unmodifiableList(logs);
for (LogEntry log : logs)
log.setParent(this);
this.logs = prepareChangeLogEntries(logs);
}

public boolean isEmptySet() {
Expand All @@ -91,6 +86,27 @@ public synchronized Map<String,Long> getRevisionMap() throws IOException {
revisionMap = SubversionSCM.parseRevisionFile(build);
return revisionMap;
}

private List<LogEntry> prepareChangeLogEntries(List<LogEntry> items) {
items = removeDuplicatedEntries(items);
// we want recent changes first
Collections.sort(items, new ReverseByRevisionComparator());
for (LogEntry log : items) {
log.setParent(this);
}
return Collections.unmodifiableList(items);
}

/**
* Removes duplicate entries, e.g. those coming form svn:externals.
*
* @param items list of items
* @return filtered list without duplicated entries
*/
static List<LogEntry> removeDuplicatedEntries(List<LogEntry> items) {
Set<LogEntry> entries = new HashSet<LogEntry>(items);
return new ArrayList<LogEntry>(entries);
}

@Exported
public List<RevisionInfo> getRevisions() throws IOException {
Expand Down Expand Up @@ -271,6 +287,42 @@ public int compare(Path o1, Path o2) {
}
});
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

LogEntry that = (LogEntry) o;

if (revision != that.revision) {
return false;
}
if (author != null ? !author.equals(that.author) : that.author != null) {
return false;
}
if (date != null ? !date.equals(that.date) : that.date != null) {
return false;
}
if (msg != null ? !msg.equals(that.msg) : that.msg != null) {
return false;
}

return true;
}

@Override
public int hashCode() {
int result = revision;
result = 31 * result + (author != null ? author.hashCode() : 0);
result = 31 * result + (date != null ? date.hashCode() : 0);
result = 31 * result + (msg != null ? msg.hashCode() : 0);
return result;
}
}

/**
Expand Down Expand Up @@ -331,4 +383,12 @@ public EditType getEditType() {
return EditType.EDIT;
}
}

private static final class ReverseByRevisionComparator implements Comparator<LogEntry>, Serializable {
private static final long serialVersionUID = 1L;

public int compare(LogEntry a, LogEntry b) {
return b.getRevision() - a.getRevision();
}
}
}
77 changes: 77 additions & 0 deletions src/test/java/hudson/scm/SubversionChangeLogSetTest.java
@@ -0,0 +1,77 @@
/*
* The MIT License
*
* Copyright (c) 2011, Oracle Corporation, Nikita Levyankov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.scm;

import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;

/**
* SubversionChangeLogSet test cases.
* <p/>
* Date: 6/28/11
*
* @author Nikita Levyankov
*/
public class SubversionChangeLogSetTest {

@Test
public void testRemoveDuplicateEntries() throws Exception{
//One duplicated entry. 7 unique, 8 total entries.
List<SubversionChangeLogSet.LogEntry> items = new ArrayList<SubversionChangeLogSet.LogEntry>();
items.add(buildChangeLogEntry(1, "Test msg"));
items.add(buildChangeLogEntry(2, "Test msg"));
items.add(buildChangeLogEntry(1, "Test msg"));
items.add(buildChangeLogEntry(3, "Test msg"));
items.add(buildChangeLogEntry(4, "Test msg"));
items.add(buildChangeLogEntry(5, "Test msg"));
items.add(buildChangeLogEntry(6, "Test msg"));
items.add(buildChangeLogEntry(1, "Test msg1"));
Assert.assertEquals("Items size is not equals to expected", items.size(), 8);
List<SubversionChangeLogSet.LogEntry> resultItems = SubversionChangeLogSet.removeDuplicatedEntries(items);
Assert.assertEquals(resultItems.size(), 7);

//No duplicated entries. Total 7
items = new ArrayList<SubversionChangeLogSet.LogEntry>();
items.add(buildChangeLogEntry(1, "Test msg"));
items.add(buildChangeLogEntry(2, "Test msg"));
items.add(buildChangeLogEntry(3, "Test msg"));
items.add(buildChangeLogEntry(4, "Test msg"));
items.add(buildChangeLogEntry(5, "Test msg"));
items.add(buildChangeLogEntry(6, "Test msg"));
items.add(buildChangeLogEntry(1, "Test msg1"));
Assert.assertEquals("Items size is not equals to expected", items.size(), 7);
resultItems = SubversionChangeLogSet.removeDuplicatedEntries(items);
Assert.assertEquals(resultItems.size(), 7);
}


private SubversionChangeLogSet.LogEntry buildChangeLogEntry(int revision, String msg) {
SubversionChangeLogSet.LogEntry entry = new SubversionChangeLogSet.LogEntry();
entry.setRevision(revision);
entry.setMsg(msg);
return entry;
}
}

0 comments on commit 8fb6755

Please sign in to comment.