From 86fd5cd5c4417816dd462f9dbbb019c49f3c3278 Mon Sep 17 00:00:00 2001 From: Eduardo Bellido Bellido Date: Sat, 5 Sep 2015 02:12:38 +0200 Subject: [PATCH 1/4] Improve ref detection in git prompt --- themes/base.theme.bash | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index c199146d..1ac494ff 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -10,8 +10,7 @@ SCM_THEME_PROMPT_PREFIX=' |' SCM_THEME_PROMPT_SUFFIX='|' SCM_THEME_BRANCH_PREFIX='' SCM_THEME_TAG_PREFIX='tag:' -SCM_THEME_COMMIT_PREFIX='commit:' -SCM_THEME_REMOTE_PREFIX='' +SCM_THEME_DETACHED_PREFIX='detached:' CLOCK_CHAR='☆' THEME_CLOCK_CHECK=${THEME_CLOCK_CHECK:=true} @@ -107,6 +106,8 @@ function git_prompt_vars { fi fi + SCM_CHANGE=$(git rev-parse --short HEAD 2>/dev/null) + local ref=$(git symbolic-ref -q HEAD 2> /dev/null) if [[ -n "$ref" ]]; then SCM_BRANCH=${SCM_THEME_BRANCH_PREFIX}${ref#refs/heads/} @@ -115,15 +116,19 @@ function git_prompt_vars { if [[ -n "$ref" ]]; then SCM_BRANCH=${SCM_THEME_TAG_PREFIX}${ref} else - local commit_re='(^remotes/)?(.+-g[a-zA-Z0-9]+)$' - local remote_re='^remotes/(.+)$' - ref=$(git describe --tags --all --always 2> /dev/null) - if [[ "$ref" =~ ${commit_re} ]]; then - SCM_BRANCH=${SCM_THEME_COMMIT_PREFIX}${BASH_REMATCH[2]} - elif [[ "$ref" =~ ${remote_re} ]]; then - SCM_BRANCH=${SCM_THEME_REMOTE_PREFIX}${BASH_REMATCH[1]} + ref=$(git describe --contains --all HEAD 2> /dev/null) + if [[ -n "$ref" ]]; then + local remote_re='^remotes/(.+)$' + if [[ "$ref" =~ ${remote_re} ]]; then + SCM_BRANCH=${SCM_THEME_DETACHED_PREFIX}${BASH_REMATCH[1]} + else + SCM_BRANCH=${SCM_THEME_DETACHED_PREFIX}${ref} + fi fi fi + if [[ -z "$ref" ]]; then + SCM_BRANCH=${SCM_THEME_DETACHED_PREFIX}${SCM_CHANGE} + fi fi local ahead_re='.+ahead ([0-9]+).+' @@ -138,7 +143,6 @@ function git_prompt_vars { SCM_PREFIX=${GIT_THEME_PROMPT_PREFIX:-$SCM_THEME_PROMPT_PREFIX} SCM_SUFFIX=${GIT_THEME_PROMPT_SUFFIX:-$SCM_THEME_PROMPT_SUFFIX} - SCM_CHANGE=$(git rev-parse HEAD 2>/dev/null) } function svn_prompt_vars { From 78cb41a1cfe32aa61db3641901a3d16c2005a6a6 Mon Sep 17 00:00:00 2001 From: Eduardo Bellido Bellido Date: Sat, 5 Sep 2015 15:04:25 +0200 Subject: [PATCH 2/4] Add the option to show git branch remote tracking info in git prompt --- themes/base.theme.bash | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 1ac494ff..b1d79ed8 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -17,6 +17,7 @@ THEME_CLOCK_CHECK=${THEME_CLOCK_CHECK:=true} THEME_BATTERY_PERCENTAGE_CHECK=${THEME_BATTERY_PERCENTAGE_CHECK:=true} SCM_GIT_SHOW_DETAILS=${SCM_GIT_SHOW_DETAILS:=true} +SCM_GIT_SHOW_REMOTE_INFO=${SCM_GIT_SHOW_REMOTE_INFO:=auto} SCM_GIT='git' SCM_GIT_CHAR='±' @@ -111,6 +112,24 @@ function git_prompt_vars { local ref=$(git symbolic-ref -q HEAD 2> /dev/null) if [[ -n "$ref" ]]; then SCM_BRANCH=${SCM_THEME_BRANCH_PREFIX}${ref#refs/heads/} + local branch_tracking_info="$(grep "${SCM_BRANCH}..." <<< "${status}")" + if [[ -n "${branch_tracking_info}" ]]; then + branch_tracking_info=${branch_tracking_info#\#\# ${SCM_BRANCH}...} + branch_tracking_info=${branch_tracking_info% [*} + local remote_name=${branch_tracking_info%%/*} + local remote_branch=${branch_tracking_info#${remote_name}/} + local remote_info="" + local num_remotes=$(git remote | wc -l 2> /dev/null) + [[ "${SCM_BRANCH}" = "${remote_branch}" ]] && local same_branch_name=true + if ([[ "${SCM_GIT_SHOW_REMOTE_INFO}" = "auto" ]] && [[ "${num_remotes}" -ge 2 ]]) || + [[ "${SCM_GIT_SHOW_REMOTE_INFO}" = "true" ]]; then + remote_info="${remote_name}" + [[ "${same_branch_name}" != "true" ]] && remote_info+="/${remote_branch}" + elif [[ ${same_branch_name} != "true" ]]; then + remote_info="${remote_branch}" + fi + [[ -n "${remote_info}" ]] && SCM_BRANCH+=" (${remote_info})" + fi else ref=$(git describe --tags --exact-match 2> /dev/null) if [[ -n "$ref" ]]; then From 0943aec34b9349a7299cd4faa4cd3975ab616df2 Mon Sep 17 00:00:00 2001 From: Eduardo Bellido Bellido Date: Sun, 6 Sep 2015 00:59:27 +0200 Subject: [PATCH 3/4] Improvements in git prompt - Git prompt now indicates when remote tracked branch is "gone" - New env var (SCM_GIT_DETACHED) that indicates when HEAD is detached - New env vars available for themes (SCM_THEME_BRANCH_TRACK_PREFIX, SCM_THEME_BRANCH_GONE_PREFIX, SCM_GIT_DETACHED_CHAR) - Refactor of git_prompt_vars function from base theme --- themes/base.theme.bash | 48 ++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/themes/base.theme.bash b/themes/base.theme.bash index b1d79ed8..de1b4ad6 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -11,6 +11,8 @@ SCM_THEME_PROMPT_SUFFIX='|' SCM_THEME_BRANCH_PREFIX='' SCM_THEME_TAG_PREFIX='tag:' SCM_THEME_DETACHED_PREFIX='detached:' +SCM_THEME_BRANCH_TRACK_PREFIX=' → ' +SCM_THEME_BRANCH_GONE_PREFIX=' ⇢ ' CLOCK_CHAR='☆' THEME_CLOCK_CHECK=${THEME_CLOCK_CHECK:=true} @@ -21,6 +23,7 @@ SCM_GIT_SHOW_REMOTE_INFO=${SCM_GIT_SHOW_REMOTE_INFO:=auto} SCM_GIT='git' SCM_GIT_CHAR='±' +SCM_GIT_DETACHED_CHAR='⌿' SCM_GIT_AHEAD_CHAR="↑" SCM_GIT_BEHIND_CHAR="↓" SCM_GIT_UNTRACKED_CHAR="?:" @@ -112,12 +115,13 @@ function git_prompt_vars { local ref=$(git symbolic-ref -q HEAD 2> /dev/null) if [[ -n "$ref" ]]; then SCM_BRANCH=${SCM_THEME_BRANCH_PREFIX}${ref#refs/heads/} - local branch_tracking_info="$(grep "${SCM_BRANCH}..." <<< "${status}")" - if [[ -n "${branch_tracking_info}" ]]; then - branch_tracking_info=${branch_tracking_info#\#\# ${SCM_BRANCH}...} - branch_tracking_info=${branch_tracking_info% [*} - local remote_name=${branch_tracking_info%%/*} - local remote_branch=${branch_tracking_info#${remote_name}/} + local tracking_info="$(grep "${SCM_BRANCH}..." <<< "${status}")" + if [[ -n "${tracking_info}" ]]; then + [[ "${tracking_info}" =~ .+\[gone\]$ ]] && local branch_gone="true" + tracking_info=${tracking_info#\#\# ${SCM_BRANCH}...} + tracking_info=${tracking_info% [*} + local remote_name=${tracking_info%%/*} + local remote_branch=${tracking_info#${remote_name}/} local remote_info="" local num_remotes=$(git remote | wc -l 2> /dev/null) [[ "${SCM_BRANCH}" = "${remote_branch}" ]] && local same_branch_name=true @@ -128,26 +132,28 @@ function git_prompt_vars { elif [[ ${same_branch_name} != "true" ]]; then remote_info="${remote_branch}" fi - [[ -n "${remote_info}" ]] && SCM_BRANCH+=" (${remote_info})" - fi - else - ref=$(git describe --tags --exact-match 2> /dev/null) - if [[ -n "$ref" ]]; then - SCM_BRANCH=${SCM_THEME_TAG_PREFIX}${ref} - else - ref=$(git describe --contains --all HEAD 2> /dev/null) - if [[ -n "$ref" ]]; then - local remote_re='^remotes/(.+)$' - if [[ "$ref" =~ ${remote_re} ]]; then - SCM_BRANCH=${SCM_THEME_DETACHED_PREFIX}${BASH_REMATCH[1]} + if [[ -n "${remote_info}" ]];then + if [[ "${branch_gone}" = "true" ]]; then + SCM_BRANCH+="${SCM_THEME_BRANCH_GONE_PREFIX}${remote_info}" else - SCM_BRANCH=${SCM_THEME_DETACHED_PREFIX}${ref} + SCM_BRANCH+="${SCM_THEME_BRANCH_TRACK_PREFIX}${remote_info}" fi fi fi - if [[ -z "$ref" ]]; then - SCM_BRANCH=${SCM_THEME_DETACHED_PREFIX}${SCM_CHANGE} + SCM_GIT_DETACHED="false" + else + local detached_prefix="" + ref=$(git describe --tags --exact-match 2> /dev/null) + if [[ -n "$ref" ]]; then + detached_prefix=${SCM_THEME_TAG_PREFIX} + else + ref=$(git describe --contains --all HEAD 2> /dev/null) + ref=${ref#remotes/} + [[ -z "$ref" ]] && ref=${SCM_CHANGE} + detached_prefix=${SCM_THEME_DETACHED_PREFIX} fi + SCM_BRANCH=${detached_prefix}${ref} + SCM_GIT_DETACHED="true" fi local ahead_re='.+ahead ([0-9]+).+' From f823f89220f16eeddb098bbacdfee925b8c16bd5 Mon Sep 17 00:00:00 2001 From: Eduardo Bellido Bellido Date: Sun, 6 Sep 2015 01:40:56 +0200 Subject: [PATCH 4/4] Update README.md --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index d3ed2c9c..63062d46 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,23 @@ Set `SCM_GIT_SHOW_DETAILS` to 'false' to **don't show** it: * `export SCM_GIT_SHOW_DETAILS=false` +### Git remotes and remote branches +In some git workflows you must work with various remotes, for this reason, Bash it can provide some useful information about your remotes and your remote branches, for example, the remote on you are working, or if your local branch is tracking a remote branch. + +You can control this feature with the flag `SCM_GIT_SHOW_REMOTE_INFO` as follows: + +Set `SCM_GIT_SHOW_REMOTE_INFO` to 'auto' (the default value) to activate it only when more than one remote is configured in the current repo: + +* `export SCM_GIT_SHOW_REMOTE_INFO=auto` + +Set `SCM_GIT_SHOW_REMOTE_INFO` to 'true' to always activate the feature: + +* `export SCM_GIT_SHOW_REMOTE_INFO=true` + +Set `SCM_GIT_SHOW_REMOTE_INFO` to 'false' to **disable the feature**: + +* `export SCM_GIT_SHOW_REMOTE_INFO=false` + #### pass function renamed to passgen The Bash it `pass` function has been renamed to `passgen` in order to avoid a naming conflict with the [pass password manager]. In order to minimize the impact on users of the legacy Bash it `pass` function, Bash it will create the alias `pass` that calls the new `passgen` function if the `pass` password manager command is not found on the `PATH` (default behavior).