zsh prompt: check whether inside git repository and not being ignored by git
8
votes
3
answers
2592
views
In my
zsh
shell, I am dynamically changing prompt depending on whether I am inside git
repository or not. I am using following git command to check:
if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
...
now I also want to distinguish whether current directory is being ignored by git
. So I have added one more check to my if
statement:
if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1) && ! $(git check-ignore . >/dev/null 2>&1); then
...
This works fine, but I was wondering whether I could simplify this into one git
command. Since the prompt is refreshed on every ENTER, it tends to slow down the shell noticeably on some slower machines.
UPDATE
======
The accepted solution from @Stephen Kitt works great, except in following situation:
I am using repository across filesystems. Lets say git resides at /.git
(because I want to track my config files in /etc
), but I also want to track some files in /var/foo
, which is a different partition/filesystem.
When I am located at /
and execute following command, everything works as expected, and I get return code 1
(because /var/foo
is being tracked):
# git check-ignore -q /var/foo
But when I am located anywhere in /var
, the same command fails with error code 128
and following error message:
# git check-ignore -q /var/foo
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
But I think this is only problem with the check-ignore
command. Otherwise git seems to work fine across filesystem. I can track files in /var/foo
fine.
The expected behavior should be that git check-ignore -q /var/foo
returns 1
, and git check-ignore -q /var/bar
returns 0
, if it is not being tracked.
**how can I fix this problem?**
Asked by Martin Vegter
(598 rep)
Jan 4, 2021, 06:10 AM
Last activity: Feb 19, 2025, 11:40 PM
Last activity: Feb 19, 2025, 11:40 PM