lib/theme: improve performance of `scm()`

- Don't invoke the source control utility when all we want to know is if we're somewhere inside the repository; use `_bash-it-find-in-ancestor()`.
pull/2038/head
John D Pell 2022-02-12 21:08:29 -08:00 committed by John D Pell
parent d86a182b6e
commit c9efc161ff
1 changed files with 5 additions and 11 deletions

View File

@ -118,19 +118,13 @@ _bash_it_library_finalize_hook+=('_bash_it_appearance_scm_init')
function scm() { function scm() {
if [[ "${SCM_CHECK:-true}" == "false" ]]; then if [[ "${SCM_CHECK:-true}" == "false" ]]; then
SCM="${SCM_NONE-NONE}" SCM="${SCM_NONE-NONE}"
elif [[ -f .git/HEAD ]] && [[ -x "${GIT_EXE-}" ]]; then elif [[ -x "${GIT_EXE-}" ]] && _bash-it-find-in-ancestor '.git' > /dev/null; then
SCM="${SCM_GIT?}" SCM="${SCM_GIT?}"
elif [[ -d .hg ]] && [[ -x "${HG_EXE-}" ]]; then elif [[ -x "${HG_EXE-}" ]] && _bash-it-find-in-ancestor '.hg' > /dev/null; then
SCM="${SCM_HG?}" SCM="${SCM_HG?}"
elif [[ -d .svn ]] && [[ -x "${SVN_EXE-}" ]]; then elif [[ -x "${SVN_EXE-}" ]] && _bash-it-find-in-ancestor '.svn' > /dev/null; then
SCM="${SCM_SVN?}" SCM="${SCM_SVN?}"
elif [[ -x "${GIT_EXE-}" ]] && [[ -n "$(git rev-parse --is-inside-work-tree 2> /dev/null)" ]]; then elif [[ -x "${P4_EXE-}" && -n "$(p4 set P4CLIENT 2> /dev/null)" ]]; then
SCM="${SCM_GIT?}"
elif [[ -x "${HG_EXE-}" ]] && [[ -n "$(hg root 2> /dev/null)" ]]; then
SCM="${SCM_HG?}"
elif [[ -x "${SVN_EXE-}" ]] && [[ -n "$(svn info --show-item wc-root 2> /dev/null)" ]]; then
SCM="${SCM_SVN?}"
elif [[ -x "${P4_EXE-}" ]] && [[ -n "$(p4 set P4CLIENT 2> /dev/null)" ]]; then
SCM="${SCM_P4?}" SCM="${SCM_P4?}"
else else
SCM="${SCM_NONE-NONE}" SCM="${SCM_NONE-NONE}"
@ -376,7 +370,7 @@ function hg_prompt_vars() {
SCM_PREFIX="${HG_THEME_PROMPT_PREFIX:-${SCM_THEME_PROMPT_PREFIX-}}" SCM_PREFIX="${HG_THEME_PROMPT_PREFIX:-${SCM_THEME_PROMPT_PREFIX-}}"
SCM_SUFFIX="${HG_THEME_PROMPT_SUFFIX:-${SCM_THEME_PROMPT_SUFFIX-}}" SCM_SUFFIX="${HG_THEME_PROMPT_SUFFIX:-${SCM_THEME_PROMPT_SUFFIX-}}"
HG_ROOT=$(_bash-it-find-in-ancestor ".hg") HG_ROOT="$(_bash-it-find-in-ancestor ".hg")/.hg"
if [[ -f "$HG_ROOT/branch" ]]; then if [[ -f "$HG_ROOT/branch" ]]; then
# Mercurial holds it's current branch in .hg/branch file # Mercurial holds it's current branch in .hg/branch file