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

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

From
Tom Hale <tom@hale.ee>
Date
May 4, 2017, 08:26 UTC
Message-ID
<c8879806-4e43-d033-bd69-918e730e2d05@hale.ee>
In-Reply-To
<xmqq60i3k6ed.fsf@gitster.mtv.corp.google.com>
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
Previous: Junio C HamanoNext: Ævar Arnfjörð Bjarmason
Message 3 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.