]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Improve git behavior in the zero- and one- commit case
authorepriestley <git@epriestley.com>
Wed, 30 Nov 2011 17:15:37 +0000 (09:15 -0800)
committerepriestley <git@epriestley.com>
Wed, 30 Nov 2011 19:17:37 +0000 (11:17 -0800)
Summary:
Git works completely differently for commits zero and one than for 2..N so add
more special casing to handle them. See:

  - {T206}
  - {T596}

The getCommitRange() block is also fatal land, although I wasn't able to reach
it. I'll follow up with @s on T596.

Test Plan:
  - Created a new, empty repository ("mkdir x; cd x; git init").
  - Ran "arc lint", "arc unit", "arc diff" against it with no commits (the first
two work, the third fails helpfully).
  - Made an initial commit.
  - Ran "arc lint", "arc unit", "arc diff" against it (all work correctly).

Reviewers: btrahan, jungejason, aran

Reviewed By: aran

CC: s, aran
Differential Revision: 1142

src/repository/api/git/ArcanistGitAPI.php
src/workflow/diff/ArcanistDiffWorkflow.php

index 6df4e49c60262b3e7ebd89791dffca2a5e3efaad..ee58e80f9ff023a01a869075a06658475717cea9 100644 (file)
@@ -25,6 +25,7 @@ class ArcanistGitAPI extends ArcanistRepositoryAPI {
 
   private $status;
   private $relativeCommit = null;
+  private $repositoryHasNoCommits = false;
   const SEARCH_LENGTH_FOR_PARENT_REVISIONS = 16;
 
   /**
@@ -41,17 +42,33 @@ class ArcanistGitAPI extends ArcanistRepositoryAPI {
     return 'git';
   }
 
+  public function getHasCommits() {
+    return !$this->repositoryHasNoCommits;
+  }
+
   public function setRelativeCommit($relative_commit) {
     $this->relativeCommit = $relative_commit;
     return $this;
   }
 
   public function getLocalCommitInformation() {
+    if ($this->repositoryHasNoCommits) {
+      // Zero commits.
+      throw new Exception(
+        "You can't get local commit information for a repository with no ".
+        "commits.");
+    } else if ($this->relativeCommit == self::GIT_MAGIC_ROOT_COMMIT) {
+      // One commit.
+      $against = 'HEAD';
+    } else {
+      // 2..N commits.
+      $against = $this->getRelativeCommit().'..HEAD';
+    }
+
     list($info) = execx(
-      '(cd %s && git log %s..%s --format=%s --)',
+      '(cd %s && git log %s --format=%s --)',
       $this->getPath(),
-      $this->getRelativeCommit(),
-      'HEAD',
+      $against,
       '%H%x00%T%x00%P%x00%at%x00%an%x00%s');
 
     $commits = array();
@@ -81,7 +98,14 @@ class ArcanistGitAPI extends ArcanistRepositoryAPI {
         '(cd %s; git rev-parse --verify HEAD^)',
         $this->getPath());
       if ($err) {
+        list($err) = exec_manual(
+          '(cd %s; git rev-parse --verify HEAD)',
+          $this->getPath());
+        if ($err) {
+          $this->repositoryHasNoCommits = true;
+        }
         $this->relativeCommit = self::GIT_MAGIC_ROOT_COMMIT;
+
       } else {
         $this->relativeCommit = 'HEAD^';
       }
@@ -159,11 +183,16 @@ class ArcanistGitAPI extends ArcanistRepositoryAPI {
 
   public function getGitCommitLog() {
     $relative = $this->getRelativeCommit();
-    if ($relative == self::GIT_MAGIC_ROOT_COMMIT) {
+    if ($this->repositoryHasNoCommits) {
+      // No commits yet.
+      return '';
+    } else if ($relative == self::GIT_MAGIC_ROOT_COMMIT) {
+      // First commit.
       list($stdout) = execx(
         '(cd %s; git log --format=medium HEAD)',
         $this->getPath());
     } else {
+      // 2..N commits.
       list($stdout) = execx(
         '(cd %s; git log --first-parent --format=medium %s..HEAD)',
         $this->getPath(),
@@ -221,8 +250,11 @@ class ArcanistGitAPI extends ArcanistRepositoryAPI {
 
       // Find uncommitted changes.
       $uncommitted_future = new ExecFuture(
-        "(cd %s; git diff {$options} --raw HEAD --)",
-        $this->getPath());
+        "(cd %s; git diff {$options} --raw %s --)",
+        $this->getPath(),
+        $this->repositoryHasNoCommits
+          ? self::GIT_MAGIC_ROOT_COMMIT
+          : 'HEAD');
 
       // Untracked files
       $untracked_future = new ExecFuture(
@@ -533,7 +565,7 @@ class ArcanistGitAPI extends ArcanistRepositoryAPI {
         $base);
       if ($err) {
         throw new ArcanistUsageException(
-          "Unable to parse git commit name '{$base}'.");
+          "Unable to find any git commit named '{$base}' in this repository.");
       }
     }
     $this->setRelativeCommit(trim($merge_base));
index fc573492b10c2c72c92d61b278182d77f24b7978..63ac69ec7f4b1cd59b8b02022e426f0fee4302c5 100644 (file)
@@ -678,23 +678,12 @@ EOTEXT
         $repository_api,
         $paths);
     } else if ($repository_api instanceof ArcanistGitAPI) {
-
       $diff = $repository_api->getFullGitDiff();
       if (!strlen($diff)) {
-        list($base, $tip) = $repository_api->getCommitRange();
-        if ($tip == 'HEAD') {
-          if (preg_match('/\^+HEAD/', $base)) {
-            $more = 'Did you mean HEAD^ instead of ^HEAD?';
-          } else {
-            $more = 'Did you specify the wrong relative commit?';
-          }
-        } else {
-          $more = 'Did you specify the wrong commit range?';
-        }
-        throw new ArcanistUsageException("No changes found. ({$more})");
+        throw new ArcanistUsageException(
+          "No changes found. (Did you specify the wrong commit range?)");
       }
       $changes = $parser->parseDiff($diff);
-
     } else if ($repository_api instanceof ArcanistMercurialAPI) {
       $diff = $repository_api->getFullMercurialDiff();
       $changes = $parser->parseDiff($diff);
@@ -937,6 +926,19 @@ EOTEXT
 
     $parser = new ArcanistDiffParser();
     $commit_messages = $repository_api->getGitCommitLog();
+
+    if (!strlen($commit_messages)) {
+      if (!$repository_api->getHasCommits()) {
+        throw new ArcanistUsageException(
+          "This git repository doesn't have any commits yet. You need to ".
+          "commit something before you can diff against it.");
+      } else {
+        throw new ArcanistUsageException(
+          "The commit range doesn't include any commits. (Did you diff ".
+          "against the wrong commit?)");
+      }
+    }
+
     $commit_messages = $parser->parseDiff($commit_messages);
 
     $problems = array();