git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Lasse Makholm <lasse.makholm@gmail.com>
Cc: Junio C Hamano <gitster@pobox.com>,
	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 07:28:32 -0400	[thread overview]
Message-ID: <20110322112832.GB32446@sigill.intra.peff.net> (raw)
In-Reply-To: <AANLkTimH+eVUh6D5qK-PbNJGg46XJwaCii5zMg7xyZ_6@mail.gmail.com>

On Tue, Mar 22, 2011 at 11:38:06AM +0100, Lasse Makholm wrote:

> > 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!
> 
> That would IMHO be The Right Way to do it. Since the documentation for
> aliases promises to to pass my alias to a shell if I prefix it with
> "!", I shouldn't have to add the "sh -c ... -" myself...

It does pass it to the shell. You can do:

  $ git config alias.autolog --oneline
  !repo=`find-git-repo-for $PWD` && git --git-dir="$repo" log

for an example of a shell-based alias. It just doesn't handle positional
parameters the way you want. The typical solution is to invoke another
shell, but you can also do:

  $ grep -B1 silly .git/config
  [alias]
          silly = "!foo() { echo hello $1; echo $# args, bye!\n}\nfoo"

which unsurprisingly looks like exactly the same solution one would use
in the shell to avoid the fact that shell aliases suck for handling
positional parameters.

> > 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.
> 
> A properly written alias that uses a trick? I guess that sums up the
> problem... :-)

Yeah. Though it also penalizes non-tricky aliases. See my other mail in
this thread.

> Anyway, doesn't the existing way potentially break when passing funky
> arguments containing spaces/quotes/something? We currently pass the
> arguments to the alias command as a single quoted string. Passing them
> as seperate elements on argv seems a lot more robust...

No, the arguments in the current scheme are properly shell-quoted before
they are appended to the shell snippet. So they are equally robust.

-Peff

  reply	other threads:[~2011-03-22 11: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 [this message]
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
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=20110322112832.GB32446@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 \
    --cc=lasse.makholm@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).