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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 17/04/17 11:24, Junio C Hamano wrote:
> "Tom \"Ravi\" Hale" <tom@xxxxxxx> 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@xxxxxxx>
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


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]