git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Allow aliases to expand to shell commands

From
Theodore Ts'o <tytso@mit.edu>
Date
Feb 10, 2007, 16:05 UTC
Message-ID
<11711235042388-git-send-email-tytso@mit.edu>
In-Reply-To
<11711235041527-git-send-email-tytso@mit.edu>

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>
---
 Documentation/config.txt |    6 ++++++
 git.c                    |   10 ++++++++++
 2 files changed, 16 insertions(+), 0 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 4e650af..de185d8 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -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 eqvuialent 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 c43d4ff..fc08396 100644
--- 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, alias_command);
+		}
 		count = split_cmdline(alias_string, &new_argv);
 		option_count = handle_options(&new_argv, &count);
 		memmove(new_argv - option_count, new_argv,
-- 
1.5.0.rc4
Previous: Theodore Ts'oNext: Linus Torvalds
Message 24 of 51 in “Git rescue mission”
  1. Bill LearFeb 8, 2007
  2. Johannes SchindelinFeb 8, 2007
  3. Bill LearFeb 8, 2007
  4. Johannes SchindelinFeb 8, 2007
  5. Bill LearFeb 8, 2007
  6. Junio C HamanoFeb 8, 2007
  7. Alexander LitvinovFeb 8, 2007
  8. Junio C HamanoFeb 9, 2007
  9. Alexander LitvinovFeb 9, 2007
  10. Bill LearFeb 8, 2007
  11. Jakub NarebskiFeb 8, 2007
  12. Jeff KingFeb 8, 2007
  13. Bill LearFeb 8, 2007
  14. Linus TorvaldsFeb 8, 2007
  15. Kalle PokkiFeb 8, 2007
  16. Linus TorvaldsFeb 8, 2007
  17. Kalle PokkiFeb 8, 2007
  18. Shawn O. PearceFeb 8, 2007
  19. Theodore TsoFeb 9, 2007
  20. Shawn O. PearceFeb 9, 2007
  21. Jakub NarebskiFeb 9, 2007
  22. Theodore Ts'oFeb 10, 2007
  23. Print a sane error message if an alias expands to an invalid git commandTheodore Ts'o, Feb 10, 2007
  24. Allow aliases to expand to shell commandsTheodore Ts'o, Feb 10, 2007
  25. Linus TorvaldsFeb 10, 2007
  26. Theodore TsoFeb 10, 2007
  27. Johannes SchindelinFeb 10, 2007
  28. Theodore TsoFeb 11, 2007
  29. Johannes SchindelinFeb 11, 2007
  30. Theodore TsoFeb 11, 2007
  31. Johannes SchindelinFeb 11, 2007
  32. Junio C HamanoFeb 11, 2007
  33. Johannes SchindelinFeb 11, 2007
  34. Theodore TsoFeb 12, 2007
  35. Shawn O. PearceFeb 12, 2007
  36. Junio C HamanoFeb 10, 2007
  37. Kalle PokkiFeb 9, 2007
  38. Bill LearFeb 8, 2007
  39. Linus TorvaldsFeb 8, 2007
  40. Bill LearFeb 8, 2007
  41. Bill LearFeb 8, 2007
  42. Shawn O. PearceFeb 8, 2007
  43. Bill LearFeb 8, 2007
  44. Shawn O. PearceFeb 8, 2007
  45. Jakub NarebskiFeb 9, 2007
  46. Linus TorvaldsFeb 9, 2007
  47. Michael S. TsirkinFeb 9, 2007
  48. Jakub NarebskiFeb 8, 2007
  49. Linus TorvaldsFeb 8, 2007
  50. Junio C HamanoFeb 9, 2007
  51. Jakub NarebskiFeb 8, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.