Created
November 7, 2011 16:40
-
-
Save tmhedberg/1345463 to your computer and use it in GitHub Desktop.
bash function to provide improved Git status indicator in primary prompt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| parse_git_branch () { | |
| local stat | |
| if ! stat=`git status -sb 2>/dev/null` || [[ -n $NO_GIT_PROMPT ]]; then | |
| return | |
| fi | |
| local ref=`sed -nr '/^##/{s/^## (Initial commit on )?//;p}' <<<"$stat"` | |
| local dirty=` | |
| awk ' | |
| NR >= 2 { | |
| if ($1 ~ /^\?\?/) | |
| untrk="?" | |
| else if ($1 ~ /^(AA|DD)/ || $1 ~ /^.?U/) | |
| cf = "!" | |
| else { | |
| fs = substr($0, 0, 2) | |
| if (fs ~ /^.[^ ]/) | |
| wd = "*" | |
| if (fs ~ /^[^ ]/) | |
| idx = "^" | |
| } | |
| } | |
| END { print wd idx untrk cf } | |
| ' <<<"$stat" | |
| ` | |
| local stashed | |
| [[ -n `git stash list 2>/dev/null` ]] && | |
| stashed=$ | |
| local g | |
| [[ -d .git ]] && | |
| g=.git || | |
| g=`git rev-parse --git-dir 2>/dev/null` | |
| local svnrefspec svn ahead behind | |
| if svnrefspec=`git config --get svn-remote.svn.fetch`; then | |
| svn=-svn | |
| local revlist=`git rev-list --left-right HEAD...${svnrefspec##*:}` | |
| ahead=`grep -c '^<' <<<"$revlist"` | |
| [[ $ahead -eq 0 ]] && | |
| ahead= | |
| behind=`grep -c '^>' <<<"$revlist"` | |
| [[ $behind -eq 0 ]] && | |
| behind= | |
| else | |
| ahead=`sed -nr 's/.*\[ahead ([[:digit:]]+).*\]/\1/;Tq;p;:q' <<<"$ref"` | |
| behind=` | |
| sed -nr ' | |
| s/.*\[(ahead [[:digit:]]+, )?behind ([[:digit:]]+).*\]/\2/ | |
| Tq | |
| p | |
| :q | |
| ' <<<"$ref" | |
| ` | |
| fi | |
| [[ -n $ahead ]] && | |
| ahead=+$ahead | |
| [[ -n $behind ]] && | |
| behind=-$behind | |
| local flag | |
| if [[ -d "$g/rebase-merge" || -f "$g/rebase-apply/rebasing" ]]; then | |
| flag=R | |
| elif [[ -d "$g/rebase-apply" ]]; then | |
| flag=A | |
| elif [[ -f "$g/MERGE_HEAD" ]]; then | |
| flag=M | |
| elif [[ -f "$g/CHERRY_PICK_HEAD" ]]; then | |
| flag=C | |
| elif [[ -f "$g/BISECT_LOG" ]]; then | |
| flag=B | |
| fi | |
| [[ -n $flag ]] && | |
| flag=:$flag | |
| [[ $ref == *\(no\ branch\)* ]] && | |
| ref=\<`git rev-parse --short HEAD`:` | |
| git name-rev HEAD | | |
| sed 's/[^ ]* //' | |
| `\> || | |
| ref=${ref%%[ .]*} | |
| echo -n " (git$svn:$ref$ahead$behind$dirty$stashed$flag)" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment