git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Andrew Sayers <andrew-git@pileofstuff.org>
To: Thomas Rast <trast@student.ethz.ch>
Cc: "Shawn O. Pearce" <spearce@spearce.org>, git@vger.kernel.org
Subject: [PATCH] bash completion: Support "divergence from upstream" messages in __git_ps1
Date: Sat, 12 Jun 2010 21:50:51 +0100	[thread overview]
Message-ID: <4C13F32B.7060106@pileofstuff.org> (raw)
In-Reply-To: <cover.1276336602.git.trast@student.ethz.ch>

Add a notification in the command prompt specifying whether (and optionally how
far) your branch has diverged from its upstream.  This is especially helpful in
small teams that very frequently (forget to) push to each other.

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

Also provide ways for the user to specify a custom upstream, or code that
figures out the upstream.

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

This is based largely on Thomas' patch, but with some significant
differences.  Thanks once again Thomas.

I've made the quieter </> behaviour the default.  A major use case for
me will be over-the-shoulder checking for the rest of my team - I can
probably add a couple of characters to their prompts without raising
any eyebrows, but " u+1-2" is enough UI to provoke people's curiosity.
If they're not interested in this feature, it will be harder for me to
justify 6+ interesting characters than 2 boring ones.  I haven't gone
with Steven's ↑/↓ idea because I don't want to field complaints
about "my terminal is Unicode-aware but those characters are
unreadable in my default font".  I'd rather people edit the source for
that sort of thing.

I've added a message in the "equal to upstream" case, to differentiate
it from the "no upstream" case.  Again, this is an over-the-shoulder
issue - when I see an "=" (or " u=") in someone's prompt, I don't have
to patronise them about whether they've e.g. misconfigured their
branch.

I've added a legacy mode to make the script work without "git rev-list
--count".  I really like the "git rev-list --count" option, but
getting my team to run a patched version of git would be quite a bit
more trouble than it's worth.  If people strongly object to this
feature then I can hide it better or remove it from the public patch.

The documentation for the "legacy" option currently reads "don't use
the '--count' option available in recent versions of git-rev-list".
If/when "--count" makes it into master, this could be changed to
"compatibility mode for git versions less than <version when --count
went in>".

I've made several efficiency improvements, only one of which is
particularly interesting: instead of doing an `echo`, the code now
sets `p=` directly.  Admittedly this is messier, but $p is dynamically
scoped and testing suggests that setting it knocks 10% or so off
run-time.

The code should now handle multiple SVN repositories, by getting all
svn-remote.*.url config options with a --get-regexp.

I like the "ref" option, but I'm not really sure when "eval" would be
useful.  I've changed it here to "cmd" so people are encouraged to put
their work in a script.

I've tried to take Szeder's comments on board, but I'm not really sure
what the problem with unnecessary empty lines is.  If this is a
convention I'm not aware of, could you explain in a bit more detail?

	- Andrew

 contrib/completion/git-completion.bash |  144 +++++++++++++++++++++++++++++++-
 1 files changed, 143 insertions(+), 1 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 57245a8..7e40f65 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -42,6 +42,23 @@
 #       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 between HEAD and its
+#       upstream, set GIT_PS1_SHOWUPSTREAM to a nonempty value.  A "<"
+#       indicates you are behind, ">" indicates you are ahead, and
+#       "<>" indicates you have diverged.  You can further control the
+#       output by setting GIT_PS1_SHOWUPSTREAM to a space-separated
+#       list of values:
+#           git           compare HEAD to @{upstream}
+#           svn           compare HEAD to your SVN upstream
+#           ref=<ref>     compare HEAD to <ref>
+#           cmd=<command> compare HEAD to the output of <command>
+#           verbose       show number of commits ahead/behind (+/-) upstream
+#           legacy        don't use the '--count' option available in recent
+#                         versions of git-rev-list
+#       If none of 'git', 'svn', 'ref' or 'cmd' are specified, your SVN
+#       upstream will be used if configured, or your git upstream otherwise.
+#
+#
 # To submit patches:
 #
 #    *) Read Documentation/SubmittingPatches
@@ -78,6 +95,126 @@ __gitdir ()
 	fi
 }
 
+# stores the divergence from upstream in $p
+# used by GIT_PS1_SHOWUPSTREAM
+__git_ps1_show_upstream ()
+{
+	local cfg=( $( git config --get-regexp '^bash\.showUpstream$|^svn-remote\..*\.url$' 2>/dev/null ) )
+	local svn_remote=() svn_url_pattern count n
+	local upstream=git legacy verbose
+
+	# get some config options from git-config
+	for (( n=0; "$n" != "${#cfg[@]}"; ++n )); do
+		case "${cfg[$n]}" in
+			bash.showUpstream)
+				GIT_PS1_SHOWUPSTREAM="${cfg[$((n+1))]}"
+				if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
+					p=""
+					return
+				fi
+				;;
+			svn-remote.*.url)
+				svn_remote[ $(( ${#svn_remote[@]} + 1 )) ]="${cfg[$((n+1))]}"
+				svn_url_pattern+="\\|${cfg[$((n+1))]}"
+				upstream=svn # default upstream is SVN if available
+				;;
+		esac
+	done
+
+	# parse configuration values
+	for option in ${GIT_PS1_SHOWUPSTREAM}; do
+		case "$option" in
+			git|svn|"ref="*|"cmd="*) upstream="$option" ;;
+			verbose) verbose=1 ;;
+			legacy)  legacy=1  ;;
+		esac
+	done
+
+	# Find our upstream
+	case "$upstream" in
+		git)    upstream="@{upstream}" ;;
+		ref\=*) upstream="${option:4}" ;;
+		cmd\=*) upstream=$( "${option:4}" ) ;;
+		svn)
+			# get the upstream from the "git-svn-id: ..." in a commit message
+			# (git-svn uses essentially the same procedure internally)
+			upstream=( $(git log --first-parent -1 \
+					--grep="^git-svn-id: \(${svn_url_pattern:2}\)") )
+			if [[ -n "$upstream" ]]; then
+				upstream=${upstream[ ${#upstream[@]} - 2 ]}
+				upstream=${upstream%@*}
+				for (( n=1; "$n" <= "${#svn_remote[@]}"; ++n )); do
+					upstream=${upstream#${svn_remote[$n]}}
+				done
+
+				if [[ -z "$upstream" ]]; then
+					# default branch name for checkouts with no layout:
+					upstream=${GIT_SVN_ID:-git-svn}
+				else
+					upstream=${upstream#/}
+				fi
+
+			fi
+			;;
+	esac
+
+	# Find how many commits we are ahead/behind our upstream
+	if [[ -z "$legacy" ]]; then
+		count="$(git rev-list --count --left-right \
+				"$upstream"...HEAD 2>/dev/null)"
+	else
+		# produce equivalent output to --count for older versions of git
+		local commits
+		if commits="$( git rev-list --left-right "$upstream"...HEAD 2>/dev/null )"
+		then
+			local commit behind=0 ahead=0
+			for commit in $commits
+			do
+				case "$commit" in
+					"<"*) let ++behind
+						;;
+					*)    let ++ahead
+						;;
+				esac
+			done
+			count="$behind	$ahead"
+		else
+			count=""
+		fi
+	fi
+
+	# calculate the result
+	if [[ -z "$verbose" ]]; then
+		case "$count" in
+			"") # no upstream
+				p="" ;;
+			"0	0") # equal to upstream
+				p="=" ;;
+			"0	"*) # ahead of upstream
+				p=">" ;;
+			*"	0") # behind upstream
+				p="<" ;;
+			*)	    # diverged from upstream
+				p="<>" ;;
+		esac
+	else
+		case "$count" in
+			"") # no upstream
+				p="" ;;
+			"0	0") # equal to upstream
+				p=" u=" ;;
+			"0	"*) # ahead of upstream
+				p=" u+${count#0	}" ;;
+			*"	0") # behind upstream
+				p=" u-${count%	0}" ;;
+			*)	    # diverged from upstream
+				p=" u+${count#*	}-${count%	*}" ;;
+		esac
+	fi
+
+}
+
+
 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
 # returns text to add to bash PS1 prompt (includes branch name)
 __git_ps1 ()
@@ -132,6 +269,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 +297,14 @@ __git_ps1 ()
 			      u="%"
 			   fi
 			fi
+
+			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
+				__git_ps1_show_upstream
+			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"
 	fi
 }
 
-- 
1.7.0.4

  parent reply	other threads:[~2010-06-12 20:51 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-06  0:05 [PATCH] bash completion: Support "unpushed commits" warnings in __git_ps1 Andrew Sayers
2010-06-06 18:14 ` Thomas Rast
2010-06-06 20:49   ` Andrew Sayers
2010-06-06 21:07     ` Jakub Narebski
2010-06-06 22:19       ` Andrew Sayers
2010-06-07  7:42     ` Thomas Rast
2010-06-08 21:36       ` [RFC/PATCHv2] bash completion: Support "divergence from upstream" " Andrew Sayers
2010-06-09  8:21         ` Peter Kjellerstedt
2010-06-09  8:45         ` John Tapsell
2010-06-09 21:02           ` Steven Michalske
2010-06-09  9:17         ` Michael J Gruber
2010-06-09 20:48           ` Michael J Gruber
2010-06-09 21:03             ` Michael J Gruber
2010-06-10 11:47         ` [PATCH 0/2] " Thomas Rast
2010-06-10 11:47           ` [PATCH 1/2] rev-list: introduce --count option Thomas Rast
2010-06-10 11:47           ` [PATCH 2/2] bash completion: Support "divergence from upstream" warnings in __git_ps1 Thomas Rast
2010-06-12  0:00             ` SZEDER Gábor
2010-06-12 10:03               ` [PATCH v2 0/2] " Thomas Rast
2010-06-12  9:59                 ` [PATCH v2 1/2] rev-list: introduce --count option Thomas Rast
2010-06-12  9:59                 ` [PATCH v2 2/2] bash completion: Support "divergence from upstream" warnings in __git_ps1 Thomas Rast
2010-06-14  3:13                   ` Junio C Hamano
2010-06-14  7:44                     ` Thomas Rast
2010-06-14 12:36                   ` SZEDER Gábor
2010-06-12 10:11                 ` vger doesn't like UTF-8 from send-email Thomas Rast
2010-06-12 15:06                   ` [PATCH] send-email: ask about and declare 8bit mails Thomas Rast
2010-06-12 16:28                     ` Junio C Hamano
2010-06-13 15:09                       ` Thomas Rast
2010-06-13  4:15                   ` vger doesn't like UTF-8 from send-email Michael Witten
2010-06-14 11:57                     ` Erik Faye-Lund
2010-06-12 20:50                 ` Andrew Sayers [this message]
2010-06-14  7:42                   ` [PATCH] bash completion: Support "divergence from upstream" messages in __git_ps1 Thomas Rast
2010-06-15 21:50                     ` [PATCHv4] " Andrew Sayers
2010-06-16 19:05                       ` Junio C Hamano
2010-06-16 19:11                         ` Thomas Rast
2010-06-17 21:31                         ` [PATCHv5 0/2] " Andrew Sayers
2010-06-18 16:10                           ` Junio C Hamano
2010-06-18 21:02                             ` Andrew Sayers
2010-06-17 21:32                         ` [PATCHv5 1/2] " Andrew Sayers
2010-06-17 21:32                         ` [PATCHv5 2/2] bash-completion: Fix __git_ps1 to work with "set -u" Andrew Sayers
2010-06-10 13:31           ` [PATCH 0/2] bash completion: Support "divergence from upstream" warnings in __git_ps1 Michael J Gruber
2010-06-10 12:03         ` [RFC/PATCHv2] " Thomas Rast
2010-06-06 20:12 ` [PATCH] bash completion: Support "unpushed commits" " Thomas Rast
  -- strict thread matches above, loose matches on Subject: below --
2010-06-23 22:09 What's cooking in git.git (Jun 2010, #04; Wed, 23) Junio C Hamano
     [not found] ` <7veifxe63j.fsf@alter.siamese.dyndns.org>
2010-06-23 22:54   ` [PATCH] bash completion: Support "divergence from upstream" messages in __git_ps1 Shawn O. Pearce

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4C13F32B.7060106@pileofstuff.org \
    --to=andrew-git@pileofstuff.org \
    --cc=git@vger.kernel.org \
    --cc=spearce@spearce.org \
    --cc=trast@student.ethz.ch \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).