Skip to content

The Unified and Autonomous Development Platform

Make permanent link of selection point to a specific commit instead of branch to avoid moving target issue.

robin shine66fcb5ae8ca1
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/depotfile/blobview/BlobViewContext.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/depotfile/blobview/BlobViewContext.java
index 30c562d5a0..c256dd2c2a 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/depotfile/blobview/BlobViewContext.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/depotfile/blobview/BlobViewContext.java
@@ -6,6 +6,7 @@ import java.util.List;
import javax.annotation.Nullable;
import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit;
import com.pmease.commons.git.BlobIdent;
@@ -32,7 +33,9 @@ public interface BlobViewContext extends Serializable {
@Nullable
Mark getMark();
- void onMark(AjaxRequestTarget target, @Nullable Mark mark);
+ void onMark(AjaxRequestTarget target, ObjectId commitId, @Nullable Mark mark);
+
+ String getMarkUrl(ObjectId commitId, Mark mark);
Mode getMode();
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/depotfile/blobview/source/SourceViewPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/depotfile/blobview/source/SourceViewPanel.java
index 5b0da2e436..cfa99d1afd 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/depotfile/blobview/source/SourceViewPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/depotfile/blobview/source/SourceViewPanel.java
@@ -42,6 +42,7 @@ import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.CssResourceReference;
import org.apache.wicket.request.resource.JavaScriptResourceReference;
import org.eclipse.jgit.lib.FileMode;
+import org.eclipse.jgit.lib.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unbescape.html.HtmlEscape;
@@ -251,7 +252,8 @@ public class SourceViewPanel extends BlobViewPanel {
@Override
public void onClick(AjaxRequestTarget target) {
mark(target, context.getComment().getMark(), true);
- context.onMark(target, context.getComment().getMark());
+ context.onMark(target, ObjectId.fromString(context.getComment().getCommit()),
+ context.getComment().getMark());
target.appendJavaScript(String.format("$('#%s').blur();", getMarkupId()));
}
@@ -310,30 +312,27 @@ public class SourceViewPanel extends BlobViewPanel {
IRequestParameters params = RequestCycle.get().getRequest().getQueryParameters();
switch(params.getParameterValue("action").toString()) {
+ case "openSelectionPopup":
+ // must use commit from source view page in case we are commenting on
+ // a branch and that branch has new commits since the file is being
+ // displayed
+ ObjectId commitId = ObjectId.fromString(params.getParameterValue("param1").toString());
+ Mark mark = getMark(params, "param2", "param3", "param4", "param5");
+ String script = String.format("gitplex.sourceview.openSelectionPopup('%s', %s, '%s', %s);",
+ commitId.name(), mark.toJSON(), context.getMarkUrl(commitId, mark),
+ SecurityUtils.getAccount()!=null);
+ target.appendJavaScript(script);
+ break;
case "mark":
- int fromLine = params.getParameterValue("param1").toInt();
- int fromCh = params.getParameterValue("param2").toInt();
- int toLine = params.getParameterValue("param3").toInt();
- int toCh = params.getParameterValue("param4").toInt();
- Mark mark = new Mark();
- mark.beginLine = fromLine;
- mark.beginChar = fromCh;
- mark.endLine = toLine;
- mark.endChar = toCh;
- context.onMark(target, mark);
+ commitId = ObjectId.fromString(params.getParameterValue("param1").toString());
+ mark = getMark(params, "param2", "param3", "param4", "param5");
+ context.onMark(target, commitId, mark);
break;
case "addComment":
Preconditions.checkNotNull(SecurityUtils.getAccount());
- // must use commit from source view page in case we are commenting on
- // a branch and that branch has new commits since the file is being
- // displayed
- String commitHash = params.getParameterValue("param1").toString();
-
- fromLine = params.getParameterValue("param2").toInt();
- fromCh = params.getParameterValue("param3").toInt();
- toLine = params.getParameterValue("param4").toInt();
- toCh = params.getParameterValue("param5").toInt();
+ commitId = ObjectId.fromString(params.getParameterValue("param1").toString());
+ mark = getMark(params, "param2", "param3", "param4", "param5");
Fragment fragment = new Fragment(BODY_ID, "newCommentFrag", SourceViewPanel.this);
fragment.setOutputMarkupId(true);
@@ -383,17 +382,11 @@ public class SourceViewPanel extends BlobViewPanel {
super.onSubmit(target, form);
CodeComment comment = new CodeComment();
- comment.setCommit(commitHash);
+ comment.setCommit(commitId.name());
comment.setPath(context.getBlobIdent().path);
- comment.setCompareCommit(commitHash);
comment.setContent(input.getModelObject());
comment.setDepot(context.getDepot());
comment.setUser(SecurityUtils.getAccount());
- Mark mark = new Mark();
- mark.beginLine = fromLine;
- mark.beginChar = fromCh;
- mark.endLine = toLine;
- mark.endChar = toCh;
comment.setMark(mark);
GitPlex.getInstance(CodeCommentManager.class).persist(comment);
@@ -454,7 +447,7 @@ public class SourceViewPanel extends BlobViewPanel {
commentContainer.replace(commentPanel);
commentContainer.setVisible(true);
target.add(commentContainer);
- String script = String.format("gitplex.sourceview.onOpenComment(%s);",
+ script = String.format("gitplex.sourceview.onOpenComment(%s);",
getJsonOfComment(commentModel.getObject()));
target.appendJavaScript(script);
context.onOpenComment(target, commentModel.getObject());
@@ -590,6 +583,20 @@ public class SourceViewPanel extends BlobViewPanel {
});
}
+ private Mark getMark(IRequestParameters params, String beginLineParam, String beginCharParam,
+ String endLineParam, String endCharParam) {
+ int beginLine = params.getParameterValue(beginLineParam).toInt();
+ int beginChar = params.getParameterValue(beginCharParam).toInt();
+ int endLine = params.getParameterValue(endLineParam).toInt();
+ int endChar = params.getParameterValue(endCharParam).toInt();
+ Mark mark = new Mark();
+ mark.beginLine = beginLine;
+ mark.beginChar = beginChar;
+ mark.endLine = endLine;
+ mark.endChar = endChar;
+ return mark;
+ }
+
private String getJsonOfComment(CodeComment comment) {
CommentInfo commentInfo = new CommentInfo();
commentInfo.id = comment.getId();
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/depotfile/blobview/source/source-view.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/depotfile/blobview/source/source-view.js
index f75cda6d7a..ee473c466c 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/depotfile/blobview/source/source-view.js
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/depotfile/blobview/source/source-view.js
@@ -1,10 +1,10 @@
gitplex.sourceview = {
init: function(fileContent, filePath, mark, symbolTooltipId,
- commitHash, blameInfos, commentInfos, commentCallback, viewState, loggedIn) {
+ commitHash, blameInfos, commentInfos, sourceCllback, viewState, loggedIn) {
var cm;
var $sourceView = $(".source-view");
- $sourceView.data("commentCallback", commentCallback);
+ $sourceView.data("sourceCllback", sourceCllback);
var $code = $sourceView.children(".code");
$sourceView.closest(".content").css("overflow", "hidden");
@@ -112,58 +112,17 @@ gitplex.sourceview = {
}, 10);
function onSelectText() {
- var from = cm.getCursor("from");
- var to = cm.getCursor("to");
- if (from.line != to.line || from.ch != to.ch) {
- var ch = (from.ch + to.ch)/2;
- var position = cm.charCoords({line:from.line, ch:ch});
- var permanentLinkCallback = function($permanentLink) {
- $permanentLink.off("click");
- var uri = new URI(window.location.href);
- uri.removeSearch("mark").addSearch("mark",
- (from.line+1) + "." + from.ch + "-" + (to.line+1) + "." + to.ch);
- $permanentLink.attr("href", uri.toString());
- $permanentLink.click(function(e) {
- e.preventDefault();
- $("#selection-popup").hide();
- pmease.commons.codemirror.clearSelection(cm);
- pmease.commons.codemirror.mark(cm, {
- beginLine: from.line,
- beginChar: from.ch,
- endLine: to.line,
- endChar: to.ch
- }, false);
- commentCallback("mark", from.line, from.ch, to.line, to.ch);
- });
- };
- var commentLinkCallback = function($commentLink) {
- $commentLink.off("click");
- if (loggedIn) {
- $commentLink.click(function() {
- if ($sourceView.find("form.dirty").length != 0
- && !confirm("There are unsaved changes, discard and continue?")) {
- return;
- }
- $("#selection-popup").hide();
- pmease.commons.codemirror.clearSelection(cm);
- pmease.commons.codemirror.mark(cm, {
- beginLine: from.line,
- beginChar: from.ch,
- endLine: to.line,
- endChar: to.ch
- }, false);
- commentCallback("addComment", commitHash, from.line, from.ch, to.line, to.ch);
- });
- } else {
- $commentLink.html("Log in to comment on selection");
- }
- };
- $("#selection-popup").data("open")(
- position, permanentLinkCallback, commentLinkCallback, $code[0]);
- } else {
- $("#selection-popup").hide();
+ if (cm.hasFocus()) {
+ var from = cm.getCursor("from");
+ var to = cm.getCursor("to");
+ if (from.line != to.line || from.ch != to.ch) {
+ sourceCllback("openSelectionPopup", commitHash, from.line, from.ch, to.line, to.ch);
+ } else {
+ $("#selection-popup").hide();
+ }
}
}
+
$code.on("mouseup", function() {
onSelectText();
});
@@ -268,7 +227,7 @@ gitplex.sourceview = {
},
addCommentGutter: function(line, commentInfos) {
var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;
- var commentCallback = $(".source-view").data("commentCallback");
+ var sourceCllback = $(".source-view").data("sourceCllback");
var $gutter = $(document.createElement("div"));
$gutter.addClass("CodeMirror-comment");
@@ -304,7 +263,7 @@ gitplex.sourceview = {
return;
}
var commentInfo = commentInfos[$(this).index()];
- commentCallback("openComment", commentInfo.id);
+ sourceCllback("openComment", commentInfo.id);
});
});
gitplex.sourceview.highlightCommentTrigger();
@@ -324,14 +283,51 @@ gitplex.sourceview = {
&& !confirm("There are unsaved changes, discard and continue?")) {
return;
}
- commentCallback("openComment", commentInfo.id);
+ sourceCllback("openComment", commentInfo.id);
});
}
cm.setGutterMarker(parseInt(line), "CodeMirror-comments", $gutter[0]);
},
+ openSelectionPopup: function(commitHash, mark, markUrl, loggedIn) {
+ var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;
+ var $sourceView = $(".source-view");
+ var sourceCllback = $sourceView.data("sourceCllback");
+ var ch = (mark.beginChar + mark.endChar)/2;
+ var position = cm.charCoords({line:mark.beginLine, ch:ch});
+ var permanentLinkCallback = function($permanentLink) {
+ $permanentLink.off("click");
+ $permanentLink.attr("href", markUrl);
+ $permanentLink.click(function(e) {
+ e.preventDefault();
+ $("#selection-popup").hide();
+ pmease.commons.codemirror.clearSelection(cm);
+ pmease.commons.codemirror.mark(cm, mark, false);
+ sourceCllback("mark", commitHash, mark.beginLine, mark.beginChar, mark.endLine, mark.endChar);
+ });
+ };
+ var commentLinkCallback = function($commentLink) {
+ $commentLink.off("click");
+ if (loggedIn) {
+ $commentLink.click(function() {
+ if ($sourceView.find("form.dirty").length != 0
+ && !confirm("There are unsaved changes, discard and continue?")) {
+ return;
+ }
+ $("#selection-popup").hide();
+ pmease.commons.codemirror.clearSelection(cm);
+ pmease.commons.codemirror.mark(cm, mark, false);
+ sourceCllback("addComment", commitHash, mark.beginLine, mark.beginChar, mark.endLine, mark.endChar);
+ });
+ } else {
+ $commentLink.html("Log in to comment on selection");
+ }
+ };
+ $("#selection-popup").data("open")(
+ position, permanentLinkCallback, commentLinkCallback, $(".source-view>.code")[0]);
+ },
onCommentAdded: function(line, commentInfo) {
var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;
- var commentCallback = $(".source-view").data("commentCallback");
+ var sourceCllback = $(".source-view").data("sourceCllback");
var lineInfo = cm.lineInfo(line);
var gutter;
if (lineInfo.gutterMarkers)
@@ -348,7 +344,7 @@ gitplex.sourceview = {
},
onCommentDeleted: function(line, commentId) {
var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;
- var commentCallback = $(".source-view").data("commentCallback");
+ var sourceCllback = $(".source-view").data("sourceCllback");
var lineInfo = cm.lineInfo(line);
var $gutter = $(lineInfo.gutterMarkers["CodeMirror-comments"]);
var commentInfos = $gutter.data("commentInfos");
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/file/DepotFilePage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/file/DepotFilePage.java
index 04c4d49209..b395b08cad 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/file/DepotFilePage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/file/DepotFilePage.java
@@ -43,6 +43,7 @@ import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import com.google.common.base.Objects;
+import com.google.common.base.Preconditions;
import com.pmease.commons.git.BlobIdent;
import com.pmease.commons.git.GitUtils;
import com.pmease.commons.git.exception.ObjectNotExistException;
@@ -822,11 +823,24 @@ public class DepotFilePage extends DepotPage implements BlobViewContext {
}
@Override
- public void onMark(AjaxRequestTarget target, Mark mark) {
+ public void onMark(AjaxRequestTarget target, ObjectId commitId, Mark mark) {
this.mark = mark;
+ if (!blobIdent.revision.equals(commitId.name())) {
+ blobIdent.revision = commitId.name();
+ newRevisionPicker(target);
+ }
pushState(target);
}
+ @Override
+ public String getMarkUrl(ObjectId commitId, Mark mark) {
+ HistoryState state = getState();
+ state.blobIdent.revision = commitId.name();
+ state.mark = mark;
+ PageParameters params = paramsOf(getDepot(), state);
+ return RequestCycle.get().urlFor(DepotFilePage.class, params).toString();
+ }
+
@Override
public Mode getMode() {
return mode;
@@ -834,6 +848,8 @@ public class DepotFilePage extends DepotPage implements BlobViewContext {
@Override
public void onSelect(AjaxRequestTarget target, BlobIdent blobIdent, @Nullable TokenPosition tokenPos) {
+ Preconditions.checkArgument(blobIdent.revision.equals(this.blobIdent.revision));
+
mark = Mark.of(tokenPos);
if (blobIdent.equals(this.blobIdent)) {
if (mark != null) {