While trying to set up my new fish shell prompt, I ended up testing ways to get the branch name and also testing if I was on a git repository. I have found (1) git rev-parse --abbrev-ref HEAD
, (2) git symbolic-ref HEAD | sed 's/refs\/heads\///'
and (3) git describe --contains --all HEAD
do the trick just fine.
I’m curious because I love the simplicity of (1), but on a new and untouched (just git init
ed) repository, it gives me a “fatal:” error, while (2) works as intended, that is, giving me a default master as output. The thing is, even with a “fatal:” error, the return code is 0. Is that the intended behavior?
To test if I was in a git repository, I ended up simply testing if the current directory had a “.git” folder: test -d ".git"
. Rudimentary solution, but it works, and it seems to be faster that using any git command.
So, my questions are:
- Shouldn’t that “fatal:” with (1) return an exit code different from 0? If indeed it is a bug, since I don’t know how git people standardised their return codes, where should I report it?
- I know if I redirect (1) with
^ /dev/null
(for those who don’t know,^
is the same as2>
), I won’t get the error, but it will sayHEAD
instead of master, but if Icat .git/HEAD
, I get:ref: refs/heads/master
. What should it really say? Withmaster
am I just being ignorant and picky, because it should instead sayHEAD
? I mean in the case of a justgit init
ed repository. - Have you actually benchmarked any possible solutions during your quest to get a nice prompt?
This is the code I use to benchmark (for fish):
set -l before (date +%s%N)
git rev-parse --abbrev-ref HEAD
set -l after (date +%s%N)
set -l elapsed_time (expr $after - $before)
echo $elapsed_time
Thanks in advance!
PS: The tools are the latest GNU versions, I mean, sed
, date
and expr
. I’m sorry if there is too much information or if something doesn’t make any sense. Bear with me. Thanks!
EDIT: As someone correctly guessed in an answer, the whole error is:
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions
fatal:
error. Tough to know how it should be treated without being able to read it. :-)