The Unified and Autonomous Development Platform
midwork
robin shine69e3a082db96
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/commit/RepoCommitsPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/commit/RepoCommitsPage.javaindex 3c1ced9634..260113c92f 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/commit/RepoCommitsPage.java+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/commit/RepoCommitsPage.java@@ -15,6 +15,7 @@ import org.apache.wicket.ajax.markup.html.AjaxLink;import org.apache.wicket.markup.head.CssHeaderItem;import org.apache.wicket.markup.head.IHeaderResponse;import org.apache.wicket.markup.head.JavaScriptHeaderItem;+import org.apache.wicket.markup.head.OnDomReadyHeaderItem;import org.apache.wicket.markup.html.WebMarkupContainer;import org.apache.wicket.markup.html.basic.Label;import org.apache.wicket.markup.html.link.BookmarkablePageLink;@@ -27,9 +28,12 @@ import org.apache.wicket.request.mapper.parameter.PageParameters;import org.apache.wicket.request.resource.CssResourceReference;import org.apache.wicket.request.resource.JavaScriptResourceReference;+import com.fasterxml.jackson.core.JsonProcessingException;+import com.fasterxml.jackson.databind.ObjectMapper;import com.pmease.commons.git.Commit;import com.pmease.commons.git.GitUtils;import com.pmease.commons.git.command.LogCommand;+import com.pmease.gitplex.core.GitPlex;import com.pmease.gitplex.core.model.Repository;import com.pmease.gitplex.web.component.avatar.AvatarMode;import com.pmease.gitplex.web.component.commithash.CommitHashPanel;@@ -199,8 +203,8 @@ public class RepoCommitsPage extends RepositoryPage {item.getMarkupId()));}target.prependJavaScript(builder);-target.add(footer);+ target.appendJavaScript(getCommitLaneScript());pushState(target);}@@ -303,6 +307,29 @@ public class RepoCommitsPage extends RepositoryPage {super.onDetach();}+ private String getCommitLaneScript() {+ List<Commit> commits = lastAndCurrentCommitsModel.getObject().current;+ Map<String, Integer> hash2index = new HashMap<>();+ for (int i=0; i<commits.size(); i++)+ hash2index.put(commits.get(i).getHash(), i);+ List<List<Integer>> commitIndexes = new ArrayList<>();+ for (Commit commit: commits) {+ List<Integer> parentIndexes = new ArrayList<>();+ for (String parentHash: commit.getParentHashes()) {+ Integer parentIndex = hash2index.get(parentHash);+ if (parentIndex != null)+ parentIndexes.add(parentIndex);+ }+ commitIndexes.add(parentIndexes);+ }+ try {+ String json = GitPlex.getInstance(ObjectMapper.class).writeValueAsString(commitIndexes);+ return String.format("gitplex.repocommits.drawCommitLane(%s);", json);+ } catch (JsonProcessingException e) {+ throw new RuntimeException(e);+ }+ }+@Overridepublic void renderHead(IHeaderResponse response) {super.renderHead(response);@@ -311,6 +338,8 @@ public class RepoCommitsPage extends RepositoryPage {new JavaScriptResourceReference(RepoCommitsPage.class, "repo-commits.js")));response.render(CssHeaderItem.forReference(new CssResourceReference(RepoCommitsPage.class, "repo-commits.css")));++ response.render(OnDomReadyHeaderItem.forScript(getCommitLaneScript()));}/*diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/commit/repo-commits.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/commit/repo-commits.jsindex 479eb2ad27..7b9cd7d7f2 100644--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/commit/repo-commits.js+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/commit/repo-commits.js@@ -1,5 +1,171 @@gitplex.repocommits = {- calcCommitLane: function(commits) {+ /*+ * commits is an array of ordered commit object, and the commit object is itself a+ * list of parent indexes+ */+ drawCommitLane: function(commits) {++ function getSortedLines(row, boolean reverse) {+ var lines = [];+ for (var line in row.keys())+ lines.add(line);++ lines.sort(function(x, y) {+ if (reverse)+ return row[y] - row[x];+ else+ return row[x] - row[y];+ });+ return lines;+ }++ function fromKey(lineKey) {+ return line.split(',').map(function(item) {+ return parseInt(item, 10);+ });+ }++ function toKey(line) {+ return line[0] + "," + line[1];+ }++ /*+ * rows store the map of line to row. 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.+ *+ */+ var rows = [];++ var parent2children = {};++ for (var i=0; i<commits.length; i++) {+ var commit = commits[i];+ for (var j=0; j<commit.length; j++) {+ var parent = commit[j];+ var children = parent2children[parent];+ if (!children) {+ children = [];+ parent2children[parent] = children;+ }+ children.add(i);+ }+ }++ for (int rowIndex=0; rowIndex<commits.length; rowIndex++) {+ var row = {};+ if (rowIndex == 0) {+ row["0,0"] = 0;+ } else {+ // special line represents the commit at rowIndex itself+ var commitLineKey = toKeyrowIndex + "," + rowIndex;+ var lastRow = rows[rowIndex-1];+ var column = 0;+ var linesOfLastRow = getSortedLines(lastRow, false);+ for (var i=0; i<linesOfLastRow.length; i++) {+ var lineOfLastRow = fromKey(linesOfLastRows[i]);+ if (lineOfLastRow[1] < 0) // line is cutted due to max columns limitation+ continue;+ if (lineOfLastRow[1] != lineOfLastRow[0]) { // not a commit point+ // line not started from last row, in this case, the line+ // only occupies a column when it goes through current row+ if (lineOfLastRow[1] == rowIndex) {+ if (!row[commitLineKey])+ row[commitLineKey] = column++;+ } else {+ row[toKey(lineOfLastRow)] = column++;+ }+ } else {+ var commit = commits[rowIndex-1];+ for (var j=0; j<commit.length; j++) {+ var parent = commit[j];+ if (parent == rowIndex) {+ if (!row[commitLineKey])+ row[commitLineKey] = column++;+ } else {+ row[toKey([rowIndex-1, parent])] = column++;+ }+ }+ }+ }++ =================================================++ if (!row.containsKey(commitLine))+ row.put(commitLine, column++);+ if (column > maxColumns) {+ for (Line line: getSortedLines(row, true)) {+ if (line[0] == rowIndex-1) {+ row.put(line.toggle(), row.remove(line));+ column--;+ if (column == maxColumns)+ break;+ }+ }+ Preconditions.checkState(column == maxColumns);+ }++ List<Line> cuttedLines = new ArrayList<>();+ List<Integer> children = mapOfParentToChildren.get(rowIndex);+ if (children != null) {+ for (int child: children) {+ if (child != rowIndex-1) {+ Line line = new Line(child, rowIndex);+ Line cuttedLine = line.toggle();+ if (!lastRow.containsKey(line)) {+ if (lastRow.containsKey(cuttedLine))+ lastRow.put(line, lastRow.remove(cuttedLine));+ else+ cuttedLines.add(cuttedLine);+ }+ }+ }+ }+ if (!cuttedLines.isEmpty()) {+ // for every disappeared line, we need to make them appear again in last row+ // so that end part of the line can be drawn from last row to this row.+ // Below code find column in last row to insert these appeared lines, and+ // we want to make sure that this column can result in minimum line crossovers.+ int commitColumn = row.get(commitLine);+ int insertColumn = 0;+ for (int i=linesOfLastRow.size()-1; i>=0; i--) {+ Line lineOfLastRow = linesOfLastRow.get(i);+ if (lineOfLastRow.isCommit()) {+ boolean found = false;+ for (String parentHash: commits.get(rowIndex-1).getParentHashes()) {+ Integer parent = mapOfHashToRow.get(parentHash);+ if (parent != null) {+ Line line = new Line(rowIndex-1, parent);+ Integer lineColumn = row.get(line);+ if (lineColumn!=null && lineColumn.intValue()<commitColumn) {+ found = true;+ break;+ }+ }+ }+ if (found) {+ insertColumn = i;+ break;+ }+ } else {+ Integer lineColumn = row.get(lineOfLastRow);+ if (lineColumn!=null && lineColumn.intValue()<commitColumn) {+ insertColumn = i;+ break;+ }+ }+ }+ column = insertColumn+1;+ for (Line cuttedLine: cuttedLines)+ lastRow.put(cuttedLine, column++);+ for (int i=insertColumn+1; i<linesOfLastRow.size(); i++)+ lastRow.put(linesOfLastRow.get(i), column++);+ }+ }+ rows.add(row);+ }}+}\ No newline at end of file