[Mercurial] Rewrote the HgMergeProvider functionality: get revision contents directly...
[idea/community.git] / plugins / hg4idea / src / org / zmlx / hg4idea / provider / HgMergeProvider.java
index 4074cf4518b971dbe9589e36ab9d096cffa6cfa6..12c37709171edd35ea28b5567a335797a5e8cc22 100644 (file)
@@ -17,6 +17,7 @@ package org.zmlx.hg4idea.provider;
 
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.Pair;
 import com.intellij.openapi.vcs.VcsBundle;
 import com.intellij.openapi.vcs.VcsException;
 import com.intellij.openapi.vcs.merge.MergeData;
@@ -25,8 +26,12 @@ import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.vcsUtil.VcsRunnable;
 import com.intellij.vcsUtil.VcsUtil;
 import org.jetbrains.annotations.NotNull;
+import org.zmlx.hg4idea.HgContentRevision;
+import org.zmlx.hg4idea.HgFile;
+import org.zmlx.hg4idea.HgRevisionNumber;
 import org.zmlx.hg4idea.HgUtil;
 import org.zmlx.hg4idea.command.HgResolveCommand;
+import org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand;
 
 /**
  * @author Kirill Likhodedov
@@ -45,10 +50,24 @@ public class HgMergeProvider implements MergeProvider {
     final MergeData mergeData = new MergeData();
     final VcsRunnable runnable = new VcsRunnable() {
       public void run() throws VcsException {
-        final HgResolveCommand.MergeData resolveData = new HgResolveCommand(myProject).getResolveData(HgUtil.getHgRootOrThrow(myProject, file), file);
-        mergeData.ORIGINAL = resolveData.getBase();
-        mergeData.CURRENT = resolveData.getLocal();
-        mergeData.LAST = resolveData.getOther();
+        // we have a merge in progress, which means we have 2 heads (parents).
+        // the latest one is "their" revision pulled from the parent repo,
+        // the earlier parent is the local change.
+        // to retrieve the base version we get the parent of the local change, i.e. the [only] parent of the second parent.
+        final HgWorkingCopyRevisionsCommand command = new HgWorkingCopyRevisionsCommand(myProject);
+        final Pair<HgRevisionNumber, HgRevisionNumber> parents = command.parents(HgUtil.getHgRootOrThrow(myProject, file), file);
+        // we are sure that we have a grandparent, because otherwise we'll get "repository is unrelated" error while pulling,
+        // due to different root changesets which is prohibited.
+        final HgRevisionNumber grandParent = command.parents(HgUtil.getHgRootOrThrow(myProject, file), file, parents.second).first;
+
+        final HgFile hgFile = new HgFile(myProject, file);
+        final HgContentRevision server = new HgContentRevision(myProject, hgFile, parents.first);
+        final HgContentRevision local  = new HgContentRevision(myProject, hgFile, parents.second);
+        final HgContentRevision base = new HgContentRevision(myProject, hgFile, grandParent);
+
+        mergeData.ORIGINAL = base.getContentAsBytes();
+        mergeData.CURRENT = local.getContentAsBytes();
+        mergeData.LAST = server.getContentAsBytes();
       }
     };
     VcsUtil.runVcsProcessWithProgress(runnable, VcsBundle.message("multiple.file.merge.loading.progress.title"), false, myProject);