]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add an "--auto" flag to arc diff to activate heuristics
authorepriestley <git@epriestley.com>
Mon, 20 Feb 2012 20:51:01 +0000 (12:51 -0800)
committerepriestley <git@epriestley.com>
Mon, 20 Feb 2012 20:51:01 +0000 (12:51 -0800)
Summary:
For some workflows (all Mercurial, all SVN, some git) I eventually want arc to
pick between "arc --create" and "arc --update" automatically.

"arc which" laid the groundwork for this. For now, implement it as an optional
flag because I think there are still some rough edges on this pathway (for
example, handling of commit messages when errors happen).

When 'arc diff --auto' is invoked, guess whether the user meant --create or
--update.

Test Plan: Created this revision with 'arc diff --auto'.

Reviewers: Makinde, btrahan

Reviewed By: btrahan

CC: aran, epriestley
Maniphest Tasks: T614

Differential Revision: https://secure.phabricator.com/D1648

src/workflow/diff/ArcanistDiffWorkflow.php

index 1fd820785723e489354448dbc824a6e4e1ba5c87..86d4a2f9550bea3925d599a84d98ec4fe96f25e8 100644 (file)
@@ -165,6 +165,13 @@ EOTEXT
         'param' => 'revision_id',
         'help'  => "Always update a specific revision.",
       ),
+      'auto' => array(
+        'help' => "(Unstable!) Heuristically select --create or --update. ".
+                  "This may become the default behvaior of arc.",
+        'conflicts' => array(
+          'raw',
+        ),
+      ),
       'nounit' => array(
         'help' =>
           "Do not run unit tests.",
@@ -277,26 +284,7 @@ EOTEXT
   }
 
   public function run() {
-
-    if ($this->requiresRepositoryAPI()) {
-      $repository_api = $this->getRepositoryAPI();
-      if ($this->getArgument('less-context')) {
-        $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();
-
-    if ($this->requiresWorkingCopy()) {
-      $this->requireCleanWorkingCopy();
-    }
+    $this->runDiffSetupBasics();
 
     $paths = $this->generateAffectedPaths();
 
@@ -319,6 +307,7 @@ EOTEXT
       'unitStatus'                => $this->getUnitStatus($unit_result),
     ) + $this->buildDiffSpecification();
 
+    $conduit = $this->getConduit();
     $diff_info = $conduit->callMethodSynchronous(
       'differential.creatediff',
       $diff_spec);
@@ -333,6 +322,8 @@ EOTEXT
     $this->updateUnitDiffProperty();
     $this->updateLocalDiffProperty();
 
+    $output_json = $this->getArgument('json');
+
     if ($this->shouldOnlyCreateDiff()) {
       if (!$output_json) {
         echo phutil_console_format(
@@ -475,6 +466,26 @@ EOTEXT
     return 0;
   }
 
+  private function runDiffSetupBasics() {
+    if ($this->requiresRepositoryAPI()) {
+      $repository_api = $this->getRepositoryAPI();
+      if ($this->getArgument('less-context')) {
+        $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();
+    }
+
+    if ($this->requiresWorkingCopy()) {
+      $this->requireCleanWorkingCopy();
+    }
+  }
+
   protected function shouldOnlyCreateDiff() {
 
     if ($this->getArgument('create')) {
@@ -485,6 +496,10 @@ EOTEXT
       return false;
     }
 
+    if ($this->getArgument('auto')) {
+      return false;
+    }
+
     if ($this->getArgument('use-commit-message')) {
       return false;
     }
@@ -1096,6 +1111,7 @@ EOTEXT
   private function buildCommitMessage() {
     $is_create = $this->getArgument('create');
     $is_update = $this->getArgument('update');
+    $is_auto   = $this->getArgument('auto');
     $is_raw = $this->isRawDiffSource();
     $is_message = $this->getArgument('use-commit-message');
 
@@ -1103,6 +1119,27 @@ EOTEXT
       return $this->getCommitMessageFromCommit($is_message);
     }
 
+    if ($is_auto) {
+      $repository_api = $this->getRepositoryAPI();
+      $revisions = $repository_api->loadWorkingCopyDifferentialRevisions(
+        $this->getConduit(),
+        array(
+          'authors' => array($this->getUserPHID()),
+        ));
+      if (!$revisions) {
+        $is_create = true;
+      } else if (count($revisions) == 1) {
+        $revision = head($revisions);
+        $is_update = $revision['id'];
+      } else {
+        throw new ArcanistUsageException(
+          "There are several revisions in the specified commit range:\n\n".
+          $this->renderRevisionList($revisions)."\n".
+          "Use '--update' to choose one, or '--create' to create a new ".
+          "revision.");
+      }
+    }
+
     $message = null;
     if ($is_create) {
       $message_file = $this->getArgument('message-file');