git@vger.kernel.org mailing list mirror (one of many)
 help / Atom feed
* Need help debugging issue in git
@ 2018-04-02  0:36 Robert Dailey
  2018-04-02  6:53 ` Johannes Sixt
  0 siblings, 1 reply; 3+ messages in thread
From: Robert Dailey @ 2018-04-02  0:36 UTC (permalink / raw)
  To: Git

I'm struggling with a bug that I found introduced in git v2.13.2. The
bug was not reproducible in v2.13.1. The issue is that using arguments
like "@{-1}" to aliases causes those curly braces to be removed, so
once the command is executed after alias processing the argument looks
like "@-1". This breaks any aliases you have that wrap `git log` and
such. I originally opened the bug on the Git for Windows project
(since I use Git mostly on Windows):

https://github.com/git-for-windows/git/issues/1220

After digging into the code of run-command.c some more, I have a
change that fixes the issue but it doesn't feel like the right
solution. Furthermore, the function I'm modifying doesn't seem to have
been changed in the diff between v2.13.1 and v2.13.2. I'll provide my
diff at the bottom of this email for those curious as to what I've
modified.

I was not able to reproduce this bug on Linux. However, the way
aliases get expanded seems extremely wasteful. Essentially if an alias
already has $@ in it, Git shouldn't try to insert another one. This
results in the alias being repeated multiple times. I'll give you an
example (Note there's a ton of detail in the github post linked
earlier, and most of this is already there).

Here is the alias being used for a test:

[alias]
    lgtest = !git log --oneline \"$@\"

And here is the command I invoke for the test:

$ git lgtest @{-1}

I should get logs for the previously-checked-out branch.

When `prepare_shell_cmd()` is called in run-command.c, it gets expanded like so:

+ [0] "sh" const char *
+ [1] "-c" const char *
+ [2] "git log --oneline \"$@\" \"$@\"" const char *
+ [3] "git log --oneline \"$@\"" const char *
+ [4] "@{-1}" const char *

With my modifications (again, patch inline below) I get this:

+ [0] "sh" const char *
+ [1] "-c" const char *
+ [2] "git log --oneline \"$@\"" const char *
+ [3] "@{-1}" const char *

The second version looks much better. I think the constant nesting of
commands inside each other that the first version does is somehow
causing curly braces to be removed. I don't understand enough about
shell processing to know why it would do this.

Essentially I don't feel like I'm addressing the root cause here. I
hope that someone has the time to take a peek and point me in the
right direction. If I can make sense of this maybe I can make a proper
fix somewhere.

Here is the patch:

diff --git a/run-command.c b/run-command.c
index 31fc5ea86e..39bab7f5b2 100644
--- a/run-command.c
+++ b/run-command.c
@@ -261,10 +261,10 @@ static const char **prepare_shell_cmd(struct
argv_array *out, const char **argv)
  if (!argv[1])
  argv_array_push(out, argv[0]);
  else
- argv_array_pushf(out, "%s \"$@\"", argv[0]);
+ argv_array_pushf(out, "%s", argv[0]);
  }

- argv_array_pushv(out, argv);
+ argv_array_pushv(out, argv+1);
  return out->argv;
 }

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Need help debugging issue in git
  2018-04-02  0:36 Need help debugging issue in git Robert Dailey
@ 2018-04-02  6:53 ` Johannes Sixt
  2018-04-14 16:39   ` Robert Dailey
  0 siblings, 1 reply; 3+ messages in thread
From: Johannes Sixt @ 2018-04-02  6:53 UTC (permalink / raw)
  To: Robert Dailey; +Cc: Git

Am 02.04.2018 um 02:36 schrieb Robert Dailey:
> I'm struggling with a bug that I found introduced in git v2.13.2. The
> bug was not reproducible in v2.13.1. The issue is that using arguments
> like "@{-1}" to aliases causes those curly braces to be removed, so
> once the command is executed after alias processing the argument looks
> like "@-1". This breaks any aliases you have that wrap `git log` and
> such. I originally opened the bug on the Git for Windows project
> (since I use Git mostly on Windows):
> 
> https://github.com/git-for-windows/git/issues/1220
...
> Here is the alias being used for a test:
> 
> [alias]
>      lgtest = !git log --oneline \"$@\"
> 
> And here is the command I invoke for the test:
> 
> $ git lgtest @{-1}
> 
> I should get logs for the previously-checked-out branch.
> 
> When `prepare_shell_cmd()` is called in run-command.c, it gets expanded like so:
> 
> + [0] "sh" const char *
> + [1] "-c" const char *
> + [2] "git log --oneline \"$@\" \"$@\"" const char *
> + [3] "git log --oneline \"$@\"" const char *
> + [4] "@{-1}" const char *
> 
> With my modifications (again, patch inline below) I get this:
> 
> + [0] "sh" const char *
> + [1] "-c" const char *
> + [2] "git log --oneline \"$@\"" const char *
> + [3] "@{-1}" const char *
> 
> The second version looks much better.

But this is wrong. Try this on the command line:

   sh -c 'echo "$@"' a b c

Notice how this prints only 'b c', not 'a b c'. The reason is that the 
argument 'a' is treated like a "script" name, i.e. what you get for 
"$0", and 'b' and 'c' as the actual arguments to the "script".

That is, you must fill in some dummy "script" name at slot [3], and 
run_command chooses to put the alias text there.

> I think the constant nesting of
> commands inside each other that the first version does is somehow
> causing curly braces to be removed. I don't understand enough about
> shell processing to know why it would do this.

Some shells expand the curly braces. They must get lost somewhere by one 
of the two shell invocations that happen on the way.

BTW, you don't happen to have a file named '@-1' in your directory, most 
likely by accident?

-- Hannes

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Need help debugging issue in git
  2018-04-02  6:53 ` Johannes Sixt
@ 2018-04-14 16:39   ` Robert Dailey
  0 siblings, 0 replies; 3+ messages in thread
From: Robert Dailey @ 2018-04-14 16:39 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: Git

On Mon, Apr 2, 2018 at 1:53 AM, Johannes Sixt <j6t@kdbg.org> wrote:
> Am 02.04.2018 um 02:36 schrieb Robert Dailey:
>>
>> I'm struggling with a bug that I found introduced in git v2.13.2. The
>> bug was not reproducible in v2.13.1. The issue is that using arguments
>> like "@{-1}" to aliases causes those curly braces to be removed, so
>> once the command is executed after alias processing the argument looks
>> like "@-1". This breaks any aliases you have that wrap `git log` and
>> such. I originally opened the bug on the Git for Windows project
>> (since I use Git mostly on Windows):
>>
>> https://github.com/git-for-windows/git/issues/1220
>
> ...
>>
>> Here is the alias being used for a test:
>>
>> [alias]
>>      lgtest = !git log --oneline \"$@\"
>>
>> And here is the command I invoke for the test:
>>
>> $ git lgtest @{-1}
>>
>> I should get logs for the previously-checked-out branch.
>>
>> When `prepare_shell_cmd()` is called in run-command.c, it gets expanded
>> like so:
>>
>> + [0] "sh" const char *
>> + [1] "-c" const char *
>> + [2] "git log --oneline \"$@\" \"$@\"" const char *
>> + [3] "git log --oneline \"$@\"" const char *
>> + [4] "@{-1}" const char *
>>
>> With my modifications (again, patch inline below) I get this:
>>
>> + [0] "sh" const char *
>> + [1] "-c" const char *
>> + [2] "git log --oneline \"$@\"" const char *
>> + [3] "@{-1}" const char *
>>
>> The second version looks much better.
>
>
> But this is wrong. Try this on the command line:
>
>   sh -c 'echo "$@"' a b c
>
> Notice how this prints only 'b c', not 'a b c'. The reason is that the
> argument 'a' is treated like a "script" name, i.e. what you get for "$0",
> and 'b' and 'c' as the actual arguments to the "script".
>
> That is, you must fill in some dummy "script" name at slot [3], and
> run_command chooses to put the alias text there.
>
>> I think the constant nesting of
>> commands inside each other that the first version does is somehow
>> causing curly braces to be removed. I don't understand enough about
>> shell processing to know why it would do this.
>
>
> Some shells expand the curly braces. They must get lost somewhere by one of
> the two shell invocations that happen on the way.
>
> BTW, you don't happen to have a file named '@-1' in your directory, most
> likely by accident?

Thanks for your help. I checked for @-1 but I do not have a file with
that name (good catch though). I contacted the MinGW mailing list and
they seem to indicate that {-1} is a valid brace expansion. I was able
to verify the git.exe code itself is not causing this problem. It
seems to be GNU bash doing it. But oddly enough, the Ubuntu version of
Bash for example does not process {-1} as a brace expansion. It seems
weird to me that Git uses a syntax for a portion of its revision
specification that could be ambiguously treated as syntax processed by
Bash. In other words, I feel like this would have been designed into
Git years ago, so I'm not sure why this is a problem now all of a
sudden.

Their suggested solution was to start quoting items in the list or
escaping the braces, but that will make git revisions less intuitive
to use and make commands more tedious to type. I am still discussing
things over there but for the purposes of the Git mailing list, I
think it's clear at this point this is not an issue with Git itself.
Having said that, thanks again for the help!!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-02  0:36 Need help debugging issue in git Robert Dailey
2018-04-02  6:53 ` Johannes Sixt
2018-04-14 16:39   ` Robert Dailey

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

Archives are clonable:
	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

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.org/gmane.comp.version-control.git

 note: .onion URLs require Tor: https://www.torproject.org/
       or Tor2web: https://www.tor2web.org/

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