Permalink
Browse files

Merge pull request #30 from lmars/add-upstream-state

Add upstream state to PS1
  • Loading branch information...
2 parents 023079f + 5d110b6 commit 467c388fe96b8f25f95ce0b6d7b15d2f15e01636 @rtomayko committed Apr 17, 2013
Showing with 25 additions and 1 deletion.
  1. +25 −1 git-sh.bash
View
@@ -177,7 +177,7 @@ _git_import_aliases () {
# PROMPT =======================================================================
-PS1='`_git_headname`!`_git_repo_state``_git_workdir``_git_dirty``_git_dirty_stash`> '
+PS1='`_git_headname``_git_upstream_state`!`_git_repo_state``_git_workdir``_git_dirty``_git_dirty_stash`> '
ANSI_RESET="\001$(git config --get-color "" "reset")\002"
@@ -215,6 +215,30 @@ _git_headname() {
_git_apply_color "$br" "color.sh.branch" "yellow reverse"
}
+# detect the deviation from the upstream branch
+_git_upstream_state() {
+ local p=""
+
+ # Find how many commits we are ahead/behind our upstream
+ local count="$(git rev-list --count --left-right "@{upstream}"...HEAD 2>/dev/null)"
+
+ # calculate the result
+ case "$count" in
+ "") # no upstream
+ p="" ;;
+ "0 0") # equal to upstream
+ p=" u=" ;;
+ "0 "*) # ahead of upstream
+ p=" u+${count#0 }" ;;
+ *" 0") # behind upstream
+ p=" u-${count% 0}" ;;
+ *) # diverged from upstream
+ p=" u+${count#* }-${count% *}" ;;
+ esac
+
+ _git_apply_color "$p" "color.sh.upstream-state" "yellow bold"
+}
+
# detect working directory relative to working tree root
_git_workdir() {
subdir=`git rev-parse --show-prefix 2>/dev/null`

0 comments on commit 467c388

Please sign in to comment.