]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Allow running `arc diff` without `git commit`
authorvrana <jakubv@fb.com>
Thu, 15 Nov 2012 20:33:36 +0000 (12:33 -0800)
committervrana <jakubv@fb.com>
Mon, 19 Nov 2012 20:06:03 +0000 (12:06 -0800)
Summary:
There's quite some logic in here:

- It automatically decides whether to create a new commit or amend.
- It partially respects 'default-relative-commit'.
- However if it points to a closed revision then it creates a new commit.

Resolves T2025.

Test Plan:
`arc diff` on:

- Clean committed repository.
- Dirty repository without commit since 'default-relative-commit'.
- Dirty repository with non-revision commit since 'default-relative-commit'.
- Dirty repository with revision commit since 'default-relative-commit'.
- `arc diff HEAD^` on dirty repository on top of closed revision.

Reviewers: epriestley, btrahan

Reviewed By: epriestley

CC: aran, Korvin
Maniphest Tasks: T2025

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

src/repository/api/ArcanistGitAPI.php
src/repository/api/ArcanistMercurialAPI.php
src/repository/api/ArcanistRepositoryAPI.php
src/repository/api/ArcanistSubversionAPI.php
src/workflow/ArcanistBaseWorkflow.php
src/workflow/ArcanistDiffWorkflow.php

index 1158f896a1d60fb8a6fd5a319da741b0dfdd5b6d..9e51c0a03b301232a2ed5af1f84e423bd0bcf8d2 100644 (file)
@@ -472,6 +472,20 @@ final class ArcanistGitAPI extends ArcanistRepositoryAPI {
     return $this->status;
   }
 
+  public function addToCommit(array $paths) {
+    $this->execxLocal(
+      'add -- %Ls',
+      $paths);
+  }
+
+  public function doCommit($message) {
+    $tmp_file = new TempFile();
+    Filesystem::writeFile($tmp_file, $message);
+    $this->execxLocal(
+      'commit --allow-empty-message -F %s',
+      $tmp_file);
+  }
+
   public function amendCommit($message) {
     $tmp_file = new TempFile();
     Filesystem::writeFile($tmp_file, $message);
index 8ddd0cd7f7fe6a42f8a71e74e22c5ff350f4b9b6..2f9aa167b034a4533ae6f60fc69914c5f0cea905 100644 (file)
@@ -594,6 +594,20 @@ final class ArcanistMercurialAPI extends ArcanistRepositoryAPI {
     $this->execxLocal('up');
   }
 
+  public function addToCommit(array $paths) {
+    $this->execxLocal(
+      'add -- %Ls',
+      $paths);
+  }
+
+  public function doCommit($message) {
+    $tmp_file = new TempFile();
+    Filesystem::writeFile($tmp_file, $message);
+    $this->execxLocal(
+      'commit -l %s',
+      $tmp_file);
+  }
+
   public function amendCommit($message) {
     $tmp_file = new TempFile();
     Filesystem::writeFile($tmp_file, $message);
index df4e899c885fba8966eeb7a1d26004c9d82651c2..cc5b27f50dd2c470da11a41f80a491db0c70c68e 100644 (file)
@@ -192,6 +192,14 @@ abstract class ArcanistRepositoryAPI {
     throw new ArcanistCapabilityNotSupportedException($this);
   }
 
+  public function addToCommit(array $paths) {
+    throw new ArcanistCapabilityNotSupportedException($this);
+  }
+
+  public function doCommit($message) {
+    throw new ArcanistCapabilityNotSupportedException($this);
+  }
+
   public function amendCommit($message) {
     throw new ArcanistCapabilityNotSupportedException($this);
   }
index b813fe41e7b362fda12011441d8a3515094888a6..16460c5ad0f710bedc5c7b722f1684eef42c1e6a 100644 (file)
@@ -171,6 +171,12 @@ final class ArcanistSubversionAPI extends ArcanistRepositoryAPI {
     }
   }
 
+  public function addToCommit(array $paths) {
+    $this->execxLocal(
+      'add -- %Ls',
+      $paths);
+  }
+
   public function getSVNProperty($path, $property) {
     list($stdout) = execx(
       'svn propget %s %s@',
index 547f1b1f1449fc1d5b789d6e71630a47dff7b2d5..4f091d8c3de3bc1e9769fa3455f4b6f86b0cf1f7 100644 (file)
  */
 abstract class ArcanistBaseWorkflow {
 
+  const COMMIT_DISABLE = 0;
+  const COMMIT_ALLOW = 1;
+  const COMMIT_ENABLE = 2;
+
+  private $commitMode = self::COMMIT_DISABLE;
+  private $shouldAmend;
+
   private $conduit;
   private $conduitURI;
   private $conduitCredentials;
@@ -51,7 +58,7 @@ abstract class ArcanistBaseWorkflow {
   private $passedArguments;
   private $command;
 
-  private $repositoryEncoding;
+  private $projectInfo;
 
   private $arcanistConfiguration;
   private $parentWorkflow;
@@ -709,9 +716,16 @@ abstract class ArcanistBaseWorkflow {
     return empty($this->arguments['allow-untracked']);
   }
 
+  public function setCommitMode($mode) {
+    $this->commitMode = $mode;
+    return $this;
+  }
+
   public function requireCleanWorkingCopy() {
     $api = $this->getRepositoryAPI();
 
+    $must_commit = array();
+
     $working_copy_desc = phutil_console_format(
       "  Working copy: __%s__\n\n",
       $api->getPath());
@@ -740,10 +754,16 @@ abstract class ArcanistBaseWorkflow {
             "may have forgotten to 'hg add' them to your commit.");
         }
 
-        $prompt = "Do you want to continue without adding these files?";
-        if (!phutil_console_confirm($prompt, $default_no = false)) {
-          throw new ArcanistUserAbortException();
+        if ($this->askForAdd()) {
+          $api->addToCommit($untracked);
+          $must_commit += array_flip($untracked);
+        } else if ($this->commitMode == self::COMMIT_DISABLE) {
+          $prompt = "Do you want to continue without adding these files?";
+          if (!phutil_console_confirm($prompt, $default_no = false)) {
+            throw new ArcanistUserAbortException();
+          }
         }
+
       }
     }
 
@@ -770,25 +790,120 @@ abstract class ArcanistBaseWorkflow {
 
     $unstaged = $api->getUnstagedChanges();
     if ($unstaged) {
-      throw new ArcanistUsageException(
-        "You have unstaged changes in this working copy. Stage and commit (or ".
-        "revert) them before proceeding.\n\n".
+      echo "You have unstaged changes in this working copy.\n\n".
         $working_copy_desc.
         "  Unstaged changes in working copy:\n".
-        "    ".implode("\n    ", $unstaged)."\n");
+        "    ".implode("\n    ", $unstaged)."\n\n";
+      if ($this->askForAdd()) {
+        $api->addToCommit($unstaged);
+        $must_commit += array_flip($unstaged);
+      } else {
+        throw new ArcanistUsageException(
+          "Stage and commit (or revert) them before proceeding.");
+      }
     }
 
     $uncommitted = $api->getUncommittedChanges();
+    foreach ($uncommitted as $key => $path) {
+      if (array_key_exists($path, $must_commit)) {
+        unset($uncommitted[$key]);
+      }
+    }
     if ($uncommitted) {
-      throw new ArcanistUncommittedChangesException(
-        "You have uncommitted changes in this working copy. Commit (or ".
-        "revert) them before proceeding.\n\n".
+      echo "You have uncommitted changes in this working copy.\n\n".
         $working_copy_desc.
-        "  Uncommitted changes in working copy\n".
-        "    ".implode("\n    ", $uncommitted)."\n");
+        "  Uncommitted changes in working copy:\n".
+        "    ".implode("\n    ", $uncommitted)."\n\n";
+      if ($this->askForAdd()) {
+        $must_commit += array_flip($uncommitted);
+      } else {
+        throw new ArcanistUncommittedChangesException(
+          "Commit (or revert) them before proceeding.");
+      }
+    }
+
+    if ($must_commit) {
+      if ($this->shouldAmend) {
+        $commit = head($api->getLocalCommitInformation());
+        $api->amendCommit($commit['message']);
+      } else if ($api->supportsRelativeLocalCommits()) {
+        $api->doCommit('');
+      }
     }
   }
 
+  private function shouldAmend() {
+    $api = $this->getRepositoryAPI();
+
+    if ($this->isHistoryImmutable() || !$api->supportsAmend()) {
+      return false;
+    }
+
+    $commits = $api->getLocalCommitInformation();
+    if (!$commits) {
+      return false;
+    }
+    $commit = reset($commits);
+
+    $message = ArcanistDifferentialCommitMessage::newFromRawCorpus(
+      $commit['message']);
+    if ($message->getGitSVNBaseRevision()) {
+      return false;
+    }
+
+    // TODO: Check last commit's author. If not me then return false.
+    // TODO: Check commits since tracking branch. If empty then return false.
+
+    $repository_phid = idx($this->getProjectInfo(), 'repositoryPHID');
+    if ($repository_phid) {
+      $repositories = $this->getConduit()->callMethodSynchronous(
+        'repository.query',
+        array());
+      $callsigns = ipull($repositories, 'callsign', 'phid');
+      $callsign = idx($callsigns, $repository_phid);
+      if ($callsign) {
+        $known_commits = $this->getConduit()->callMethodSynchronous(
+          'diffusion.getcommits',
+          array('commits' => array('r'.$callsign.$commit['commit'])));
+        if ($known_commits) {
+          return false;
+        }
+      }
+    }
+
+    if (!$message->getRevisionID()) {
+      return true;
+    }
+
+    $in_working_copy = $api->loadWorkingCopyDifferentialRevisions(
+      $this->getConduit(),
+      array(
+        'authors' => array($this->getUserPHID()),
+        'status' => 'status-open',
+      ));
+    if ($in_working_copy) {
+      return true;
+    }
+
+    return false;
+  }
+
+  private function askForAdd() {
+    if ($this->commitMode == self::COMMIT_DISABLE) {
+      return false;
+    } else if ($this->commitMode == self::COMMIT_ENABLE) {
+      return true;
+    }
+    if ($this->shouldAmend === null) {
+      $this->shouldAmend = $this->shouldAmend();
+    }
+    if ($this->shouldAmend) {
+      $prompt = "Do you want to amend these files to the commit?";
+    } else {
+      $prompt = "Do you want to add these files to the commit?";
+    }
+    return phutil_console_confirm($prompt);
+  }
 
   protected function loadDiffBundleFromConduit(
     ConduitClient $conduit,
@@ -1276,26 +1391,25 @@ abstract class ArcanistBaseWorkflow {
   }
 
   protected function getRepositoryEncoding() {
-    if ($this->repositoryEncoding) {
-      return $this->repositoryEncoding;
-    }
-
     $default = 'UTF-8';
+    return nonempty(idx($this->getProjectInfo(), 'encoding'), $default);
+  }
 
-    $project_id = $this->getWorkingCopy()->getProjectID();
-    if (!$project_id) {
-      return $default;
+  protected function getProjectInfo() {
+    if ($this->projectInfo === null) {
+      $project_id = $this->getWorkingCopy()->getProjectID();
+      if (!$project_id) {
+        $this->projectInfo = array();
+      } else {
+        $this->projectInfo = $this->getConduit()->callMethodSynchronous(
+          'arcanist.projectinfo',
+          array(
+            'name' => $project_id,
+          ));
+      }
     }
 
-    $project_info = $this->getConduit()->callMethodSynchronous(
-      'arcanist.projectinfo',
-      array(
-        'name' => $project_id,
-      ));
-
-    $this->repositoryEncoding = nonempty($project_info['encoding'], $default);
-
-    return $this->repositoryEncoding;
+    return $this->projectInfo;
   }
 
   protected function newInteractiveEditor($text) {
index 43c049e557360f3fe2003981632ec18ed48e993c..881843da6e204f431ab1f48f4982df68286ea161 100644 (file)
@@ -267,12 +267,17 @@ EOTEXT
           'lint' => true,
         ),
       ),
+      'add-all' => array(
+        'help' =>
+          'Automatically add all untracked, unstaged and uncommitted files to '.
+          'the commit.',
+      ),
       'json' => array(
         'help' =>
           'Emit machine-readable JSON. EXPERIMENTAL! Probably does not work!',
       ),
       'no-amend' => array(
-        'help' => 'Never amend commits in the working copy.',
+        'help' => 'Never amend commits in the working copy with lint patches.',
       ),
       'uncommitted' => array(
         'help' => 'Suppress warning about uncommitted changes.',
@@ -582,6 +587,13 @@ EOTEXT
 
     if ($this->requiresWorkingCopy()) {
       try {
+        if ($this->getArgument('add-all')) {
+          $this->setCommitMode(self::COMMIT_ENABLE);
+        } else if ($this->getArgument('uncommitted')) {
+          $this->setCommitMode(self::COMMIT_DISABLE);
+        } else {
+          $this->setCommitMode(self::COMMIT_ALLOW);
+        }
         $this->requireCleanWorkingCopy();
       } catch (ArcanistUncommittedChangesException $ex) {
         $repository_api = $this->getRepositoryAPI();