]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Added automatic stash / unstash support for Git in arc diff.
authorJames Rhodes <jamesr@pageuppeople.com>
Thu, 21 Mar 2013 22:51:39 +0000 (15:51 -0700)
committerepriestley <git@epriestley.com>
Thu, 21 Mar 2013 23:00:05 +0000 (16:00 -0700)
Summary:
- Added arc.autostash option to have this behaviour off by
default (but configurable on a per-project basis).
- Automatic stashing of changes now informs the user of how
to restore their working directory if Arcanist unexpectedly
terminates.
- Fixed an issue with finalizeWorkingCopy when the workflow
didn't require a clean working copy.

Test Plan:
Test `arc diff` when there are changes in the working
directory; by default it should tell you to stash or commit.
Turn on the arc.autostash option and try again; it should
automatically stash with a message on how to recover, and
it should restore the working directory automatically under
almost any circumstances (other than an unrecoverable error).

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin
Differential Revision: https://secure.phabricator.com/D5385

scripts/arcanist.php
src/configuration/ArcanistSettings.php
src/repository/api/ArcanistGitAPI.php
src/repository/api/ArcanistRepositoryAPI.php
src/workflow/ArcanistBaseWorkflow.php

index d9e14cd67b2097825912868ef1e0bee0a0b94a62..6236c5a6161e67fd49d1c94901e4dcc2db272b66 100755 (executable)
@@ -268,8 +268,14 @@ try {
 
   $config->willRunWorkflow($command, $workflow);
   $workflow->willRunWorkflow();
-  $err = $workflow->run();
-  $config->didRunWorkflow($command, $workflow, $err);
+  try {
+    $err = $workflow->run();
+    $config->didRunWorkflow($command, $workflow, $err);
+  } catch (Exception $e) {
+    $workflow->finalize();
+    throw $e;
+  }
+  $workflow->finalize();
   exit((int)$err);
 
 } catch (ArcanistNoEffectException $ex) {
index e2057ed7da3b6414e0f511e468e913c68fc3538e..4867f980db5bc1024c481090f80f13c19321e8ca 100644 (file)
@@ -94,6 +94,16 @@ final class ArcanistSettings {
           "Password to use for basic auth over http transports",
         'example' => '"bobhasasecret"',
       ),
+      'arc.autostash' => array(
+        'type' => 'bool',
+        'help' =>
+          'Whether arc should permit the automatic stashing of changes in '.
+          'the working directory when requiring a clean working copy.  '.
+          'This option should only be used when users understand how '.
+          'to restore their working directory from the local stash if '.
+          'an Arcanist operation causes an unrecoverable error.',
+        'example' => 'false',
+      ),
     );
   }
 
index b6704a74cbb6e398b93a3eb883550e5f1baf9393..bcf0a87631efbe24d002d03544c5725f2039e916 100644 (file)
@@ -1000,4 +1000,17 @@ final class ArcanistGitAPI extends ArcanistRepositoryAPI {
     return null;
   }
 
+  public function canStashChanges() {
+    return true;
+  }
+
+  public function stashChanges() {
+    $this->execxLocal('stash');
+    $this->reloadWorkingCopy();
+  }
+
+  public function unstashChanges() {
+    $this->execxLocal('stash pop');
+  }
+
 }
index 1d41c67774fb94c2229dd89a7d5e0ee4c08c9504..5e42918b1cd40499186761a24584709609f698ef 100644 (file)
@@ -422,6 +422,17 @@ abstract class ArcanistRepositoryAPI {
 
   abstract protected function buildLocalFuture(array $argv);
 
+  public function canStashChanges() {
+    return false;
+  }
+
+  public function stashChanges() {
+    throw new ArcanistCapabilityNotSupportedException($this);
+  }
+
+  public function unstashChanges() {
+    throw new ArcanistCapabilityNotSupportedException($this);
+  }
 
 /* -(  Scratch Files  )------------------------------------------------------ */
 
index 5a7883f519fb9f4f9fa195f3248914ca86f8ff22..1406dba90fe1b1d80d4fa34ab2bc2a1ea72e9ce5 100644 (file)
@@ -60,6 +60,8 @@ abstract class ArcanistBaseWorkflow extends Phobject {
   private $passedArguments;
   private $command;
 
+  private $stashed;
+
   private $projectInfo;
 
   private $arcanistConfiguration;
@@ -77,6 +79,14 @@ abstract class ArcanistBaseWorkflow extends Phobject {
 
   abstract public function run();
 
+  /**
+   * Finalizes any cleanup operations that need to occur regardless of
+   * whether the command succeeded or failed.
+   */
+  public function finalize() {
+    $this->finalizeWorkingCopy();
+  }
+
   /**
    * Return the command used to invoke this workflow from the command like,
    * e.g. "help" for @{class:ArcanistHelpWorkflow}.
@@ -761,6 +771,14 @@ abstract class ArcanistBaseWorkflow extends Phobject {
     return $this;
   }
 
+  public function finalizeWorkingCopy() {
+    if ($this->stashed) {
+      $api = $this->getRepositoryAPI();
+      $api->unstashChanges();
+      echo "Restored stashed changes to the working directory.\n";
+    }
+  }
+
   public function requireCleanWorkingCopy() {
     $api = $this->getRepositoryAPI();
 
@@ -838,8 +856,18 @@ abstract class ArcanistBaseWorkflow extends Phobject {
         $api->addToCommit($unstaged);
         $must_commit += array_flip($unstaged);
       } else {
-        throw new ArcanistUsageException(
-          "Stage and commit (or revert) them before proceeding.");
+        $permit_autostash = $this->getWorkingCopy()->getConfigFromAnySource(
+          'arc.autostash',
+          false);
+        if ($permit_autostash && $api->canStashChanges()) {
+          echo "Stashing uncommitted changes. (You can restore them with ".
+               "`git stash pop`.)\n";
+          $api->stashChanges();
+          $this->stashed = true;
+        } else {
+          throw new ArcanistUsageException(
+            "Stage and commit (or revert) them before proceeding.");
+        }
       }
     }