Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 467c388

Browse files
committed
Merge pull request #30 from lmars/add-upstream-state
Add upstream state to PS1
2 parents 023079f + 5d110b6 commit 467c388

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

git-sh.bash

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ _git_import_aliases () {
177177

178178
# PROMPT =======================================================================
179179

180-
PS1='`_git_headname`!`_git_repo_state``_git_workdir``_git_dirty``_git_dirty_stash`> '
180+
PS1='`_git_headname``_git_upstream_state`!`_git_repo_state``_git_workdir``_git_dirty``_git_dirty_stash`> '
181181

182182
ANSI_RESET="\001$(git config --get-color "" "reset")\002"
183183

@@ -215,6 +215,30 @@ _git_headname() {
215215
_git_apply_color "$br" "color.sh.branch" "yellow reverse"
216216
}
217217

218+
# detect the deviation from the upstream branch
219+
_git_upstream_state() {
220+
local p=""
221+
222+
# Find how many commits we are ahead/behind our upstream
223+
local count="$(git rev-list --count --left-right "@{upstream}"...HEAD 2>/dev/null)"
224+
225+
# calculate the result
226+
case "$count" in
227+
"") # no upstream
228+
p="" ;;
229+
"0 0") # equal to upstream
230+
p=" u=" ;;
231+
"0 "*) # ahead of upstream
232+
p=" u+${count#0 }" ;;
233+
*" 0") # behind upstream
234+
p=" u-${count% 0}" ;;
235+
*) # diverged from upstream
236+
p=" u+${count#* }-${count% *}" ;;
237+
esac
238+
239+
_git_apply_color "$p" "color.sh.upstream-state" "yellow bold"
240+
}
241+
218242
# detect working directory relative to working tree root
219243
_git_workdir() {
220244
subdir=`git rev-parse --show-prefix 2>/dev/null`

0 commit comments

Comments
 (0)