The Unified and Autonomous Development Platform
midwork
robin shinedea8d218f44d
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.javaindex 1a2dae5853..6cf6a12083 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,9 +14,21 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;@SuppressWarnings("serial")public class CommitLane implements Serializable {- private final List<Map<Column, Integer>> rows = new ArrayList<>();+ /*+ * Each list item represents a row, and each row represents a map of+ * line to its drawn column at this row. With this info, one can+ * go through each row to draw every child->parent line. Taking+ * line (2,4) for instance, we first find commit column at row 2+ * (by looking up the special line (2,2)), then find its column+ * at row 3, and then find commit column at row 4 (by looking up the+ * special line (4,4)). Finally connect these columns we got the+ * line drawn.+ */+ private final List<Map<Line, Integer>> rows = new ArrayList<>();++ private final Map<Line, String> colorAssignments = new HashMap<>();- public CommitLane(List<Commit> commits) {+ public CommitLane(List<Commit> commits, List<String> colors) {Map<String, Integer> commitRowIndexes = new HashMap<>();for (int i=0; i<commits.size(); i++) {Commit commit = commits.get(i);@@ -24,108 +36,142 @@ public class CommitLane implements Serializable {}for (int rowIndex=0; rowIndex<commits.size(); rowIndex++) {- Map<Column, Integer> row = new HashMap<>();+ Map<Line, Integer> row = new HashMap<>();if (rowIndex == 0) {- row.put(new Column(0, 0), 0);+ row.put(new Line(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>() {+ // special line represents the commit at rowIndex itself+ Line commitLine = new Line(rowIndex, rowIndex);+ final Map<Line, Integer> lastRow = rows.get(rowIndex-1);+ List<Line> linesOfLastRow = new ArrayList<>(lastRow.keySet());++ // examine lines of last row from left to right, so we sort them+ Collections.sort(linesOfLastRow, new Comparator<Line>() {@Override- public int compare(Column column1, Column column2) {- return lastRow.get(column1) - lastRow.get(column2);+ public int compare(Line line1, Line line2) {+ return lastRow.get(line1) - lastRow.get(line2);}});- int columnIndex = 0;- for (int columnIndexOfLastRow = 0; columnIndexOfLastRow<columnsOfLastRow.size(); columnIndexOfLastRow++) {- Column columnOfLastRow = columnsOfLastRow.get(columnIndexOfLastRow);- if (columnOfLastRow.commitRowIndex != columnOfLastRow.parentRowIndex) {- if (columnOfLastRow.parentRowIndex == rowIndex) {- if (!row.containsKey(commitColumn))- row.put(commitColumn, columnIndex++);+ int column = 0;+ for (int columnOfLastRow = 0; columnOfLastRow<linesOfLastRow.size(); columnOfLastRow++) {+ Line lineOfLastRow = linesOfLastRow.get(columnOfLastRow);++ if (lineOfLastRow.childRowIndex != lineOfLastRow.parentRowIndex) {+ // line not started from last row, in this case, the line+ // only occupies a column when it goes through current row+ if (lineOfLastRow.parentRowIndex == rowIndex) {+ if (!row.containsKey(commitLine))+ row.put(commitLine, column++);} else {- row.put(columnOfLastRow, columnIndex++);+ row.put(lineOfLastRow, column++);}} else {+ // determine columns for lines starting from last row is a bit complicated,+ // if commit of last row has N parents, we have to assign N columns at+ // current row, the order of these columns matters: to minimize line+ // crossovers (at least for these parents), we calculate score of each line+ // and use the score to order the lineCommit lastCommit = commits.get(rowIndex-1);- final Map<Column, Integer> columnScores = new HashMap<>();+ final Map<Line, Integer> lineScores = new HashMap<>();for (String parentHash: lastCommit.getParentHashes()) {Integer parentRowIndex = commitRowIndexes.get(parentHash);if (parentRowIndex != null) {- Column column;+ Line line;if (parentRowIndex.intValue() == rowIndex) {- if (!row.containsKey(commitColumn))- column = commitColumn;+ if (!row.containsKey(commitLine))+ line = commitLine;else- column = null;+ line = null;} else {- column = new Column(rowIndex-1, parentRowIndex);+ line = new Line(rowIndex-1, parentRowIndex);}- if (column != null) {+ if (line != null) {int score = 0;- for (int i=0; i<columnIndexOfLastRow; i++) {- if (columnsOfLastRow.get(i).parentRowIndex == column.parentRowIndex)+ for (int i=0; i<columnOfLastRow; i++) {+ // minus score by one if there is a line pulling current line+ // from left+ if (linesOfLastRow.get(i).parentRowIndex == line.parentRowIndex)score--;}- for (int i=columnIndexOfLastRow+1; i<columnsOfLastRow.size(); i++) {- if (columnsOfLastRow.get(i).parentRowIndex == column.parentRowIndex)+ for (int i=columnOfLastRow+1; i<linesOfLastRow.size(); i++) {+ // plus score by one if there is a line pulling current line+ // from right+ if (linesOfLastRow.get(i).parentRowIndex == line.parentRowIndex)score++;}+ // normalize the score so we can use score subtraction to arrange the+ // order laterif (score > 0)score = 1;else if (score < 0)score = -1;- columnScores.put(column, score);+ lineScores.put(line, score);}}}- List<Column> columns = new ArrayList<>(columnScores.keySet());- Collections.sort(columns, new Comparator<Column>() {+ List<Line> lines = new ArrayList<>(lineScores.keySet());+ Collections.sort(lines, new Comparator<Line>() {@Override- public int compare(Column column1, Column column2) {- int score1 = columnScores.get(column1);- int score2 = columnScores.get(column2);- if (score1 != score2)- return score1 - score2;+ public int compare(Line line1, Line line2) {+ int score1 = lineScores.get(line1);+ int score2 = lineScores.get(line2);+ if (score1 != score2)+ // put the line at the side where there is a pull from that side+ return score1 - score2;else if (score1>0)- return column2.parentRowIndex - column1.parentRowIndex;+ // if both lines are pulled from right side, we put longer line at left side,+ // otherwise there will exist a line crossover+ return line2.parentRowIndex - line1.parentRowIndex;else- return column1.parentRowIndex - column2.parentRowIndex;+ // if both lines are pulled from left side, we put shorter line at left side,+ // otherwise there will exist a line crossover. Additionally, if both lines+ // have score of 0, we put shorter line at left side in order to make commit+ // points at left side as possible as we can+ return line1.parentRowIndex - line2.parentRowIndex;}});- for (Column column: columns)- row.put(column, columnIndex++);+ for (Line line: lines)+ row.put(line, column++);}}- if (!row.containsKey(commitColumn))- row.put(commitColumn, columnIndex++);+ if (!row.containsKey(commitLine))+ row.put(commitLine, column++);}rows.add(row);}}- public List<Map<Column, Integer>> getRows() {+ public List<Map<Line, Integer>> getRows() {return rows;}- public static class Column implements Serializable {- private final int commitRowIndex;+ /**+ * A line represents a child->parent relationship. For instance line(1,5)+ * represents the line from commit at row 1 (the child ) to commit at+ * row 5 (the parent). In case child row index equals parent row index,+ * the line represents a commit.+ *+ * @author robin+ *+ */+ public static class Line implements Serializable {++ private final int childRowIndex;private final int parentRowIndex;- public Column(int commitRowIndex, int parentRowIndex) {- this.commitRowIndex = commitRowIndex;+ public Line(int commitRowIndex, int parentRowIndex) {+ this.childRowIndex = commitRowIndex;this.parentRowIndex = parentRowIndex;}- public int getCommitRowIndex() {- return commitRowIndex;+ public int getChildRowIndex() {+ return childRowIndex;}public int getParentRowIndex() {@@ -134,23 +180,29 @@ public class CommitLane implements Serializable {@Overridepublic boolean equals(Object other) {- if (!(other instanceof Column))+ if (!(other instanceof Line))return false;if (this == other)return true;- Column otherColumn = (Column) other;+ Line otherCell = (Line) other;return new EqualsBuilder()- .append(commitRowIndex, otherColumn.commitRowIndex)- .append(parentRowIndex, otherColumn.parentRowIndex)+ .append(childRowIndex, otherCell.childRowIndex)+ .append(parentRowIndex, otherCell.parentRowIndex).isEquals();}@Overridepublic int hashCode() {return new HashCodeBuilder(17, 37)- .append(commitRowIndex)+ .append(childRowIndex).append(parentRowIndex).toHashCode();}++ @Override+ public String toString() {+ return childRowIndex+","+parentRowIndex;+ }+}}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.javaindex 9de12df6fe..8dcc164d60 100644--- a/commons.git/src/test/java/com/pmease/commons/git/CommitLaneTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/CommitLaneTest.java@@ -1,6 +1,6 @@package com.pmease.commons.git;-import static org.junit.Assert.*;+import static org.junit.Assert.assertEquals;import org.junit.Test;@@ -19,9 +19,9 @@ public class CommitLaneTest extends AbstractGitTest {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)));+ assertEquals((Integer)0, lane.getRows().get(0).get(new CommitLane.Line(0, 0)));+ assertEquals((Integer)0, lane.getRows().get(1).get(new CommitLane.Line(1, 1)));+ assertEquals((Integer)0, lane.getRows().get(2).get(new CommitLane.Line(2, 2)));}@Test@@ -36,10 +36,10 @@ public class CommitLaneTest extends AbstractGitTest {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)));+ assertEquals((Integer)0, lane.getRows().get(0).get(new CommitLane.Line(0, 0)));+ assertEquals((Integer)0, lane.getRows().get(1).get(new CommitLane.Line(1, 1)));+ assertEquals((Integer)1, lane.getRows().get(1).get(new CommitLane.Line(0, 2)));+ assertEquals((Integer)0, lane.getRows().get(2).get(new CommitLane.Line(2, 2)));}@Test@@ -54,12 +54,12 @@ public class CommitLaneTest extends AbstractGitTest {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)));+ assertEquals((Integer)0, lane.getRows().get(0).get(new CommitLane.Line(0, 0)));+ assertEquals((Integer)0, lane.getRows().get(1).get(new CommitLane.Line(1, 1)));+ assertEquals((Integer)1, lane.getRows().get(1).get(new CommitLane.Line(0, 2)));+ assertEquals((Integer)0, lane.getRows().get(2).get(new CommitLane.Line(1, 3)));+ assertEquals((Integer)1, lane.getRows().get(2).get(new CommitLane.Line(2, 2)));+ assertEquals((Integer)0, lane.getRows().get(3).get(new CommitLane.Line(3, 3)));}@@ -76,11 +76,11 @@ public class CommitLaneTest extends AbstractGitTest {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)));+ assertEquals((Integer)0, lane.getRows().get(0).get(new CommitLane.Line(0, 0)));+ assertEquals((Integer)0, lane.getRows().get(1).get(new CommitLane.Line(0, 2)));+ assertEquals((Integer)1, lane.getRows().get(1).get(new CommitLane.Line(1, 1)));+ assertEquals((Integer)0, lane.getRows().get(2).get(new CommitLane.Line(2, 2)));+ assertEquals((Integer)0, lane.getRows().get(3).get(new CommitLane.Line(3, 3)));}}