Skip to content

The Unified and Autonomous Development Platform

midwork

robin shineb3207d53014b
diff --git a/commons.git/src/main/java/com/pmease/commons/git/CommitLane.java b/commons.git/src/main/java/com/pmease/commons/git/CommitLane.java
index fdda4da31f..1a2dae5853 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/CommitLane.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/CommitLane.java
@@ -14,7 +14,7 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
@SuppressWarnings("serial")
public class CommitLane implements Serializable {
- List<Map<Column, Integer>> rows = new ArrayList<>();
+ private final List<Map<Column, Integer>> rows = new ArrayList<>();
public CommitLane(List<Commit> commits) {
Map<String, Integer> commitRowIndexes = new HashMap<>();
@@ -28,6 +28,7 @@ public class CommitLane implements Serializable {
if (rowIndex == 0) {
row.put(new Column(0, 0), 0);
} else {
+ Column commitColumn = new Column(rowIndex, rowIndex);
final Map<Column, Integer> lastRow = rows.get(rowIndex-1);
List<Column> columnsOfLastRow = new ArrayList<>(lastRow.keySet());
Collections.sort(columnsOfLastRow, new Comparator<Column>() {
@@ -43,11 +44,10 @@ public class CommitLane implements Serializable {
Column columnOfLastRow = columnsOfLastRow.get(columnIndexOfLastRow);
if (columnOfLastRow.commitRowIndex != columnOfLastRow.parentRowIndex) {
if (columnOfLastRow.parentRowIndex == rowIndex) {
- Column column = new Column(rowIndex, rowIndex);
- if (!row.containsKey(column))
- row.put(column, columnIndex++);
+ if (!row.containsKey(commitColumn))
+ row.put(commitColumn, columnIndex++);
} else {
- row.put(new Column(columnOfLastRow), columnIndex++);
+ row.put(columnOfLastRow, columnIndex++);
}
} else {
Commit lastCommit = commits.get(rowIndex-1);
@@ -57,8 +57,9 @@ public class CommitLane implements Serializable {
if (parentRowIndex != null) {
Column column;
if (parentRowIndex.intValue() == rowIndex) {
- column = new Column(rowIndex, rowIndex);
- if (row.containsKey(column))
+ if (!row.containsKey(commitColumn))
+ column = commitColumn;
+ else
column = null;
} else {
column = new Column(rowIndex-1, parentRowIndex);
@@ -101,25 +102,36 @@ public class CommitLane implements Serializable {
row.put(column, columnIndex++);
}
}
+ if (!row.containsKey(commitColumn))
+ row.put(commitColumn, columnIndex++);
}
rows.add(row);
}
+
}
- static class Column implements Serializable {
- int commitRowIndex;
+ public List<Map<Column, Integer>> getRows() {
+ return rows;
+ }
+
+ public static class Column implements Serializable {
+ private final int commitRowIndex;
- int parentRowIndex;
+ private final int parentRowIndex;
- Column(int commitRowIndex, int parentRowIndex) {
+ public Column(int commitRowIndex, int parentRowIndex) {
this.commitRowIndex = commitRowIndex;
this.parentRowIndex = parentRowIndex;
}
- Column(Column column) {
- this(column.commitRowIndex, column.parentRowIndex);
+ public int getCommitRowIndex() {
+ return commitRowIndex;
}
-
+
+ public int getParentRowIndex() {
+ return parentRowIndex;
+ }
+
@Override
public boolean equals(Object other) {
if (!(other instanceof Column))
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/LogCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/LogCommand.java
index 459f7a7831..9cd28d0a52 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/LogCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/LogCommand.java
@@ -28,6 +28,8 @@ public class LogCommand extends GitCommand<List<Commit>> {
private Date untilDate;
+ private boolean allBranchesAndTags;
+
private String path;
private int maxCount;
@@ -50,7 +52,11 @@ public class LogCommand extends GitCommand<List<Commit>> {
return this;
}
- public LogCommand sinceDate(Date sinceDate) {
+ public void allBranchesAndTags(boolean allBranchesAndTags) {
+ this.allBranchesAndTags = allBranchesAndTags;
+ }
+
+ public LogCommand sinceDate(Date sinceDate) {
this.sinceDate = sinceDate;
return this;
}
@@ -98,6 +104,9 @@ public class LogCommand extends GitCommand<List<Commit>> {
cmd.addArgs(toRev);
}
+ if (allBranchesAndTags)
+ cmd.addArgs("--branches", "--tags");
+
if (sinceDate != null)
cmd.addArgs("--since").addArgs(DateFormatUtils.ISO_DATE_FORMAT.format(sinceDate));
diff --git a/commons.git/src/test/java/com/pmease/commons/git/CommitLaneTest.java b/commons.git/src/test/java/com/pmease/commons/git/CommitLaneTest.java
new file mode 100644
index 0000000000..9de12df6fe
--- /dev/null
+++ b/commons.git/src/test/java/com/pmease/commons/git/CommitLaneTest.java
@@ -0,0 +1,86 @@
+package com.pmease.commons.git;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+import com.pmease.commons.git.command.LogCommand;
+import com.pmease.commons.git.command.MergeCommand.FastForwardMode;
+
+public class CommitLaneTest extends AbstractGitTest {
+
+ @Test
+ public void testLinear() {
+ addFileAndCommit("file1", "", "file1");
+ addFileAndCommit("file2", "", "file2");
+ addFileAndCommit("file3", "", "file3");
+
+ LogCommand logCommand = new LogCommand(git.repoDir());
+ CommitLane lane = new CommitLane(logCommand.call());
+
+ assertEquals(3, lane.getRows().size());
+ assertEquals((Integer)0, lane.getRows().get(0).get(new CommitLane.Column(0, 0)));
+ assertEquals((Integer)0, lane.getRows().get(1).get(new CommitLane.Column(1, 1)));
+ assertEquals((Integer)0, lane.getRows().get(2).get(new CommitLane.Column(2, 2)));
+ }
+
+ @Test
+ public void testNonFastForwardMerge() {
+ addFileAndCommit("file1", "", "file1");
+ git.checkout("master", "dev");
+ addFileAndCommit("file2", "", "file2");
+ git.checkout("master", null);
+ git.merge("dev", FastForwardMode.NO_FF, null, null, null);
+
+ LogCommand logCommand = new LogCommand(git.repoDir());
+ CommitLane lane = new CommitLane(logCommand.call());
+
+ assertEquals(3, lane.getRows().size());
+ assertEquals((Integer)0, lane.getRows().get(0).get(new CommitLane.Column(0, 0)));
+ assertEquals((Integer)0, lane.getRows().get(1).get(new CommitLane.Column(1, 1)));
+ assertEquals((Integer)1, lane.getRows().get(1).get(new CommitLane.Column(0, 2)));
+ assertEquals((Integer)0, lane.getRows().get(2).get(new CommitLane.Column(2, 2)));
+ }
+
+ @Test
+ public void testMerge() {
+ addFileAndCommit("file1", "", "file1");
+ addFileAndCommit("file2", "", "file2");
+ git.checkout("master~1", "dev");
+ addFileAndCommit("file3", "", "file3");
+ git.merge("master", null, null, null, null);
+
+ LogCommand logCommand = new LogCommand(git.repoDir());
+ CommitLane lane = new CommitLane(logCommand.call());
+
+ assertEquals(4, lane.getRows().size());
+ assertEquals((Integer)0, lane.getRows().get(0).get(new CommitLane.Column(0, 0)));
+ assertEquals((Integer)0, lane.getRows().get(1).get(new CommitLane.Column(1, 1)));
+ assertEquals((Integer)1, lane.getRows().get(1).get(new CommitLane.Column(0, 2)));
+ assertEquals((Integer)0, lane.getRows().get(2).get(new CommitLane.Column(1, 3)));
+ assertEquals((Integer)1, lane.getRows().get(2).get(new CommitLane.Column(2, 2)));
+ assertEquals((Integer)0, lane.getRows().get(3).get(new CommitLane.Column(3, 3)));
+ }
+
+
+ @Test
+ public void testMultiHeads() {
+ addFileAndCommit("file1", "", "file1");
+ addFileAndCommit("file2", "", "file2");
+ addFileAndCommit("file3", "", "file3");
+ git.checkout("master~1", "dev");
+ addFileAndCommit("file4", "", "file4");
+
+ LogCommand logCommand = new LogCommand(git.repoDir());
+ logCommand.allBranchesAndTags(true);
+ CommitLane lane = new CommitLane(logCommand.call());
+
+ assertEquals(4, lane.getRows().size());
+ assertEquals((Integer)0, lane.getRows().get(0).get(new CommitLane.Column(0, 0)));
+ assertEquals((Integer)0, lane.getRows().get(1).get(new CommitLane.Column(0, 2)));
+ assertEquals((Integer)1, lane.getRows().get(1).get(new CommitLane.Column(1, 1)));
+ assertEquals((Integer)0, lane.getRows().get(2).get(new CommitLane.Column(2, 2)));
+ assertEquals((Integer)0, lane.getRows().get(3).get(new CommitLane.Column(3, 3)));
+ }
+
+}