git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: "Σταύρος Ντέντος" <stdedos@gmail.com>
Cc: git <git@vger.kernel.org>
Subject: Re: Git alias syntax help
Date: Tue, 14 Jan 2020 17:28:02 -0500
Message-ID: <20200114222802.GC3957260@coredump.intra.peff.net> (raw)
In-Reply-To: <CAHMHMxWpLAnj3w8DGLMFbfy-A-pBjDxNdMeiM-fyuu-gnZyg+Q@mail.gmail.com>

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.

> 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

  reply	other threads:[~2020-01-14 22:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-14 15:21 Σταύρος Ντέντος
2020-01-14 22:28 ` Jeff King [this message]
2020-01-14 23:13   ` Σταύρος Ντέντος
2020-01-15 18:13     ` Jeff King
2020-03-28 17:05       ` Philip Oakley

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

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200114222802.GC3957260@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=stdedos@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

git@vger.kernel.org list mirror (unofficial, one of many)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 git git/ https://public-inbox.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git
	nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for the project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git