Re: [PATCH v2] git-prompt: preserve value of $? inside shell prompt
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Dec 22, 2014, 19:58 UTC
- Message-ID
- <xmqqsig78nim.fsf@gitster.dls.corp.google.com>
- In-Reply-To
- <alpine.LSU.2.00.1412221808110.2546@hermes-1.csi.cam.ac.uk>
Tony Finch <dot@dotat.at> writes:
Show 26 quoted lines
> 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> > --- > contrib/completion/git-prompt.sh | 4 ++++ > 1 file changed, 4 insertions(+) > > I hope that explains it properly :-)
Yes. I wouldn't have spent 20 minutes experimenting with various hypothetical use cases if the above were there in the first place.
Thanks. Will queue.
Show 20 quoted lines
> diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
> index c5473dc..5fe69d0 100644
> --- a/contrib/completion/git-prompt.sh
> +++ b/contrib/completion/git-prompt.sh
> @@ -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 '
> @@ -511,4 +512,7 @@ __git_ps1 ()
> else
> printf -- "$printf_format" "$gitstring"
> fi
> +
> + # preserve exit status
> + return $exit
> }