Skip to content

Commit e1b3460

Browse files
committed
Merge pull request #93 from DenJohX/master
FIX #92 GIT_PROMPT_ONLY_IN_REPO=1 should honor whatever previus prompt was
2 parents 5496b4c + 222a49b commit e1b3460

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

gitprompt.sh

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,37 @@ function setLastCommandState() {
279279
GIT_PROMPT_LAST_COMMAND_STATE=$?
280280
}
281281

282+
function we_are_on_repo() {
283+
if [[ -e "$(git rev-parse --git-dir 2> /dev/null)" ]]; then
284+
echo 1
285+
fi
286+
echo 0
287+
}
288+
289+
function update_old_git_prompt() {
290+
local in_repo=$(we_are_on_repo)
291+
if [[ $GIT_PROMPT_OLD_DIR_WAS_GIT = 0 ]]; then
292+
OLD_GITPROMPT=$PS1
293+
fi
294+
295+
GIT_PROMPT_OLD_DIR_WAS_GIT=$in_repo
296+
}
297+
282298
function setGitPrompt() {
299+
update_old_git_prompt
300+
301+
local repo=`git rev-parse --show-toplevel 2> /dev/null`
302+
if [[ ! -e "$repo" ]] && [[ "$GIT_PROMPT_ONLY_IN_REPO" = 1 ]]; then
303+
# we do not permit bash-git-prompt outside git repos, so nothing to do
304+
PS1="$OLD_GITPROMPT"
305+
return
306+
fi
307+
283308
local EMPTY_PROMPT
284309
local __GIT_STATUS_CMD
285310

286311
git_prompt_config
287312

288-
local repo=`git rev-parse --show-toplevel 2> /dev/null`
289313
if [[ ! -e "$repo" ]]; then
290314
PS1="$EMPTY_PROMPT"
291315
return
@@ -440,10 +464,14 @@ function gp_install_prompt {
440464
else
441465
prompt_callback="prompt_callback_default"
442466
fi
443-
467+
444468
if [ -z "$OLD_GITPROMPT" ]; then
445469
OLD_GITPROMPT=$PS1
446470
fi
471+
472+
if [ -z "$GIT_PROMPT_OLD_DIR_WAS_GIT" ]; then
473+
GIT_PROMPT_OLD_DIR_WAS_GIT=$(we_are_on_repo)
474+
fi
447475

448476
if [ -z "$PROMPT_COMMAND" ]; then
449477
PROMPT_COMMAND=setGitPrompt

0 commit comments

Comments
 (0)