]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Allow users to define shell aliases in arc with "!"
authorepriestley <git@epriestley.com>
Thu, 24 May 2012 00:52:37 +0000 (17:52 -0700)
committerepriestley <git@epriestley.com>
Thu, 24 May 2012 00:52:37 +0000 (17:52 -0700)
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
src/workflow/alias/ArcanistAliasWorkflow.php

index 058fdedf8a6a3505a84f4ddeb23cd6e680aaa7ab..120793df01b3a738eaa43f5b7e6a682240d82f22 100755 (executable)
@@ -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);
index b90ce4b4aebd7f7c3bfd36deb9b2e01a7f59f559..cc71733951a0a497cd84781df898c1743c49b771 100644 (file)
@@ -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);