From 2871d00f9606d412376ba836e8a12afc29f240cc Mon Sep 17 00:00:00 2001 From: vrana Date: Wed, 12 Dec 2012 14:01:26 -0800 Subject: [PATCH] Allow checking out branches in `arc branch` 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 | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/workflow/ArcanistBranchWorkflow.php b/src/workflow/ArcanistBranchWorkflow.php index da389e40..3bb891b8 100644 --- a/src/workflow/ArcanistBranchWorkflow.php +++ b/src/workflow/ArcanistBranchWorkflow.php @@ -16,6 +16,7 @@ final class ArcanistBranchWorkflow extends ArcanistBaseWorkflow { public function getCommandSynopses() { return phutil_console_format(<< 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(); -- 2.43.0