Allow aliases to expand to shell commands
authorTheodore Ts'o <tytso@mit.edu>
Sun, 11 Feb 2007 00:33:58 +0000 (19:33 -0500)
committerJunio C Hamano <junkio@cox.net>
Sun, 11 Feb 2007 06:46:34 +0000 (22:46 -0800)
If the alias expansion is prefixed with an exclamation point, treat
it as a shell command which is run using system(3).

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Signed-off-by: Junio C Hamano <junkio@cox.net>
Documentation/config.txt
git.c
index 4e650af01af9e0c528805b472e6970e5e24322e4..0129b1fd69ff4b8a82c09b5fc10fb7a999da919f 100644 (file)
@@ -222,6 +222,12 @@ alias.*::
        spaces, the usual shell quoting and escaping is supported.
        quote pair and a backslash can be used to quote them.
 
+       If the alias expansion is prefixed with an exclamation point,
+       it will be treated as a shell command.  For example, defining
+       "alias.new = !gitk --all --not ORIG_HEAD", the invocation
+       "git new" is equivalent to running the shell command
+       "gitk --all --not ORIG_HEAD".
+
 apply.whitespace::
        Tells `git-apply` how to handle whitespaces, in the same way
        as the '--whitespace' option. See gitlink:git-apply[1].
diff --git a/git.c b/git.c
index c43d4ff1fdf6c056af1b5569079ad9090df564c8..45265f14d0cc225dedcae84b7e2ef5700b966f20 100644 (file)
--- a/git.c
+++ b/git.c
@@ -159,6 +159,16 @@ static int handle_alias(int *argcp, const char ***argv)
        alias_command = (*argv)[0];
        git_config(git_alias_config);
        if (alias_string) {
+               if (alias_string[0] == '!') {
+                       trace_printf("trace: alias to shell cmd: %s => %s\n",
+                                    alias_command, alias_string + 1);
+                       ret = system(alias_string + 1);
+                       if (ret >= 0 && WIFEXITED(ret) &&
+                           WEXITSTATUS(ret) != 127)
+                               exit(WEXITSTATUS(ret));
+                       die("Failed to run '%s' when expanding alias '%s'\n",
+                           alias_string + 1, alias_command);
+               }
                count = split_cmdline(alias_string, &new_argv);
                option_count = handle_options(&new_argv, &count);
                memmove(new_argv - option_count, new_argv,