Get git boolean status without slow diff list

I have a bash PS1 to get red if my git directory is changed or green if it has not been 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 work is wonderful! But the problem is that in some works, dir is very slow, because there are many changes and big diff.

What is the best way to get git boolean status (yes changed or not changed) without full

I tried with git status --shortor git status --porcelain, but so far very slowly.

+4
source share
2 answers

Perhaps this answer might be useful: fooobar.com/questions/6099 / ...

In short, you can tailor the checks below to your needs. See the related 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
+1
source

: https://gist.github.com/sindresorhus/3898739 git diff --quiet --ignore-submodules HEAD 2>/dev/null

, , changed==0 boolean, .

git_ps1_style ()
{
    local GREEN="\001\033[0;32m\002"
    local RED="\001\033[0;91m\002"
    local git_branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)";
    local git_ps1_style="";
    if [ -n "$git_branch" ]; then
        (git diff --quiet --ignore-submodules HEAD 2>/dev/null)
        local git_changed=$?
        if [ "$git_changed" == 0 ]; then
            git_ps1_style=$GREEN$git_branch;
        else
            git_ps1_style=$RED$git_branch;
        fi
    fi
    echo -e "$git_ps1_style"
}
echo $(git_ps1_style)
0

Source: https://habr.com/ru/post/1628596/


All Articles