Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get git status boolean without slow list of diff

Tags:

git

I have a bash PS1 to get a red color if my git dir is changed or green if not is changed.

if [ $? -eq 0 ]; then \
   echo "$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; \
   if [ "$?" -eq "0" ]; then \
     # @4 - Clean repository - nothing to commit
     echo "\n'$Green'"$(__git_ps1 "(%s)"'$Color_Off'); \
   else \
     # @5 - Changes to working tree
     echo "\n'$IRed'"$(__git_ps1 "(%s)"'$Color_Off'); \
   fi)\$ "; \
 fi)'

This works fine! But the problem is that in some work dir is very slow because exists many changes and a big diff.

What is the better way to get git status boolean (yes changed or no changed) without a full

I tried with git status --short or git status --porcelain but is very slow yet.

like image 435
moylop260 Avatar asked Sep 25 '22 09:09

moylop260


1 Answers

Perhaps this answer might be useful: https://stackoverflow.com/a/2659808/3903076

In short, you can adapt the checks below to your needs. See the linked answer for more details.

if ! git diff-index --quiet --cached HEAD; then
    # Index has changes.
    exit 1;
elif ! git diff-files --quiet; then
    # Working tree has changes.
    exit 1;
elif [ -n "`git ls-files --others --exclude-standard`" ]; then
    # There are untracked unignored files.
    exit 1;
fi
like image 54
filipos Avatar answered Nov 27 '22 05:11

filipos