git-prompt.sh: optionally show upstream branch name

When working with multiple remotes, it is common to switch the upstream from a remote to another. Doing so, the prompt may not be the expected one. Providing an option to display tracking information sounds useful. Add a "name" option to GIT_PS1_SHOWUPSTREAM which will show the upstream abbrev name. This option is ignored if "verbose" is false. Signed-off-by: Julien Carsique <julien.carsique@gmail.com> Improved-by: SZEDER Gábor <szeder@ira.uka.de> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

Julien Carsique committed Oct 10, 2013 at 16:40 UTC 1f6806cf2da84e30d162ca3aee15d16db4f1daef
1 file changed +6 -1
contrib/completion/git-prompt.sh
+6 -1
@@ -60,6 +60,7 @@
60 # of values:
61 #
62 # verbose show number of commits ahead/behind (+/-) upstream
63 +# name if verbose, then also show the upstream abbrev name
64 # legacy don't use the '--count' option available in recent
65 # versions of git-rev-list
66 # git always compare HEAD to @{upstream}
@@ -94,7 +95,7 @@ __git_ps1_show_upstream ()
95 {
96 local key value
97 local svn_remote svn_url_pattern count n
97 - local upstream=git legacy="" verbose=""
98 + local upstream=git legacy="" verbose="" name=""
99
100 svn_remote=()
101 # get some config options from git-config
@@ -122,6 +123,7 @@ __git_ps1_show_upstream ()
123 git|svn) upstream="$option" ;;
124 verbose) verbose=1 ;;
125 legacy) legacy=1 ;;
126 + name) name=1 ;;
127 esac
128 done
129
@@ -204,6 +206,9 @@ __git_ps1_show_upstream ()
206 *) # diverged from upstream
207 p=" u+${count#* }-${count% *}" ;;
208 esac
209 + if [[ -n "$count" && -n "$name" ]]; then
210 + p="$p $(git rev-parse --abbrev-ref "$upstream" 2>/dev/null)"
211 + fi
212 fi
213
214 }