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

[PATCH] An alias that starts with "!!" runs in the current directory.

From
Eli Barzilay <eli@barzilay.org>
Date
May 5, 2010, 20:28 UTC
Message-ID
<19425.54504.163875.379151@winooski.ccs.neu.edu>
In-Reply-To
<7v1vdql4c9.fsf@alter.siamese.dyndns.org>

With "!"-prefixed shell aliases, the shell command not only gets executed at the repository top level, but there is no way to know the current directory of the original call. This adds "!!"-prefixed aliases as a similar variant for "!"-prefixed ones, but the commands are executed in the original directory instead of the top level.

Signed-off-by: Eli Barzilay <eli@barzilay.org>
---
(Sending as a reply to the earlier message, keeping CCs.)

It looks like setup_git_directory_gently() returns the original CWD, but since it's not documented or commented, I don't know if this is reliable or not, so it might need to change.

Also, it might make more sense to document the "!!" variant first, since it is generally more useful, but the way things evolved with "!!" being the longer prefix, it seems to me that documenting it after "!" is more sensible.

 Documentation/config.txt |    9 +++++++--
 git.c                    |    8 ++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 92f851e..055f4e3 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -549,13 +549,18 @@ 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,
+If the alias expansion is prefixed with a single 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".  Note that shell commands will be
+"gitk --all --not ORIG_HEAD".  Note that such shell commands will be
 executed from the top-level directory of a repository, which may
 not necessarily be the current directory.
++
+If the alias expansion is prefixed with two exclamation points,
+it will be treader similarly to the above, except that the shell commands
+are executed at the current directory.
+
 
 am.keepcr::
 	If true, git-am will call git-mailsplit for patches in mbox format
diff --git a/git.c b/git.c
index 6bae305..f3f8346 100644
--- a/git.c
+++ b/git.c
@@ -167,6 +167,14 @@ static int handle_alias(int *argcp, const char ***argv)
 				free(alias_string);
 				alias_string = buf.buf;
 			}
+			/* going to exit anyway, so it's fine to change
+			 * alias_string to the actual command */
+			alias_string += 1;
+			if (alias_string[0] == '!') {
+				alias_string += 1;
+				if (subdir && chdir(subdir))
+					die_errno("Cannot change to '%s'", subdir);
+			}
 			trace_printf("trace: alias to shell cmd: %s => %s\n",
 				     alias_command, alias_string + 1);
 			ret = system(alias_string + 1);
-- 
1.7.1
Previous: Junio C HamanoNext: Jared Hance
Message 7 of 23 in “Set GIT_PATHNAME_PREFIX with aliases.”
  1. Set GIT_PATHNAME_PREFIX with aliases.Jared Hance, May 5, 2010
  2. Jeff KingMay 5, 2010
  3. Johannes SixtMay 5, 2010
  4. Jeff KingMay 5, 2010
  5. Eli BarzilayMay 5, 2010
  6. Junio C HamanoMay 5, 2010
  7. An alias that starts with "!!" runs in the current directory.Eli Barzilay, May 5, 2010
  8. Jared HanceMay 5, 2010
  9. Eli BarzilayMay 5, 2010
  10. Eli BarzilayMay 5, 2010
  11. Re-submitting patchesJonathan Nieder, May 5, 2010
  12. Eli BarzilayMay 5, 2010
  13. An alias that starts with "!!" runs in the current directory.Eli Barzilay, May 5, 2010
  14. Will PalmerMay 5, 2010
  15. Eli BarzilayMay 5, 2010
  16. Junio C HamanoMay 5, 2010
  17. Adam BrewsterMay 6, 2010
  18. Will PalmerMay 6, 2010
  19. Will PalmerMay 6, 2010
  20. Johannes SixtMay 6, 2010
  21. Matthieu MoyMay 6, 2010
  22. Junio C HamanoMay 5, 2010
  23. Eli BarzilayMay 5, 2010

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.