From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: Erik Faye-Lund <kusmabite@gmail.com>,
Dun Peal <dunpealer@gmail.com>,
git@vger.kernel.org
Subject: Re: Weird behavior of shell variables in git aliases
Date: Tue, 22 Mar 2011 09:28:20 -0400 [thread overview]
Message-ID: <20110322132820.GA14559@sigill.intra.peff.net> (raw)
In-Reply-To: <20110322111844.GA32446@sigill.intra.peff.net>
On Tue, Mar 22, 2011 at 07:18:44AM -0400, Jeff King wrote:
> Doesn't it also break a lot of other more garden-variety aliases that
> rely on the automagic "$@", like:
>
> $ git config alias.log-nopager
> !git --no-pager log
> [...]
> $ GIT_TRACE=1 git.master log-nopager --oneline
> trace: exec: 'git-log-nopager' '--oneline'
> trace: run_command: 'git-log-nopager' '--oneline'
> trace: run_command: 'git --no-pager log' '--oneline'
> trace: exec: 'sh' '-c' 'git --no-pager log "$@"' 'git --no-pager log' '--oneline'
> trace: built-in: git 'log' '--oneline'
> 93c7d44 foo
>
> $ GIT_TRACE=1 git.jch.shell-alias log-nopager --oneline
> trace: exec: 'git-log-nopager' '--oneline'
> trace: run_command: 'git-log-nopager' '--oneline'
> trace: run_command: 'sh' '-c' 'git --no-pager log' '-' '--oneline'
> trace: exec: 'sh' '-c' 'git --no-pager log' '-' '--oneline'
> trace: built-in: git 'log'
> commit 93c7d44635e8bb56a4fd864d024ce75a2ad4ffcf
> Author: Jeff King <peff@peff.net>
> Date: Tue Mar 22 06:57:02 2011 -0400
>
> foo
One other solution would be to make the "$@" more magic by detecting
when the alias uses positional parameters and omitting it in that
case. Something like (on top of your patch):
diff --git a/git.c b/git.c
index 8d54466..1daf89c 100644
--- a/git.c
+++ b/git.c
@@ -161,6 +161,27 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
return handled;
}
+static int alias_uses_positional_parameters(const char *s)
+{
+ while ((s = strchr(s, '$')))
+ if (s[1] == '@' || s[1] == '#' ||
+ (s[1] >= '1' && s[1] <= '9'))
+ return 1;
+ return 0;
+}
+
+static char *alias_to_shell(const char *in)
+{
+ struct strbuf out = STRBUF_INIT;
+
+ if (alias_uses_positional_parameters(in))
+ return xstrdup(in);
+
+ strbuf_addstr(&out, in);
+ strbuf_addstr(&out, " \"$@\"");
+ return strbuf_detach(&out, NULL);
+}
+
static int handle_alias(int *argcp, const char ***argv)
{
int envchanged = 0, ret = 0, saved_errno = errno;
@@ -186,7 +207,7 @@ static int handle_alias(int *argcp, const char ***argv)
alias_argv = xmalloc(sizeof(*alias_argv) * (argc + 4));
alias_argv[0] = "sh";
alias_argv[1] = "-c";
- alias_argv[2] = alias_string + 1;
+ alias_argv[2] = alias_to_shell(alias_string + 1);
alias_argv[3] = "-";
for (i = 1; i < argc; ++i)
alias_argv[i + 3] = (*argv)[i];
But I think that is a little too magic for my taste. Although the false
positives ("!echo 'literal $#'") and false negatives (you want "!foo" to
_ignore_ its parameters) are pretty obscure, I would prefer to keep
things simple.
-Peff
next prev parent reply other threads:[~2011-03-22 13:28 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-03-21 16:39 Weird behavior of shell variables in git aliases Dun Peal
2011-03-21 21:53 ` Jeff King
2011-03-21 22:21 ` Junio C Hamano
2011-03-21 22:33 ` Junio C Hamano
2011-03-22 10:38 ` Lasse Makholm
2011-03-22 11:28 ` Jeff King
2011-03-22 12:59 ` Lasse Makholm
2011-03-22 10:52 ` Ævar Arnfjörð Bjarmason
2011-03-22 11:18 ` Jeff King
2011-03-22 13:28 ` Jeff King [this message]
2011-03-22 13:35 ` Lasse Makholm
2011-03-22 13:43 ` Jeff King
2011-03-22 13:53 ` Lasse Makholm
2011-03-22 15:06 ` Dun Peal
2011-03-22 17:57 ` Junio C Hamano
2011-03-22 18:32 ` Jeff King
2011-03-22 22:22 ` Lasse Makholm
2011-03-23 3:01 ` Junio C Hamano
2011-03-22 17:36 ` Junio C Hamano
2011-03-22 17:35 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20110322132820.GA14559@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=dunpealer@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=kusmabite@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).