From 54543440f5d133d74712fc1eb35caec0b225e4bf Mon Sep 17 00:00:00 2001 From: oGre Date: Thu, 8 Oct 2015 15:39:07 +0200 Subject: [PATCH 1/9] Add upstream tracked branch to gitstatus.sh and possible to show in prompt --- gitprompt.sh | 34 ++++++++++++++++++++++------------ gitstatus.sh | 9 ++++++++- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index 5f3a4fe..b880e35 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -418,7 +418,7 @@ function replaceSymbols() { local VALUE=${1//_AHEAD_/${GIT_PROMPT_SYMBOLS_AHEAD}} local VALUE1=${VALUE//_BEHIND_/${GIT_PROMPT_SYMBOLS_BEHIND}} - local VALUE2=${VALUE1//_NO_REMOTE_TRACKING_/${GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING}} + local VALUE2=${VALUE1//_NO_REMOTE_TRACKING_/"{${GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING}}"} echo ${VALUE2//_PREHASH_/${GIT_PROMPT_SYMBOLS_PREHASH}} } @@ -434,23 +434,32 @@ function updatePrompt() { git_prompt_config export __GIT_PROMPT_IGNORE_STASH=${GIT_PROMPT_IGNORE_STASH} - local -a GitStatus - GitStatus=($("$__GIT_STATUS_CMD" 2>/dev/null)) + export __GIT_PROMPT_IGNORE_UPSTREAM=${GIT_PROMPT_IGNORE_UPSTREAM} + local -a git_status_fields + git_status_fields=($("$__GIT_STATUS_CMD" 2>/dev/null)) - local GIT_BRANCH=$(replaceSymbols ${GitStatus[0]}) - local GIT_REMOTE="$(replaceSymbols ${GitStatus[1]})" + local GIT_BRANCH=$(replaceSymbols ${git_status_fields[0]}) + local GIT_REMOTE="$(replaceSymbols ${git_status_fields[1]})" if [[ "." == "$GIT_REMOTE" ]]; then unset GIT_REMOTE fi - local GIT_STAGED=${GitStatus[2]} - local GIT_CONFLICTS=${GitStatus[3]} - local GIT_CHANGED=${GitStatus[4]} - local GIT_UNTRACKED=${GitStatus[5]} - local GIT_STASHED=${GitStatus[6]} - local GIT_CLEAN=${GitStatus[7]} + + local GIT_UPSTREAM="${git_status_fields[2]}" + if [[ -n "${__GIT_PROMPT_IGNORE_UPSTREAM}" || "^" == "$GIT_UPSTREAM" ]]; then + unset GIT_UPSTREAM + else + GIT_UPSTREAM=" ${Blue}{$GIT_UPSTREAM}${ResetColor}" + fi + + local GIT_STAGED=${git_status_fields[3]} + local GIT_CONFLICTS=${git_status_fields[4]} + local GIT_CHANGED=${git_status_fields[5]} + local GIT_UNTRACKED=${git_status_fields[6]} + local GIT_STASHED=${git_status_fields[7]} + local GIT_CLEAN=${git_status_fields[8]} local NEW_PROMPT="$EMPTY_PROMPT" - if [[ -n "$GitStatus" ]]; then + if [[ -n "$git_status_fields" ]]; then local STATUS="${PROMPT_LEADING_SPACE}${GIT_PROMPT_PREFIX}${GIT_PROMPT_BRANCH}${GIT_BRANCH}${ResetColor}" # __add_status KIND VALEXPR INSERT @@ -481,6 +490,7 @@ function updatePrompt() { eval "STATUS=\"$STATUS$1\"" } + __chk_gitvar_status 'UPSTREAM' '-n' __chk_gitvar_status 'REMOTE' '-n' __add_status "$GIT_PROMPT_SEPARATOR" __chk_gitvar_status 'STAGED' '-ne 0' diff --git a/gitstatus.sh b/gitstatus.sh index 9f345e5..6fe1c41 100755 --- a/gitstatus.sh +++ b/gitstatus.sh @@ -53,6 +53,7 @@ fi IFS="^" read -ra branch_fields <<< "${branch_line/\#\# }" branch="${branch_fields[0]}" remote= +upstream= if [[ "$branch" == *"Initial commit on"* ]]; then IFS=" " read -ra fields <<< "$branch" @@ -70,6 +71,7 @@ else remote="_NO_REMOTE_TRACKING_" else IFS="[,]" read -ra remote_fields <<< "${branch_fields[1]}" + upstream="${remote_fields[0]}" for remote_field in "${remote_fields[@]}"; do if [[ "$remote_field" == *ahead* ]]; then num_ahead=${remote_field:6} @@ -88,9 +90,14 @@ if [[ -z "$remote" ]] ; then remote='.' fi -printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \ +if [[ -z "$upstream" ]] ; then + upstream='^' +fi + +printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \ "$branch" \ "$remote" \ + "$upstream" \ $num_staged \ $num_conflicts \ $num_changed \ From baa54d3673bba5582ce4436697223df9771493e1 Mon Sep 17 00:00:00 2001 From: oGre Date: Thu, 8 Oct 2015 17:30:43 +0200 Subject: [PATCH 2/9] Added config variable analogue to GIT_PROMPT_VIRTUALENV --- gitprompt.sh | 10 +++++----- themes/Default.bgptheme | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index b880e35..d76987b 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -418,7 +418,7 @@ function replaceSymbols() { local VALUE=${1//_AHEAD_/${GIT_PROMPT_SYMBOLS_AHEAD}} local VALUE1=${VALUE//_BEHIND_/${GIT_PROMPT_SYMBOLS_BEHIND}} - local VALUE2=${VALUE1//_NO_REMOTE_TRACKING_/"{${GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING}}"} + local VALUE2=${VALUE1//_NO_REMOTE_TRACKING_/${GIT_PROMPT_SYMBOLS_NO_REMOTE_TRACKING}} echo ${VALUE2//_PREHASH_/${GIT_PROMPT_SYMBOLS_PREHASH}} } @@ -434,7 +434,7 @@ function updatePrompt() { git_prompt_config export __GIT_PROMPT_IGNORE_STASH=${GIT_PROMPT_IGNORE_STASH} - export __GIT_PROMPT_IGNORE_UPSTREAM=${GIT_PROMPT_IGNORE_UPSTREAM} + export __GIT_PROMPT_SHOW_UPSTREAM=${GIT_PROMPT_SHOW_UPSTREAM} local -a git_status_fields git_status_fields=($("$__GIT_STATUS_CMD" 2>/dev/null)) @@ -445,10 +445,10 @@ function updatePrompt() { fi local GIT_UPSTREAM="${git_status_fields[2]}" - if [[ -n "${__GIT_PROMPT_IGNORE_UPSTREAM}" || "^" == "$GIT_UPSTREAM" ]]; then + if [[ -z "${__GIT_PROMPT_SHOW_UPSTREAM}" || "^" == "$GIT_UPSTREAM" ]]; then unset GIT_UPSTREAM else - GIT_UPSTREAM=" ${Blue}{$GIT_UPSTREAM}${ResetColor}" + GIT_UPSTREAM="${GIT_PROMPT_UPSTREAM//_UPSTREAM_/${GIT_UPSTREAM}}" fi local GIT_STAGED=${git_status_fields[3]} @@ -490,7 +490,7 @@ function updatePrompt() { eval "STATUS=\"$STATUS$1\"" } - __chk_gitvar_status 'UPSTREAM' '-n' + __add_status '$GIT_UPSTREAM' __chk_gitvar_status 'REMOTE' '-n' __add_status "$GIT_PROMPT_SEPARATOR" __chk_gitvar_status 'STAGED' '-ne 0' diff --git a/themes/Default.bgptheme b/themes/Default.bgptheme index b37b0e3..4e8e6b1 100644 --- a/themes/Default.bgptheme +++ b/themes/Default.bgptheme @@ -64,6 +64,8 @@ define_undefined_git_prompt_colors() { # use the placeholder _VIRTUALENV_ will be replaced with # the name of the current virtual environment (currently CONDA and VIRTUAL_ENV) if [[ -z ${GIT_PROMPT_VIRTUALENV} ]]; then GIT_PROMPT_VIRTUALENV="(${Blue}_VIRTUALENV_${ResetColor}) "; fi + + if [[ -z ${GIT_PROMPT_UPSTREAM} ]]; then GIT_PROMPT_UPSTREAM=" {${Blue}_UPSTREAM_${ResetColor}}"; fi # _LAST_COMMAND_INDICATOR_ will be replaced by the appropriate GIT_PROMPT_COMMAND_OK OR GIT_PROMPT_COMMAND_FAIL if [[ -z ${GIT_PROMPT_START_USER} ]]; then GIT_PROMPT_START_USER="_LAST_COMMAND_INDICATOR_ ${Yellow}${PathShort}${ResetColor}"; fi From 87ecc3cae9d99a74486dfdc59b3aca940d523396 Mon Sep 17 00:00:00 2001 From: oGre Date: Thu, 8 Oct 2015 17:32:33 +0200 Subject: [PATCH 3/9] Documentation --- themes/Default.bgptheme | 3 +++ 1 file changed, 3 insertions(+) diff --git a/themes/Default.bgptheme b/themes/Default.bgptheme index 4e8e6b1..8ed02fa 100644 --- a/themes/Default.bgptheme +++ b/themes/Default.bgptheme @@ -65,6 +65,9 @@ define_undefined_git_prompt_colors() { # the name of the current virtual environment (currently CONDA and VIRTUAL_ENV) if [[ -z ${GIT_PROMPT_VIRTUALENV} ]]; then GIT_PROMPT_VIRTUALENV="(${Blue}_VIRTUALENV_${ResetColor}) "; fi + # template for displaying the current remote tracking branch + # use the placeholder _UPSTREAM_ will be replaced with + # the name of the current remote tracking branch if [[ -z ${GIT_PROMPT_UPSTREAM} ]]; then GIT_PROMPT_UPSTREAM=" {${Blue}_UPSTREAM_${ResetColor}}"; fi # _LAST_COMMAND_INDICATOR_ will be replaced by the appropriate GIT_PROMPT_COMMAND_OK OR GIT_PROMPT_COMMAND_FAIL From 41aa8fe8257feef2afc5690eed3f83d44963f326 Mon Sep 17 00:00:00 2001 From: oGre Date: Thu, 8 Oct 2015 20:31:52 +0200 Subject: [PATCH 4/9] Unset the new variable. Documentation added in README.md Removed unexpected token warning. --- README.md | 8 +++++++- gitprompt.sh | 4 +++- themes/Default.bgptheme | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 278de6f..fba7bf1 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ The prompt may look like the following: By default, the general appearance of the prompt is:: - ( |) + ( |) The symbols are as follows: @@ -71,6 +71,10 @@ The symbols are as follows: - ``✚n``: there are ``n`` changed but *unstaged* files - ``…n``: there are ``n`` untracked files - ``⚑n``: there are ``n`` stash entries +- Upstream branch + - Shows the remote tracking branch + - Disabled by default + - Enable by setting GIT_PROMPT_SHOW_UPSTREAM=1 - Branch Tracking Symbols - ``↑n``: ahead of remote by ``n`` commits - ``↓n``: behind remote by ``n`` commits @@ -118,6 +122,8 @@ git clone https://github.com/magicmonty/bash-git-prompt.git .bash-git-prompt GIT_PROMPT_ONLY_IN_REPO=1 # GIT_PROMPT_FETCH_REMOTE_STATUS=0 # uncomment to avoid fetching remote status + + # GIT_PROMPT_SHOW_UPSTREAM=1 # uncomment to show upstream tracking branch # GIT_PROMPT_START=... # uncomment for custom prompt start sequence # GIT_PROMPT_END=... # uncomment for custom prompt end sequence diff --git a/gitprompt.sh b/gitprompt.sh index d76987b..4aad850 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -435,6 +435,7 @@ function updatePrompt() { export __GIT_PROMPT_IGNORE_STASH=${GIT_PROMPT_IGNORE_STASH} export __GIT_PROMPT_SHOW_UPSTREAM=${GIT_PROMPT_SHOW_UPSTREAM} + local -a git_status_fields git_status_fields=($("$__GIT_STATUS_CMD" 2>/dev/null)) @@ -533,7 +534,8 @@ function is_function { #Helper function that truncates $PWD depending on window width function gp_truncate_pwd { - local newPWD="${PWD/#$HOME/~}" + local tilde="~" + local newPWD="${PWD/#${HOME}/${tilde}}" local pwdmaxlen=$((${COLUMNS:-80}/3)) [ ${#newPWD} -gt $pwdmaxlen ] && newPWD="...${newPWD:3-$pwdmaxlen}" echo -n "$newPWD" diff --git a/themes/Default.bgptheme b/themes/Default.bgptheme index 8ed02fa..057038a 100644 --- a/themes/Default.bgptheme +++ b/themes/Default.bgptheme @@ -17,6 +17,7 @@ unset_git_prompt_colors() { unset GIT_PROMPT_COMMAND_OK unset GIT_PROMPT_COMMAND_FAIL unset GIT_PROMPT_VIRTUALENV + unset GIT_PROMPT_UPSTREAM unset GIT_PROMPT_START_USER unset GIT_PROMPT_START_ROOT unset GIT_PROMPT_END_USER From a266d2967d4534f3ce64eb3caef0183798df54fa Mon Sep 17 00:00:00 2001 From: oGre Date: Wed, 14 Oct 2015 08:18:30 +0200 Subject: [PATCH 5/9] Added support for showing upstream tracking branch in older gitstatus --- gitstatus_pre-1.7.10.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gitstatus_pre-1.7.10.sh b/gitstatus_pre-1.7.10.sh index f613709..ac6f241 100755 --- a/gitstatus_pre-1.7.10.sh +++ b/gitstatus_pre-1.7.10.sh @@ -56,6 +56,7 @@ if (( num_changed == 0 && num_staged == 0 && num_U == 0 && num_untracked == 0 && fi remote= +upstream= if [[ -z "$branch" ]]; then tag=`git describe --exact-match` @@ -81,12 +82,13 @@ else fi # detect if the local branch have a remote tracking branch - cmd_output=$(git rev-parse --abbrev-ref ${branch}@{upstream} 2>&1 >/dev/null) + upstream=$( git rev-parse --abbrev-ref ${branch}@{upstream} 2>&1 ) if [[ $? == 0 ]]; then has_remote_tracking=1 else has_remote_tracking=0 + unset upstream fi # get the revision list, and count the leading "<" and ">" @@ -110,9 +112,14 @@ if [[ "$has_remote_tracking" == "0" ]] ; then remote='_NO_REMOTE_TRACKING_' fi -printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \ +if [[ -z "$upstream" ]] ; then + remote='.' +fi + +printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \ "$branch" \ "$remote" \ + "$upstream" \ $num_staged \ $num_conflicts \ $num_changed \ From 4a841435022cf7455402b7e6a81d6d801e135093 Mon Sep 17 00:00:00 2001 From: oGre Date: Wed, 14 Oct 2015 08:34:02 +0200 Subject: [PATCH 6/9] Bugfix --- gitstatus_pre-1.7.10.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitstatus_pre-1.7.10.sh b/gitstatus_pre-1.7.10.sh index ac6f241..655c195 100755 --- a/gitstatus_pre-1.7.10.sh +++ b/gitstatus_pre-1.7.10.sh @@ -113,7 +113,7 @@ if [[ "$has_remote_tracking" == "0" ]] ; then fi if [[ -z "$upstream" ]] ; then - remote='.' + upstream='^' fi printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \ From 801d45605ba3fd5d240ffdfafbddccf01c7992ef Mon Sep 17 00:00:00 2001 From: oGre Date: Wed, 14 Oct 2015 08:56:03 +0200 Subject: [PATCH 7/9] Cleanup --- gitstatus_pre-1.7.10.sh | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/gitstatus_pre-1.7.10.sh b/gitstatus_pre-1.7.10.sh index 655c195..04a205b 100755 --- a/gitstatus_pre-1.7.10.sh +++ b/gitstatus_pre-1.7.10.sh @@ -19,7 +19,7 @@ if [ -z "${__GIT_PROMPT_DIR}" ]; then __GIT_PROMPT_DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )" fi -gitsym=`git symbolic-ref HEAD` +gitsym=$( git symbolic-ref HEAD ) # if "fatal: Not a git repo .., then exit case "$gitsym" in fatal*) exit 0 ;; esac @@ -59,17 +59,17 @@ remote= upstream= if [[ -z "$branch" ]]; then - tag=`git describe --exact-match` + tag=$( git describe --exact-match 2>/dev/null) if [[ -n "$tag" ]]; then branch="$tag" else - branch="_PREHASH_`git rev-parse --short HEAD`" + branch="_PREHASH_$( git rev-parse --short HEAD )" fi else - remote_name=`git config branch.${branch}.remote` + remote_name=$( git config branch.${branch}.remote ) if [[ -n "$remote_name" ]]; then - merge_name=`git config branch.${branch}.merge` + merge_name=$( git config branch.${branch}.merge ) else remote_name='origin' merge_name="refs/heads/${branch}" @@ -92,15 +92,17 @@ else fi # get the revision list, and count the leading "<" and ">" - revgit=`git rev-list --left-right ${remote_ref}...HEAD` - num_revs=`all_lines "$revgit"` - num_ahead=`count_lines "$revgit" "^>"` - num_behind=$(( num_revs - num_ahead )) - if (( num_behind > 0 )) ; then - remote="${remote}_BEHIND_${num_behind}" - fi - if (( num_ahead > 0 )) ; then - remote="${remote}_AHEAD_${num_ahead}" + revgit=$( git rev-list --left-right ${remote_ref}...HEAD 2>/dev/null ) + if [[ $? == 0 ]]; then + num_revs=$( all_lines "$revgit" ) + num_ahead=$( count_lines "$revgit" "^>" ) + num_behind=$(( num_revs - num_ahead )) + if (( num_behind > 0 )) ; then + remote="${remote}_BEHIND_${num_behind}" + fi + if (( num_ahead > 0 )) ; then + remote="${remote}_AHEAD_${num_ahead}" + fi fi fi From b84215df4f3d6193da46458848fe8bee5657d110 Mon Sep 17 00:00:00 2001 From: oGre Date: Wed, 14 Oct 2015 10:27:54 +0200 Subject: [PATCH 8/9] Fixed bug in determining branch when detached --- gitstatus_pre-1.7.10.sh | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/gitstatus_pre-1.7.10.sh b/gitstatus_pre-1.7.10.sh index 04a205b..9302324 100755 --- a/gitstatus_pre-1.7.10.sh +++ b/gitstatus_pre-1.7.10.sh @@ -19,26 +19,27 @@ if [ -z "${__GIT_PROMPT_DIR}" ]; then __GIT_PROMPT_DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )" fi -gitsym=$( git symbolic-ref HEAD ) +branch=$( git rev-parse --abbrev-ref HEAD ) -# if "fatal: Not a git repo .., then exit -case "$gitsym" in fatal*) exit 0 ;; esac +#If error code we are not in a repo - exit +if [[ $? != 0 ]]; then exit 0; fi -# the current branch is the tail end of the symbolic reference -branch="${gitsym##refs/heads/}" # get the basename after "refs/heads/" +#If we are detached, branch name will be HEAD +if [[ "$branch" == "HEAD" ]]; then + unset branch +fi -gitstatus=`git diff --name-status 2>&1` +gitstatus=$( git diff --name-status 2>&1 ) # if the diff is fatal, exit now -case "$gitstatus" in fatal*) exit 0 ;; esac - +if [[ $? != 0 ]]; then exit 0; fi -staged_files=`git diff --staged --name-status` +staged_files=$( git diff --staged --name-status ) num_changed=$(( `all_lines "$gitstatus"` - `count_lines "$gitstatus" U` )) num_conflicts=`count_lines "$staged_files" U` num_staged=$(( `all_lines "$staged_files"` - num_conflicts )) -num_untracked=`git ls-files --others --exclude-standard $(git rev-parse --show-cdup) | wc -l` +num_untracked=$( git ls-files --others --exclude-standard $(git rev-parse --show-cdup) | wc -l ) num_stashed=0 if [[ "$__GIT_PROMPT_IGNORE_STASH" != "1" ]]; then From 107514959d5091797e9cf666fb9217bc8df8a97c Mon Sep 17 00:00:00 2001 From: oGre Date: Wed, 14 Oct 2015 17:10:11 +0200 Subject: [PATCH 9/9] Replaced all remaining backticks --- gitstatus_pre-1.7.10.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gitstatus_pre-1.7.10.sh b/gitstatus_pre-1.7.10.sh index 9302324..1f60657 100755 --- a/gitstatus_pre-1.7.10.sh +++ b/gitstatus_pre-1.7.10.sh @@ -36,9 +36,9 @@ if [[ $? != 0 ]]; then exit 0; fi staged_files=$( git diff --staged --name-status ) -num_changed=$(( `all_lines "$gitstatus"` - `count_lines "$gitstatus" U` )) -num_conflicts=`count_lines "$staged_files" U` -num_staged=$(( `all_lines "$staged_files"` - num_conflicts )) +num_changed=$(( $( all_lines "$gitstatus" ) - $( count_lines "$gitstatus" U ) )) +num_conflicts=$( count_lines "$staged_files" U ) +num_staged=$(( $( all_lines "$staged_files" ) - num_conflicts )) num_untracked=$( git ls-files --others --exclude-standard $(git rev-parse --show-cdup) | wc -l ) num_stashed=0 @@ -60,7 +60,7 @@ remote= upstream= if [[ -z "$branch" ]]; then - tag=$( git describe --exact-match 2>/dev/null) + tag=$( git describe --exact-match 2>/dev/null ) if [[ -n "$tag" ]]; then branch="$tag" else