Merge "Fix comparison with change edit in preview diff screen"
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/ChangeApi.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/ChangeApi.java
index 44d1d49..4f30993 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/ChangeApi.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/ChangeApi.java
@@ -17,6 +17,8 @@
 import com.google.gerrit.plugin.client.rpc.RestApi;
 import com.google.gwt.user.client.rpc.AsyncCallback;
 
+import com.googlesource.gerrit.plugins.xdocs.client.ChangeInfo.EditInfo;
+
 import java.util.EnumSet;
 
 public class ChangeApi {
@@ -39,11 +41,23 @@
     return change(id).view(action);
   }
 
-  public static RestApi change(String id) {
+  private static RestApi change(String id) {
+    return new RestApi("/changes/").id(id);
+  }
+
+  private static RestApi change(int id) {
     return new RestApi("/changes/").id(id);
   }
 
   public static void addOptions(RestApi call, EnumSet<ListChangesOption> s) {
     call.addParameterRaw("O", Integer.toHexString(ListChangesOption.toBits(s)));
   }
+
+  public static void edit(int id, AsyncCallback<EditInfo> cb) {
+    edit(id).get(cb);
+  }
+
+  private static RestApi edit(int id) {
+    return change(id).view("edit");
+  }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/ChangeInfo.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/ChangeInfo.java
index d02913e..3583461 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/ChangeInfo.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/ChangeInfo.java
@@ -32,10 +32,24 @@
   }
 
   public static class RevisionInfo extends JavaScriptObject {
+    public static RevisionInfo fromEdit(EditInfo edit) {
+      RevisionInfo revisionInfo = createObject().cast();
+      revisionInfo.takeFromEdit(edit);
+      return revisionInfo;
+    }
+
+    private final native void takeFromEdit(EditInfo edit) /*-{
+      this._number = 0;
+      this.name = edit.name;
+      this.commit = edit.commit;
+      this.edit_base = edit.base_revision;
+    }-*/;
+
     public final native int _number() /*-{ return this._number; }-*/;
     public final native String name() /*-{ return this.name; }-*/;
     public final native String ref() /*-{ return this.ref; }-*/;
     public final native boolean is_edit() /*-{ return this._number == 0; }-*/;
+    public final native CommitInfo commit() /*-{ return this.commit; }-*/;
     public final native String edit_base() /*-{ return this.edit_base; }-*/;
 
     public static int findEditParent(JsArray<RevisionInfo> list) {
@@ -75,4 +89,11 @@
     protected RevisionInfo () {
     }
   }
+
+  public static class EditInfo extends JavaScriptObject {
+    public final native String name() /*-{ return this.name; }-*/;
+
+    protected EditInfo() {
+    }
+  }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/XDocDiffScreen.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/XDocDiffScreen.java
index 8f308cb..96555d3 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/XDocDiffScreen.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/XDocDiffScreen.java
@@ -29,6 +29,7 @@
 import com.google.gwt.user.client.ui.VerticalPanel;
 import com.google.gwt.user.client.ui.Widget;
 
+import com.googlesource.gerrit.plugins.xdocs.client.ChangeInfo.EditInfo;
 import com.googlesource.gerrit.plugins.xdocs.client.ChangeInfo.RevisionInfo;
 
 import java.util.List;
@@ -53,6 +54,28 @@
 
       @Override
       public void onSuccess(final ChangeInfo change) {
+        change.revisions().copyKeysIntoChildren("name");
+        if (Plugin.get().isSignedIn()) {
+          ChangeApi.edit(change._number(), new AsyncCallback<EditInfo>() {
+            @Override
+            public void onSuccess(EditInfo edit) {
+              if (edit != null) {
+                change.revisions().put(edit.name(), RevisionInfo.fromEdit(edit));
+              }
+              initRevisionsAndShow(change);
+            }
+
+            @Override
+            public void onFailure(Throwable caught) {
+              // never invoked
+            }
+          });
+        } else {
+          initRevisionsAndShow(change);
+        }
+      }
+
+      private void initRevisionsAndShow(final ChangeInfo change) {
         parseRevisions(change, patchSet);
         if (revisionA == null) {
           ProjectApi.getCommitInfo(change.project(), change.current_revision(),
@@ -125,6 +148,9 @@
 
   private static String getRevision(ChangeInfo change, int patchSet) {
     for (RevisionInfo rev : Natives.asList(change.revisions().values())) {
+      if (rev.is_edit()) {
+        return rev.commit().commit();
+      }
       if (rev._number() == patchSet) {
         return rev.ref();
       }