aks: optimzations; don't lookup the same files over and over

master
Alan Stebbens 11 years ago
parent 7bf6b96b71
commit a9cd7f330c
  1. 68
      gitprompt.sh

@ -24,6 +24,7 @@ function git_prompt_dir()
# gp_set_file_var ENVAR SOMEFILE # gp_set_file_var ENVAR SOMEFILE
# #
# If ENVAR is set, check that it's value exists as a readable file. Otherwise,
# Set ENVAR to the path to SOMEFILE, based on $HOME, $__GIT_PROMPT_DIR, and the # Set ENVAR to the path to SOMEFILE, based on $HOME, $__GIT_PROMPT_DIR, and the
# directory of the current script. The SOMEFILE can be prefixed with '.', or # directory of the current script. The SOMEFILE can be prefixed with '.', or
# not. # not.
@ -33,7 +34,13 @@ function git_prompt_dir()
function gp_set_file_var() { function gp_set_file_var() {
local envar="$1" local envar="$1"
local file="$2" local file="$2"
if eval "test -z \"\$$envar\"" ; then if eval "[[ -n \"\$$envar\" && -r \"\$$envar\" ]]" ; then # is envar set to a readable file?
local basefile
eval "basefile=\"\`basename \\\"\$$envar\\\"\`\"" # assign basefile
if [[ "$basefile" = "$file" || "$basefile" = ".$file" ]]; then
return 0
fi
else # envar is not set, or it's set to a different file than requested
eval "$envar=" # set empty envar eval "$envar=" # set empty envar
gp_maybe_set_envar_to_path "$envar" "$HOME/.$file" "$HOME/$file" "$HOME/lib/$file" && return 0 gp_maybe_set_envar_to_path "$envar" "$HOME/.$file" "$HOME/$file" "$HOME/lib/$file" && return 0
git_prompt_dir git_prompt_dir
@ -73,24 +80,18 @@ function git_prompt_config()
# git-prompt-colors.sh -- sets the GIT_PROMPT color scheme, using names from prompt-colors.sh # git-prompt-colors.sh -- sets the GIT_PROMPT color scheme, using names from prompt-colors.sh
if gp_set_file_var __PROMPT_COLORS_SH prompt-colors.sh ; then if gp_set_file_var __PROMPT_COLORS_SH prompt-colors.sh ; then
if [[ -n "${__PROMPT_COLORS_SH}" ]]; then source "${__PROMPT_COLORS_SH}" # outsource the color defs
source "${__PROMPT_COLORS_SH}" # outsource the color defs else
else echo 1>&2 "Cannot find prompt-colors.sh!"
echo 1>&2 "Cannot find prompt-colors.sh!"
fi
fi fi
# source the user's ~/.git-prompt-colors.sh file, or the one that should be # source the user's ~/.git-prompt-colors.sh file, or the one that should be
# sitting in the same directory as this script # sitting in the same directory as this script
if gp_set_file_var __GIT_PROMPT_COLORS_FILE git-prompt-colors.sh ; then if gp_set_file_var __GIT_PROMPT_COLORS_FILE git-prompt-colors.sh ; then
source "${__GIT_PROMPT_COLORS_FILE}"
# if the envar is defined, source the file for custom colors else
if [[ -n "${__GIT_PROMPT_COLORS_FILE}" ]]; then echo 1>&2 "Cannot find git-prompt-colors.sh!"
source "${__GIT_PROMPT_COLORS_FILE}"
else
echo 1>&2 "Cannot find git-prompt-colors.sh!"
fi
fi fi
# Do this only once to define PROMPT_START and PROMPT_END # Do this only once to define PROMPT_START and PROMPT_END
@ -110,7 +111,7 @@ function git_prompt_config()
PROMPT_START="$GIT_PROMPT_START_USER" PROMPT_START="$GIT_PROMPT_START_USER"
fi fi
else else
PROMPT_START="${GIT_PROMPT_START}" PROMPT_START="$GIT_PROMPT_START"
fi fi
if [[ -z "${GIT_PROMPT_END}" ]] ; then if [[ -z "${GIT_PROMPT_END}" ]] ; then
@ -120,27 +121,27 @@ function git_prompt_config()
PROMPT_END="$GIT_PROMPT_END_USER" PROMPT_END="$GIT_PROMPT_END_USER"
fi fi
else else
PROMPT_END="${GIT_PROMPT_END}" PROMPT_END="$GIT_PROMPT_END"
fi fi
fi fi
# set GIT_PROMPT_LEADING_SPACE to 0 if you want to have no leading space in front of the GIT prompt # set GIT_PROMPT_LEADING_SPACE to 0 if you want to have no leading space in front of the GIT prompt
if [ "x${GIT_PROMPT_LEADING_SPACE}" == "x0" ]; then if [[ "$GIT_PROMPT_LEADING_SPACE" = 0 ]]; then
PROMPT_LEADING_SPACE="" PROMPT_LEADING_SPACE=""
else else
PROMPT_LEADING_SPACE=" " PROMPT_LEADING_SPACE=" "
fi fi
if [ "x${GIT_PROMPT_ONLY_IN_REPO}" == "x1" ]; then if [[ "$GIT_PROMPT_ONLY_IN_REPO" = 1 ]]; then
EMPTY_PROMPT=$OLD_GITPROMPT EMPTY_PROMPT="$OLD_GITPROMPT"
else else
if [[ -n "${VIRTUAL_ENV}" ]]; then local ps="$LAST_COMMAND_INDICATOR"
EMPTY_PROMPT="${LAST_COMMAND_INDICATOR}(${Blue}$(basename "${VIRTUAL_ENV}")${ResetColor}) ${PROMPT_START}$($prompt_callback)${PROMPT_END}" if [[ -n "$VIRTUAL_ENV" ]]; then
ps="$ps(${Blue}$(basename \"${VIRTUAL_ENV}\")${ResetColor}) "
elif [[ -n "${CONDA_DEFAULT_ENV}" ]]; then elif [[ -n "${CONDA_DEFAULT_ENV}" ]]; then
EMPTY_PROMPT="${LAST_COMMAND_INDICATOR}(${Blue}$(basename "${CONDA_DEFAULT_ENV}")${ResetColor}) ${PROMPT_START}$($prompt_callback)${PROMPT_END}" ps="$ps(${Blue}$(basename \"${CONDA_DEFAULT_ENV}\")${ResetColor}) "
else
EMPTY_PROMPT="${LAST_COMMAND_INDICATOR}${PROMPT_START}$($prompt_callback)${PROMPT_END}"
fi fi
EMPTY_PROMPT="$ps${PROMPT_START}$($prompt_callback)${PROMPT_END}"
fi fi
# fetch remote revisions every other $GIT_PROMPT_FETCH_TIMEOUT (default 5) minutes # fetch remote revisions every other $GIT_PROMPT_FETCH_TIMEOUT (default 5) minutes
@ -150,6 +151,7 @@ function git_prompt_config()
if ! gp_maybe_set_envar_to_path __GIT_STATUS_CMD "$__GIT_PROMPT_DIR/gitstatus.sh" "$__GIT_PROMPT_DIR/gitstatus.py" ; then if ! gp_maybe_set_envar_to_path __GIT_STATUS_CMD "$__GIT_PROMPT_DIR/gitstatus.sh" "$__GIT_PROMPT_DIR/gitstatus.py" ; then
echo 1>&2 "Cannot find gitstatus.sh or gitstatus.py!" echo 1>&2 "Cannot find gitstatus.sh or gitstatus.py!"
fi fi
# __GIT_STATUS_CMD defined
fi fi
} }
@ -163,21 +165,21 @@ function setGitPrompt() {
local repo=`git rev-parse --show-toplevel 2> /dev/null` local repo=`git rev-parse --show-toplevel 2> /dev/null`
if [[ ! -e "${repo}" ]]; then if [[ ! -e "${repo}" ]]; then
PS1="${EMPTY_PROMPT}" PS1="$EMPTY_PROMPT"
return return
fi fi
local FETCH_REMOTE_STATUS=1 local FETCH_REMOTE_STATUS=1
if [[ "x${GIT_PROMPT_FETCH_REMOTE_STATUS}" == "x0" ]]; then if [[ "$GIT_PROMPT_FETCH_REMOTE_STATUS" = 0 ]]; then
FETCH_REMOTE_STATUS=0 FETCH_REMOTE_STATUS=0
fi fi
if [[ -e "${repo}/.bash-git-rc" ]]; then if [[ -e "$repo/.bash-git-rc" ]]; then
source "${repo}/.bash-git-rc" source "$repo/.bash-git-rc"
fi fi
if [ "x${FETCH_REMOTE_STATUS}" == "x1" ]; then if [ "$FETCH_REMOTE_STATUS" = 1 ]; then
checkUpstream checkUpstream
fi fi
updatePrompt updatePrompt
@ -189,7 +191,7 @@ function checkUpstream() {
local FETCH_HEAD="${repo}/.git/FETCH_HEAD" local FETCH_HEAD="${repo}/.git/FETCH_HEAD"
# Fech repo if local is stale for more than $GIT_FETCH_TIMEOUT minutes # Fech repo if local is stale for more than $GIT_FETCH_TIMEOUT minutes
if [[ ! -e "${FETCH_HEAD}" || -e `find "${FETCH_HEAD}" -mmin +${GIT_PROMPT_FETCH_TIMEOUT}` ]] if [[ ! -e "$FETCH_HEAD" || -e `find "$FETCH_HEAD" -mmin +$GIT_PROMPT_FETCH_TIMEOUT` ]]
then then
if [[ -n $(git remote show) ]]; then if [[ -n $(git remote show) ]]; then
( (
@ -270,15 +272,15 @@ function updatePrompt() {
PS1="${LAST_COMMAND_INDICATOR}${PROMPT_START}$($prompt_callback)${STATUS}${PROMPT_END}" PS1="${LAST_COMMAND_INDICATOR}${PROMPT_START}$($prompt_callback)${STATUS}${PROMPT_END}"
if [[ -n "${VIRTUAL_ENV}" ]]; then if [[ -n "${VIRTUAL_ENV}" ]]; then
PS1="(${Blue}$(basename ${VIRTUAL_ENV})${ResetColor}) ${PS1}" PS1="(${Blue}$(basename \"$VIRTUAL_ENV\")${ResetColor}) ${PS1}"
fi fi
if [[ -n "${CONDA_DEFAULT_ENV}" ]]; then if [[ -n "${CONDA_DEFAULT_ENV}" ]]; then
PS1="(${Blue}$(basename ${CONDA_DEFAULT_ENV})${ResetColor}) ${PS1}" PS1="(${Blue}$(basename \"$CONDA_DEFAULT_ENV\")${ResetColor}) ${PS1}"
fi fi
else else
PS1="${EMPTY_PROMPT}" PS1="$EMPTY_PROMPT"
fi fi
} }

Loading…
Cancel
Save