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

Re: Git alias syntax help

From
ΣΝΣταύρος Ντέντος <stdedos@gmail.com>
Date
Jan 14, 2020, 23:13 UTC
Message-ID
<CAHMHMxVajKxjBweG=mps0gLwE1o8M69DvPb1iUQYgLXx0VO5AA@mail.gmail.com>
In-Reply-To
<20200114222802.GC3957260@coredump.intra.peff.net>
On Wed, 15 Jan 2020 at 00:28, Jeff King <peff@peff.net> wrote:
Show 15 quoted lines
>
> On Tue, Jan 14, 2020 at 05:21:20PM +0200, Σταύρος Ντέντος wrote:
>
> > I am having an issue with git-aliases - specifically, the intricacies
> > involved in their syntax.
> >
> > In general, the syntax is confusing to me, especially when it is
> > _wise_ to use quotes inside a `!sh` alias.
> > e.g. which one would be the correct one
> > new = "!f() { : git log ; git log \"${1}@{1}..${1}@{0}\" \"$@\" ; } ; f"
> > new = !f() { : git log ; git log "${1}@{1}..${1}@{0}" "$@" ; } ; f
>
> Only the first one is correct. In addition to the quotes in the second
> one being eaten by the config parser, the unquoted semicolon starts a
> comment.
Could somehow the latter "become" the correct option?
Especially in the case of `!sh`:
1) You need to quote everything after `=` sign ("forced" double quotes), then
2) `sh -c` needs another set (singles are most safe here, I think), and
3) If, for some reason, you need to quote further ("$@" would be a
common suspect usually)

Apart from the [1] feeling unneeded (the equivalent of Python's `alias_cmd = cfg_line.split()[1]` could be enough), this brings a quoting mess on [3]

Show 35 quoted lines
> > The alias confusing me is more specifically this:
> > https://git.wiki.kernel.org/index.php/Aliases#simple_diff_ignoring_line_number_changes
> >
> > diffsort = !sh -c 'git diff "$@" | grep "^[+-]" | sort --key=1.2 | uniq -u -s1'
> >
> > The output of:
> > $  colordiff -su <(git diffsort HEAD^..HEAD) <(git diffsort HEAD^^..HEAD^)
> > Files /dev/fd/63 and /dev/fd/62 are identical
> > is a little unexpected, since I know for a fact that one of the
> > referced commits is not a code block moved.
>
> The issue here isn't with Git's alias mechanism, but a quirk of how "sh
> -c" works.  You can run with GIT_TRACE to see what we're passing to the
> shell (though note that your double-quotes don't make it through):
>
>   $ GIT_TRACE=1 git diffsort HEAD^..HEAD
>   17:22:47.644542 [pid=3959333] git.c:708           trace: exec: git-diffsort HEAD^..HEAD
>   17:22:47.644648 [pid=3959333] run-command.c:663   trace: run_command: git-diffsort HEAD^..HEAD
>   17:22:47.645038 [pid=3959333] run-command.c:663   trace: run_command: 'sh -c '\''git diff $@ | grep ^[+-] | sort --key=1.2 | uniq -u -s1'\''' HEAD^..HEAD
>   17:22:47.650319 [pid=3959336] git.c:439           trace: built-in: git diff
>
> The problem is that "sh -c" takes the first non-option argument as $0,
> not $1. For example:
>
>   $ sh -c 'echo 0=$0, @=$@' foo bar baz
>   0=foo, @=bar baz
>
> You can add any extra string there to become $0, like:
>
>   diffsort = "!sh -c 'git diff \"$@\" | grep \"^[+-]\" | sort --key=1.2 | uniq -u -s1' --"
>
> which will do what you want. You can use whatever string you like, since
> you know that your "-c" snippet does not ever look at $0.
>
> -Peff
Thank you very much for a complete explaination of all of this .

Can some of this be documented somewhere? Are they somewhere and I missed them?

If nothing more, a link to this e-mail chain either on the wiki (if https://git.wiki.kernel.org/index.php/Aliases is an official page) or on git-alias help (here https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases or in some "advanced" section, which I cannot find)

If https://git.wiki.kernel.org/index.php/Aliases is an official page, then: was this written for an earlier version? Could it also be updated?

-- Ntentos Stavros

Previous: Jeff KingNext: Jeff King
Message 3 of 5 in “Git alias syntax help”
  1. Σταύρος ΝτέντοςJan 14, 2020
  2. Jeff KingJan 14, 2020
  3. Σταύρος ΝτέντοςJan 14, 2020
  4. Jeff KingJan 15, 2020
  5. Philip OakleyMar 28, 2020

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.