Skip to content

Commit

Permalink
git-prompt: preserve value of $? inside shell prompt
Browse files Browse the repository at this point in the history
If you have a prompt which displays the command exit status,
__git_ps1 without this change corrupts it, although it has
the correct value in the parent shell:

	~/src/git (master) 0 $ set | grep ^PS1
	PS1='\w$(__git_ps1) $? \$ '
	~/src/git (master) 0 $ false
	~/src/git (master) 0 $ echo $?
	1
	~/src/git (master) 0 $

There is a slightly ugly workaround:

	~/src/git (master) 0 $ set | grep ^PS1
	PS1='\w$(x=$?; __git_ps1; exit $x) $? \$ '
	~/src/git (master) 0 $ false
	~/src/git (master) 1 $

This change makes the workaround unnecessary.

Signed-off-by: Tony Finch <dot@dotat.at>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Tony Finch authored and Junio C Hamano committed Dec 22, 2014
1 parent bef111d commit eb443e3
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions contrib/completion/git-prompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ __git_eread ()
# In this mode you can request colored hints using GIT_PS1_SHOWCOLORHINTS=true
__git_ps1 ()
{
local exit=$?
local pcmode=no
local detached=no
local ps1pc_start='\u@\h:\w '
Expand Down Expand Up @@ -511,4 +512,7 @@ __git_ps1 ()
else
printf -- "$printf_format" "$gitstring"
fi

# preserve exit status
return $exit
}

0 comments on commit eb443e3

Please sign in to comment.