From fb4f3c9446e02e2d3d5166fddacd0dec69258250 Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 23 May 2012 17:52:37 -0700 Subject: [PATCH] Allow users to define shell aliases in arc with "!" Summary: Git supports this feature (https://git.wiki.kernel.org/index.php/Aliases), it's fairly simple to implement, and gives us a little more ammunition to dissuade users with unusual workflow requirements from actually requesting features. Test Plan: Defined the alias described in the documentation. Ran "arc ls", "arc ls -alh", etc. Reviewers: btrahan, csilvers Reviewed By: csilvers CC: aran Differential Revision: https://secure.phabricator.com/D2552 --- scripts/arcanist.php | 37 ++++++++++++++++---- src/workflow/alias/ArcanistAliasWorkflow.php | 19 +++++++++- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/scripts/arcanist.php b/scripts/arcanist.php index 058fdedf..120793df 100755 --- a/scripts/arcanist.php +++ b/scripts/arcanist.php @@ -170,28 +170,51 @@ try { // normally to prevent you from doing silly things like aliasing 'alias' // to something else. + $aliases = ArcanistAliasWorkflow::getAliases($working_copy); list($new_command, $args) = ArcanistAliasWorkflow::resolveAliases( $command, $config, $args, $working_copy); + $full_alias = idx($aliases, $command, array()); + $full_alias = implode(' ', $full_alias); + + // Run shell command aliases. + + if (ArcanistAliasWorkflow::isShellCommandAlias($new_command)) { + $shell_cmd = substr($full_alias, 1); + + if ($config_trace_mode) { + echo "[alias: 'arc {$command}' -> \$ {$full_alias}]\n"; + } + + if ($args) { + $err = phutil_passthru('%C %Ls', $shell_cmd, $args); + } else { + $err = phutil_passthru('%C', $shell_cmd); + } + exit($err); + } + + // Run arc command aliases. + if ($new_command) { $workflow = $config->buildWorkflow($new_command); + if ($workflow) { + if ($config_trace_mode) { + echo "[alias: 'arc {$command}' -> 'arc {$full_alias}']\n"; + } + $command = $new_command; + } } if (!$workflow) { throw new ArcanistUsageException( "Unknown command '{$command}'. Try 'arc help'."); - } else { - if ($config_trace_mode) { - $aliases = ArcanistAliasWorkflow::getAliases($working_copy); - $target = implode(' ', idx($aliases, $command, array())); - echo "[alias: 'arc {$command}' -> 'arc {$target}']\n"; - } - $command = $new_command; } } + $workflow->setArcanistConfiguration($config); $workflow->setCommand($command); $workflow->setWorkingDirectory($working_directory); diff --git a/src/workflow/alias/ArcanistAliasWorkflow.php b/src/workflow/alias/ArcanistAliasWorkflow.php index b90ce4b4..cc717339 100644 --- a/src/workflow/alias/ArcanistAliasWorkflow.php +++ b/src/workflow/alias/ArcanistAliasWorkflow.php @@ -44,6 +44,15 @@ EOTEXT 'arc patch --force ...' when run. NOTE: use "--" before specifying options! + If you start an alias with "!", the remainder of the alias will be + invoked as a shell command. For example, if you want to implement + 'arc ls', you can do so like this: + + arc alias ls '!ls' + + You can now run "arc ls" and it will behave like "ls". Of course, this + example is silly and would make your life worse. + You can not overwrite builtins, including 'alias' itself. The builtin will always execute, even if it was added after your alias. @@ -124,7 +133,6 @@ EOTEXT } $aliases[$argv[0]] = array_slice($argv, 1); - echo phutil_console_format( "Aliased '**arc %s**' to '**arc %s**'.\n", $argv[0], @@ -136,6 +144,10 @@ EOTEXT return 0; } + public static function isShellCommandAlias($command) { + return preg_match('/^!/', $command); + } + public static function resolveAliases( $command, ArcanistConfiguration $config, @@ -148,6 +160,11 @@ EOTEXT } $new_command = head($aliases[$command]); + + if (self::isShellCommandAlias($new_command)) { + return array($new_command, $argv); + } + $workflow = $config->buildWorkflow($new_command); if (!$workflow) { return array(null, $argv);