I have a git controlled directory (actually my home directory) within which there are ignored directories (e.g. trash space, and directories controlled by other VCSs). I want to be able to have my bash prompt show whether a directory is version controlled and if so by which VCS, but e.g. git rev-parse
will always find the topmost .git
directory.
Is there a way to ask git whether I'm in an untracked directory?
I've found this to work:
if ! git rev-parse 2>&/dev/null; then
echo "not in git"
else
PREFIX=$(git rev-parse --show-prefix)
if [ -z "$PREFIX" ]; then
echo "at git top level"
elif [ -z $(cd $(git rev-parse --show-toplevel); \
git ls-files -o --directory "${PREFIX%%/}")
echo "tracked by git"
else
echo "untracked"
fi
fi
However it seems very hackish and brittle. Is there a better way?
git clean -nd
approach: If it is ignored directory, then git clean -d
wants to remove it, so it is easy way to check status of current directory.
if git clean -xnd `pwd` | grep 'Would remove \./' > /dev/null; then
echo "Inside ignored or untracked directory"
else
echo "Inside normal directory"
fi
Tweak git clean
to change rules about untracked files. Checked in my $HOME repository.
Note: don't experiment with git clean
without -n
lightly, it can clear things from your home.