Skip to content

The Unified and Autonomous Development Platform

Draft version of blame view.

robin shine1498012f42dc
diff --git a/commons.util/src/main/java/com/pmease/commons/util/ContentDetector.java b/commons.util/src/main/java/com/pmease/commons/util/ContentDetector.java
index b234c37b84..a7a6216445 100644
--- a/commons.util/src/main/java/com/pmease/commons/util/ContentDetector.java
+++ b/commons.util/src/main/java/com/pmease/commons/util/ContentDetector.java
@@ -50,6 +50,9 @@ public class ContentDetector {
}
public static boolean isBinary(byte[] contentBytes, @Nullable String fileName) {
+ if (contentBytes.length == 0)
+ return false;
+
MediaType mediaType = detectMediaType(contentBytes, fileName);
return !mediaType.getType().equalsIgnoreCase("text")
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/common.js b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/common.js
index 3f677f7a27..3abe1591f9 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/common.js
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/common.js
@@ -496,7 +496,7 @@ pmease.commons = {
ajaxLoadingIndicator[0].timer = setTimeout(function() {
if (!$(".ajax-indicator").is(":visible"))
ajaxLoadingIndicator.show();
- }, 5000);
+ }, 2000);
});
Wicket.Event.subscribe('/ajax/call/complete', function() {
@@ -835,7 +835,7 @@ pmease.commons = {
$(function() {
pmease.commons.setupDropdownAndModel();
-// pmease.commons.setupAjaxLoadingIndicator();
+ pmease.commons.setupAjaxLoadingIndicator();
pmease.commons.form.setupDirtyCheck();
pmease.commons.focus.setupAutoFocus();
pmease.commons.scroll.setupScrollStop();
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewContext.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewContext.java
index 457b3bc999..975c3772e4 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewContext.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewContext.java
@@ -24,7 +24,11 @@ public abstract class BlobViewContext implements Serializable {
* changed
*/
private final BlobIdent blobIdent;
-
+
+ private TokenPosition tokenPosition;
+
+ private boolean blame;
+
public BlobViewContext(BlobIdent blobIdent) {
this.blobIdent = blobIdent;
}
@@ -32,8 +36,22 @@ public abstract class BlobViewContext implements Serializable {
public abstract Repository getRepository();
@Nullable
- public abstract TokenPosition getTokenPosition();
-
+ public TokenPosition getTokenPosition() {
+ return tokenPosition;
+ }
+
+ public void setTokenPosition(@Nullable TokenPosition tokenPosition) {
+ this.tokenPosition = tokenPosition;
+ }
+
+ public boolean isBlame() {
+ return blame;
+ }
+
+ public void setBlame(boolean blame) {
+ this.blame = blame;
+ }
+
public abstract void onSelect(AjaxRequestTarget target, BlobIdent blobIdent, TokenPosition tokenPos);
public abstract void onSearchComplete(AjaxRequestTarget target, List<QueryHit> hits);
@@ -52,7 +70,7 @@ public abstract class BlobViewContext implements Serializable {
if (panel != null)
return panel;
}
-
+
throw new IllegalStateException("No applicable blob renderer found for current context.");
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.html
index a5a096c8d5..a03fdffd7e 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.html
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.html
@@ -13,7 +13,7 @@
<div class="pull-right actions">
<div class="btn-group">
<a wicket:id="raw" class="btn btn-default btn-xs"><i class="fa fa-file-o"></i> Raw</a>
- <a wicket:id="blame" class="btn btn-default btn-xs"><i class="fa fa-comments"></i> Blame</a>
+ <a wicket:id="blame" class="btn btn-default btn-xs blame-toggle"><i class="fa fa-comments"></i> Blame</a>
<a wicket:id="history" class="btn btn-default btn-xs"><i class="fa fa-history"></i> History</a>
<a wicket:id="edit" class="btn btn-default btn-xs"><i class="fa fa-pencil"></i> Edit</a>
<a wicket:id="delete" class="btn btn-default btn-xs"><i class="fa fa-trash"></i> Delete</a>
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
index 797f691761..50357532ce 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
@@ -17,6 +17,7 @@ import org.apache.wicket.request.resource.JavaScriptResourceReference;
import com.google.common.base.Preconditions;
import com.pmease.commons.wicket.assets.closestdescendant.ClosestDescendantResourceReference;
+import com.pmease.gitplex.web.component.blobview.source.SourceViewPanel;
import com.pmease.gitplex.web.resource.BlobResource;
import com.pmease.gitplex.web.resource.BlobResourceReference;
@@ -84,6 +85,38 @@ public abstract class BlobViewPanel extends Panel {
@Override
public void onClick(AjaxRequestTarget target) {
+ BlobViewPanel blobView = BlobViewPanel.this;
+
+ if (blobView instanceof SourceViewPanel) {
+ SourceViewPanel sourceView = (SourceViewPanel) blobView;
+ if (sourceView.isBlamed()) {
+ BlobViewPanel newBlobView = sourceView.getContext().render(sourceView.getId());
+ if (newBlobView instanceof SourceViewPanel) {
+ sourceView.blame(target, false);
+ target.appendJavaScript("$('.blame-toggle').removeClass('active').blur();");
+ } else {
+ blobView.replaceWith(newBlobView);
+ target.add(newBlobView);
+ target.appendJavaScript("$(window).resize();");
+ }
+ } else {
+ sourceView.blame(target, true);
+ target.appendJavaScript("$('.blame-toggle').addClass('active');");
+ }
+ } else {
+ SourceViewPanel sourceView = new SourceViewPanel(blobView.getId(), context, true);
+ blobView.replaceWith(sourceView);
+ target.add(sourceView);
+ target.appendJavaScript("$(window).resize();");
+ target.appendJavaScript("$('.blame-toggle').addClass('active');");
+ }
+ }
+
+ @Override
+ protected void onConfigure() {
+ super.onConfigure();
+
+ setVisible(context.getBlob().getText() != null);
}
});
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/SourceViewPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/SourceViewPanel.java
index ab9799e981..9fccf5f685 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/SourceViewPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/SourceViewPanel.java
@@ -34,7 +34,9 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
+import com.pmease.commons.git.Blame;
import com.pmease.commons.git.BlobIdent;
+import com.pmease.commons.git.GitUtils;
import com.pmease.commons.lang.ExtractException;
import com.pmease.commons.lang.Extractor;
import com.pmease.commons.lang.Extractors;
@@ -51,7 +53,9 @@ import com.pmease.gitplex.search.query.SymbolQuery;
import com.pmease.gitplex.search.query.TextQuery;
import com.pmease.gitplex.web.component.blobview.BlobViewContext;
import com.pmease.gitplex.web.component.blobview.BlobViewPanel;
+import com.pmease.gitplex.web.page.repository.commit.RepoCommitPage;
import com.pmease.gitplex.web.page.repository.file.SearchResultPanel;
+import com.pmease.gitplex.web.util.DateUtils;
@SuppressWarnings("serial")
public class SourceViewPanel extends BlobViewPanel {
@@ -68,10 +72,18 @@ public class SourceViewPanel extends BlobViewPanel {
private String symbol = "";
+ private boolean blamed;
+
private List<QueryHit> symbolHits = new ArrayList<>();
private final List<Symbol> symbols = new ArrayList<>();
+ public SourceViewPanel(String id, BlobViewContext context, boolean blamed) {
+ this(id, context);
+
+ this.blamed = blamed;
+ }
+
public SourceViewPanel(String id, BlobViewContext context) {
super(id, context);
@@ -258,13 +270,14 @@ public class SourceViewPanel extends BlobViewPanel {
throw new RuntimeException(e);
}
ResourceReference ajaxIndicator = new PackageResourceReference(SourceViewPanel.class, "ajax-indicator.gif");
- String script = String.format("gitplex.sourceview.init('%s', '%s', '%s', %s, '%s', %s);",
+ String script = String.format("gitplex.sourceview.init('%s', '%s', '%s', %s, '%s', %s, %s);",
codeContainer.getMarkupId(),
StringEscapeUtils.escapeEcmaScript(context.getBlob().getText().getContent()),
context.getBlobIdent().path,
highlightToken,
RequestCycle.get().urlFor(ajaxIndicator, new PageParameters()),
- getCallbackFunction(CallbackParameter.explicit("symbol")));
+ getCallbackFunction(CallbackParameter.explicit("symbol")),
+ getBlameBlocks());
response.render(OnDomReadyHeaderItem.forScript(script));
}
@@ -272,5 +285,62 @@ public class SourceViewPanel extends BlobViewPanel {
setOutputMarkupId(true);
}
+
+ private String getBlameBlocks() {
+ if (blamed) {
+ int fromLine = 0;
+ List<BlameBlock> blocks = new ArrayList<>();
+ for (Blame blame: context.getRepository().git().blame(
+ context.getBlobIdent().path, context.getBlobIdent().revision, -1, -1)) {
+ BlameBlock block = new BlameBlock();
+ block.authorDate = DateUtils.formatDate(blame.getCommit().getAuthor().getWhen());
+ block.authorName = StringEscapeUtils.escapeHtml4(blame.getCommit().getAuthor().getName());
+ block.commitHash = GitUtils.abbreviateSHA(blame.getCommit().getHash(), 7);
+ block.commitMessage = blame.getCommit().getSubject();
+ PageParameters params = RepoCommitPage.paramsOf(context.getRepository(), blame.getCommit().getHash());
+ block.commitUrl = RequestCycle.get().urlFor(RepoCommitPage.class, params).toString();
+ block.fromLine = fromLine;
+ block.toLine = fromLine + blame.getLines().size();
+ fromLine = block.toLine;
+ blocks.add(block);
+ }
+ try {
+ return GitPlex.getInstance(ObjectMapper.class).writeValueAsString(blocks);
+ } catch (JsonProcessingException e) {
+ throw new RuntimeException(e);
+ }
+ } else {
+ return "undefined";
+ }
+ }
+
+ public void blame(AjaxRequestTarget target, boolean blamed) {
+ this.blamed = blamed;
+ String script = String.format("gitplex.sourceview.blame('%s', %s);",
+ codeContainer.getMarkupId(), getBlameBlocks());
+ target.appendJavaScript(script);
+ }
+
+ public boolean isBlamed() {
+ return blamed;
+ }
+
+ @SuppressWarnings("unused")
+ private static class BlameBlock {
+
+ String commitMessage;
+
+ String commitHash;
+
+ String commitUrl;
+
+ String authorName;
+
+ String authorDate;
+
+ int fromLine;
+
+ int toLine;
+ }
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.css b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.css
index 705ab409c5..4aa4075fa4 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.css
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.css
@@ -22,3 +22,36 @@
.source-view>.outline .tree-node {
line-height: 1.6;
}
+
+.CodeMirror {
+ font-size: 13px;
+}
+.CodeMirror-gutters {
+ background: white;
+}
+.CodeMirror-annotations {
+ width: 240px;
+}
+
+.CodeMirror-annotation {
+ font-size: 12px;
+ font-family: monospace;
+ text-overflow: ellipsis;
+ overflow: hidden;
+ white-space: nowrap;
+ padding: 0 4px;
+ color: white;
+}
+.CodeMirror-annotation .hash, .CodeMirror-annotation .date {
+ margin-right: 8px;
+}
+.CodeMirror-annotation a, .CodeMirror-annotation a:hover {
+ color: #B3D5F3;
+}
+.CodeMirror-annotation.even {
+ background: #2B8D17;
+}
+.CodeMirror-annotation.odd {
+ background: #F81E1E;
+
+}
\ No newline at end of file
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.js
index 67460d2fc4..0cf86d5106 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.js
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.js
@@ -1,5 +1,5 @@
gitplex.sourceview = {
- init: function(codeId, fileContent, filePath, tokenPos, ajaxIndicatorUrl, symbolQuery) {
+ init: function(codeId, fileContent, filePath, tokenPos, ajaxIndicatorUrl, symbolQuery, blameBlocks) {
var cm;
var $code = $("#" + codeId);
@@ -90,6 +90,8 @@ gitplex.sourceview = {
if (tokenPos)
gitplex.sourceview.highlightToken(cm, tokenPos);
+ if (blameBlocks)
+ gitplex.sourceview.blame(cm, blameBlocks);
}
cm.setSize($code.width(), $code.height());
});
@@ -119,6 +121,33 @@ gitplex.sourceview = {
cm.setCursor(tokenPos.line);
}
- }
+ },
+
+ blame: function(cm, blocks) {
+ if (typeof cm === "string")
+ cm = $("#"+ cm + ">.CodeMirror")[0].CodeMirror;
+ if (blocks) {
+ cm.setOption("gutters", ["CodeMirror-annotations", "CodeMirror-linenumbers", "CodeMirror-foldgutter"]);
+ for (var i in blocks) {
+ var block = blocks[i];
+ for (var line=block.fromLine; line<block.toLine; line++) {
+ var $ele = $(document.createElement("div"));
+ $ele.addClass("CodeMirror-annotation");
+ if (i % 2 == 0)
+ $ele.addClass("even");
+ else
+ $ele.addClass("odd");
+ $("<a class='hash'>" + block.commitHash + "</a>").appendTo($ele).attr("href", block.commitUrl).attr("title", block.commitMessage);
+ $ele.append("<span class='date'>" + block.authorDate + "</span>");
+ $ele.append("<span class='author'>" + block.authorName + "</span>");
+ cm.setGutterMarker(line, "CodeMirror-annotations", $ele[0]);
+ }
+ }
+ } else {
+ cm.clearGutter("CodeMirror-annotations");
+ cm.setOption("gutters", ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]);
+ }
+ }
+
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java
index b9879df18f..eca832616b 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java
@@ -52,6 +52,7 @@ import com.pmease.gitplex.search.hit.QueryHit;
import com.pmease.gitplex.web.component.blobsearch.advanced.AdvancedSearchPanel;
import com.pmease.gitplex.web.component.blobsearch.instant.InstantSearchPanel;
import com.pmease.gitplex.web.component.blobview.BlobViewContext;
+import com.pmease.gitplex.web.component.blobview.BlobViewPanel;
import com.pmease.gitplex.web.component.blobview.source.SourceViewPanel;
import com.pmease.gitplex.web.component.filelist.FileListPanel;
import com.pmease.gitplex.web.component.filenavigator.FileNavigator;
@@ -67,7 +68,7 @@ public class RepoFilePage extends RepositoryPage {
private static final String PARAM_REVISION = "revision";
private static final String PARAM_PATH = "path";
-
+
private static final String REVISION_SELECTOR_ID = "revisionSelector";
private static final String FILE_NAVIGATOR_ID = "fileNavigator";
@@ -125,6 +126,7 @@ public class RepoFilePage extends RepositoryPage {
jgitRepo.close();
}
}
+
}
private ObjectId getCommitId() {
@@ -256,6 +258,7 @@ public class RepoFilePage extends RepositoryPage {
protected void onPopState(AjaxRequestTarget target, Serializable state) {
file = ((State) state).file;
tokenPos = ((State) state).tokenPos;
+
trait.revision = file.revision;
target.add(revisionIndexing);
@@ -334,8 +337,18 @@ public class RepoFilePage extends RepositoryPage {
if (target != null && fileViewer instanceof SourceViewPanel) {
SourceViewPanel sourceViewer = (SourceViewPanel) fileViewer;
if (sourceViewer.getContext().getBlobIdent().equals(file)) {
- if (tokenPos != null)
+ sourceViewer.getContext().setTokenPosition(tokenPos);
+ if (tokenPos != null) {
sourceViewer.highlightToken(target, tokenPos);
+ } else {
+ BlobViewPanel blobViewer = sourceViewer.getContext().render(FILE_VIEWER_ID);
+ if (!(blobViewer instanceof SourceViewPanel)) {
+ fileViewer.replaceWith(blobViewer);
+ fileViewer = blobViewer;
+ target.add(fileViewer);
+ target.appendJavaScript("$(window).resize();");
+ }
+ }
return;
}
}
@@ -356,18 +369,13 @@ public class RepoFilePage extends RepositoryPage {
};
} else {
- fileViewer = new BlobViewContext(new BlobIdent(file)) {
+ BlobViewContext context = new BlobViewContext(new BlobIdent(file)) {
@Override
public Repository getRepository() {
return RepoFilePage.this.getRepository();
}
- @Override
- public TokenPosition getTokenPosition() {
- return tokenPos;
- }
-
@Override
public void onSelect(AjaxRequestTarget target, BlobIdent blobIdent, TokenPosition tokenPos) {
RepoFilePage.this.file = new BlobIdent(file.revision, blobIdent.path,
@@ -385,7 +393,10 @@ public class RepoFilePage extends RepositoryPage {
renderSearchResult(target, hits);
}
- }.render(FILE_VIEWER_ID);
+ };
+ context.setTokenPosition(tokenPos);
+
+ fileViewer = context.render(FILE_VIEWER_ID);
}
lastCommit = new AjaxLazyLoadPanel(LAST_COMMIT_ID) {
@@ -507,6 +518,7 @@ public class RepoFilePage extends RepositoryPage {
BlobIdent file = new BlobIdent();
TokenPosition tokenPos;
+
}
public static class IndexedListener implements IndexListener {