]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Allow checking out branches in `arc branch`
authorvrana <jakubv@fb.com>
Wed, 12 Dec 2012 22:01:26 +0000 (14:01 -0800)
committervrana <jakubv@fb.com>
Wed, 19 Dec 2012 00:13:22 +0000 (16:13 -0800)
Summary:
The main added value is loading the branch name from revision.
Sometimes I know the revision ID but I don't know the branch name.

The missing piece is the starting point of the branch.
I was thinking about using `arc.land.onto.default` but we also need to get 'origin'.

This is also the last step of a simple workflow where underlying VCS is not abstracted away.
In future, we can implement this for other APIs.

Test Plan:
  $ arc branch new_branch
  $ arc branch new_branch
  $ arc branch D4168

Reviewers: epriestley

Reviewed By: epriestley

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

src/workflow/ArcanistBranchWorkflow.php

index da389e40e3cdc502851d13d8ecf9bc41676e75b0..3bb891b87f138d182cc66b94ecab385d8d0630ac 100644 (file)
@@ -16,6 +16,7 @@ final class ArcanistBranchWorkflow extends ArcanistBaseWorkflow {
   public function getCommandSynopses() {
     return phutil_console_format(<<<EOTEXT
       **branch** [__options__]
+      **branch** __name__ [__start__]
 EOTEXT
       );
   }
@@ -31,6 +32,10 @@ EOTEXT
 
           By default, branches that are "Closed" or "Abandoned" are not
           displayed. You can show them with __--view-all__.
+
+          With __name__, it creates or checks out a branch. If the branch
+          __name__ doesn't exist and is in format D123 then the branch of
+          revision D123 is checked out.
 EOTEXT
       );
   }
@@ -56,6 +61,7 @@ EOTEXT
       'by-status' => array(
         'help' => 'Sort branches by status instead of time.',
       ),
+      '*' => 'names',
     );
   }
 
@@ -66,6 +72,14 @@ EOTEXT
         'arc branch is only supported under git.');
     }
 
+    $names = $this->getArgument('names');
+    if ($names) {
+      if (count($names) > 2) {
+        throw new ArcanistUsageException("Specify only one branch.");
+      }
+      return $this->checkoutBranch($names);
+    }
+
     $branches = $repository_api->getAllBranches();
     if (!$branches) {
       throw new ArcanistUsageException('No branches in this working copy.');
@@ -80,6 +94,45 @@ EOTEXT
     return 0;
   }
 
+  private function checkoutBranch(array $names) {
+    $api = $this->getRepositoryAPI();
+
+    list($err, $stdout, $stderr) = $api->execManualLocal(
+      'checkout %s',
+      reset($names));
+
+    if ($err) {
+      $match = null;
+      if (preg_match('/^D(\d+)$/', reset($names), $match)) {
+        try {
+          $diff = $this->getConduit()->callMethodSynchronous(
+            'differential.getdiff',
+            array(
+              'revision_id' => $match[1],
+            ));
+
+          if ($diff['branch'] != '') {
+            $names[0] = $diff['branch'];
+            list($err, $stdout, $stderr) = $api->execManualLocal(
+              'checkout %s',
+              reset($names));
+          }
+        } catch (ConduitException $ex) {
+        }
+      }
+    }
+
+    if ($err) {
+      list($err, $stdout, $stderr) = $api->execManualLocal(
+        'checkout -b %Ls',
+        $names);
+    }
+
+    echo $stdout;
+    fprintf(STDERR, $stderr);
+    return $err;
+  }
+
   private function loadCommitInfo(array $branches) {
     $repository_api = $this->getRepositoryAPI();