threads / patch / 45709

patch__git_ps1: Don't kill shell if user types `set -e`

Subject: [PATCH] __git_ps1: Don't kill shell if user types `set -e`

## tl;dr

5 messages between Apr 16, 2017 and May 4, 2017. Diffs are folded; open one to read it.

replies: 4people: 3as markdown or json

Tom "Ravi" Hale· Apr 16, 2017, 08:28 UTC · lore

If a user types `set -e` in an interactive shell, and is using __git_ps1 to set their prompt, the shell will die if the current directory isn't inside a git repository.

This is because `set -e` instructs the shell to exit upon a command returning a non-zero exit status, and the following command exits with status 128:

	repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
		--is-bare-repository --is-inside-work-tree \
		--short HEAD 2>/dev/null)"
I couldn't write a test to illustrate this - the test harness seems to
be preventing
`set -e` from having an effect.
	
<attachment>
Signed-off-by: Tom "Ravi" Hale <tom@hale.ee>
From 7867768f4516559c129ed9eb07ae29c36a6c9367 Mon Sep 17 00:00:00 2001
From: "Tom \"Ravi\" Hale" <tom@hale.ee>
Date: Sun, 16 Apr 2017 14:10:10 +0700
Subject: [PATCH] __git_ps1: Don't kill shell if user types `set -e`
Ensure that `set +e` is in effect inside __git_ps1 to prevent any commands
exiting non-zero from killing an interactive shell in which the user has run
`set -e`.
---
 contrib/completion/git-prompt.sh | 4 ++++
 1 file changed, 4 insertions(+)
Show changes to contrib/completion/git-prompt.sh +4 −0
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 97eacd7..15a0b74 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -304,6 +304,10 @@ __git_ps1 ()
 	local ps1pc_start='\u@\h:\w '
 	local ps1pc_end='\$ '
 	local printf_format=' (%s)'
+	# Prevent any non-zero exit statuses from killing an interactive shell in the
+	# case that a user types `set -e`
+	local -- - # Shadow shell options in $- (restored upon return)
+	set +e
 
 	case "$#" in
 		2|3)	pcmode=yes
-- 
2.11.1
Junio C Hamano· Apr 17, 2017, 04:24 UTC · re: Tom "Ravi" Hale · lore

Re: [PATCH] __git_ps1: Don't kill shell if user types `set -e`

"Tom \"Ravi\" Hale" <tom@hale.ee> writes:
Show 12 quoted lines
> If a user types `set -e` in an interactive shell, and is using __git_ps1
> to set
> their prompt, the shell will die if the current directory isn't inside a git
> repository.
>
> This is because `set -e` instructs the shell to exit upon a command
> returning a non-zero exit status, and the following command exits with
> status 128:
>
> 	repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
> 		--is-bare-repository --is-inside-work-tree \
> 		--short HEAD 2>/dev/null)"
Hmph.  So the fix would be something like this?
 	repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
 		--is-bare-repository --is-inside-work-tree \
- 		--short HEAD 2>/dev/null)"
+ 		--short HEAD 2>/dev/null || :)"

I am too afraid to ask what a user would try to achieve by doing the "set -e" thing in an interactive session. It is understandable (I am not saying I think it is necessarily a good idea) to do so in a script, but for an interactive shell?

Tom Hale· May 4, 2017, 08:26 UTC · re: Junio C Hamano · lore

Re: [PATCH] __git_ps1: Don't kill shell if user types `set -e`

On 17/04/17 11:24, Junio C Hamano wrote:
Show 13 quoted lines
> "Tom \"Ravi\" Hale" <tom@hale.ee> writes:
>
> > If a user types `set -e` in an interactive shell, and is using __git_ps1
> > to set
> > their prompt, the shell will die if the current directory isn't inside a git
> > repository.
> >
> Hmph.  So the fix would be something like this?
>
>      repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
>          --is-bare-repository --is-inside-work-tree \
> -         --short HEAD 2>/dev/null)"
> +         --short HEAD 2>/dev/null || :)"
Nope, that would cause the next line
    rev_parse_exit_code="$?"
to always be assigned 0.
I believe the patch we're after is attached.
-- 
Cheers,

Tom


From 74fa2e2fa058f89605cf0b2774ad8e9df981cf86 Mon Sep 17 00:00:00 2001
From: "Tom \"Ravi\" Hale" <tom@hale.ee>
Date: Sun, 16 Apr 2017 14:15:14 +0700
Subject: [PATCH] Prevent non-zero exit if outside a repository

---
 contrib/completion/git-prompt.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 97eacd7..d0d890c 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -361,8 +361,8 @@ __git_ps1 ()
 	local repo_info rev_parse_exit_code
 	repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
 		--is-bare-repository --is-inside-work-tree \
-		--short HEAD 2>/dev/null)"
-	rev_parse_exit_code="$?"
+		--short HEAD 2>/dev/null)" && 
+		rev_parse_exit_code="$?" || : # Don't die if "set -e"
 
 	if [ -z "$repo_info" ]; then
 		return $exit
-- 
2.12.2
Ævar Arnfjörð Bjarmason· May 4, 2017, 09:16 UTC · re: Tom Hale · lore

Re: [PATCH] __git_ps1: Don't kill shell if user types `set -e`

On Thu, May 4, 2017 at 10:26 AM, Tom Hale <tom@hale.ee> wrote:
Show 21 quoted lines
>
> On 17/04/17 11:24, Junio C Hamano wrote:
>> "Tom \"Ravi\" Hale" <tom@hale.ee> writes:
>>
>> > If a user types `set -e` in an interactive shell, and is using __git_ps1
>> > to set
>> > their prompt, the shell will die if the current directory isn't inside a git
>> > repository.
>> >
>> Hmph.  So the fix would be something like this?
>>
>>      repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
>>          --is-bare-repository --is-inside-work-tree \
>> -         --short HEAD 2>/dev/null)"
>> +         --short HEAD 2>/dev/null || :)"
>
> Nope, that would cause the next line
>     rev_parse_exit_code="$?"
> to always be assigned 0.
>
> I believe the patch we're after is attached.

I see how this would fix things, but this just seems like a rather crazy thing for us to support, we can take this patch, but it's going to take quite some maintenance burden to ensure that this code is set -e clean going forward, and I don't think we should take a patch like this without some general support in the regression tests to ensure that the completion code is set -e clean, otherwise this is going to very easily regress the next time someone not aware of this patches it.

What's the real use-case here? If you do "set -e" in your shell you also get e.g.:

    $ ls -l blah
    ls: cannot access blah: No such file or directory
    === Command terminated with exit status 2 (Thu May  4 11:16:03 2017) ===

I.e. any little failure will terminate your shell, are you actually running a shell like this? For what purpose?

Tom Hale· May 4, 2017, 09:35 UTC · re: Ævar Arnfjörð Bjarmason · lore

Re: [PATCH] __git_ps1: Don't kill shell if user types `set -e`

On 04/05/17 16:16, Ævar Arnfjörð Bjarmason wrote:
Show 9 quoted lines
> What's the real use-case here? If you do "set -e" in your shell you
> also get e.g.:
> 
>     $ ls -l blah
>     ls: cannot access blah: No such file or directory
>     === Command terminated with exit status 2 (Thu May  4 11:16:03 2017) ===
> 
> I.e. any little failure will terminate your shell, are you actually
> running a shell like this? For what purpose?

If I want to copy and paste a list of commands and have the execution stop at a failure, I type "bash" then "set -e", then paste the commands.

It saves me creating a script file and then removing it later.
-- 
Tom Hale

← back to recent threads