From 299b9c4c6bf0939403095aeba167bfed7df70814 Mon Sep 17 00:00:00 2001 From: vrana Date: Wed, 30 Jan 2013 17:33:32 -0800 Subject: [PATCH] Support `arc bookmark` in Mercurial Summary: Branch in Mercurial means something else. Hopefully users wouldn't be too confused. Test Plan: $ arc help $ arc help branch $ arc help feature $ arc feature $ arc bookmark $ arc branch # In hg repo: $ arc feature $ arc feature new $ arc feature new Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Maniphest Tasks: T2332 Differential Revision: https://secure.phabricator.com/D4753 --- src/__phutil_library_map__.php | 6 +- src/repository/api/ArcanistMercurialAPI.php | 20 ++ src/workflow/ArcanistBookmarkWorkflow.php | 39 +++ src/workflow/ArcanistBranchWorkflow.php | 279 +--------------- src/workflow/ArcanistFeatureWorkflow.php | 337 ++++++++++++++++++++ 5 files changed, 406 insertions(+), 275 deletions(-) create mode 100644 src/workflow/ArcanistBookmarkWorkflow.php create mode 100644 src/workflow/ArcanistFeatureWorkflow.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index efa18576..0495dc65 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -21,6 +21,7 @@ phutil_register_library_map(array( 'ArcanistBaseUnitTestEngine' => 'unit/engine/ArcanistBaseUnitTestEngine.php', 'ArcanistBaseWorkflow' => 'workflow/ArcanistBaseWorkflow.php', 'ArcanistBaseXHPASTLinter' => 'lint/linter/ArcanistBaseXHPASTLinter.php', + 'ArcanistBookmarkWorkflow' => 'workflow/ArcanistBookmarkWorkflow.php', 'ArcanistBranchWorkflow' => 'workflow/ArcanistBranchWorkflow.php', 'ArcanistBrowseWorkflow' => 'workflow/ArcanistBrowseWorkflow.php', 'ArcanistBundle' => 'parser/ArcanistBundle.php', @@ -55,6 +56,7 @@ phutil_register_library_map(array( 'ArcanistDownloadWorkflow' => 'workflow/ArcanistDownloadWorkflow.php', 'ArcanistEventType' => 'events/constant/ArcanistEventType.php', 'ArcanistExportWorkflow' => 'workflow/ArcanistExportWorkflow.php', + 'ArcanistFeatureWorkflow' => 'workflow/ArcanistFeatureWorkflow.php', 'ArcanistFilenameLinter' => 'lint/linter/ArcanistFilenameLinter.php', 'ArcanistFlagWorkflow' => 'workflow/ArcanistFlagWorkflow.php', 'ArcanistFlake8Linter' => 'lint/linter/ArcanistFlake8Linter.php', @@ -175,7 +177,8 @@ phutil_register_library_map(array( 'ArcanistBaseCommitParserTestCase' => 'ArcanistTestCase', 'ArcanistBaseWorkflow' => 'Phobject', 'ArcanistBaseXHPASTLinter' => 'ArcanistLinter', - 'ArcanistBranchWorkflow' => 'ArcanistBaseWorkflow', + 'ArcanistBookmarkWorkflow' => 'ArcanistFeatureWorkflow', + 'ArcanistBranchWorkflow' => 'ArcanistFeatureWorkflow', 'ArcanistBrowseWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistBundleTestCase' => 'ArcanistTestCase', 'ArcanistCallConduitWorkflow' => 'ArcanistBaseWorkflow', @@ -198,6 +201,7 @@ phutil_register_library_map(array( 'ArcanistDownloadWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistEventType' => 'PhutilEventType', 'ArcanistExportWorkflow' => 'ArcanistBaseWorkflow', + 'ArcanistFeatureWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistFilenameLinter' => 'ArcanistLinter', 'ArcanistFlagWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistFlake8Linter' => 'ArcanistLinter', diff --git a/src/repository/api/ArcanistMercurialAPI.php b/src/repository/api/ArcanistMercurialAPI.php index fdd3dd4a..f59d4dc6 100644 --- a/src/repository/api/ArcanistMercurialAPI.php +++ b/src/repository/api/ArcanistMercurialAPI.php @@ -453,6 +453,26 @@ final class ArcanistMercurialAPI extends ArcanistRepositoryAPI { return true; } + public function getAllBranches() { + list($branch_info) = $this->execxLocal('bookmarks'); + $matches = null; + preg_match_all( + '/^\s*(\*?)\s*(.+)\s(\S+)$/m', + $branch_info, + $matches, + PREG_SET_ORDER); + + $return = array(); + foreach ($matches as $match) { + list(, $current, $name) = $match; + $return[] = array( + 'current' => (bool)$current, + 'name' => rtrim($name), + ); + } + return $return; + } + public function hasLocalCommit($commit) { try { $this->getCanonicalRevisionName($commit); diff --git a/src/workflow/ArcanistBookmarkWorkflow.php b/src/workflow/ArcanistBookmarkWorkflow.php new file mode 100644 index 00000000..edf9ef7f --- /dev/null +++ b/src/workflow/ArcanistBookmarkWorkflow.php @@ -0,0 +1,39 @@ +getRepositoryAPI(); + if (!($repository_api instanceof ArcanistMercurialAPI)) { + throw new ArcanistUsageException( + 'arc bookmark is only supported under Mercurial.'); + } + return parent::run(); + } + +} diff --git a/src/workflow/ArcanistBranchWorkflow.php b/src/workflow/ArcanistBranchWorkflow.php index 3394f14f..0ec4dc26 100644 --- a/src/workflow/ArcanistBranchWorkflow.php +++ b/src/workflow/ArcanistBranchWorkflow.php @@ -1,13 +1,11 @@ array( - 'help' => 'Include closed and abandoned revisions', - ), - 'by-status' => array( - 'help' => 'Sort branches by status instead of time.', - ), - '*' => 'names', - ); - } - public function run() { $repository_api = $this->getRepositoryAPI(); if (!($repository_api instanceof ArcanistGitAPI)) { throw new ArcanistUsageException( - '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.'); - } - - $branches = $this->loadCommitInfo($branches); - - $revisions = $this->loadRevisions($branches); - - $this->printBranches($branches, $revisions); - - 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(); - - $futures = array(); - foreach ($branches as $branch) { - // NOTE: "-s" is an option deep in git's diff argument parser that doesn't - // seem to have much documentation and has no long form. It suppresses any - // diff output. - $futures[$branch['name']] = $repository_api->execFutureLocal( - 'show -s --format=%C %s --', - '%H%x01%ct%x01%T%x01%s%x01%b', - $branch['name']); - } - - $branches = ipull($branches, null, 'name'); - - foreach (Futures($futures)->limit(16) as $name => $future) { - list($info) = $future->resolvex(); - list($hash, $epoch, $tree, $desc, $text) = explode("\1", trim($info), 5); - - $branch = $branches[$name]; - $branch['hash'] = $hash; - $branch['desc'] = $desc; - - try { - $text = $desc."\n".$text; - $message = ArcanistDifferentialCommitMessage::newFromRawCorpus($text); - $id = $message->getRevisionID(); - - $branch += array( - 'epoch' => (int)$epoch, - 'tree' => $tree, - 'revisionID' => $id, - ); - } catch (ArcanistUsageException $ex) { - // In case of invalid commit message which fails the parsing, - // do nothing. - } - - $branches[$name] = $branch; - } - - return $branches; - } - - private function loadRevisions(array $branches) { - $ids = array(); - $hashes = array(); - - foreach ($branches as $branch) { - if ($branch['revisionID']) { - $ids[] = $branch['revisionID']; - } - $hashes[] = array('gtcm', $branch['hash']); - $hashes[] = array('gttr', $branch['tree']); - } - - $calls = array(); - - if ($ids) { - $calls[] = $this->getConduit()->callMethod( - 'differential.query', - array( - 'ids' => $ids, - )); - } - - if ($hashes) { - $calls[] = $this->getConduit()->callMethod( - 'differential.query', - array( - 'commitHashes' => $hashes, - )); - } - - $results = array(); - foreach (Futures($calls) as $call) { - $results[] = $call->resolve(); - } - - return array_mergev($results); - } - - private function printBranches(array $branches, array $revisions) { - $revisions = ipull($revisions, null, 'id'); - - static $color_map = array( - 'Closed' => 'cyan', - 'Needs Review' => 'magenta', - 'Needs Revision' => 'red', - 'Accepted' => 'green', - 'No Revision' => 'blue', - 'Abandoned' => 'default', - ); - - static $ssort_map = array( - 'Closed' => 1, - 'No Revision' => 2, - 'Needs Review' => 3, - 'Needs Revision' => 4, - 'Accepted' => 5, - ); - - $out = array(); - foreach ($branches as $branch) { - $revision = idx($revisions, idx($branch, 'revisionID')); - - // If we haven't identified a revision by ID, try to identify it by hash. - if (!$revision) { - foreach ($revisions as $rev) { - $hashes = idx($rev, 'hashes', array()); - foreach ($hashes as $hash) { - if (($hash[0] == 'gtcm' && $hash[1] == $branch['hash']) || - ($hash[0] == 'gttr' && $hash[1] == $branch['tree'])) { - $revision = $rev; - break; - } - } - } - } - - if ($revision) { - $desc = 'D'.$revision['id'].': '.$revision['title']; - $status = $revision['statusName']; - } else { - $desc = $branch['desc']; - $status = 'No Revision'; - } - - if (!$this->getArgument('view-all') && !$branch['current']) { - if ($status == 'Closed' || $status == 'Abandoned') { - continue; - } - } - - $epoch = $branch['epoch']; - - $color = idx($color_map, $status, 'default'); - $ssort = sprintf('%d%012d', idx($ssort_map, $status, 0), $epoch); - - $out[] = array( - 'name' => $branch['name'], - 'current' => $branch['current'], - 'status' => $status, - 'desc' => $desc, - 'color' => $color, - 'esort' => $epoch, - 'ssort' => $ssort, - ); - } - - $len_name = max(array_map('strlen', ipull($out, 'name'))) + 2; - $len_status = max(array_map('strlen', ipull($out, 'status'))) + 2; - - if ($this->getArgument('by-status')) { - $out = isort($out, 'ssort'); - } else { - $out = isort($out, 'esort'); - } - - $console = PhutilConsole::getConsole(); - foreach ($out as $line) { - $color = $line['color']; - $console->writeOut( - "%s **%s** %s %s\n", - $line['current'] ? '* ' : ' ', - str_pad($line['name'], $len_name), - str_pad($line['status'], $len_status), - $line['desc']); + 'arc branch is only supported under Git.'); } + return parent::run(); } } diff --git a/src/workflow/ArcanistFeatureWorkflow.php b/src/workflow/ArcanistFeatureWorkflow.php new file mode 100644 index 00000000..927dd454 --- /dev/null +++ b/src/workflow/ArcanistFeatureWorkflow.php @@ -0,0 +1,337 @@ + array( + 'help' => 'Include closed and abandoned revisions.', + ), + 'by-status' => array( + 'help' => 'Sort branches by status instead of time.', + ), + '*' => 'names', + ); + } + + public function run() { + $repository_api = $this->getRepositoryAPI(); + if (!($repository_api instanceof ArcanistGitAPI) && + !($repository_api instanceof ArcanistMercurialAPI)) { + throw new ArcanistUsageException( + 'arc feature is only supported under Git and Mercurial.'); + } + + $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.'); + } + + $branches = $this->loadCommitInfo($branches); + + $revisions = $this->loadRevisions($branches); + + $this->printBranches($branches, $revisions); + + return 0; + } + + private function checkoutBranch(array $names) { + $api = $this->getRepositoryAPI(); + + if ($api instanceof ArcanistMercurialAPI) { + $command = 'update %s'; + } else { + $command = 'checkout %s'; + } + + list($err, $stdout, $stderr) = $api->execManualLocal( + $command, + 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( + $command, + reset($names)); + } + } catch (ConduitException $ex) { + } + } + } + + if ($err) { + if ($api instanceof ArcanistMercurialAPI) { + $rev = ''; + if (isset($names[1])) { + $rev = csprintf('-r %s', hgsprintf($names[1])); + } + $exec = $api->execManualLocal( + 'update %C %s', + $rev, + $names[0]); + + } else { + $exec = $api->execManualLocal( + 'checkout -b %Ls', + $names); + } + + list($err, $stdout, $stderr) = $exec; + } + + echo $stdout; + fprintf(STDERR, $stderr); + return $err; + } + + private function loadCommitInfo(array $branches) { + $repository_api = $this->getRepositoryAPI(); + + $futures = array(); + foreach ($branches as $branch) { + if ($repository_api instanceof ArcanistMercurialAPI) { + $futures[$branch['name']] = $repository_api->execFutureLocal( + "log -l 1 --template '%C' -r %s", + "{node}\1{date|hgdate}\1{p1node}\1{desc|firstline}\1{desc}", + hgsprintf($branch['name'])); + + } else { + // NOTE: "-s" is an option deep in git's diff argument parser that + // doesn't seem to have much documentation and has no long form. It + // suppresses any diff output. + $futures[$branch['name']] = $repository_api->execFutureLocal( + 'show -s --format=%C %s --', + '%H%x01%ct%x01%T%x01%s%x01%s%n%n%b', + $branch['name']); + } + } + + $branches = ipull($branches, null, 'name'); + + foreach (Futures($futures)->limit(16) as $name => $future) { + list($info) = $future->resolvex(); + list($hash, $epoch, $tree, $desc, $text) = explode("\1", trim($info), 5); + + $branch = $branches[$name]; + $branch['hash'] = $hash; + $branch['desc'] = $desc; + + try { + $message = ArcanistDifferentialCommitMessage::newFromRawCorpus($text); + $id = $message->getRevisionID(); + + $branch += array( + 'epoch' => (int)$epoch, + 'tree' => $tree, + 'revisionID' => $id, + ); + } catch (ArcanistUsageException $ex) { + // In case of invalid commit message which fails the parsing, + // do nothing. + } + + $branches[$name] = $branch; + } + + return $branches; + } + + private function loadRevisions(array $branches) { + $ids = array(); + $hashes = array(); + + foreach ($branches as $branch) { + if ($branch['revisionID']) { + $ids[] = $branch['revisionID']; + } + $hashes[] = array('gtcm', $branch['hash']); + $hashes[] = array('gttr', $branch['tree']); + } + + $calls = array(); + + if ($ids) { + $calls[] = $this->getConduit()->callMethod( + 'differential.query', + array( + 'ids' => $ids, + )); + } + + if ($hashes) { + $calls[] = $this->getConduit()->callMethod( + 'differential.query', + array( + 'commitHashes' => $hashes, + )); + } + + $results = array(); + foreach (Futures($calls) as $call) { + $results[] = $call->resolve(); + } + + return array_mergev($results); + } + + private function printBranches(array $branches, array $revisions) { + $revisions = ipull($revisions, null, 'id'); + + static $color_map = array( + 'Closed' => 'cyan', + 'Needs Review' => 'magenta', + 'Needs Revision' => 'red', + 'Accepted' => 'green', + 'No Revision' => 'blue', + 'Abandoned' => 'default', + ); + + static $ssort_map = array( + 'Closed' => 1, + 'No Revision' => 2, + 'Needs Review' => 3, + 'Needs Revision' => 4, + 'Accepted' => 5, + ); + + $out = array(); + foreach ($branches as $branch) { + $revision = idx($revisions, idx($branch, 'revisionID')); + + // If we haven't identified a revision by ID, try to identify it by hash. + if (!$revision) { + foreach ($revisions as $rev) { + $hashes = idx($rev, 'hashes', array()); + foreach ($hashes as $hash) { + if (($hash[0] == 'gtcm' && $hash[1] == $branch['hash']) || + ($hash[0] == 'gttr' && $hash[1] == $branch['tree'])) { + $revision = $rev; + break; + } + } + } + } + + if ($revision) { + $desc = 'D'.$revision['id'].': '.$revision['title']; + $status = $revision['statusName']; + } else { + $desc = $branch['desc']; + $status = 'No Revision'; + } + + if (!$this->getArgument('view-all') && !$branch['current']) { + if ($status == 'Closed' || $status == 'Abandoned') { + continue; + } + } + + $epoch = $branch['epoch']; + + $color = idx($color_map, $status, 'default'); + $ssort = sprintf('%d%012d', idx($ssort_map, $status, 0), $epoch); + + $out[] = array( + 'name' => $branch['name'], + 'current' => $branch['current'], + 'status' => $status, + 'desc' => $desc, + 'color' => $color, + 'esort' => $epoch, + 'ssort' => $ssort, + ); + } + + $len_name = max(array_map('strlen', ipull($out, 'name'))) + 2; + $len_status = max(array_map('strlen', ipull($out, 'status'))) + 2; + + if ($this->getArgument('by-status')) { + $out = isort($out, 'ssort'); + } else { + $out = isort($out, 'esort'); + } + + $console = PhutilConsole::getConsole(); + foreach ($out as $line) { + $color = $line['color']; + $console->writeOut( + "%s **%s** %s %s\n", + $line['current'] ? '* ' : ' ', + str_pad($line['name'], $len_name), + str_pad($line['status'], $len_status), + $line['desc']); + } + } + +} -- 2.43.0