]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Provide a "--json" flag to 'arc diff'
authorepriestley <git@epriestley.com>
Thu, 23 Jun 2011 21:37:43 +0000 (14:37 -0700)
committerepriestley <git@epriestley.com>
Thu, 23 Jun 2011 23:03:37 +0000 (16:03 -0700)
Summary:
This should be implemented more elegantly, but this is a mostly-reasonable
attempt at it.

Test Plan:
Ran 'arc diff --only --json' with this diff.

Reviewed By: gc3
Reviewers: gc3
CC: aran, epriestley, gc3
Differential Revision: 509

src/workflow/diff/ArcanistDiffWorkflow.php

index 8d2a8418f69c8f70090e73e69a7bc2c6772fb4de..e80fb0d5d8c83c3fcac14ccd858ff37e6e2ec495 100644 (file)
@@ -184,6 +184,10 @@ EOTEXT
           'lint' => true,
         ),
       ),
+      'json' => array(
+        'help' =>
+          'Emit machine-readable JSON. EXPERIMENTAL! Probably does not work!',
+      ),
       '*' => 'paths',
     );
   }
@@ -195,6 +199,13 @@ EOTEXT
       $repository_api->setDiffLinesOfContext(3);
     }
 
+    $output_json = $this->getArgument('json');
+    if ($output_json) {
+      // TODO: We should move this to a higher-level and put an indirection
+      // layer between echoing stuff and stdout.
+      ob_start();
+    }
+
     $conduit = $this->getConduit();
     $this->requireCleanWorkingCopy();
 
@@ -343,10 +354,20 @@ EOTEXT
     }
 
     if ($this->shouldOnlyCreateDiff()) {
-      echo phutil_console_format(
-        "Created a new Differential diff:\n".
-        "        **Diff URI:** __%s__\n\n",
-        $diff_info['uri']);
+      if (!$output_json) {
+        echo phutil_console_format(
+          "Created a new Differential diff:\n".
+          "        **Diff URI:** __%s__\n\n",
+          $diff_info['uri']);
+      } else {
+        $human = ob_get_clean();
+        echo json_encode(array(
+          'diffURI' => $diff_info['uri'],
+          'diffID'  => $diff_info['diffid'],
+          'human'   => $human,
+        ))."\n";
+        ob_start();
+      }
     } else {
       $message = $commit_message;
 
@@ -498,6 +519,10 @@ EOTEXT
 
     $this->diffID = $diff_info['diffid'];
 
+    if ($output_json) {
+      ob_get_clean();
+    }
+
     return 0;
   }
 
@@ -595,13 +620,15 @@ EOTEXT
 
       if ($bases) {
         $rev = reset($bases);
+
+        $revlist = array();
+        foreach ($bases as $path => $baserev) {
+          $revlist[] = "    Revision {$baserev}, {$path}";
+        }
+        $revlist = implode("\n", $revlist);
+
         foreach ($bases as $path => $baserev) {
           if ($baserev !== $rev) {
-            $revlist = array();
-            foreach ($bases as $path => $baserev) {
-              $revlist[] = "    Revision {$baserev}, {$path}";
-            }
-            $revlist = implode("\n", $revlist);
             throw new ArcanistUsageException(
               "Base revisions of changed paths are mismatched. Update all ".
               "paths to the same base revision before creating a diff: ".