]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add "nocommit" git-only flag to arc patch workflow
authorBob Trahan <bob.trahan@gmail.com>
Tue, 17 Jan 2012 23:47:00 +0000 (15:47 -0800)
committerBob Trahan <bob.trahan@gmail.com>
Wed, 18 Jan 2012 23:25:54 +0000 (15:25 -0800)
Summary:
without "nocommit" we commit the patch to the working copy. add the nocommit
flag and this commit does not happen.

making this happen required adding revisionID to arc bundle to fetch the proper
commit message.  if we can't get a commit message -- suppose the fetch fails or
the source is self::SOURCE_PATCH, we ask the user for the commit message on the
command line

Test Plan:
git reset --hard <SOME_REV>
arc patch DX, where this got us to <SOME_REV + 1>
observe correct patch committed with correct commit message in working copy

git reset --hard <SOME_REV>
arc patch --nocommit DX, where this got us to <SOME_REV + 1>
observe correct patch landed BUT NOT committed.  note "commit message" does not
exist since there isn't a commit...!

git diff HEAD^1 > ~/file.patch
git reset --hard HEAD^1
arc patch --patch ~/file.patch
observe prompted for commit message and patch committed with commit message i
typed in

Reviewers: epriestley

Reviewed By: epriestley

CC: aran
Maniphest Tasks: T479

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

src/parser/bundle/ArcanistBundle.php
src/workflow/base/ArcanistBaseWorkflow.php
src/workflow/export/ArcanistExportWorkflow.php
src/workflow/patch/ArcanistPatchWorkflow.php
src/workflow/patch/__init__.php

index 1599e4ebd3886a094ab7529a107ded122ee90d8a..5acc8603db1c43d43892ff70f860c43e91f55426 100644 (file)
@@ -29,6 +29,7 @@ class ArcanistBundle {
   private $diskPath;
   private $projectID;
   private $baseRevision;
+  private $revisionID;
 
   public function setConduit(ConduitClient $conduit) {
     $this->conduit = $conduit;
@@ -50,6 +51,15 @@ class ArcanistBundle {
     return $this->baseRevision;
   }
 
+  public function setRevisionID($revision_id) {
+    $this->revisionID = $revision_id;
+    return $this;
+  }
+
+  public function getRevisionID() {
+    return $this->revisionID;
+  }
+
   public static function newFromChanges(array $changes) {
     $obj = new ArcanistBundle();
     $obj->changes = $changes;
@@ -72,14 +82,16 @@ class ArcanistBundle {
           'tar xfO %s meta.json',
           $path));
       $meta_info = $future->resolveJSON();
-      $version = idx($meta_info, 'version', 0);
-      $project_name = idx($meta_info, 'projectName');
+      $version       = idx($meta_info, 'version', 0);
+      $project_name  = idx($meta_info, 'projectName');
       $base_revision = idx($meta_info, 'baseRevision');
+      $revision_id   = idx($meta_info, 'revisionID');
     // this arc bundle was probably made before we started storing meta info
     } else {
-      $version = 0;
-      $project_name = null;
+      $version       = 0;
+      $project_name  = null;
       $base_revision = null;
+      $revision_id   = null;
     }
 
     $future = new ExecFuture(
@@ -104,6 +116,7 @@ class ArcanistBundle {
     $obj->diskPath = $path;
     $obj->setProjectID($project_name);
     $obj->setBaseRevision($base_revision);
+    $obj->setRevisionID($revision_id);
 
     return $obj;
   }
@@ -151,9 +164,10 @@ class ArcanistBundle {
     }
 
     $meta_info = array(
-      'version'      => 2,
+      'version'      => 3,
       'projectName'  => $this->getProjectID(),
       'baseRevision' => $this->getBaseRevision(),
+      'revisionID'   => $this->getRevisionID(),
     );
 
     $dir = Filesystem::createTemporaryDirectory();
index 1400263a35437e92dd0c7e7c53e1928e1ab317ef..2960505d3e7250fa60a97cc05a97771bb3e160a8 100644 (file)
@@ -753,6 +753,7 @@ class ArcanistBaseWorkflow {
     $bundle->setConduit($conduit);
     $bundle->setProjectID($diff['projectName']);
     $bundle->setBaseRevision($diff['sourceControlBaseRevision']);
+    $bundle->setRevisionID($diff['revisionID']);
     return $bundle;
   }
 
index ff9b5daf690d9df2773f6d24e60dd96c7362cf65..9b1ce4e8f3e534958280016376829500e0656a6a 100644 (file)
@@ -189,6 +189,7 @@ EOTEXT
         $bundle->setProjectID($this->getWorkingCopy()->getProjectID());
         $bundle->setBaseRevision(
           $repository_api->getSourceControlBaseRevision());
+        // note we can't get a revision ID for SOURCE_LOCAL
         break;
       case self::SOURCE_REVISION:
         $bundle = $this->loadRevisionBundleFromConduit(
index 76cd249e887b26712224df149cc895af84065913..9cae0b2b23efcecfe734466adc4ebd79bd95a141 100644 (file)
@@ -38,7 +38,7 @@ final class ArcanistPatchWorkflow extends ArcanistBaseWorkflow {
       **patch** __--diff__ __diff_id__
       **patch** __--patch__ __file__
       **patch** __--arcbundle__ __bundlefile__
-          Supports: git, svn
+          Supports: git, svn, hg
           Apply the changes in a Differential revision, patchfile, or arc
           bundle to the working copy.
 EOTEXT
@@ -75,6 +75,14 @@ EOTEXT
         'help' =>
           "Apply changes from a git patchfile or unified patchfile.",
       ),
+      'nocommit' => array(
+        'supports' => array(
+          'git'
+        ),
+        'help' =>
+          "Normally under git if the patch is successful the changes are ".
+          "committed to the working copy. This flag prevents the commit.",
+      ),
       'force' => array(
         'help' =>
           "Do not run any sanity checks.",
@@ -154,6 +162,15 @@ EOTEXT
     return $this->sourceParam;
   }
 
+  private function shouldCommit() {
+    $no_commit = $this->getArgument('nocommit', false);
+    if ($no_commit) {
+      return false;
+    }
+
+    return true;
+  }
+
   public function run() {
 
     $source = $this->getSource();
@@ -404,14 +421,31 @@ EOTEXT
 
       return $patch_err;
     } else if ($repository_api instanceof ArcanistGitAPI) {
+      // if we're going to commit, we should make sure the working copy
+      // is clean
+      if ($this->shouldCommit()) {
+        $this->requireCleanWorkingCopy();
+      }
+
       $future = new ExecFuture(
         '(cd %s; git apply --index --reject)',
         $repository_api->getPath());
       $future->write($bundle->toGitPatch());
       $future->resolvex();
 
+      if ($this->shouldCommit()) {
+        $commit_message = $this->getCommitMessage($bundle);
+        $future = new ExecFuture(
+          '(cd %s; git commit -a -F -)',
+          $repository_api->getPath());
+        $future->write($commit_message);
+        $future->resolvex();
+        $verb = 'committed';
+      } else {
+        $verb = 'applied';
+      }
       echo phutil_console_format(
-        "<bg:green>** OKAY **</bg> Successfully applied patch.\n");
+        "<bg:green>** OKAY **</bg> Successfully {$verb} patch.\n");
     } else if ($repository_api instanceof ArcanistMercurialAPI) {
       $future = new ExecFuture(
         '(cd %s; hg import --no-commit -)',
@@ -428,6 +462,50 @@ EOTEXT
     return 0;
   }
 
+  private function getCommitMessage(ArcanistBundle $bundle) {
+    $revision_id    = $bundle->getRevisionID();
+    $commit_message = null;
+    $prompt_message = null;
+
+    // if we have a revision id the commit message is in differential
+    if ($revision_id) {
+      $conduit        = $this->getConduit();
+      $commit_message = $conduit->callMethodSynchronous(
+        'differential.getcommitmessage',
+        array(
+          'revision_id' => $revision_id,
+        ));
+      $prompt_message = "  Note arcanist failed to load the commit message ".
+                        "from differential for revision D{$revision_id}.";
+    }
+
+    // no revision id or failed to fetch commit message so get it from the
+    // user on the command line
+    if (!$commit_message) {
+      $template =
+        "\n\n".
+        "# Enter a commit message for this patch.  If you just want to apply ".
+        "the patch to the working copy without committing, re-run arc patch ".
+        "with the --nocommit flag.".
+        $prompt_message.
+        "\n";
+
+      $commit_message = id(new PhutilInteractiveEditor($template))
+        ->setName('arcanist-patch-commit-message')
+        ->editInteractively();
+
+      $commit_message = preg_replace('/^\s*#.*$/m',
+                                     '',
+                                     $commit_message);
+      $commit_message = rtrim($commit_message);
+      if (!strlen($commit_message)) {
+        throw new ArcanistUserAbortException();
+      }
+    }
+
+    return $commit_message;
+  }
+
   public function getShellCompletions(array $argv) {
     // TODO: Pull open diffs from 'arc list'?
     return array('ARGUMENT');
index 35015c6feed4107c78850cf233a5d1862db45141..7e2852ad73ca4adcc5be79465ace957c93709790 100644 (file)
@@ -15,6 +15,7 @@ phutil_require_module('arcanist', 'parser/diff/changetype');
 phutil_require_module('arcanist', 'workflow/base');
 
 phutil_require_module('phutil', 'console');
+phutil_require_module('phutil', 'console/editor');
 phutil_require_module('phutil', 'filesystem');
 phutil_require_module('phutil', 'filesystem/tempfile');
 phutil_require_module('phutil', 'future/exec');