diff --git a/README.md b/README.md index 278de6f..9452119 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 @@ -119,6 +123,8 @@ git clone https://github.com/magicmonty/bash-git-prompt.git .bash-git-prompt # 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 3c06abf..df4f168 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -434,23 +434,33 @@ 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_SHOW_UPSTREAM=${GIT_PROMPT_SHOW_UPSTREAM} - local GIT_BRANCH=$(replaceSymbols ${GitStatus[0]}) - local GIT_REMOTE="$(replaceSymbols ${GitStatus[1]})" + local -a git_status_fields + git_status_fields=($("$__GIT_STATUS_CMD" 2>/dev/null)) + + 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 [[ -z "${__GIT_PROMPT_SHOW_UPSTREAM}" || "^" == "$GIT_UPSTREAM" ]]; then + unset GIT_UPSTREAM + else + GIT_UPSTREAM="${GIT_PROMPT_UPSTREAM//_UPSTREAM_/${GIT_UPSTREAM}}" + 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 +491,7 @@ function updatePrompt() { eval "STATUS=\"$STATUS$1\"" } + __add_status '$GIT_UPSTREAM' __chk_gitvar_status 'REMOTE' '-n' __add_status "$GIT_PROMPT_SEPARATOR" __chk_gitvar_status 'STAGED' '-ne 0' @@ -522,7 +533,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/gitstatus.sh b/gitstatus.sh index 95d312a..787775d 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 \ diff --git a/gitstatus_pre-1.7.10.sh b/gitstatus_pre-1.7.10.sh index f613709..1f60657 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_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 if [[ "$__GIT_PROMPT_IGNORE_STASH" != "1" ]]; then @@ -56,19 +57,20 @@ 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` + 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}" @@ -81,24 +83,27 @@ 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 ">" - 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 @@ -110,9 +115,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 + 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 \ diff --git a/themes/Default.bgptheme b/themes/Default.bgptheme index fd823cb..b24a11e 100644 --- a/themes/Default.bgptheme +++ b/themes/Default.bgptheme @@ -18,6 +18,7 @@ unset_git_prompt_colors() { unset GIT_PROMPT_COMMAND_FAIL unset GIT_PROMPT_STATUS_COMMAND unset GIT_PROMPT_VIRTUALENV + unset GIT_PROMPT_UPSTREAM unset GIT_PROMPT_START_USER unset GIT_PROMPT_START_ROOT unset GIT_PROMPT_END_USER @@ -73,6 +74,11 @@ 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 if [[ -z ${GIT_PROMPT_START_USER} ]]; then GIT_PROMPT_START_USER="_LAST_COMMAND_INDICATOR_ ${Yellow}${PathShort}${ResetColor}"; fi if [[ -z ${GIT_PROMPT_START_ROOT} ]]; then GIT_PROMPT_START_ROOT="${GIT_PROMPT_START_USER}"; fi