mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-21 22:32:41 +01:00
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
This commit is contained in:
parent
ed7034ab38
commit
fb4f3c9446
2 changed files with 48 additions and 8 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue