Skip to content

Commit a1c94fc

Browse files
committed
Merge pull request magicmonty#189 from ogr3/display-upstream-branch
Enhancement: Display upstream branch
2 parents d494105 + 1075149 commit a1c94fc

File tree

5 files changed

+83
-42
lines changed

5 files changed

+83
-42
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ The prompt may look like the following:
6060

6161
By default, the general appearance of the prompt is::
6262

63-
(<branch> <branch tracking>|<local status>)
63+
(<branch> <upstream branch> <branch tracking>|<local status>)
6464

6565
The symbols are as follows:
6666

@@ -71,6 +71,10 @@ The symbols are as follows:
7171
- ``✚n``: there are ``n`` changed but *unstaged* files
7272
- ``…n``: there are ``n`` untracked files
7373
- ``⚑n``: there are ``n`` stash entries
74+
- Upstream branch
75+
- Shows the remote tracking branch
76+
- Disabled by default
77+
- Enable by setting GIT_PROMPT_SHOW_UPSTREAM=1
7478
- Branch Tracking Symbols
7579
- ``↑n``: ahead of remote by ``n`` commits
7680
- ``↓n``: behind remote by ``n`` commits
@@ -119,6 +123,8 @@ git clone https://github.com/magicmonty/bash-git-prompt.git .bash-git-prompt
119123

120124
# GIT_PROMPT_FETCH_REMOTE_STATUS=0 # uncomment to avoid fetching remote status
121125

126+
# GIT_PROMPT_SHOW_UPSTREAM=1 # uncomment to show upstream tracking branch
127+
122128
# GIT_PROMPT_START=... # uncomment for custom prompt start sequence
123129
# GIT_PROMPT_END=... # uncomment for custom prompt end sequence
124130

gitprompt.sh

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -434,23 +434,33 @@ function updatePrompt() {
434434
git_prompt_config
435435

436436
export __GIT_PROMPT_IGNORE_STASH=${GIT_PROMPT_IGNORE_STASH}
437-
local -a GitStatus
438-
GitStatus=($("$__GIT_STATUS_CMD" 2>/dev/null))
437+
export __GIT_PROMPT_SHOW_UPSTREAM=${GIT_PROMPT_SHOW_UPSTREAM}
439438

440-
local GIT_BRANCH=$(replaceSymbols ${GitStatus[0]})
441-
local GIT_REMOTE="$(replaceSymbols ${GitStatus[1]})"
439+
local -a git_status_fields
440+
git_status_fields=($("$__GIT_STATUS_CMD" 2>/dev/null))
441+
442+
local GIT_BRANCH=$(replaceSymbols ${git_status_fields[0]})
443+
local GIT_REMOTE="$(replaceSymbols ${git_status_fields[1]})"
442444
if [[ "." == "$GIT_REMOTE" ]]; then
443445
unset GIT_REMOTE
444446
fi
445-
local GIT_STAGED=${GitStatus[2]}
446-
local GIT_CONFLICTS=${GitStatus[3]}
447-
local GIT_CHANGED=${GitStatus[4]}
448-
local GIT_UNTRACKED=${GitStatus[5]}
449-
local GIT_STASHED=${GitStatus[6]}
450-
local GIT_CLEAN=${GitStatus[7]}
447+
448+
local GIT_UPSTREAM="${git_status_fields[2]}"
449+
if [[ -z "${__GIT_PROMPT_SHOW_UPSTREAM}" || "^" == "$GIT_UPSTREAM" ]]; then
450+
unset GIT_UPSTREAM
451+
else
452+
GIT_UPSTREAM="${GIT_PROMPT_UPSTREAM//_UPSTREAM_/${GIT_UPSTREAM}}"
453+
fi
454+
455+
local GIT_STAGED=${git_status_fields[3]}
456+
local GIT_CONFLICTS=${git_status_fields[4]}
457+
local GIT_CHANGED=${git_status_fields[5]}
458+
local GIT_UNTRACKED=${git_status_fields[6]}
459+
local GIT_STASHED=${git_status_fields[7]}
460+
local GIT_CLEAN=${git_status_fields[8]}
451461

452462
local NEW_PROMPT="$EMPTY_PROMPT"
453-
if [[ -n "$GitStatus" ]]; then
463+
if [[ -n "$git_status_fields" ]]; then
454464
local STATUS="${PROMPT_LEADING_SPACE}${GIT_PROMPT_PREFIX}${GIT_PROMPT_BRANCH}${GIT_BRANCH}${ResetColor}"
455465

456466
# __add_status KIND VALEXPR INSERT
@@ -481,6 +491,7 @@ function updatePrompt() {
481491
eval "STATUS=\"$STATUS$1\""
482492
}
483493

494+
__add_status '$GIT_UPSTREAM'
484495
__chk_gitvar_status 'REMOTE' '-n'
485496
__add_status "$GIT_PROMPT_SEPARATOR"
486497
__chk_gitvar_status 'STAGED' '-ne 0'
@@ -522,7 +533,8 @@ function is_function {
522533
}
523534
#Helper function that truncates $PWD depending on window width
524535
function gp_truncate_pwd {
525-
local newPWD="${PWD/#$HOME/~}"
536+
local tilde="~"
537+
local newPWD="${PWD/#${HOME}/${tilde}}"
526538
local pwdmaxlen=$((${COLUMNS:-80}/3))
527539
[ ${#newPWD} -gt $pwdmaxlen ] && newPWD="...${newPWD:3-$pwdmaxlen}"
528540
echo -n "$newPWD"

gitstatus.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ fi
5353
IFS="^" read -ra branch_fields <<< "${branch_line/\#\# }"
5454
branch="${branch_fields[0]}"
5555
remote=
56+
upstream=
5657

5758
if [[ "$branch" == *"Initial commit on"* ]]; then
5859
IFS=" " read -ra fields <<< "$branch"
@@ -70,6 +71,7 @@ else
7071
remote="_NO_REMOTE_TRACKING_"
7172
else
7273
IFS="[,]" read -ra remote_fields <<< "${branch_fields[1]}"
74+
upstream="${remote_fields[0]}"
7375
for remote_field in "${remote_fields[@]}"; do
7476
if [[ "$remote_field" == *ahead* ]]; then
7577
num_ahead=${remote_field:6}
@@ -88,9 +90,14 @@ if [[ -z "$remote" ]] ; then
8890
remote='.'
8991
fi
9092

91-
printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \
93+
if [[ -z "$upstream" ]] ; then
94+
upstream='^'
95+
fi
96+
97+
printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \
9298
"$branch" \
9399
"$remote" \
100+
"$upstream" \
94101
$num_staged \
95102
$num_conflicts \
96103
$num_changed \

gitstatus_pre-1.7.10.sh

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,27 @@ if [ -z "${__GIT_PROMPT_DIR}" ]; then
1919
__GIT_PROMPT_DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
2020
fi
2121

22-
gitsym=`git symbolic-ref HEAD`
22+
branch=$( git rev-parse --abbrev-ref HEAD )
2323

24-
# if "fatal: Not a git repo .., then exit
25-
case "$gitsym" in fatal*) exit 0 ;; esac
24+
#If error code we are not in a repo - exit
25+
if [[ $? != 0 ]]; then exit 0; fi
2626

27-
# the current branch is the tail end of the symbolic reference
28-
branch="${gitsym##refs/heads/}" # get the basename after "refs/heads/"
27+
#If we are detached, branch name will be HEAD
28+
if [[ "$branch" == "HEAD" ]]; then
29+
unset branch
30+
fi
2931

30-
gitstatus=`git diff --name-status 2>&1`
32+
gitstatus=$( git diff --name-status 2>&1 )
3133

3234
# if the diff is fatal, exit now
33-
case "$gitstatus" in fatal*) exit 0 ;; esac
34-
35+
if [[ $? != 0 ]]; then exit 0; fi
3536

36-
staged_files=`git diff --staged --name-status`
37+
staged_files=$( git diff --staged --name-status )
3738

38-
num_changed=$(( `all_lines "$gitstatus"` - `count_lines "$gitstatus" U` ))
39-
num_conflicts=`count_lines "$staged_files" U`
40-
num_staged=$(( `all_lines "$staged_files"` - num_conflicts ))
41-
num_untracked=`git ls-files --others --exclude-standard $(git rev-parse --show-cdup) | wc -l`
39+
num_changed=$(( $( all_lines "$gitstatus" ) - $( count_lines "$gitstatus" U ) ))
40+
num_conflicts=$( count_lines "$staged_files" U )
41+
num_staged=$(( $( all_lines "$staged_files" ) - num_conflicts ))
42+
num_untracked=$( git ls-files --others --exclude-standard $(git rev-parse --show-cdup) | wc -l )
4243

4344
num_stashed=0
4445
if [[ "$__GIT_PROMPT_IGNORE_STASH" != "1" ]]; then
@@ -56,19 +57,20 @@ if (( num_changed == 0 && num_staged == 0 && num_U == 0 && num_untracked == 0 &&
5657
fi
5758

5859
remote=
60+
upstream=
5961

6062
if [[ -z "$branch" ]]; then
61-
tag=`git describe --exact-match`
63+
tag=$( git describe --exact-match 2>/dev/null )
6264
if [[ -n "$tag" ]]; then
6365
branch="$tag"
6466
else
65-
branch="_PREHASH_`git rev-parse --short HEAD`"
67+
branch="_PREHASH_$( git rev-parse --short HEAD )"
6668
fi
6769
else
68-
remote_name=`git config branch.${branch}.remote`
70+
remote_name=$( git config branch.${branch}.remote )
6971

7072
if [[ -n "$remote_name" ]]; then
71-
merge_name=`git config branch.${branch}.merge`
73+
merge_name=$( git config branch.${branch}.merge )
7274
else
7375
remote_name='origin'
7476
merge_name="refs/heads/${branch}"
@@ -81,24 +83,27 @@ else
8183
fi
8284

8385
# detect if the local branch have a remote tracking branch
84-
cmd_output=$(git rev-parse --abbrev-ref ${branch}@{upstream} 2>&1 >/dev/null)
86+
upstream=$( git rev-parse --abbrev-ref ${branch}@{upstream} 2>&1 )
8587

8688
if [[ $? == 0 ]]; then
8789
has_remote_tracking=1
8890
else
8991
has_remote_tracking=0
92+
unset upstream
9093
fi
9194

9295
# get the revision list, and count the leading "<" and ">"
93-
revgit=`git rev-list --left-right ${remote_ref}...HEAD`
94-
num_revs=`all_lines "$revgit"`
95-
num_ahead=`count_lines "$revgit" "^>"`
96-
num_behind=$(( num_revs - num_ahead ))
97-
if (( num_behind > 0 )) ; then
98-
remote="${remote}_BEHIND_${num_behind}"
99-
fi
100-
if (( num_ahead > 0 )) ; then
101-
remote="${remote}_AHEAD_${num_ahead}"
96+
revgit=$( git rev-list --left-right ${remote_ref}...HEAD 2>/dev/null )
97+
if [[ $? == 0 ]]; then
98+
num_revs=$( all_lines "$revgit" )
99+
num_ahead=$( count_lines "$revgit" "^>" )
100+
num_behind=$(( num_revs - num_ahead ))
101+
if (( num_behind > 0 )) ; then
102+
remote="${remote}_BEHIND_${num_behind}"
103+
fi
104+
if (( num_ahead > 0 )) ; then
105+
remote="${remote}_AHEAD_${num_ahead}"
106+
fi
102107
fi
103108
fi
104109

@@ -110,9 +115,14 @@ if [[ "$has_remote_tracking" == "0" ]] ; then
110115
remote='_NO_REMOTE_TRACKING_'
111116
fi
112117

113-
printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \
118+
if [[ -z "$upstream" ]] ; then
119+
upstream='^'
120+
fi
121+
122+
printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \
114123
"$branch" \
115124
"$remote" \
125+
"$upstream" \
116126
$num_staged \
117127
$num_conflicts \
118128
$num_changed \

themes/Default.bgptheme

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ unset_git_prompt_colors() {
1818
unset GIT_PROMPT_COMMAND_FAIL
1919
unset GIT_PROMPT_STATUS_COMMAND
2020
unset GIT_PROMPT_VIRTUALENV
21+
unset GIT_PROMPT_UPSTREAM
2122
unset GIT_PROMPT_START_USER
2223
unset GIT_PROMPT_START_ROOT
2324
unset GIT_PROMPT_END_USER
@@ -73,6 +74,11 @@ define_undefined_git_prompt_colors() {
7374
# the name of the current virtual environment (currently CONDA and VIRTUAL_ENV)
7475
if [[ -z ${GIT_PROMPT_VIRTUALENV} ]]; then GIT_PROMPT_VIRTUALENV="(${Blue}_VIRTUALENV_${ResetColor}) "; fi
7576

77+
# template for displaying the current remote tracking branch
78+
# use the placeholder _UPSTREAM_ will be replaced with
79+
# the name of the current remote tracking branch
80+
if [[ -z ${GIT_PROMPT_UPSTREAM} ]]; then GIT_PROMPT_UPSTREAM=" {${Blue}_UPSTREAM_${ResetColor}}"; fi
81+
7682
# _LAST_COMMAND_INDICATOR_ will be replaced by the appropriate GIT_PROMPT_COMMAND_OK OR GIT_PROMPT_COMMAND_FAIL
7783
if [[ -z ${GIT_PROMPT_START_USER} ]]; then GIT_PROMPT_START_USER="_LAST_COMMAND_INDICATOR_ ${Yellow}${PathShort}${ResetColor}"; fi
7884
if [[ -z ${GIT_PROMPT_START_ROOT} ]]; then GIT_PROMPT_START_ROOT="${GIT_PROMPT_START_USER}"; fi

0 commit comments

Comments
 (0)