Skip to content

Commit 2ea7e40

Browse files
committed
Merge branch 'jd/prompt-upstream-mark'
Tweaks in the command line prompt (in contrib/) code around its GIT_PS1_SHOWUPSTREAM feature. * jd/prompt-upstream-mark: git-prompt: put upstream comments together git-prompt: make long upstream state indicator consistent git-prompt: make upstream state indicator location consistent git-prompt: rename `upstream` to `upstream_type`
2 parents d723492 + 094b540 commit 2ea7e40

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

contrib/completion/git-prompt.sh

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
# git always compare HEAD to @{upstream}
6767
# svn always compare HEAD to your SVN upstream
6868
#
69+
# By default, __git_ps1 will compare HEAD to your SVN upstream if it can
70+
# find one, or @{upstream} otherwise. Once you have set
71+
# GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by
72+
# setting the bash.showUpstream config variable.
73+
#
6974
# You can change the separator between the branch name and the above
7075
# state symbols by setting GIT_PS1_STATESEPARATOR. The default separator
7176
# is SP.
@@ -79,11 +84,6 @@
7984
# single '?' character by setting GIT_PS1_COMPRESSSPARSESTATE, or omitted
8085
# by setting GIT_PS1_OMITSPARSESTATE.
8186
#
82-
# By default, __git_ps1 will compare HEAD to your SVN upstream if it can
83-
# find one, or @{upstream} otherwise. Once you have set
84-
# GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by
85-
# setting the bash.showUpstream config variable.
86-
#
8787
# If you would like to see more information about the identity of
8888
# commits checked out as a detached HEAD, set GIT_PS1_DESCRIBE_STYLE
8989
# to one of these values:
@@ -115,7 +115,7 @@ __git_ps1_show_upstream ()
115115
{
116116
local key value
117117
local svn_remote svn_url_pattern count n
118-
local upstream=git legacy="" verbose="" name=""
118+
local upstream_type=git legacy="" verbose="" name=""
119119

120120
svn_remote=()
121121
# get some config options from git-config
@@ -132,7 +132,7 @@ __git_ps1_show_upstream ()
132132
svn-remote.*.url)
133133
svn_remote[$((${#svn_remote[@]} + 1))]="$value"
134134
svn_url_pattern="$svn_url_pattern\\|$value"
135-
upstream=svn+git # default upstream is SVN if available, else git
135+
upstream_type=svn+git # default upstream type is SVN if available, else git
136136
;;
137137
esac
138138
done <<< "$output"
@@ -141,16 +141,16 @@ __git_ps1_show_upstream ()
141141
local option
142142
for option in ${GIT_PS1_SHOWUPSTREAM}; do
143143
case "$option" in
144-
git|svn) upstream="$option" ;;
144+
git|svn) upstream_type="$option" ;;
145145
verbose) verbose=1 ;;
146146
legacy) legacy=1 ;;
147147
name) name=1 ;;
148148
esac
149149
done
150150

151-
# Find our upstream
152-
case "$upstream" in
153-
git) upstream="@{upstream}" ;;
151+
# Find our upstream type
152+
case "$upstream_type" in
153+
git) upstream_type="@{upstream}" ;;
154154
svn*)
155155
# get the upstream from the "git-svn-id: ..." in a commit message
156156
# (git-svn uses essentially the same procedure internally)
@@ -167,24 +167,24 @@ __git_ps1_show_upstream ()
167167

168168
if [[ -z "$svn_upstream" ]]; then
169169
# default branch name for checkouts with no layout:
170-
upstream=${GIT_SVN_ID:-git-svn}
170+
upstream_type=${GIT_SVN_ID:-git-svn}
171171
else
172-
upstream=${svn_upstream#/}
172+
upstream_type=${svn_upstream#/}
173173
fi
174-
elif [[ "svn+git" = "$upstream" ]]; then
175-
upstream="@{upstream}"
174+
elif [[ "svn+git" = "$upstream_type" ]]; then
175+
upstream_type="@{upstream}"
176176
fi
177177
;;
178178
esac
179179

180180
# Find how many commits we are ahead/behind our upstream
181181
if [[ -z "$legacy" ]]; then
182182
count="$(git rev-list --count --left-right \
183-
"$upstream"...HEAD 2>/dev/null)"
183+
"$upstream_type"...HEAD 2>/dev/null)"
184184
else
185185
# produce equivalent output to --count for older versions of git
186186
local commits
187-
if commits="$(git rev-list --left-right "$upstream"...HEAD 2>/dev/null)"
187+
if commits="$(git rev-list --left-right "$upstream_type"...HEAD 2>/dev/null)"
188188
then
189189
local commit behind=0 ahead=0
190190
for commit in $commits
@@ -214,26 +214,26 @@ __git_ps1_show_upstream ()
214214
*) # diverged from upstream
215215
p="<>" ;;
216216
esac
217-
else
217+
else # verbose, set upstream instead of p
218218
case "$count" in
219219
"") # no upstream
220-
p="" ;;
220+
upstream="" ;;
221221
"0 0") # equal to upstream
222-
p=" u=" ;;
222+
upstream="|u=" ;;
223223
"0 "*) # ahead of upstream
224-
p=" u+${count#0 }" ;;
224+
upstream="|u+${count#0 }" ;;
225225
*" 0") # behind upstream
226-
p=" u-${count% 0}" ;;
226+
upstream="|u-${count% 0}" ;;
227227
*) # diverged from upstream
228-
p=" u+${count#* }-${count% *}" ;;
228+
upstream="|u+${count#* }-${count% *}" ;;
229229
esac
230230
if [[ -n "$count" && -n "$name" ]]; then
231231
__git_ps1_upstream_name=$(git rev-parse \
232-
--abbrev-ref "$upstream" 2>/dev/null)
232+
--abbrev-ref "$upstream_type" 2>/dev/null)
233233
if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then
234-
p="$p \${__git_ps1_upstream_name}"
234+
upstream="$upstream \${__git_ps1_upstream_name}"
235235
else
236-
p="$p ${__git_ps1_upstream_name}"
236+
upstream="$upstream ${__git_ps1_upstream_name}"
237237
# not needed anymore; keep user's
238238
# environment clean
239239
unset __git_ps1_upstream_name
@@ -512,7 +512,8 @@ __git_ps1 ()
512512
local u=""
513513
local h=""
514514
local c=""
515-
local p=""
515+
local p="" # short version of upstream state indicator
516+
local upstream="" # verbose version of upstream state indicator
516517

517518
if [ "true" = "$inside_gitdir" ]; then
518519
if [ "true" = "$bare_repo" ]; then
@@ -568,8 +569,8 @@ __git_ps1 ()
568569
b="\${__git_ps1_branch_name}"
569570
fi
570571

571-
local f="$h$w$i$s$u"
572-
local gitstring="$c$b${f:+$z$f}${sparse}$r$p"
572+
local f="$h$w$i$s$u$p"
573+
local gitstring="$c$b${f:+$z$f}${sparse}$r${upstream}"
573574

574575
if [ $pcmode = yes ]; then
575576
if [ "${__git_printf_supports_v-}" != yes ]; then

0 commit comments

Comments
 (0)