]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Parse 'hg status -C' to show file move/copy sources
authorepriestley <git@epriestley.com>
Sun, 25 Sep 2011 16:59:41 +0000 (09:59 -0700)
committerepriestley <git@epriestley.com>
Mon, 26 Sep 2011 21:25:50 +0000 (14:25 -0700)
Summary: For Mercurial parsing in Diffusion, it looks like the only command we
can use to get move/copy information is "hg status -C --rev <rev>", so add a
parser for it.

Test Plan: Ran unit tests.

Reviewers: Makinde, jungejason, nh, tuomaspelkonen, aran

Reviewed By: Makinde

CC: aran, Makinde
Differential Revision: 958

src/repository/parser/mercurial/ArcanistMercurialParser.php
src/repository/parser/mercurial/__init__.php
src/repository/parser/mercurial/__tests__/ArcanistMercurialParserTestCase.php
src/repository/parser/mercurial/__tests__/data/status-moves.txt [new file with mode: 0644]

index a5831e291f2e4dedd12894e9871bec7f1da6b26e..7820768a07f2457ca99fefa12da62d8237cc1d42 100644 (file)
@@ -30,13 +30,15 @@ final class ArcanistMercurialParser {
 
 
   /**
-   * Parse the output of "hg status".
+   * Parse the output of "hg status". This provides detailed information, you
+   * can get less detailed information with @{method:parseMercurialStatus}. In
+   * particular, this will parse copy sources as per "hg status -C".
    *
    * @param string The stdout from running an "hg status" command.
-   * @return dict Map of paths to ArcanistRepositoryAPI status flags.
+   * @return dict Map of paths to status dictionaries.
    * @task parse
    */
-  public static function parseMercurialStatus($stdout) {
+  public static function parseMercurialStatusDetails($stdout) {
     $result = array();
 
     $stdout = trim($stdout);
@@ -44,16 +46,21 @@ final class ArcanistMercurialParser {
       return $result;
     }
 
+    $last_path = null;
     $lines = explode("\n", $stdout);
     foreach ($lines as $line) {
       $flags = 0;
-      list($code, $path) = explode(' ', $line, 2);
+      if ($line[1] !== ' ') {
+        throw new Exception("Unparsable Mercurial status line '{$line}'.");
+      }
+      $code = $line[0];
+      $path = substr($line, 2);
       switch ($code) {
         case 'A':
           $flags |= ArcanistRepositoryAPI::FLAG_ADDED;
           break;
         case 'R':
-          $flags |= ArcanistRepositoryAPI::FLAG_REMOVED;
+          $flags |= ArcanistRepositoryAPI::FLAG_DELETED;
           break;
         case 'M':
           $flags |= ArcanistRepositoryAPI::FLAG_MODIFIED;
@@ -71,17 +78,45 @@ final class ArcanistMercurialParser {
         case 'I':
           // This is "ignored" and included only for completeness.
           break;
+        case ' ':
+          // This shows the source of a file move, so update the last file we
+          // parsed to set its source.
+          if ($last_path === null) {
+            throw new Exception(
+              "Unexpected copy source in hg status, '{$line}'.");
+          }
+          $result[$last_path]['from'] = $path;
+          continue 2;
         default:
           throw new Exception("Unknown Mercurial status '{$code}'.");
       }
 
-      $result[$path] = $flags;
+      $result[$path] = array(
+        'flags' => $flags,
+        'from'  => null,
+      );
+      $last_path = $path;
     }
 
     return $result;
   }
 
 
+  /**
+   * Parse the output of "hg status". This provides only basic information, you
+   * can get more detailed information by invoking
+   * @{method:parseMercurialStatusDetails}.
+   *
+   * @param string The stdout from running an "hg status" command.
+   * @return dict Map of paths to ArcanistRepositoryAPI status flags.
+   * @task parse
+   */
+  public static function parseMercurialStatus($stdout) {
+    $result = self::parseMercurialStatusDetails($stdout);
+    return ipull($result, 'flags');
+  }
+
+
   /**
    * Parse the output of "hg log". This also parses "hg outgoing", "hg parents",
    * and other similar commands. This assumes "--style default".
index 9de7b75e49f923876e400187dd8b4ed613f65259..2758caadd4ccd1d298c6f5c975384f66a4b3b0e0 100644 (file)
@@ -8,5 +8,7 @@
 
 phutil_require_module('arcanist', 'repository/api/base');
 
+phutil_require_module('phutil', 'utils');
+
 
 phutil_require_source('ArcanistMercurialParser.php');
index 939efdad038cb4e0b77367041d9146ae970338ae..19a9f7b4879ecc5817f43d127de29f18d7caa4fb 100644 (file)
@@ -20,7 +20,7 @@ final class ArcanistMercurialParserTestCase extends ArcanistPhutilTestCase {
 
   public function testParseAll() {
     $root = dirname(__FILE__).'/data/';
-    foreach (Filesystem::listDirectory($root) as $file) {
+    foreach (Filesystem::listDirectory($root, $hidden = false) as $file) {
       $this->parseData(
         basename($file),
         Filesystem::readFile($root.'/'.$file));
@@ -64,6 +64,21 @@ final class ArcanistMercurialParserTestCase extends ArcanistPhutilTestCase {
           array('changed', 'added', 'removed', 'untracked'),
           array_keys($output));
         break;
+      case 'status-moves.txt':
+        $output = ArcanistMercurialParser::parseMercurialStatusDetails($data);
+        $this->assertEqual(
+          'move_source',
+          $output['moved_file']['from']);
+        $this->assertEqual(
+          null,
+          $output['changed_file']['from']);
+        $this->assertEqual(
+          'copy_source',
+          $output['copied_file']['from']);
+        $this->assertEqual(
+          null,
+          idx($output, 'copy_source'));
+        break;
       default:
         throw new Exception("No test information for test data '{$name}'!");
     }
diff --git a/src/repository/parser/mercurial/__tests__/data/status-moves.txt b/src/repository/parser/mercurial/__tests__/data/status-moves.txt
new file mode 100644 (file)
index 0000000..aa69b51
--- /dev/null
@@ -0,0 +1,6 @@
+M changed_file
+A moved_file
+  move_source
+R moved_source
+A copied_file
+  copy_source