]> git.friedersdorff.com Git - max/dotfiles.git/commitdiff
More reliable git repo detection
authorMaximilian Friedersdorff <max@friedersdorff.com>
Thu, 26 Apr 2018 16:25:11 +0000 (17:25 +0100)
committerMaximilian Friedersdorff <max@friedersdorff.com>
Thu, 26 Apr 2018 16:25:11 +0000 (17:25 +0100)
bash/.bashrc

index 249787cff3aff307933010d97dcb383294d03c4d..6349cc8a2f1a5ca163681db2e9746bb4534608d9 100644 (file)
@@ -83,22 +83,20 @@ function __prompt_command()
 
   # check if inside git repo
   local git_status="`git status -unormal 2>&1`"    
-  if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then
+  if git rev-parse --git-dir > /dev/null 2>&1; then
+    git_status=$(git status --porcelain)
     # parse the porcelain output of git status
-    if [[ "$git_status" =~ nothing\ to\ commit ]]; then
+    if [[ ! $git_status = *[![:space:]]* ]]; then
       local Color_On=$GREEN
-    elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then
+    elif [[ $git_status =~ ^\?\? ]]; then
       local Color_On=$CYAN
     else
       local Color_On=$RED
     fi
 
-    if [[ "$git_status" =~ On\ branch\ ([^[:space:]]+) ]]; then
-      branch=${BASH_REMATCH[1]}
-    else
-      # Detached HEAD. (branch=HEAD is a faster alternative.)
-      branch="(`git describe --all --contains --abbrev=4 HEAD 2> /dev/null || echo HEAD`)"
-    fi
+    branch=$(git symbolic-ref -q HEAD)
+    branch=${branch##refs/heads/}
+    branch=${branch:-HEAD}
 
     # add the result to prompt
     PS1+="\n\[$Color_On\][$branch]\[$RESET\]\n"