]> git.rmz.io Git - dotfiles.git/commitdiff
zsh/prompt: use porcelain=v2 for getting git status info
authorSamir Benmendil <me@rmz.io>
Sun, 31 Jan 2021 16:13:28 +0000 (16:13 +0000)
committerSamir Benmendil <me@rmz.io>
Sun, 31 Jan 2021 16:13:28 +0000 (16:13 +0000)
There's a bit more info in there and it's better structured.

zsh/lib/prompt.zsh

index aaa269426a3e0431fd7e13e154d349775bfc8242..92b63a046fc00740544fad22adeec9697d74e50c 100644 (file)
@@ -37,45 +37,40 @@ git_prompt_status() {
   if ! git rev-parse --is-inside-work-tree &>/dev/null; then
     return
   fi
-  local branch ahead behind 
+  local oid head ahead behind
   local added deleted modified renamed unmerged untracked dirty
   # Use porcelain status for easy parsing.
-  local status_cmd="git status --porcelain -b"
+  local status_cmd="git status --porcelain=v2 -b"
 
   # Get current status.
   while IFS=$'\n' read line; do
-    if [[ "$line" == \#\#\ * ]]; then
-      [[ "$line" =~ '## (.*?)(\.\.\.|$)' ]] && branch=$match[1]
-      [[ "$line" =~ 'ahead ([0-9]+)'  ]]  && ahead=$match[1]
-      [[ "$line" =~ 'behind ([0-9]+)'  ]] && behind=$match[1]
+    if [[ "$line" == \#\ * ]]; then
+      [[ "$line" =~ '# branch.oid ([0-9a-f]+)' ]]         && oid=$match[1]
+      [[ "$line" =~ '# branch.head (.*)' ]]               && head=$match[1]
+      [[ "$line" =~ '# branch.ab \+([0-9]+) -([0-9]+)' ]] && ahead=$match[1] && behind=$match[2]
     else
       # Count added, deleted, modified, renamed, unmerged, untracked, dirty.
       # T (type change) is undocumented, see http://git.io/FnpMGw.
       # index
-      [[ "$line" == M[\ MTD]\ * ]]      && (( updated++ ))
-      [[ "$line" == [AC][\ MTD]\ * ]]   && (( added++ ))
-      [[ "$line" == D[\ MT]\ * ]]       && (( deleted++ ))
-      [[ "$line" == R[\ MTD]\ * ]]      && (( renamed++ ))
+      [[ "$line" == 1\ M[.MTD]\ * ]]      && (( updated++ ))
+      [[ "$line" == 1\ [AC][.MTD]\ * ]]   && (( added++ ))
+      [[ "$line" == 1\ D[.MT]\ * ]]       && (( deleted++ ))
+      [[ "$line" == 2\ R[.MTD]\ * ]]      && (( renamed++ ))
 
       # work tree
-      [[ "$line" == [\ MARCT]M\ * ]]    && (( modified++ ))
-      [[ "$line" == [\ MARCT]D\ * ]]    && (( deleted_wt++ ))
-      [[ "$line" == \?\?\ * ]]          && (( untracked++ ))
+      [[ "$line" == 1\ [.MARCT]M\ * ]]    && (( modified++ ))
+      [[ "$line" == 1\ [.MARCT]D\ * ]]    && (( deleted_wt++ ))
+      [[ "$line" == \?\ * ]]              && (( untracked++ ))
 
-      # morge conflicts
-      [[ "$line" == (AA|DD|U?|?U)\ * ]] && (( unmerged++ ))
+      # merge conflicts
+      [[ "$line" == u\ (AA|DD|U?|?U)\ * ]] && (( unmerged++ ))
     fi
   done < <(${(z)status_cmd} 2> /dev/null)
 
-  local git_status=" %{$fg[yellow]%}"
+  local git_status=" %{$fg_bold[green]%}"
 
-  # Format branch
-  if [[ -n $branch ]]; then
-    git_status+="$branch"
-  else
-    git_status+="$(git rev-parse --short HEAD 2> /dev/null)"
-    [[ $? -ne 0 ]] && return
-  fi
+  # Format branch and commit
+  git_status+="$head%{$fg_no_bold[yellow]%}(${oid[1,8]})"
 
   # Format upstream
   local upstream_str