git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Tom "Ravi" Hale <tom@hale.ee>
Date
Apr 16, 2017, 08:28 UTC
Message-ID
<b344d0c3-c8b8-da27-79f6-ae750be6830a@hale.ee>

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(+)
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
Next: Junio C Hamano
Message 1 of 5 in “__git_ps1: Don't kill shell if user types `set -e`”
  1. __git_ps1: Don't kill shell if user types `set -e`Tom "Ravi" Hale, Apr 16, 2017
  2. Junio C HamanoApr 17, 2017
  3. Tom HaleMay 4, 2017
  4. Ævar Arnfjörð BjarmasonMay 4, 2017
  5. Tom HaleMay 4, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.