2

I'm tryingto write log alias

git config --global --replace-all alias.lol6 "! f() { echo "\$1"; git --no-pager log --oneline  --graph -15 \${@}; }; f"

and use it like this:

git lol5 '@{-1}'
git log '@{-1}'  #works

but the commit-ish passged to git is @-1

fatal: ambiguous argument '@-1': unknown revision or path not in the working tree

I read this post but didn't understand how to use it

Thanks Boaz

Some more information, tried @vonc advise

git config --global --replace-all alias.lol6 '! f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f'

Got same results, turn on GIT_TRACE

git lol6 @{-1}
20:20:37.454153 git.c:576               trace: exec: git-lol6 '@{-1}'
20:20:37.454153 run-command.c:640       trace: run_command: git-lol6 '@{-1}'
20:20:37.463150 run-command.c:640       trace: run_command: ' f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f' '@{-1}'
@-1
22:20:37.607150 git.c:344               trace: built-in: git log --oneline --graph -15 @-1
fatal: ambiguous argument '@-1': unknown revision or path not in the working tree.

Git is adding '' around first argument

But !!!, if I tried

git lol6 HEAD
20:30:35.621257 run-command.c:640       trace: run_command: ' f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f' HEAD

Git doesn't add '' around HEAD

Boaz Nahum
  • 1,049
  • 8
  • 9

1 Answers1

0

I prefer using string quotes ' instead of double-quotes: less escaping to do in your case.

See this example, done in a git bash session.

vonc@voncavn7:/mnt/d/git/git$ 
git config --global --replace-all alias.lol6 \
  '! f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f'

First, I check that I can print a bogus argument:

vonc@voncavn7:/mnt/d/git/git$ git config --global --replace-all alias.lol6 '! f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f'
vonc@voncavn7:/mnt/d/git/git$ git lol6 e
e
fatal: ambiguous argument 'e': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Then I try, without any quotes when calling the alias: git lol6 @{-1}

vonc@voncavn7:/mnt/d/git/git$ git lol6 @{-1}
@{-1}
* 29533fb168 (tmp2) RelNotes: the eleventh batch
*   dc8fb4dd7e Merge branch 'rb/quick-install-doc'
|\
| * 65289e9dcd install-doc-quick: allow specifying what ref to install

If the alias/command above does not work, always double-check with a simplified PATH:

set G=c:\path\to\latest\git
set PATH=%G%\bin;%G%\usr\bin;%G%\mingw64\bin
set PATH=%PATH%;C:\windows\system32;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\

The OP Boaz Nahum confirms in the comments:

using 'simplified PATH' -it works.
Now what it means ? Why it works ?

It works because there is no telling what is in your original PATH, in C:\Windows\System32 and other folders placed before Git, and overshadowing some of the Git executable.

How can I make standard Git installation work too ?

Simply replace c:\path\to\latest\git above by your own Git current installation path.

Then set your PATH:

set PATH=%G%\bin;%G%\usr\bin;%G%\mingw64\bin

Next, add all your other folders form your original %PATH%, and always end with C:\windows\system32;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\.
That way, you:

  • start with the most specific (Git: git/bin, git/usr/bin, git/ming64/bin), making sure Git is working as expected
  • go on with more general application paths (the ones you had in your original PATH)
  • end with the most general PATH of all: the OS Windows ones (the C:\windows\system32\... ones)

By going from the most specific to the most general, you lower the rate of "unexpected behavior" of your installed software.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks @vonc, Tried you suggestion without any luck, add 'set -x' and it clear that @-1 is passed to git. Maybe it related for bash implementation come with git under windows, will try on linux – Boaz Nahum Jun 02 '18 at 19:00
  • What is your OS and your got version? – VonC Jun 02 '18 at 19:01
  • You asked while I'm typing :) Windows 10, git 2.17.0.windows.1 – Boaz Nahum Jun 02 '18 at 19:02
  • I will try simple bash script not related to git – Boaz Nahum Jun 02 '18 at 19:03
  • Then the answer illustrates exactly what I typed and see in a Windows Git bash session – VonC Jun 02 '18 at 19:03
  • From bash (mingw64) 'function f() { echo $1; }' f @{-1} works just fine. The same git alias display @-1 git config --global --replace-all alias.lol6 '! f() { echo $1; }; f' git lol6 @{-1} I got @-1 – Boaz Nahum Jun 02 '18 at 19:11
  • @BoazNahum And if you type what I typed, does it work? – VonC Jun 02 '18 at 19:25
  • 1
    @BoazNahum Also, try again with the same commands and git bash, but using a simplified PATH: https://stackoverflow.com/a/50245596/6309 – VonC Jun 02 '18 at 19:56
  • No, it doesn't work if I typed what you typed. Will try simplified PATH – Boaz Nahum Jun 03 '18 at 10:15
  • Did you test it with the simplified PATH I recommended above? – VonC Jun 03 '18 at 10:17
  • using 'simplified PATH' -it works. Now what it means ? Why it works ? How can I make standard Git installation work too ? MANY THANKS !!! – Boaz Nahum Jun 04 '18 at 12:58
  • I knew it. The standard PATH on Windows is often an ungodly mess. Try and add your original PATH folders *after* the current simplified one. – VonC Jun 04 '18 at 13:00
  • But the simplified one is composed of portable git- and I can’t use it. Can you try to explain what is important and what is not in your change so I can refine it ? – Boaz Nahum Jun 05 '18 at 11:47
  • Why can't you use it? Or why can't you reorder your path in order to put your own git first, and system32 last? – VonC Jun 05 '18 at 11:58
  • I just said that I can't user portable Git. And I don't understand what is important the order or the fact that we use portable git. I will try to reorder using installed git. Still I don't understand what the issue is. I wonder if you can spend some time and trying to explain it to me. – Boaz Nahum Jun 05 '18 at 15:15
  • @BoazNahum My edited answer precisely addresses the fact you cannot use the portable Git. – VonC Jun 05 '18 at 15:15
  • @BoazNahum The edited answer is me spending some time and trying to explain to you what the issue is. – VonC Jun 05 '18 at 15:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172523/discussion-between-boaz-nahum-and-vonc). – Boaz Nahum Jun 05 '18 at 15:22