threads / patch / 24021

patchbash completion: Support "unpushed commits" warnings in __git_ps1

Subject: [PATCH] bash completion: Support "unpushed commits" warnings in __git_ps1

## tl;dr

20 messages between Jun 6, 2010 and Jun 12, 2010. Diffs are folded; open one to read it.

replies: 19people: 8as markdown or json

Andrew Sayers· Jun 6, 2010, 00:05 UTC · lore

People working in small teams sometimes forget to push their changes, causing general confusion. A gentle reminder in the command prompt should help.

Users migrating from centralised version control systems are especially likely to forget, so I've included git-svn support as a special case. Only SVN is supported because it's the only centralised version control system I have any experience with. The code is designed to make adding other version control systems easy for anyone that's interested.

Signed-off-by: Andrew Sayers <andrew-git@pileofstuff.org>
---
 contrib/completion/git-completion.bash |   33 ++++++++++++++++++++++++++++++-
 1 files changed, 31 insertions(+), 2 deletions(-)
Show changes to contrib/completion/git-completion.bash +31 −2
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index d3fec32..4bb0fee 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -48,6 +48,12 @@
 #       set GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're
 #       untracked files, then a '%' will be shown next to the branch name.
 #
+#       If you would like to see if there're unpushed commits, then
+#       you can set GIT_PS1_SHOWUNPUSHED to a nonempty value. If
+#       there're unpushed commits, then a '!' will be shown next to
+#       the branch name.  Setting GIT_PS1_SHOWUNPUSHED=svn will look
+#       for unpushed git-svn commits.
+#
 # To submit patches:
 #
 #    *) Read Documentation/SubmittingPatches
@@ -138,6 +144,7 @@ __git_ps1 ()
 		local s
 		local u
 		local c
+		local p
 
 		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
 			if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
@@ -167,12 +174,34 @@ __git_ps1 ()
 			      u="%"
 			   fi
 			fi
+
+			if [ -n "${GIT_PS1_SHOWUNPUSHED-}" ]; then
+			   local head
+			   local upstream
+			   if [ "${GIT_PS1_SHOWUNPUSHED-}" = "svn" ]; then # git-svn upstream checking
+			      local remote_branch=$( git config --get svn-remote.svn.url | sed 's/\//\\\//g' )
+			      upstream=$( git log | sed -ne "/^    git-svn-id: / { s/^    git-svn-id: $remote_branch\/\([^@]*\).*/\1/p ; q }" )
+			   else # git upstream checking
+			      upstream="@{upstream}"
+			   fi
+
+			   if git rev-parse --quiet "$upstream" HEAD 2>/dev/null | {
+			      read upstream
+			      read head
+			      [ -n "$head" -a -n "$upstream" -a "$head" != "$upstream" ]
+			   }; then
+			       p='!'
+			   else
+			       p=
+			   fi
+			fi
+
 		fi
 
 		if [ -n "${1-}" ]; then
-			printf "$1" "$c${b##refs/heads/}$w$i$s$u$r"
+			printf "$1" "$c${b##refs/heads/}$w$i$s$u$r$p"
 		else
-			printf " (%s)" "$c${b##refs/heads/}$w$i$s$u$r"
+			printf " (%s)" "$c${b##refs/heads/}$w$i$s$u$r$p"
 		fi
 	fi
 }
Thomas Rast· Jun 6, 2010, 18:14 UTC · re: Andrew Sayers · lore

Re: [PATCH] bash completion: Support "unpushed commits" warnings in __git_ps1

Andrew Sayers wrote:
> People working in small teams sometimes forget to push their changes, causing
> general confusion.  A gentle reminder in the command prompt should help.
I think this is a good idea but...
Show 5 quoted lines
> +#       If you would like to see if there're unpushed commits, then
> +#       you can set GIT_PS1_SHOWUNPUSHED to a nonempty value. If
> +#       there're unpushed commits, then a '!' will be shown next to
> +#       the branch name.  Setting GIT_PS1_SHOWUNPUSHED=svn will look
> +#       for unpushed git-svn commits.

At least the svn/git setting should definitely be configurable through git-config, so that it becomes per-repo configurable. Also, you could try to trigger the svn mode by default if a svn-remote.svn.url exists.

> +			      upstream=$( git log | sed -ne "/^    git-svn-id: / { s/^    git-svn-id: $remote_branch\/\([^@]*\).*/\1/p ; q }" )
How about starting with
  git log -1 --first-parent --grep="^git-svn-id: $remote_branch"

which should be faster and is also closer to the truth. It still doesn't work if the repo was cloned with --no-metadata. Doing it right probably would require a new informational command in git-svn that reports the branch name.

-- 
Thomas Rast
trast@{inf,student}.ethz.ch
Andrew Sayers· Jun 6, 2010, 20:49 UTC · re: Thomas Rast · lore

Re: [PATCH] bash completion: Support "unpushed commits" warnings in __git_ps1

On 06/06/10 19:14, Thomas Rast wrote:
Show 5 quoted lines
> 
> At least the svn/git setting should definitely be configurable through
> git-config, so that it becomes per-repo configurable.  Also, you could
> try to trigger the svn mode by default if a svn-remote.svn.url exists.
> 

I'm not averse to adding a config setting, or to exploring the "--no-metadata" case. I am concerned about the strict time constraints for code called in a command prompt though. For instance, calling git-svn could easily make your prompt take over half a second to respond because of all the Perl you'd have to compile. I'd also want a few real-world use cases and a nod from Shawn before diverging any further from the existing GIT_PS1_FOO= convention.

Having said all that, I'm quite happy to resubmit with GIT_PS1_SHOWUNPUSHED=svn treated like GIT_PS1_SHOWUNPUSHED=1 when $GIT_DIR/svn doesn't exist.

Show 8 quoted lines
> How about starting with
> 
>   git log -1 --first-parent --grep="^git-svn-id: $remote_branch"
> 
> which should be faster and is also closer to the truth.  It still
> doesn't work if the repo was cloned with --no-metadata.  Doing it
> right probably would require a new informational command in git-svn
> that reports the branch name.
Good idea about "-1" and "--grep", but multi-parent commits aren't
compatible with SVN, so I don't see how "--first-parent" is useful here?
 In fact I'm tempted to look for merge commits and print a big warning
when one is found.

I don't see how to actually split out the branch name without calling sed or using one of the disallowed ${} constructs. So I compared the following commands on my PC (2.1 GHz dual-core Athlon) using a repository with 100 empty commits between HEAD and a git-svn-id:

time git log | \
	sed -ne "/^    git-svn-id: / { s/^    git-svn-id: "\
	"$remote_branch\/\([^@]*\).*/\1/p ; q }"
time git log -1 --grep="^git-svn-id: $remote_branch" | \
	sed -ne "s/^    git-svn-id: $remote_branch\/\([^@]*\).*/\1/p"

The former averaged about 0.017 seconds, whereas the latter averaged about 0.015. So when I resubmit this patch, I'll include your version if merge commits are somehow useful to SVNers, or a merge warning otherwise.

Incidentally, a quick bit of etiquette - would you rather I submitted an RFC patch or two with these changes, or wait until the discussion has reached a more stable point?

	- Andrew
Jakub Narebski· Jun 6, 2010, 21:07 UTC · re: Andrew Sayers · lore

Re: [PATCH] bash completion: Support "unpushed commits" warnings in __git_ps1

Andrew Sayers <andrew-git@pileofstuff.org> writes:
Show 11 quoted lines
> I don't see how to actually split out the branch name without calling
> sed or using one of the disallowed ${} constructs.  So I compared the
> following commands on my PC (2.1 GHz dual-core Athlon) using a
> repository with 100 empty commits between HEAD and a git-svn-id:
> 
> time git log | \
> 	sed -ne "/^    git-svn-id: / { s/^    git-svn-id: "\
> 	"$remote_branch\/\([^@]*\).*/\1/p ; q }"
> 
> time git log -1 --grep="^git-svn-id: $remote_branch" | \
> 	sed -ne "s/^    git-svn-id: $remote_branch\/\([^@]*\).*/\1/p"
What disallowed ${} constructs?

First, ${var#pattern}, ${var##pattern}, ${var%pattern} and ${var%%pattern} are POSIX, so you can use it in git shell scripts.

Second, this is _bash_ completion, so you can use also bash-specific expansion, like ${var/pattern/string} or ${var//pattern/string}.

-- 
Jakub Narebski
Poland
ShadeHawk on #git
Andrew Sayers· Jun 6, 2010, 22:19 UTC · re: Jakub Narebski · lore

Re: [PATCH] bash completion: Support "unpushed commits" warnings in __git_ps1

On 06/06/10 22:07, Jakub Narebski wrote:
> 
> What disallowed ${} constructs?

My mistake, I misread that part of CodingGuidelines. The following bash-specific implementation takes about 0.013 seconds:

BRANCH=( $( git log --first-parent -1 \
		--grep="^git-svn-id: $remote_branch" ) )
BRANCH=${BRANCH[ ${#BRANCH[@]} - 2 ]}
BRANCH=${BRANCH%@*}
BRANCH=${BRANCH#*$remote_branch/}
Using an array here saves about 0.001 of a second.
	- Andrew
Thomas Rast· Jun 7, 2010, 07:42 UTC · re: Andrew Sayers · lore

Re: [PATCH] bash completion: Support "unpushed commits" warnings in __git_ps1

Andrew Sayers wrote:
Show 6 quoted lines
> 
> I'm not averse to adding a config setting, or to exploring the
> "--no-metadata" case.  I am concerned about the strict time constraints
> for code called in a command prompt though.  For instance, calling
> git-svn could easily make your prompt take over half a second to respond
> because of all the Perl you'd have to compile.
True, I forgot about that.
> I'd also want a few
> real-world use cases and a nod from Shawn before diverging any further
> from the existing GIT_PS1_FOO= convention.
> Having said all that, I'm quite happy to resubmit with
> GIT_PS1_SHOWUNPUSHED=svn treated like GIT_PS1_SHOWUNPUSHED=1 when
> $GIT_DIR/svn doesn't exist.

There's the precedent of bash.showDirtyState that allows you to disable the +* indicators _per repository_. The latter idea also sounds reasonable, though I personally would find the configuration more useful. I have several repos that push to both git (for real work) and SVN (for buildbots) where I'd prefer the indicator to refer to git.

Show 8 quoted lines
> > How about starting with
> > 
> >   git log -1 --first-parent --grep="^git-svn-id: $remote_branch"
> 
> Good idea about "-1" and "--grep", but multi-parent commits aren't
> compatible with SVN, so I don't see how "--first-parent" is useful here?
>  In fact I'm tempted to look for merge commits and print a big warning
> when one is found.
Please don't; you'd immediately lose me as a user :-)

First, --first-parent is actually how git-svn looks for the git-svn-id, and any other search is liable to turn up wrong results. See working_head_info in git-svn.perl.

Second, the behaviour of git-svn with merges is very useful and well-defined. (Yes, there are stern warnings in the manpage, but if you know what you're doing just ignore them...)

'git svn dcommit' will first determine the correct upstream branch according to the above logic, i.e., it *always* commits to the first-parent ancestry. Then it commits the diff along this line of history (i.e., always diffing to the first parent) but it does rewrite the commits so that merges are preserved.

In pictures, if you have
    o---B---o---o---o    trunk
         \
          *---*---*---*    topic
and run 'git checkout trunk; git merge topic; git svn dcommit' you get
    o---B---o---o---o---M'    trunk
         \             /
          *---*---*---*    topic

(writing M' since this is already the rewritten version of an original merge M). M' looks to SVN like a 'git merge --squash topic' because git-svn doesn't (yet?) store the merge info.

So you get to keep the git merge info, which is very useful for later merges of 'topic'.

Note: NEVER run 'git checkout topic; git merge trunk'.  git-svn will
think (because of the first-parent rule) that you want to commit on
top of B!  I don't know what happens next but I expect it won't be too
pretty.
-- 
Thomas Rast
trast@{inf,student}.ethz.ch
Andrew Sayers· Jun 8, 2010, 21:36 UTC · re: Thomas Rast · lore

[RFC/PATCHv2] bash completion: Support "divergence from upstream" warnings in __git_ps1

Add a notification in the command prompt specifying whether you're ahead of (>), behind (<), diverged from (<>) or at (=) your upstream. This is especially helpful in small teams that (forget to) push to each other very frequently.

Support git-svn upstream detection as a special case, as migraters from centralised version control systems are especially likely to forget to push.

Support for other types of upstream than SVN should be easy to add if anyone is so inclined.

Signed-off-by: Andrew Sayers <andrew-git@pileofstuff.org>
---
This patch includes Thomas Rast's feedback - thanks Thomas for the education :)

This patch makes unashamed use of shell arrays and substring expansion that would normally not be allowed. As Jakub Narebski mentioned, this is probably ok in a bash-specific script.

Unlike other prompt options, I've put the divergence characters on the left of the branch name. I'm really not sure about this, and I'd like to hear people's opinions.

This patch produces output like this when I have unpushed commits:
[andrew@pc myrepo >master] # my master is ahead of upstream

Intuitively, I like having a ">" when I'm ahead, although it would be more logical to have something like this:

[andrew@pc myrepo <master] # upstream less-than master

Putting the symbol on the right makes this problem go away, but looks ridiculous if you use a prompt like PS1='\W:$(__git_ps1 "(%s)")> '

myrepo:master>> # master greater-than upstream myrepo:master<> # master less-than upstream myrepo:master<>> # master and upstream have diverged

I'd rather not rely on colour prompts to clear this up - using colour as the only way to convey important information to the user rarely ends well.

Adding a "u" to the symbol could also clear this up:
[andrew@pc myrepo u<master] # upstream less-than master

Using "u<", "u=", "u>" and "<>" would mean that the prompt always used either two or zero characters, which would keep prompts lined up over time. But it would also eat horizontal space for an issue you'd stop seeing after a few minutes.

Finally, my apologies to anyone that tried to apply my previous patch - to make a long story short, it turns out I need this feature more than I realised :)

 contrib/completion/git-completion.bash |   58 +++++++++++++++++++++++++++++++-
 1 files changed, 57 insertions(+), 1 deletions(-)
Show changes to contrib/completion/git-completion.bash +57 −1
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 57245a8..1dc80fd 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -42,6 +42,14 @@
 #       set GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're
 #       untracked files, then a '%' will be shown next to the branch name.
 #
+#       If you would like to see the difference bitween HEAD and its
+#       upstream, set GIT_PS1_SHOWUPSTREAM to a nonempty value.
+#       Unpushed commits (>), unmerged commits (<), both (<>) and
+#       neither (=) will be shown on the left of the branch name.  You
+#       can enable git-svn mode by setting GIT_PS1_SHOWUPSTREAM=svn
+#       and set the value per-repository with the bash.showUpstream
+#       variable.
+#
 # To submit patches:
 #
 #    *) Read Documentation/SubmittingPatches
@@ -132,6 +140,7 @@ __git_ps1 ()
 		local s
 		local u
 		local c
+		local p
 
 		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
 			if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
@@ -159,10 +168,57 @@ __git_ps1 ()
 			      u="%"
 			   fi
 			fi
+
+			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
+
+				# Note: 'p' is used as a temporary throughout this block,
+				# before finally being assigned its correct value
+
+				if p="$(git config --get bash.showUpstream)"
+				then
+					GIT_PS1_SHOWUPSTREAM="$p"
+				fi
+
+				local upstream
+
+				if [ "${GIT_PS1_SHOWUPSTREAM-}" = "svn" ]; then
+
+					# git-svn upstream checking
+					p="$( git config --get svn-remote.svn.url )"
+					upstream=( $( git log --first-parent -1 \
+						--grep="^git-svn-id: $p" ) )
+					upstream=${upstream[ ${#upstream[@]} - 2 ]}
+					upstream=${upstream%@*}
+					upstream=${upstream#*$p/}
+
+				else # git upstream checking
+				  upstream="@{upstream}"
+				fi
+
+				if p="$( git rev-list \
+						--left-right "$upstream"...HEAD 2>/dev/null )"
+				then
+					case "$p" in
+						\<*\>*|\>*\<* ) p="<>" ;;
+						*\<*          ) p="<"  ;;
+						*\>*          ) p=">"  ;;
+						""            ) p="="  ;;
+
+						# the following case shouldn't be possible
+						# if you see this, please report it as a bug
+						* ) p="?ERROR($p)?" ;;
+
+					esac
+				else
+					p=""
+				fi
+
+			fi
+
 		fi
 
 		local f="$w$i$s$u"
-		printf "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r"
+		printf "${1:- (%s)}" "$c$p${b##refs/heads/}${f:+ $f}$r"
 	fi
 }
 
-- 
1.7.0.4
Peter Kjellerstedt· Jun 9, 2010, 08:21 UTC · re: Andrew Sayers · lore

RE: [RFC/PATCHv2] bash completion: Support "divergence from upstream" warnings in __git_ps1

Show 76 quoted lines
> -----Original Message-----
> From: git-owner@vger.kernel.org [mailto:git-owner@vger.kernel.org] On
> Behalf Of Andrew Sayers
> Sent: den 8 juni 2010 23:37
> To: Thomas Rast
> Cc: Shawn O. Pearce; Git Mailing List
> Subject: [RFC/PATCHv2] bash completion: Support "divergence from
> upstream" warnings in __git_ps1
> 
> Add a notification in the command prompt specifying whether you're ahead of
> (>), behind (<), diverged from (<>) or at (=) your upstream.  This is
> especially helpful in small teams that (forget to) push to each other very
> frequently.
> 
> Support git-svn upstream detection as a special case, as migraters from
> centralised version control systems are especially likely to forget to push.
> 
> Support for other types of upstream than SVN should be easy to add if anyone is
> so inclined.
> 
> Signed-off-by: Andrew Sayers <andrew-git@pileofstuff.org>
> ---
> 
> This patch includes Thomas Rast's feedback - thanks Thomas for the education :)
> 
> This patch makes unashamed use of shell arrays and substring expansion that
> would normally not be allowed.  As Jakub Narebski mentioned, this is probably ok
> in a bash-specific script.
> 
> Unlike other prompt options, I've put the divergence characters on the left of the
> branch name.  I'm really not sure about this, and I'd like to hear people's
> opinions.
> 
> This patch produces output like this when I have unpushed commits:
> 
> [andrew@pc myrepo >master] # my master is ahead of upstream
> 
> Intuitively, I like having a ">" when I'm ahead, although it would be more
> logical to have something like this:
> 
> [andrew@pc myrepo <master] # upstream less-than master
> 
> Putting the symbol on the right makes this problem go away, but looks ridiculous
> if you use a prompt like PS1='\W:$(__git_ps1 "(%s)")> '
> 
> myrepo:master>> # master greater-than upstream
> myrepo:master<> # master less-than upstream
> myrepo:master<>> # master and upstream have diverged
> 
> I'd rather not rely on colour prompts to clear this up - using colour as the
> only way to convey important information to the user rarely ends well.
> 
> Adding a "u" to the symbol could also clear this up:
> 
> [andrew@pc myrepo u<master] # upstream less-than master
> 
> Using "u<", "u=", "u>" and "<>" would mean that the prompt always used either
> two or zero characters, which would keep prompts lined up over time. But it
> would also eat horizontal space for an issue you'd stop seeing after a few
> minutes.
> 
> Finally, my apologies to anyone that tried to apply my previous patch - to make
> a long story short, it turns out I need this feature more than I realised :)
> 
>  contrib/completion/git-completion.bash |   58 +++++++++++++++++++++++++++++++-
>  1 files changed, 57 insertions(+), 1 deletions(-)
> 
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 57245a8..1dc80fd 100755
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -42,6 +42,14 @@
>  #       set GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're
>  #       untracked files, then a '%' will be shown next to the branch name.
>  #
> +#       If you would like to see the difference bitween HEAD and its
Typo: bitween -> between
Show 79 quoted lines
> +#       upstream, set GIT_PS1_SHOWUPSTREAM to a nonempty value.
> +#       Unpushed commits (>), unmerged commits (<), both (<>) and
> +#       neither (=) will be shown on the left of the branch name.  You
> +#       can enable git-svn mode by setting GIT_PS1_SHOWUPSTREAM=svn
> +#       and set the value per-repository with the bash.showUpstream
> +#       variable.
> +#
>  # To submit patches:
>  #
>  #    *) Read Documentation/SubmittingPatches
> @@ -132,6 +140,7 @@ __git_ps1 ()
>  		local s
>  		local u
>  		local c
> +		local p
> 
>  		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
>  			if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
> @@ -159,10 +168,57 @@ __git_ps1 ()
>  			      u="%"
>  			   fi
>  			fi
> +
> +			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
> +
> +				# Note: 'p' is used as a temporary throughout this block,
> +				# before finally being assigned its correct value
> +
> +				if p="$(git config --get bash.showUpstream)"
> +				then
> +					GIT_PS1_SHOWUPSTREAM="$p"
> +				fi
> +
> +				local upstream
> +
> +				if [ "${GIT_PS1_SHOWUPSTREAM-}" = "svn" ]; then
> +
> +					# git-svn upstream checking
> +					p="$( git config --get svn-remote.svn.url )"
> +					upstream=( $( git log --first-parent -1 \
> +						--grep="^git-svn-id: $p" ) )
> +					upstream=${upstream[ ${#upstream[@]} - 2 ]}
> +					upstream=${upstream%@*}
> +					upstream=${upstream#*$p/}
> +
> +				else # git upstream checking
> +				  upstream="@{upstream}"
> +				fi
> +
> +				if p="$( git rev-list \
> +						--left-right "$upstream"...HEAD 2>/dev/null )"
> +				then
> +					case "$p" in
> +						\<*\>*|\>*\<* ) p="<>" ;;
> +						*\<*          ) p="<"  ;;
> +						*\>*          ) p=">"  ;;
> +						""            ) p="="  ;;
> +
> +						# the following case shouldn't be possible
> +						# if you see this, please report it as a bug
> +						* ) p="?ERROR($p)?" ;;
> +
> +					esac
> +				else
> +					p=""
> +				fi
> +
> +			fi
> +
>  		fi
> 
>  		local f="$w$i$s$u"
> -		printf "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r"
> +		printf "${1:- (%s)}" "$c$p${b##refs/heads/}${f:+ $f}$r"
>  	fi
>  }
> 
> --
> 1.7.0.4
//Peter
John Tapsell· Jun 9, 2010, 08:45 UTC · re: Andrew Sayers · lore

Re: [RFC/PATCHv2] bash completion: Support "divergence from upstream" warnings in __git_ps1

On 9 June 2010 06:36, Andrew Sayers <andrew-git@pileofstuff.org> wrote:
> Add a notification in the command prompt specifying whether you're ahead of
> (>), behind (<), diverged from (<>) or at (=) your upstream.  This is
> especially helpful in small teams that (forget to) push to each other very
> frequently.
I hate to get all feature-bloat on you...
But could it state the number of commits as well please? :) :)
John
Steven Michalske· Jun 9, 2010, 21:02 UTC · re: John Tapsell · lore

Re: [RFC/PATCHv2] bash completion: Support "divergence from upstream" warnings in __git_ps1

On Jun 9, 2010, at 1:45 AM, John Tapsell wrote:
Show 10 quoted lines
> On 9 June 2010 06:36, Andrew Sayers <andrew-git@pileofstuff.org> wrote:
>> Add a notification in the command prompt specifying whether you're ahead of
>> (>), behind (<), diverged from (<>) or at (=) your upstream.  This is
>> especially helpful in small teams that (forget to) push to each other very
>> frequently.
> 
> I hate to get all feature-bloat on you...
> 
> But could it state the number of commits as well please? :) :)
> 
I like the idea of ahead, at, behind, and diverged
but I would want to use unicode/utf8  as my terminal supports it, would use it.

⇅ UPWARDS ARROW LEFTWARDS OF DOWNWARDS ARROW is diverged. ↑ UPWARDS ARROW master is ahead. ↓ DOWNWARDS ARROW is master is behind.

Steve
Michael J Gruber· Jun 9, 2010, 09:17 UTC · re: Andrew Sayers · lore

Re: [RFC/PATCHv2] bash completion: Support "divergence from upstream" warnings in __git_ps1

[I haven't followed the previous discussion, just this RFC.]
Andrew Sayers venit, vidit, dixit 08.06.2010 23:36:
Show 23 quoted lines
> Add a notification in the command prompt specifying whether you're ahead of
> (>), behind (<), diverged from (<>) or at (=) your upstream.  This is
> especially helpful in small teams that (forget to) push to each other very
> frequently.
> 
> Support git-svn upstream detection as a special case, as migraters from
> centralised version control systems are especially likely to forget to push.
> 
> Support for other types of upstream than SVN should be easy to add if anyone is
> so inclined.
> 
> Signed-off-by: Andrew Sayers <andrew-git@pileofstuff.org>
> ---
> 
> This patch includes Thomas Rast's feedback - thanks Thomas for the education :)
> 
> This patch makes unashamed use of shell arrays and substring expansion that
> would normally not be allowed.  As Jakub Narebski mentioned, this is probably ok
> in a bash-specific script.
> 
> Unlike other prompt options, I've put the divergence characters on the left of the
> branch name.  I'm really not sure about this, and I'd like to hear people's
> opinions.

I'd say it would make sense to you use the same order as "branch -vv" and "status -s", i.e. after the branch name.

Show 7 quoted lines
> 
> This patch produces output like this when I have unpushed commits:
> 
> [andrew@pc myrepo >master] # my master is ahead of upstream
> 
> Intuitively, I like having a ">" when I'm ahead, although it would be more
> logical to have something like this:
That is really illogical, please don't.
Show 5 quoted lines
> 
> [andrew@pc myrepo <master] # upstream less-than master
> 
> Putting the symbol on the right makes this problem go away, but looks ridiculous
> if you use a prompt like PS1='\W:$(__git_ps1 "(%s)")> '
Patient: If I move my arm like this then it hurts.
Doctor: Then don't do that ;)
Show 17 quoted lines
> 
> myrepo:master>> # master greater-than upstream
> myrepo:master<> # master less-than upstream
> myrepo:master<>> # master and upstream have diverged
> 
> I'd rather not rely on colour prompts to clear this up - using colour as the
> only way to convey important information to the user rarely ends well.
> 
> Adding a "u" to the symbol could also clear this up:
> 
> [andrew@pc myrepo u<master] # upstream less-than master
> 
> Using "u<", "u=", "u>" and "<>" would mean that the prompt always used either
> two or zero characters, which would keep prompts lined up over time.  But it
> would also eat horizontal space for an issue you'd stop seeing after a few
> minutes.
> 

I think a simple space would help already, i.e. myrepo:master<> >

An alternative would be to use something like myrepo:master+5-3

The numbers should make this distinguishable from the "--cached +". Of course it eats up more space and is slightly confusing unless it is read master = upstream + 5 -3. OTOH it is very analogous to status -s and branch -vv.

> Finally, my apologies to anyone that tried to apply my previous patch - to make
> a long story short, it turns out I need this feature more than I realised :)
> 
;)
Show 63 quoted lines
>  contrib/completion/git-completion.bash |   58 +++++++++++++++++++++++++++++++-
>  1 files changed, 57 insertions(+), 1 deletions(-)
> 
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 57245a8..1dc80fd 100755
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -42,6 +42,14 @@
>  #       set GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're
>  #       untracked files, then a '%' will be shown next to the branch name.
>  #
> +#       If you would like to see the difference bitween HEAD and its
> +#       upstream, set GIT_PS1_SHOWUPSTREAM to a nonempty value.
> +#       Unpushed commits (>), unmerged commits (<), both (<>) and
> +#       neither (=) will be shown on the left of the branch name.  You
> +#       can enable git-svn mode by setting GIT_PS1_SHOWUPSTREAM=svn
> +#       and set the value per-repository with the bash.showUpstream
> +#       variable.
> +#
>  # To submit patches:
>  #
>  #    *) Read Documentation/SubmittingPatches
> @@ -132,6 +140,7 @@ __git_ps1 ()
>  		local s
>  		local u
>  		local c
> +		local p
>  
>  		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
>  			if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
> @@ -159,10 +168,57 @@ __git_ps1 ()
>  			      u="%"
>  			   fi
>  			fi
> +
> +			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
> +
> +				# Note: 'p' is used as a temporary throughout this block,
> +				# before finally being assigned its correct value
> +
> +				if p="$(git config --get bash.showUpstream)"
> +				then
> +					GIT_PS1_SHOWUPSTREAM="$p"
> +				fi
> +
> +				local upstream
> +
> +				if [ "${GIT_PS1_SHOWUPSTREAM-}" = "svn" ]; then
> +
> +					# git-svn upstream checking
> +					p="$( git config --get svn-remote.svn.url )"
> +					upstream=( $( git log --first-parent -1 \
> +						--grep="^git-svn-id: $p" ) )
> +					upstream=${upstream[ ${#upstream[@]} - 2 ]}
> +					upstream=${upstream%@*}
> +					upstream=${upstream#*$p/}
> +
> +				else # git upstream checking
> +				  upstream="@{upstream}"
> +				fi
> +
> +				if p="$( git rev-list \
> +						--left-right "$upstream"...HEAD 2>/dev/null )"

Here you have all the info to do a "grep \<|wc -l" etc. instead of the below if you go for the +5-3.

Show 26 quoted lines
> +				then
> +					case "$p" in
> +						\<*\>*|\>*\<* ) p="<>" ;;
> +						*\<*          ) p="<"  ;;
> +						*\>*          ) p=">"  ;;
> +						""            ) p="="  ;;
> +
> +						# the following case shouldn't be possible
> +						# if you see this, please report it as a bug
> +						* ) p="?ERROR($p)?" ;;
> +
> +					esac
> +				else
> +					p=""
> +				fi
> +
> +			fi
> +
>  		fi
>  
>  		local f="$w$i$s$u"
> -		printf "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r"
> +		printf "${1:- (%s)}" "$c$p${b##refs/heads/}${f:+ $f}$r"
>  	fi
>  }
>  

Cheers, Michael

Michael J Gruber· Jun 9, 2010, 20:48 UTC · re: Michael J Gruber · lore

Re: [RFC/PATCHv2] bash completion: Support "divergence from upstream" warnings in __git_ps1

Michael J Gruber venit, vidit, dixit 09.06.2010 11:17: ...

Show 7 quoted lines
> An alternative would be to use something like
> myrepo:master+5-3
> 
> The numbers should make this distinguishable from the "--cached +". Of
> course it eats up more space and is slightly confusing unless it is read
> master = upstream + 5 -3.
> OTOH it is very analogous to status -s and branch -vv.
...
Show 11 quoted lines
>> +				if p="$( git rev-list \
>> +						--left-right "$upstream"...HEAD 2>/dev/null )"
> 
> Here you have all the info to do a "grep \<|wc -l" etc. instead of the
> below if you go for the +5-3.
> 
>> +				then
>> +					case "$p" in
>> +						\<*\>*|\>*\<* ) p="<>" ;;
>> +						*\<*          ) p="<"  ;;
>> +						*\>*          ) p=">"  ;;
I mean something like this
					case "$p" in
						\<*\>*|\>*\<* ) p="+"$(echo "$p"|grep ">"|wc -l)"-"$(echo
"$p"|grep "<"|wc -l)  ;;
						*\<*          ) p="-"$(echo "$p"|grep "<"|wc -l)  ;;
						*\>*          ) p="+"$(echo "$p"|grep ">"|wc -l)  ;;
with
		printf "${1:- (%s)}" "$c${b##refs/heads/}$p${f:+ $f}$r"
resulting in a prompt like
[mjg@localhost git (mjg/sane-symlink-diff+4-153 *+%)]
on my branch "mjg/sane-symlink-diff".

Cheers, Michael

Michael J Gruber· Jun 9, 2010, 21:03 UTC · re: Michael J Gruber · lore

Re: [RFC/PATCHv2] bash completion: Support "divergence from upstream" warnings in __git_ps1

Michael J Gruber venit, vidit, dixit 09.06.2010 22:48:
Show 6 quoted lines
> 					case "$p" in
> 						\<*\>*|\>*\<* ) p="+"$(echo "$p"|grep ">"|wc -l)"-"$(echo
> "$p"|grep "<"|wc -l)  ;;
> 						*\<*          ) p="-"$(echo "$p"|grep "<"|wc -l)  ;;
> 						*\>*          ) p="+"$(echo "$p"|grep ">"|wc -l)  ;;
> 
...make this
grep -Fc ">"
etc. rather than
grep ">"|wc -l
etc. of course. Sorry for the bash-spam :)
Michael
Thomas Rast· Jun 10, 2010, 11:47 UTC · re: Andrew Sayers · lore

[PATCH 0/2] bash completion: Support "divergence from upstream" warnings in __git_ps1

Andrew Sayers wrote:
Show 11 quoted lines
> +                                       case "$p" in
> +                                               \<*\>*|\>*\<* ) p="<>" ;;
> +                                               *\<*          ) p="<"  ;;
> +                                               *\>*          ) p=">"  ;;
> +                                               ""            ) p="="  ;;
> +
> +                                               # the following case shouldn't be possible
> +                                               # if you see this, please report it as a bug
> +                                               * ) p="?ERROR($p)?" ;;
> +
> +                                       esac

I didn't really like this bit, since rev-list has to format the output only so you can match it. I realized that I too have written code in the past to detect which way this goes (in post-receive hooks), and/or the number of commits as per the request

John Tapsell wrote:
> I hate to get all feature-bloat on you...
> 
> But could it state the number of commits as well please? :) :)

So here's an additional patch to provide rev-list logic that supports this feature. I have then tweaked your original patch to make use of it a u+7-5 style.

I'm not too picky about the exact style that it turns out to have in the end; I have used my own customized version ever since we started printing a space before the (master *+) dirtiness indicator. However, I think we should try to be as unambiguous and short as possible. Hence I picked the 'u' prefix to avoid ambiguity with the dirtiness indicator.

Andrew Sayers (1):
  bash completion: Support "divergence from upstream" warnings in
    __git_ps1
Thomas Rast (1):
  rev-list: introduce --count option
 Documentation/rev-list-options.txt     |    9 +++++
 builtin/rev-list.c                     |   16 +++++++++
 contrib/completion/git-completion.bash |   57 +++++++++++++++++++++++++++++++-
 revision.c                             |    2 +
 revision.h                             |    5 +++
 t/t6007-rev-list-cherry-pick-file.sh   |   29 ++++++++++++++++
 6 files changed, 117 insertions(+), 1 deletions(-)
Thomas Rast· Jun 10, 2010, 11:47 UTC · re: Thomas Rast · lore

[PATCH 1/2] rev-list: introduce --count option

Add a --count option that, instead of actually listing the commits, merely counts them.

This is mostly geared towards script use, and to this end it acts specially when used with --left-right: it outputs the left and right counts separately. Previously, scripts would have to run a shell loop or small inline script over to achieve the same. (Without --left-right, a simple |wc -l does the job.)

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
 Documentation/rev-list-options.txt   |    9 +++++++++
 builtin/rev-list.c                   |   16 ++++++++++++++++
 revision.c                           |    2 ++
 revision.h                           |    5 +++++
 t/t6007-rev-list-cherry-pick-file.sh |   29 +++++++++++++++++++++++++++++
 5 files changed, 61 insertions(+), 0 deletions(-)
Show changes to 5 files +61 −0

Documentation/rev-list-options.txt, builtin/rev-list.c, revision.c, revision.h, t/t6007-rev-list-cherry-pick-file.sh

diff --git a/Documentation/rev-list-options.txt b/Documentation/rev-list-options.txt
index b9fb7a8..066ade9 100644
--- a/Documentation/rev-list-options.txt
+++ b/Documentation/rev-list-options.txt
@@ -98,6 +98,15 @@ you would get an output like this:
 This implies the '--topo-order' option by default, but the
 '--date-order' option may also be specified.
 
+ifdef::git-rev-list[]
+--count::
+	Print a number stating how many commits would have been
+	listed, and suppress all other output.  When used together
+	with '--left-right', instead print the counts for left and
+	right commits, separated by a tab.
+endif::git-rev-list[]
+
+
 ifndef::git-rev-list[]
 Diff Formatting
 ~~~~~~~~~~~~~~~
diff --git a/builtin/rev-list.c b/builtin/rev-list.c
index 51ceb19..efe9360 100644
--- a/builtin/rev-list.c
+++ b/builtin/rev-list.c
@@ -50,6 +50,15 @@ static void show_commit(struct commit *commit, void *data)
 
 	graph_show_commit(revs->graph);
 
+	if (revs->count) {
+		if (commit->object.flags & SYMMETRIC_LEFT)
+			revs->count_left++;
+		else
+			revs->count_right++;
+		finish_commit(commit, data);
+		return;
+	}
+
 	if (info->show_timestamp)
 		printf("%lu ", commit->date);
 	if (info->header_prefix)
@@ -400,5 +409,12 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
 			     quiet ? finish_object : show_object,
 			     &info);
 
+	if (revs.count) {
+		if (revs.left_right)
+			printf("%d\t%d\n", revs.count_left, revs.count_right);
+		else
+			printf("%d\n", revs.count_left + revs.count_right);
+	}
+
 	return 0;
 }
diff --git a/revision.c b/revision.c
index b209d49..94fca7f 100644
--- a/revision.c
+++ b/revision.c
@@ -1146,6 +1146,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 		revs->boundary = 1;
 	} else if (!strcmp(arg, "--left-right")) {
 		revs->left_right = 1;
+	} else if (!strcmp(arg, "--count")) {
+		revs->count = 1;
 	} else if (!strcmp(arg, "--cherry-pick")) {
 		revs->cherry_pick = 1;
 		revs->limited = 1;
diff --git a/revision.h b/revision.h
index 568f1c9..bafa728 100644
--- a/revision.h
+++ b/revision.h
@@ -57,6 +57,7 @@ struct rev_info {
 			limited:1,
 			unpacked:1,
 			boundary:2,
+			count:1,
 			left_right:1,
 			rewrite_parents:1,
 			print_parents:1,
@@ -131,6 +132,10 @@ struct rev_info {
 
 	/* notes-specific options: which refs to show */
 	struct display_notes_opt notes_opt;
+
+	/* commit counts */
+	int count_left;
+	int count_right;
 };
 
 #define REV_TREE_SAME		0
diff --git a/t/t6007-rev-list-cherry-pick-file.sh b/t/t6007-rev-list-cherry-pick-file.sh
index 4b8611c..b565638 100755
--- a/t/t6007-rev-list-cherry-pick-file.sh
+++ b/t/t6007-rev-list-cherry-pick-file.sh
@@ -32,6 +32,23 @@ test_expect_success setup '
 	git tag B
 '
 
+cat >expect <<EOF
+<tags/B
+>tags/C
+EOF
+
+test_expect_success '--left-right' '
+	git rev-list --left-right B...C > actual &&
+	git name-rev --stdin --name-only --refs="*tags/*" \
+		< actual > actual.named &&
+	test_cmp actual.named expect
+'
+
+test_expect_success '--count' '
+	git rev-list --count B...C > actual &&
+	test "$(cat actual)" = 2
+'
+
 test_expect_success '--cherry-pick foo comes up empty' '
 	test -z "$(git rev-list --left-right --cherry-pick B...C -- foo)"
 '
@@ -54,4 +71,16 @@ test_expect_success '--cherry-pick with independent, but identical branches' '
 		HEAD...master -- foo)"
 '
 
+cat >expect <<EOF
+1	2
+EOF
+
+# Insert an extra commit to break the symmetry
+test_expect_success '--count --left-right' '
+	git checkout branch &&
+	test_commit D &&
+	git rev-list --count --left-right B...D > actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
1.7.1.553.ge4d5c.dirty
Thomas Rast· Jun 10, 2010, 11:47 UTC · re: Thomas Rast · lore

[PATCH 2/2] bash completion: Support "divergence from upstream" warnings in __git_ps1

From: Andrew Sayers <andrew-git@pileofstuff.org>

Add a notification in the command prompt specifying whether you're ahead of or behind your upstream. This is especially helpful in small teams that (forget to) push to each other very frequently.

Support git-svn upstream detection as a special case, as migraters from centralised version control systems are especially likely to forget to push.

Support for other types of upstream than SVN should be easy to add if anyone is so inclined.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
 contrib/completion/git-completion.bash |   57 +++++++++++++++++++++++++++++++-
 1 files changed, 56 insertions(+), 1 deletions(-)
Show changes to contrib/completion/git-completion.bash +56 −1
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index de5e6c1..49253a1 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -42,6 +42,14 @@
 #       set GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're
 #       untracked files, then a '%' will be shown next to the branch name.
 #
+#       If you would like to see the difference bitween HEAD and its
+#       upstream, set GIT_PS1_SHOWUPSTREAM to a nonempty value.  The
+#       difference will be shown as, e.g., "u+7-5" meaning that you
+#       are 7 commits ahead of and 5 commits behind the upstream.  You
+#       can enable git-svn mode by setting GIT_PS1_SHOWUPSTREAM=svn
+#       and set the value per-repository with the bash.showUpstream
+#       variable.
+#
 # To submit patches:
 #
 #    *) Read Documentation/SubmittingPatches
@@ -132,6 +140,7 @@ __git_ps1 ()
 		local s
 		local u
 		local c
+		local p
 
 		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
 			if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
@@ -159,10 +168,56 @@ __git_ps1 ()
 			      u="%"
 			   fi
 			fi
+
+			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
+
+				# Note: 'p' is used as a temporary throughout this block,
+				# before finally being assigned its correct value
+
+				if p="$(git config --get bash.showUpstream)"
+				then
+					GIT_PS1_SHOWUPSTREAM="$p"
+				fi
+
+				local upstream
+
+				if [ "${GIT_PS1_SHOWUPSTREAM-}" = "svn" ]; then
+
+					# git-svn upstream checking
+					p="$( git config --get svn-remote.svn.url )"
+					upstream=( $( git log --first-parent -1 \
+						--grep="^git-svn-id: $p" ) )
+					upstream=${upstream[ ${#upstream[@]} - 2 ]}
+					upstream=${upstream%@*}
+					upstream=${upstream#*$p/}
+
+				else # git upstream checking
+					upstream="@{upstream}"
+				fi
+
+				p=$(git rev-list --count --left-right "$upstream"...HEAD 2>/dev/null)
+				debug_p="$p"
+				case "$p" in
+				"0	0"|"") # empty means no --count support or no upstream
+					p=
+					;;
+				"0	"*)
+					p="+${p#0	}"
+					;;
+				*"	0")
+					p="-${p%	0}"
+					;;
+				*)
+					p="+${p#*	}-${p%	*}"
+					;;
+				esac
+
+			fi
+
 		fi
 
 		local f="$w$i$s$u"
-		printf "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r"
+		printf "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r${p:+ u$p}"
 	fi
 }
 
-- 
1.7.1.553.ge4d5c.dirty
SZEDER Gábor· Jun 12, 2010, 00:00 UTC · re: Thomas Rast · lore

Re: [PATCH 2/2] bash completion: Support "divergence from upstream" warnings in __git_ps1

Hi,
On Thu, Jun 10, 2010 at 01:47:24PM +0200, Thomas Rast wrote:
> From: Andrew Sayers <andrew-git@pileofstuff.org>
Show 15 quoted lines
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index de5e6c1..49253a1 100755
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -42,6 +42,14 @@
>  #       set GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're
>  #       untracked files, then a '%' will be shown next to the branch name.
>  #
> +#       If you would like to see the difference bitween HEAD and its
> +#       upstream, set GIT_PS1_SHOWUPSTREAM to a nonempty value.  The
> +#       difference will be shown as, e.g., "u+7-5" meaning that you
> +#       are 7 commits ahead of and 5 commits behind the upstream.  You
> +#       can enable git-svn mode by setting GIT_PS1_SHOWUPSTREAM=svn
> +#       and set the value per-repository with the bash.showUpstream
> +#       variable.

I find the last sentence of this description ambiguous. What value should bash.showUpstream be set to? Do I really need to set both GIT_PS1_SHOWUPSTREAM and bash.showUpstream? What if GIT_PS1_SHOWUPSTREAM=foo and bash.showUpstream=svn?

Furthermore, I think it would be good to provide means to disable this feature for some repositories while keeping it enabled for others. In the current version I could either disable or enable it globally. Perhaps we could disable it when bash.showUpstream is set to an empty value.

Show 22 quoted lines
> +#
>  # To submit patches:
>  #
>  #    *) Read Documentation/SubmittingPatches
> @@ -132,6 +140,7 @@ __git_ps1 ()
>  		local s
>  		local u
>  		local c
> +		local p
>  
>  		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
>  			if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
> @@ -159,10 +168,56 @@ __git_ps1 ()
>  			      u="%"
>  			   fi
>  			fi
> +
> +			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
> +
> +				# Note: 'p' is used as a temporary throughout this block,
> +				# before finally being assigned its correct value
> +

Back in the old days when I was just learning programming, I got my ass kicked when I dared to reuse the same variable for different purposes. C'mon, just how much shorter it is to create one more variable than this two lines long comment?! ;) It could even be squashed together with the "local upstream" line.

Show 8 quoted lines
> +				if p="$(git config --get bash.showUpstream)"
> +				then
> +					GIT_PS1_SHOWUPSTREAM="$p"
> +				fi
> +
> +				local upstream
> +
> +				if [ "${GIT_PS1_SHOWUPSTREAM-}" = "svn" ]; then

No need to use default value here, because GIT_PS1_SHOWUPSTREAM has already been set above.

Show 9 quoted lines
> +
> +					# git-svn upstream checking
> +					p="$( git config --get svn-remote.svn.url )"
> +					upstream=( $( git log --first-parent -1 \
> +						--grep="^git-svn-id: $p" ) )
> +					upstream=${upstream[ ${#upstream[@]} - 2 ]}
> +					upstream=${upstream%@*}
> +					upstream=${upstream#*$p/}
> +
Unnecessary empty lines before and after this block of code.
Show 6 quoted lines
> +				else # git upstream checking
> +					upstream="@{upstream}"
> +				fi
> +
> +				p=$(git rev-list --count --left-right "$upstream"...HEAD 2>/dev/null)
> +				debug_p="$p"
The leftover debugging mentioned by Michael.
Show 18 quoted lines
> +				case "$p" in
> +				"0	0"|"") # empty means no --count support or no upstream
> +					p=
> +					;;
> +				"0	"*)
> +					p="+${p#0	}"
> +					;;
> +				*"	0")
> +					p="-${p%	0}"
> +					;;
> +				*)
> +					p="+${p#*	}-${p%	*}"
> +					;;
> +				esac
> +
> +			fi
> +
>  		fi
Unnecessary empty lines before both fi.
Show 11 quoted lines
>  
>  		local f="$w$i$s$u"
> -		printf "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r"
> +		printf "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r${p:+ u$p}"
>  	fi
>  }
>  
> -- 
> 1.7.1.553.ge4d5c.dirty
> 
> 
Michael J Gruber· Jun 10, 2010, 13:31 UTC · re: Thomas Rast · lore

Re: [PATCH 0/2] bash completion: Support "divergence from upstream" warnings in __git_ps1

Thomas Rast venit, vidit, dixit 10.06.2010 13:47:
Show 50 quoted lines
> Andrew Sayers wrote:
>> +                                       case "$p" in
>> +                                               \<*\>*|\>*\<* ) p="<>" ;;
>> +                                               *\<*          ) p="<"  ;;
>> +                                               *\>*          ) p=">"  ;;
>> +                                               ""            ) p="="  ;;
>> +
>> +                                               # the following case shouldn't be possible
>> +                                               # if you see this, please report it as a bug
>> +                                               * ) p="?ERROR($p)?" ;;
>> +
>> +                                       esac
> 
> I didn't really like this bit, since rev-list has to format the output
> only so you can match it.  I realized that I too have written code in
> the past to detect which way this goes (in post-receive hooks), and/or
> the number of commits as per the request
> 
> John Tapsell wrote:
>> I hate to get all feature-bloat on you...
>>
>> But could it state the number of commits as well please? :) :)
> 
> So here's an additional patch to provide rev-list logic that supports
> this feature.  I have then tweaked your original patch to make use of
> it a u+7-5 style.
> 
> I'm not too picky about the exact style that it turns out to have in
> the end; I have used my own customized version ever since we started
> printing a space before the (master *+) dirtiness indicator.  However,
> I think we should try to be as unambiguous and short as possible.
> Hence I picked the 'u' prefix to avoid ambiguity with the dirtiness
> indicator.
> 
> 
> Andrew Sayers (1):
>   bash completion: Support "divergence from upstream" warnings in
>     __git_ps1
> 
> Thomas Rast (1):
>   rev-list: introduce --count option
> 
>  Documentation/rev-list-options.txt     |    9 +++++
>  builtin/rev-list.c                     |   16 +++++++++
>  contrib/completion/git-completion.bash |   57 +++++++++++++++++++++++++++++++-
>  revision.c                             |    2 +
>  revision.h                             |    5 +++
>  t/t6007-rev-list-cherry-pick-file.sh   |   29 ++++++++++++++++
>  6 files changed, 117 insertions(+), 1 deletions(-)
> 

Checked and tested. After setting DIFF, even the tests pass! But we can probably do without the debug_p line ;)

Liked-by: Michael J Gruber <git@drmicha.warpmail.net>
Thomas Rast· Jun 10, 2010, 12:03 UTC · re: Andrew Sayers · lore

Re: [RFC/PATCHv2] bash completion: Support "divergence from upstream" warnings in __git_ps1

Andrew Sayers wrote:
Show 9 quoted lines
> +				if [ "${GIT_PS1_SHOWUPSTREAM-}" = "svn" ]; then
> +
> +					# git-svn upstream checking
> +					p="$( git config --get svn-remote.svn.url )"
> +					upstream=( $( git log --first-parent -1 \
> +						--grep="^git-svn-id: $p" ) )
> +					upstream=${upstream[ ${#upstream[@]} - 2 ]}
> +					upstream=${upstream%@*}
> +					upstream=${upstream#*$p/}

This appears to be tailored to --stdlayout, and sadly doesn't work in the (probably fairly common) case of single-branch checkouts where git-svn only puts a "remotes/git-svn" branch.

I can't come up with any easy-but-robust method of checking that though. Perhaps it suffices to check that

  git config 'svn-remote.svn.(branches|tags)'
comes up empty, and in this case simply use git-svn as the remote.
-- 
Thomas Rast
trast@{inf,student}.ethz.ch
Thomas Rast· Jun 6, 2010, 20:12 UTC · re: Andrew Sayers · lore

Re: [PATCH] bash completion: Support "unpushed commits" warnings in __git_ps1

Andrew Sayers wrote:
> People working in small teams sometimes forget to push their changes, causing
> general confusion.  A gentle reminder in the command prompt should help.
[...]
> +#       there're unpushed commits, then a '!' will be shown next to

BTW, here's another idea: how about using '>' and '<' to signal being ahead and behind upstream, resp. (and both if they diverged)?

(I think the hard question here is whether it's helpful enough to weigh against the confusion of having it show nothing, because the user hasn't fetched after an update on the remote...)

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

← back to recent threads