# [PATCH] git-prompt.sh: show the upstream abbrev name

5 messages from 2013-10-10 to 2013-10-15. Participants: Julien Carsique, SZEDER Gábor.
Thread: https://gitlist.dev/t/35097

## Julien Carsique, 2013-10-10 13:32

Subject: [PATCH] git-prompt.sh: show the upstream abbrev name
Message-ID: <1381411933-20600-1-git-send-email-jcarsique@nuxeo.com>
URL: https://gitlist.dev/e/1381411933-20600-1-git-send-email-jcarsique%40nuxeo.com

```
From: Julien Carsique <julien.carsique@gmail.com>

When working with multiple remotes, it is common to switch the upstream
from a remote to another. Doing so, the prompt may not be the expected
one. Providing an option to display tracking information sounds useful.

Add a "name" option to GIT_PS1_SHOWUPSTREAM which will show the upstream
abbrev name. This option is ignored if "verbose" is false.

Signed-off-by: Julien Carsique <julien.carsique@gmail.com>
---
 contrib/completion/git-prompt.sh | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index d6c61b2..452f7ee 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -60,6 +60,7 @@
 # of values:
 #
 #     verbose       show number of commits ahead/behind (+/-) upstream
+#     name          if verbose, then also show the upstream abbrev name
 #     legacy        don't use the '--count' option available in recent
 #                   versions of git-rev-list
 #     git           always compare HEAD to @{upstream}
@@ -94,7 +95,7 @@ __git_ps1_show_upstream ()
 {
 	local key value
 	local svn_remote svn_url_pattern count n
-	local upstream=git legacy="" verbose=""
+	local upstream=git legacy="" verbose="" name=""
 
 	svn_remote=()
 	# get some config options from git-config
@@ -122,6 +123,7 @@ __git_ps1_show_upstream ()
 		git|svn) upstream="$option" ;;
 		verbose) verbose=1 ;;
 		legacy)  legacy=1  ;;
+		name)   name=1 ;;
 		esac
 	done
 
@@ -204,6 +206,9 @@ __git_ps1_show_upstream ()
 		*)	    # diverged from upstream
 			p=" u+${count#*	}-${count%	*}" ;;
 		esac
+		if [[ -n "$count" && -n "$name" ]]; then
+			p+=" $(git rev-parse --abbrev-ref "$upstream" 2>/dev/null)"
+		fi
 	fi
 
 }
-- 
1.8.1.2

```

## SZEDER Gábor, 2013-10-10 13:58

Subject: Re: [PATCH] git-prompt.sh: show the upstream abbrev name
Message-ID: <20131010135813.GF19704@goldbirke>
URL: https://gitlist.dev/e/20131010135813.GF19704%40goldbirke
In-Reply-To: <1381411933-20600-1-git-send-email-jcarsique@nuxeo.com>

```
Hi,

On Thu, Oct 10, 2013 at 03:32:13PM +0200, Julien Carsique wrote:
> @@ -204,6 +206,9 @@ __git_ps1_show_upstream ()
>  		*)	    # diverged from upstream
>  			p=" u+${count#*	}-${count%	*}" ;;
>  		esac
> +		if [[ -n "$count" && -n "$name" ]]; then
> +			p+=" $(git rev-parse --abbrev-ref "$upstream" 2>/dev/null)"

The '+=' operator is not supported in all Bash versions we care about.
Please write this as p="$p $(...)".

Thanks,
Gábor

```

## Julien Carsique, 2013-10-10 14:40

Subject: [PATCH] git-prompt.sh: show the upstream abbrev name
Message-ID: <1381416039-16576-1-git-send-email-jcarsique@nuxeo.com>
URL: https://gitlist.dev/e/1381416039-16576-1-git-send-email-jcarsique%40nuxeo.com
In-Reply-To: <20131010135813.GF19704@goldbirke>

```
From: Julien Carsique <julien.carsique@gmail.com>

When working with multiple remotes, it is common to switch the upstream
from a remote to another. Doing so, the prompt may not be the expected
one. Providing an option to display tracking information sounds useful.

Add a "name" option to GIT_PS1_SHOWUPSTREAM which will show the upstream
abbrev name. This option is ignored if "verbose" is false.

Signed-off-by: Julien Carsique <julien.carsique@gmail.com>
Reviewed-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-prompt.sh | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index d6c61b2..84cd3a8 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -60,6 +60,7 @@
 # of values:
 #
 #     verbose       show number of commits ahead/behind (+/-) upstream
+#     name          if verbose, then also show the upstream abbrev name
 #     legacy        don't use the '--count' option available in recent
 #                   versions of git-rev-list
 #     git           always compare HEAD to @{upstream}
@@ -94,7 +95,7 @@ __git_ps1_show_upstream ()
 {
 	local key value
 	local svn_remote svn_url_pattern count n
-	local upstream=git legacy="" verbose=""
+	local upstream=git legacy="" verbose="" name=""
 
 	svn_remote=()
 	# get some config options from git-config
@@ -122,6 +123,7 @@ __git_ps1_show_upstream ()
 		git|svn) upstream="$option" ;;
 		verbose) verbose=1 ;;
 		legacy)  legacy=1  ;;
+		name)   name=1 ;;
 		esac
 	done
 
@@ -204,6 +206,9 @@ __git_ps1_show_upstream ()
 		*)	    # diverged from upstream
 			p=" u+${count#*	}-${count%	*}" ;;
 		esac
+		if [[ -n "$count" && -n "$name" ]]; then
+			p="$p $(git rev-parse --abbrev-ref "$upstream" 2>/dev/null)"
+		fi
 	fi
 
 }
-- 
1.8.1.2

```

## Julien Carsique, 2013-10-10 14:43

Subject: Re: [PATCH] git-prompt.sh: show the upstream abbrev name
Message-ID: <5256BD0A.30706@gmail.com>
URL: https://gitlist.dev/e/5256BD0A.30706%40gmail.com
In-Reply-To: <20131010135813.GF19704@goldbirke>

```
On 10/10/2013 15:58, SZEDER Gábor wrote:
> Hi,
>
> On Thu, Oct 10, 2013 at 03:32:13PM +0200, Julien Carsique wrote:
>> @@ -204,6 +206,9 @@ __git_ps1_show_upstream ()
>>  		*)	    # diverged from upstream
>>  			p=" u+${count#*	}-${count%	*}" ;;
>>  		esac
>> +		if [[ -n "$count" && -n "$name" ]]; then
>> +			p+=" $(git rev-parse --abbrev-ref "$upstream" 2>/dev/null)"
> The '+=' operator is not supported in all Bash versions we care about.
> Please write this as p="$p $(...)".
>
> Thanks,
> Gábor
>

Hi,

It's fixed.

Note '+=' was already used line 114:

            svn_url_pattern+="\\|$value"


Thank you for the review,

```

## SZEDER Gábor, 2013-10-15 12:21

Subject: Re: [PATCH] git-prompt.sh: show the upstream abbrev name
Message-ID: <20131015122111.GT19704@goldbirke>
URL: https://gitlist.dev/e/20131015122111.GT19704%40goldbirke
In-Reply-To: <5256BD0A.30706@gmail.com>

```
On Thu, Oct 10, 2013 at 04:43:22PM +0200, Julien Carsique wrote:
> It's fixed.

Thanks, the updated patch looks good to me.

> Note '+=' was already used line 114:
> 
>             svn_url_pattern+="\\|$value"

I guess noone has tried to use the upstream status indicator with an
SVN upstream and an ancient Bash version yet, thanks for pointing it
out.

-- >8 --
Subject: [PATCH] bash prompt: don't use '+=' operator in show upstream code path

The '+=' operator is not supported by old Bash versions (3.0) we still
care about.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-prompt.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 202e2e520f..7b732d2aeb 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -111,7 +111,7 @@ __git_ps1_show_upstream ()
 			;;
 		svn-remote.*.url)
 			svn_remote[$((${#svn_remote[@]} + 1))]="$value"
-			svn_url_pattern+="\\|$value"
+			svn_url_pattern="$svn_url_pattern\\|$value"
 			upstream=svn+git # default upstream is SVN if available, else git
 			;;
 		esac
-- 
1.8.4.1.495.gd8d272e

```
