changeset 706:c27e501ed0c3

use own command for hg subrepos
author eugene.petrenko@jetbrains.com
date Wed, 08 Jan 2014 18:32:42 +0100
parents d113beb9e519
children 12ffac409559
files .idea/artifacts/mercurial_common_jar.xml .idea/compiler.xml mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/LoadSubstatesCommand.java mercurial-common/src/python/load-substates-command.py mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialMountPointsSupport.java mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/ServerHgRepo.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialMountPointsSupportTest.java
diffstat 7 files changed, 98 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/.idea/artifacts/mercurial_common_jar.xml	Wed Jan 08 18:31:21 2014 +0100
+++ b/.idea/artifacts/mercurial_common_jar.xml	Wed Jan 08 18:32:42 2014 +0100
@@ -3,6 +3,9 @@
     <output-path>$PROJECT_DIR$/out/artifacts/mercurial_common_jar</output-path>
     <root id="archive" name="mercurial-common.jar">
       <element id="module-output" name="mercurial-common" />
+      <element id="directory" name="python">
+        <element id="dir-copy" path="$PROJECT_DIR$/mercurial-common/src/python" />
+      </element>
     </root>
   </artifact>
 </component>
\ No newline at end of file
--- a/.idea/compiler.xml	Wed Jan 08 18:31:21 2014 +0100
+++ b/.idea/compiler.xml	Wed Jan 08 18:32:42 2014 +0100
@@ -20,6 +20,7 @@
       <entry name="?*.tag" />
       <entry name="?*.template" />
       <entry name="do-not-load-in-vcs-mode" />
+      <entry name="?*.py" />
     </wildcardResourcePatterns>
     <annotationProcessing>
       <profile default="true" name="Default" enabled="false">
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/LoadSubstatesCommand.java	Wed Jan 08 18:31:21 2014 +0100
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/LoadSubstatesCommand.java	Wed Jan 08 18:32:42 2014 +0100
@@ -1,14 +1,13 @@
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
 import com.intellij.openapi.diagnostic.Logger;
-import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.diff.*;
-import jetbrains.buildServer.util.StringUtil;
+import jetbrains.buildServer.buildTriggers.vcs.mercurial.HgFileUtil;
+import jetbrains.buildServer.util.FileUtil;
 import jetbrains.buildServer.vcs.VcsException;
 import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
 
 import java.io.File;
-import java.util.*;
+import java.io.IOException;
 
 /**
  * Created 03.01.14 14:53
@@ -26,58 +25,56 @@
   }
 
   @NotNull
-  private Set<String> cat(@NotNull final String commit,
-                          @NotNull final String file) throws VcsException {
+  private File createTmpDir() throws VcsException {
+    try {
+      return HgFileUtil.createTempDir();
+    } catch (IOException e) {
+      throw new VcsException("Unable to create temporary directory", e);
+    }
+  }
+
+  @NotNull
+  private File extractCommandPy(@NotNull final File root) throws VcsException {
+    try {
+      final File py = new File(root, "load-substates-command.py");
+
+      FileUtil.copyResource(getClass(), "/python/load-substates-command.py", py);
+
+      if (py.length() < 100) throw new IOException("Failed to unpack command resource");
+      return py;
+    } catch (IOException e) {
+      throw new VcsException("Failed to extract .py file: " + e.getMessage(), e);
+    }
+  }
+
+  public void call(@NotNull final Object consumer) throws VcsException {
+    final File root = createTmpDir();
+
+    try {
+      final File py = extractCommandPy(root);
+
+      callImpl(root, py, consumer);
+    } finally {
+      FileUtil.delete(root);
+    }
+  }
+
+  private void callImpl(@NotNull final File root,
+                        @NotNull final File commandPy,
+                        @NotNull final Object consumer) throws VcsException {
     final MercurialCommandLine cli = createCommandLine();
-    cli.addParameter("cat");
-    cli.addParameter("--rev");
-    cli.addParameter(commit);
-    cli.addParameter(file);
+    cli.addParameter("--debug");
+    cli.addParameter("--config");
+    cli.addParameter("extensions.logextcj=" + commandPy);
+    cli.addParameter("load-substates-command");
+    cli.addParameter(new File(root, "output.txt").getPath());
 
     final CommandResult res = runCommand(cli);
-    return new LinkedHashSet<String>(Arrays.asList(StringUtil.splitByLines(res.getStdout())));
-  }
-
-  public void call(@NotNull final ContentProcessor consumer) throws VcsException {
-    final MercurialCommandLine cli = createCommandLine();
-    cli.addParameter("log");
-    cli.addParameter("--template=" + DiffParser.COMMITS_SEPARATOR + " {node|short} {p1node|short} {p2node|short}\\n");
-    cli.addParameter("--patch");
-    cli.addParameter(".hgsubstate");
-    cli.addParameter(".hgsub");
-
-    CommandResult res = runCommand(cli);
     final String output = res.getStdout();
 
-    //TODO: reverse patches stream so to minimize used memory for changes graph
+    if (!output.contains("##Completed##")) throw new VcsException("Command failed: " + output);
 
-    final DiffTree diff = new DiffTree() {
-      @NotNull
-      @Override
-      protected DiffFileTree createDiffFileTree(@NotNull final String file) {
-        return new DiffFileTree() {
-          @Override
-          protected void fetchContents(@NotNull List<String> commits, @NotNull Map<String, Set<String>> commitToLines) throws VcsException {
-            for (String commit : commits) {
-              LOG.debug("Fetching content for " + file + " @ " + commit + "...");
-              commitToLines.put(commit, cat(commit, file));
-            }
-          }
-        };
-      }
-    };
-    DiffParser.parse(lines(output), diff.processor());
-    diff.processDiffs(consumer);
+
   }
 
-  private LinesIterator lines(@NotNull final String output) {
-    return new LinesIterator() {
-      final StringTokenizer st = new StringTokenizer(output, "\r\n");
-      @Nullable
-      public String nextLine() {
-        if (st.hasMoreTokens()) return st.nextToken();
-        return null;
-      }
-    };
-  }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial-common/src/python/load-substates-command.py	Wed Jan 08 18:32:42 2014 +0100
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+'''
+load-substates-command
+
+Copyright 2014 JetBrains <eugene.petrenko@jetbrains.com>
+Mercurial extension commands for JetBrains' Mercurial integration
+'''
+
+import base64
+from mercurial import util, node, scmutil, subrepo
+from mercurial.i18n import _
+from os import path, makedirs
+
+
+def load_substates_command(ui, repo, outputFile, **opts):
+    """Tons of docs"""
+
+    ui.write("Searching for mappings...\n")
+
+    with open(outputFile, "w", 5 * 1024 * 1024) as result:
+      result.write("format: prefix commitID base64(.hgsub) base64(.hgsubstate) \n")
+      result.flush()
+
+      for r in repo.changelog:
+          ctx = repo[r]
+
+          if '.hgsubstate' in ctx and '.hgsub' in ctx:
+               commitId = node.hex(ctx.node())
+               result.write("$$@@@@ " + commitId + " " + base64.b64encode(ctx['.hgsub'].data()) + " " + base64.b64encode(ctx['.hgsubstate'].data()) + "\n")
+
+
+    ui.write("\n##Completed##\n")
+
+
+#so here goes command registration and options
+cmdtable = {
+    "load-substates-command": (load_substates_command, [ ], " [options] OUTPUT_FILE")
+}
+
+testedwith = '2.2.2'
+buglink = "@jonnyzzz"
+
--- a/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialMountPointsSupport.java	Wed Jan 08 18:31:21 2014 +0100
+++ b/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialMountPointsSupport.java	Wed Jan 08 18:32:42 2014 +0100
@@ -1,14 +1,11 @@
 package jetbrains.buildServer.buildTriggers.vcs.mercurial;
 
 import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.HgVcsRoot;
-import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.diff.ContentProcessor;
 import jetbrains.buildServer.vcs.CheckoutRules;
 import jetbrains.buildServer.vcs.VcsException;
 import jetbrains.buildServer.vcs.VcsRoot;
 import org.jetbrains.annotations.NotNull;
 
-import java.util.Collection;
-
 /**
  * Created 03.01.14 13:57
  *
@@ -33,18 +30,7 @@
     final HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
     final ServerHgRepo repo = mySupport.createRepo(hgRoot);
 
-    mySupport.syncRepository(hgRoot);
-
-    repo.loadSubstates(new ContentProcessor() {
-      public void processContent(@NotNull String commit, @NotNull String file, @NotNull Collection<String> lines) {
-        System.out.println(commit + "  " + file + "\n");
-        for (String line : lines) {
-          System.out.println("  " + line);
-        }
-        System.out.println();
-      }
-    });
-
+    repo.loadSubstates().call(consumer);
   }
 
 }
--- a/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/ServerHgRepo.java	Wed Jan 08 18:31:21 2014 +0100
+++ b/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/ServerHgRepo.java	Wed Jan 08 18:32:42 2014 +0100
@@ -2,7 +2,6 @@
 
 import com.intellij.openapi.util.Pair;
 import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.*;
-import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.diff.ContentProcessor;
 import jetbrains.buildServer.util.graph.DAG;
 import jetbrains.buildServer.util.graph.DAGs;
 import jetbrains.buildServer.vcs.ModificationData;
@@ -109,8 +108,9 @@
     return DAGs.createFromEdges(edges);
   }
 
-  public void loadSubstates(@NotNull final ContentProcessor processor) throws VcsException {
-    new LoadSubstatesCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings).call(processor);
+  @NotNull
+  public LoadSubstatesCommand loadSubstates() throws VcsException {
+    return new LoadSubstatesCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings);
   }
 
   public Map<String, SubRepo> getSubrepositories(@NotNull ModificationData m) {
--- a/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialMountPointsSupportTest.java	Wed Jan 08 18:31:21 2014 +0100
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialMountPointsSupportTest.java	Wed Jan 08 18:32:42 2014 +0100
@@ -61,9 +61,8 @@
     myRoot = vcsRoot().withUrl(myRemoteRepo1.getAbsolutePath()).withSubrepoChanges(true).build();
   }
 
-  @Test(enabled = false)
   public void should_return_commits_for_every_revision_in_state_local(HgVersion _) throws Exception {
-    VcsRoot root = vcsRoot().withUrl("F:\\Work\\ReSharper\\Psi.Features").build();
+    VcsRoot root = vcsRoot().withLocalRepository(new File("F:\\Work\\ReSharper")).build();
     myVcs.syncRepository(root);
 
     final long start = System.currentTimeMillis();