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

Re: Weird behavior of shell variables in git aliases

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 21, 2011, 22:33 UTC
Message-ID
<7v4o6wnlxm.fsf@alter.siamese.dyndns.org>
In-Reply-To
<7v8vw8nmh2.fsf@alter.siamese.dyndns.org>
Junio C Hamano <gitster@pobox.com> writes:
Show 20 quoted lines
> Jeff King <peff@peff.net> writes:
>
>> But interestingly, that's _not_ the behavior as of Erik's 7f51f8b
>> (alias: use run_command api to execute aliases, 2011-01-07), which is in
>> master but not yet released. With that, we end up executing:
>>
>>   sh -c 'echo $1 && echo $1 "$@"' 'echo $1 && echo $1' 'foo'
>>
>> which prints "foo foo". So it is technically a regression. I don't know
>> how much we care; using positional parameters like this was already
>> nonsensical, as shown above.
>>
>> For reference, what you actually want (in either system) is:
>>
>>   $ git config alias.test1
>>   !sh -c 'echo $1 && echo $1' -
>
> Oh, I should have been paying a bit more attention.  I've been assuming
> that we were turning "!anything" into { "sh", "-c", "anything", "-" }
> followed by the user supplied arguments.
The attached quick hack gives
  $ git config alias.silly
  !echo hello $1; echo $# args, bye!
  $ GIT_TRACE=1 ./git silly world funny
  trace: exec: 'git-silly' 'world' 'funny'
  trace: run_command: 'git-silly' 'world' 'funny'
  trace: run_command: 'sh' '-c' 'echo hello $1; echo $# args, bye'\!'' '-' 'world' 'funny'
  trace: exec: 'sh' '-c' 'echo hello $1; echo $# args, bye'\!''
  '-' 'world' 'funny'
  hello world
  2 args, bye!

but it would penalize a properly written alias that uses "sh -c <it> -" trick itself by double forking, which is not very nice and I am unhappy about.

 git.c |   11 +++++++----
 1 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/git.c b/git.c
index ef598c3..8d54466 100644
--- a/git.c
+++ b/git.c
@@ -183,11 +183,14 @@ static int handle_alias(int *argcp, const char ***argv)
 			commit_pager_choice();
 
 			/* build alias_argv */
-			alias_argv = xmalloc(sizeof(*alias_argv) * (argc + 1));
-			alias_argv[0] = alias_string + 1;
+			alias_argv = xmalloc(sizeof(*alias_argv) * (argc + 4));
+			alias_argv[0] = "sh";
+			alias_argv[1] = "-c";
+			alias_argv[2] = alias_string + 1;
+			alias_argv[3] = "-";
 			for (i = 1; i < argc; ++i)
-				alias_argv[i] = (*argv)[i];
-			alias_argv[argc] = NULL;
+				alias_argv[i + 3] = (*argv)[i];
+			alias_argv[argc + 3] = NULL;
 
 			ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
 			if (ret >= 0)   /* normal exit */
Previous: Junio C HamanoNext: Lasse Makholm
Message 4 of 20 in “Weird behavior of shell variables in git aliases”
  1. Dun PealMar 21, 2011
  2. Jeff KingMar 21, 2011
  3. Junio C HamanoMar 21, 2011
  4. Junio C HamanoMar 21, 2011
  5. Lasse MakholmMar 22, 2011
  6. Jeff KingMar 22, 2011
  7. Lasse MakholmMar 22, 2011
  8. Ævar Arnfjörð BjarmasonMar 22, 2011
  9. Jeff KingMar 22, 2011
  10. Jeff KingMar 22, 2011
  11. Lasse MakholmMar 22, 2011
  12. Jeff KingMar 22, 2011
  13. Lasse MakholmMar 22, 2011
  14. Dun PealMar 22, 2011
  15. Junio C HamanoMar 22, 2011
  16. Jeff KingMar 22, 2011
  17. Lasse MakholmMar 22, 2011
  18. Junio C HamanoMar 23, 2011
  19. Junio C HamanoMar 22, 2011
  20. Junio C HamanoMar 22, 2011

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.