From 222a49bcfc6fcc74f26ffa8f058c4fc32e580869 Mon Sep 17 00:00:00 2001 From: Dennis Xiloj Date: Fri, 17 Oct 2014 03:57:32 -0600 Subject: [PATCH] FIX GIT_PROMPT_ONLY_IN_REPO=1 should honor whatever previus prompt was --- gitprompt.sh | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index 1e3e691c..69dc207a 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -279,13 +279,37 @@ function setLastCommandState() { GIT_PROMPT_LAST_COMMAND_STATE=$? } +function we_are_on_repo() { + if [[ -e "$(git rev-parse --git-dir 2> /dev/null)" ]]; then + echo 1 + fi + echo 0 +} + +function update_old_git_prompt() { + local in_repo=$(we_are_on_repo) + if [[ $GIT_PROMPT_OLD_DIR_WAS_GIT = 0 ]]; then + OLD_GITPROMPT=$PS1 + fi + + GIT_PROMPT_OLD_DIR_WAS_GIT=$in_repo +} + function setGitPrompt() { + update_old_git_prompt + + local repo=`git rev-parse --show-toplevel 2> /dev/null` + if [[ ! -e "$repo" ]] && [[ "$GIT_PROMPT_ONLY_IN_REPO" = 1 ]]; then + # we do not permit bash-git-prompt outside git repos, so nothing to do + PS1="$OLD_GITPROMPT" + return + fi + local EMPTY_PROMPT local __GIT_STATUS_CMD git_prompt_config - local repo=`git rev-parse --show-toplevel 2> /dev/null` if [[ ! -e "$repo" ]]; then PS1="$EMPTY_PROMPT" return @@ -440,10 +464,14 @@ function gp_install_prompt { else prompt_callback="prompt_callback_default" fi - + if [ -z "$OLD_GITPROMPT" ]; then OLD_GITPROMPT=$PS1 fi + + if [ -z "$GIT_PROMPT_OLD_DIR_WAS_GIT" ]; then + GIT_PROMPT_OLD_DIR_WAS_GIT=$(we_are_on_repo) + fi if [ -z "$PROMPT_COMMAND" ]; then PROMPT_COMMAND=setGitPrompt