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

22 messages from 2010-06-12 to 2010-06-18. Participants: Thomas Rast, Junio C Hamano, Andrew Sayers, Michael Witten, Erik Faye-Lund, SZEDER Gábor.
Thread: https://gitlist.dev/t/24085

## Thomas Rast, 2010-06-12 09:59

Subject: [PATCH v2 1/2] rev-list: introduce --count option
Message-ID: <74f186ce201602262fe3df71d4fa22e8184608ec.1276336602.git.trast@student.ethz.ch>
URL: https://gitlist.dev/e/74f186ce201602262fe3df71d4fa22e8184608ec.1276336602.git.trast%40student.ethz.ch
In-Reply-To: <cover.1276336602.git.trast@student.ethz.ch>

```
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(-)

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 f4b8b38..21b133c 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.561.g94582

```

## Thomas Rast, 2010-06-12 09:59

Subject: [PATCH v2 2/2] bash completion: Support "divergence from upstream" warnings in __git_ps1
Message-ID: <93842467ca22405712cab23a9b3920c106df0f17.1276336602.git.trast@student.ethz.ch>
URL: https://gitlist.dev/e/93842467ca22405712cab23a9b3920c106df0f17.1276336602.git.trast%40student.ethz.ch
In-Reply-To: <cover.1276336602.git.trast@student.ethz.ch>

```
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 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.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
 contrib/completion/git-completion.bash |   89 +++++++++++++++++++++++++++++++-
 1 files changed, 88 insertions(+), 1 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 57245a8..a6cb435 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -42,6 +42,17 @@
 #       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 one of the following:
+#           git          use @{upstream}
+#           svn          attempt to DWIM svn upstream for normal and --stdlayout
+#           ref <ref>    unconditionally use <ref>
+#           eval <code>  evaluate <code> which should print the commit to use
+#       Any other value DWIMs either svn or git, preferring svn if configured.
+#
+#       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.
+#
 # To submit patches:
 #
 #    *) Read Documentation/SubmittingPatches
@@ -78,6 +89,77 @@ __gitdir ()
 	fi
 }
 
+__git_ps1_divergence_from_upstream ()
+{
+	local cfg
+	if cfg="$(git config --get bash.showUpstream)"
+	then
+		GIT_PS1_SHOWUPSTREAM="$cfg"
+	fi
+	if [ -n "${GIT_PS1_SHOWUPSTREAM}" ]; then
+		local upstream count
+		case "${GIT_PS1_SHOWUPSTREAM}" in
+		svn|git|"ref "*|"eval "*)
+			;;
+		*)
+			# try to dwim the type
+			if git config --get svn-remote.svn.url >/dev/null; then
+				GIT_PS1_SHOWUPSTREAM=svn
+			else
+				GIT_PS1_SHOWUPSTREAM=git
+			fi
+			;;
+		esac
+		case "${GIT_PS1_SHOWUPSTREAM}" in
+		git)
+			upstream="@{upstream}"
+			;;
+		svn)
+			local url
+			# git-svn upstream checking: if it has a
+			# remotes/git-svn, that is probably the upstream.
+			# Otherwise try to figure out the branch for
+			# --stdlayout repos.
+			if ! upstream="$(git rev-parse remotes/git-svn 2>/dev/null)"; then
+				url="$(git config --get svn-remote.svn.url)"
+				upstream=( $(git log --first-parent -1 \
+						 --grep="^git-svn-id: $url") )
+				if [ -n "$upstream" ]; then
+					upstream=${upstream[ ${#upstream[@]} - 2 ]}
+					upstream=${upstream%@*}
+					upstream=${upstream#*$url/}
+				fi
+			fi
+			;;
+		"eval "*)
+			# custom shell command that determines upstream
+			upstream="$(eval "${GIT_PS1_SHOWUPSTREAM#eval }")"
+			;;
+		"ref "*)
+			upstream="${GIT_PS1_SHOWUPSTREAM#ref }"
+			;;
+		esac
+
+		count=$(git rev-list --count --left-right \
+				"$upstream"...HEAD 2>/dev/null)
+		case "$count" in
+		"0	0"|"")
+			# empty = no upstream or no --count
+			;;
+		"0	"*)
+			echo "+${count#0	}"
+			;;
+		*"	0")
+			echo "-${count%	0}"
+			;;
+		*)
+			echo "+${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 +214,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 +242,14 @@ __git_ps1 ()
 			      u="%"
 			   fi
 			fi
+
+			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
+				p="$(__git_ps1_divergence_from_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:+ u$p}"
 	fi
 }
 
-- 
1.7.1.561.g94582

```

## Thomas Rast, 2010-06-12 10:03

Subject: [PATCH v2 0/2] bash completion: Support "divergence from upstream" warnings in __git_ps1
Message-ID: <cover.1276336602.git.trast@student.ethz.ch>
URL: https://gitlist.dev/e/cover.1276336602.git.trast%40student.ethz.ch
In-Reply-To: <20100612000002.GA30196@neumann>

```
[Argh.  Or maybe it's an encoding problem?]

SZEDER Gabor wrote:
> 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.

Well, I wanted to leave this to Andrew but since I'm already messing
around with it, here's my take on it.  I might be getting a bit
feature creepy, but it should be prepared for all possible uses now.

The semantics now are that (as with e.g. GIT_PS1_SHOWDIRTYSTATE) you
have to set the environment variable to get anything, but after that,
the config *always* overrides (so you can disable again).

Furthermore, the SVN code tries remotes/git-svn first (for
single-branch clones), and there are new features to set a certain ref
or provide a small snippet of hook code.


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 |   89 +++++++++++++++++++++++++++++++-
 revision.c                             |    2 +
 revision.h                             |    5 ++
 t/t6007-rev-list-cherry-pick-file.sh   |   29 ++++++++++
 6 files changed, 149 insertions(+), 1 deletions(-)

```

## Thomas Rast, 2010-06-12 10:11

Subject: vger doesn't like UTF-8 from send-email
Message-ID: <201006121211.12870.trast@student.ethz.ch>
URL: https://gitlist.dev/e/201006121211.12870.trast%40student.ethz.ch
In-Reply-To: <cover.1276336602.git.trast@student.ethz.ch>

```
Thomas Rast wrote:
> [Argh.  Or maybe it's an encoding problem?]

First, sorry everyone on the Cc list for the triple post.  I first
blamed it on the fact that I was Cc'ing Gabor, but apparently the
problem was in the content.

The files I handed to git-send-email were UTF-8, and I used my usual
git alias to --cc Gabor on the first pass which also results in an
UTF-8 encoded name.

I got this back from our university mail server:

  git@vger.kernel.org
  vger.kernel.org #550 5.7.1 Content-Policy reject msg: Wrong MIME labeling on 8-bit character texts. BF:<H 0>; S1753608Ab0FLKCQ ##

AFAICT the original message did not declare an encoding:

  Subject: [PATCH v2 0/2] bash completion: Support "divergence from upstream" warnings in __git_ps1
  Date: Sat, 12 Jun 2010 12:02:14 +0200
  Message-ID: <cover.1276336602.git.trast@student.ethz.ch>
  X-Mailer: git-send-email 1.7.1.561.g94582
  In-Reply-To: <20100612000002.GA30196@neumann>
  References: <20100612000002.GA30196@neumann>
  MIME-Version: 1.0
  Content-Type: text/plain
  Return-Path: trast@student.ethz.ch

It's hard to be 100% sure though because in the infinite wisdom of MS
Exchange, the bounce came back with everything wrapped in a layer of
HTML(!) and declared latin-1.

Is this a new vger policy, or am I hitting a send-email bug?

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

```

## Thomas Rast, 2010-06-12 15:06

Subject: [PATCH] send-email: ask about and declare 8bit mails
Message-ID: <cebe57bb68b5e8ea445e560bbe6305c915ce8a1c.1276354971.git.trast@student.ethz.ch>
URL: https://gitlist.dev/e/cebe57bb68b5e8ea445e560bbe6305c915ce8a1c.1276354971.git.trast%40student.ethz.ch
In-Reply-To: <201006121211.12870.trast@student.ethz.ch>

```
git-send-email passes on an 8bit mail as-is even if it does not
declare a content-type.  Because the user can edit email between
format-patch and send-email, such invalid mails are unfortunately not
very hard to come by.

Make git-send-email stop and ask about the encoding to use if it
encounters any such mail.  Also provide a configuration setting to
permanently configure an encoding.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---

This takes care of what I ran into earlier today.  However, there's
another problem: format-patch doesn't even mark the patch 8bit if its
patch contents (not log message) are non-ASCII.  I'm really not sure
what to do there.

On the practical hand, there's the problem that the entire
log_tree_commit() call chain is geared towards printing on a file, at
which time it's too late.  So we would either have to go in and fix
all of that to support formatting to a strbuf, or rewrite the patch if
it turns out to be non-ASCII.

On the philosophical hand, we don't really care about file encodings
so far, but this requires declaring one.

Either way, I think if vger doesn't accept format-patch;send-email,
something is really wrong :-)


 Documentation/git-send-email.txt |    9 ++++
 git-send-email.perl              |   59 +++++++++++++++++++++++++++++
 t/t9001-send-email.sh            |   77 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 145 insertions(+), 0 deletions(-)

diff --git a/Documentation/git-send-email.txt b/Documentation/git-send-email.txt
index 12622fc..c283084 100644
--- a/Documentation/git-send-email.txt
+++ b/Documentation/git-send-email.txt
@@ -101,6 +101,15 @@ See the CONFIGURATION section for 'sendemail.multiedit'.
 +
 The --to option must be repeated for each user you want on the to list.
 
+--8bit-encoding=<encoding>::
+	When encountering a non-ASCII message or subject that does not
+	declare its encoding, add headers/quoting to indicate it is
+	encoded in <encoding>.  Default is the value of the
+	'sendemail.assume8bitEncoding'; if that is unspecified, this
+	will be prompted for if any non-ASCII files are encountered.
++
+Note that no attempts whatsoever are made to validate the encoding.
+
 
 Sending
 ~~~~~~~
diff --git a/git-send-email.perl b/git-send-email.perl
index 111c981..6b2ac79 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -54,6 +54,7 @@ git send-email [options] <file | directory | rev-list options >
     --in-reply-to           <str>  * Email "In-Reply-To:"
     --annotate                     * Review each patch that will be sent in an editor.
     --compose                      * Open an editor for introduction.
+    --8bit-encoding         <str>  * Encoding to assume 8bit mails if undeclared
 
   Sending:
     --envelope-sender       <str>  * Email envelope sender.
@@ -191,6 +192,7 @@ my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_encryption);
 my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts, $smtp_domain);
 my ($validate, $confirm);
 my (@suppress_cc);
+my ($auto_8bit_encoding);
 
 my ($debug_net_smtp) = 0;		# Net::SMTP, see send_message()
 
@@ -222,6 +224,7 @@ my %config_settings = (
     "multiedit" => \$multiedit,
     "confirm"   => \$confirm,
     "from" => \$sender,
+    "assume8bitencoding" => \$auto_8bit_encoding,
 );
 
 # Help users prepare for 1.7.0
@@ -297,6 +300,7 @@ my $rc = GetOptions("sender|from=s" => \$sender,
 		    "thread!" => \$thread,
 		    "validate!" => \$validate,
 		    "format-patch!" => \$format_patch,
+		    "8bit-encoding=s" => \$auto_8bit_encoding,
 	 );
 
 unless ($rc) {
@@ -669,6 +673,34 @@ sub ask {
 	return undef;
 }
 
+my %broken_encoding;
+
+sub file_declares_8bit_cte($) {
+	my $fn = shift;
+	open (my $fh, '<', $fn);
+	while (my $line = <$fh>) {
+		return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
+	}
+	close $fh;
+	return 0;
+}
+
+foreach my $f (@files) {
+	next unless (body_or_subject_has_nonascii($f)
+		     && !file_declares_8bit_cte($f));
+	$broken_encoding{$f} = 1;
+}
+
+if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
+	print "The following files are 8bit, but do not declare " .
+		"a Content-Transfer-Encoding.\n";
+	foreach my $f (sort keys %broken_encoding) {
+		print "    $f\n";
+	}
+	$auto_8bit_encoding = ask("Which 8bit encoding should I declare [UTF-8]? ",
+				  default => "UTF-8");
+}
+
 my $prompting = 0;
 if (!defined $sender) {
 	$sender = $repoauthor || $repocommitter || '';
@@ -1221,6 +1253,18 @@ foreach my $t (@files) {
 			or die "(cc-cmd) failed to close pipe to '$cc_cmd'";
 	}
 
+	if ($broken_encoding{$t} && !$has_content_type) {
+		$has_content_type = 1;
+		push @xh, "MIME-Version: 1.0",
+			"Content-Type: text/plain; charset=$auto_8bit_encoding",
+			"Content-Transfer-Encoding: 8bit";
+		$body_encoding = $auto_8bit_encoding;
+	}
+
+	if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
+		$subject = quote_rfc2047($subject, $auto_8bit_encoding);
+	}
+
 	if (defined $author and $author ne $sender) {
 		$message = "From: $author\n\n$message";
 		if (defined $author_encoding) {
@@ -1233,6 +1277,7 @@ foreach my $t (@files) {
 				}
 			}
 			else {
+				$has_content_type = 1;
 				push @xh,
 				  'MIME-Version: 1.0',
 				  "Content-Type: text/plain; charset=$author_encoding",
@@ -1310,3 +1355,17 @@ sub file_has_nonascii {
 	}
 	return 0;
 }
+
+sub body_or_subject_has_nonascii {
+	my $fn = shift;
+	open(my $fh, '<', $fn)
+		or die "unable to open $fn: $!\n";
+	while (my $line = <$fh>) {
+		last if $line =~ /^$/;
+		return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
+	}
+	while (my $line = <$fh>) {
+		return 1 if $line =~ /[^[:ascii:]]/;
+	}
+	return 0;
+}
diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh
index 640b3d2..0b8a591 100755
--- a/t/t9001-send-email.sh
+++ b/t/t9001-send-email.sh
@@ -918,4 +918,81 @@ test_expect_success '--no-bcc overrides sendemail.bcc' '
 	! grep "RCPT TO:<other@ex.com>" stdout
 '
 
+cat >email-using-8bit <<EOF
+From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
+Message-Id: <bogus-message-id@example.com>
+From: author@example.com
+Date: Sat, 12 Jun 2010 15:53:58 +0200
+Subject: subject goes here
+
+Dieser deutsche Text enthält einen Umlaut!
+EOF
+
+cat >content-type-decl <<EOF
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+EOF
+
+test_expect_success 'asks about and fixes 8bit encodings' '
+	clean_fake_sendmail &&
+	echo |
+	git send-email --from=author@example.com --to=nobody@example.com \
+			--smtp-server="$(pwd)/fake.sendmail" \
+			email-using-8bit >stdout &&
+	grep "do not declare a Content-Transfer-Encoding" stdout &&
+	grep email-using-8bit stdout &&
+	grep "Which 8bit encoding" stdout &&
+	grep "Content\\|MIME" msgtxt1 >actual &&
+	test_cmp actual content-type-decl
+'
+
+test_expect_success 'sendemail.8bitEncoding works' '
+	clean_fake_sendmail &&
+	git config sendemail.assume8bitEncoding UTF-8 &&
+	echo bogus |
+	git send-email --from=author@example.com --to=nobody@example.com \
+			--smtp-server="$(pwd)/fake.sendmail" \
+			email-using-8bit >stdout &&
+	grep "Content\\|MIME" msgtxt1 >actual &&
+	test_cmp actual content-type-decl
+'
+
+test_expect_success '--8bit-encoding overrides sendemail.8bitEncoding' '
+	clean_fake_sendmail &&
+	git config sendemail.assume8bitEncoding "bogus too" &&
+	echo bogus |
+	git send-email --from=author@example.com --to=nobody@example.com \
+			--smtp-server="$(pwd)/fake.sendmail" \
+			--8bit-encoding=UTF-8 \
+			email-using-8bit >stdout &&
+	grep "Content\\|MIME" msgtxt1 >actual &&
+	test_cmp actual content-type-decl
+'
+
+cat >email-using-8bit <<EOF
+From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
+Message-Id: <bogus-message-id@example.com>
+From: author@example.com
+Date: Sat, 12 Jun 2010 15:53:58 +0200
+Subject: Dieser Betreff enthält auch einen Umlaut!
+
+Nothing to see here.
+EOF
+
+cat >expected <<EOF
+Subject: =?UTF-8?q?Dieser=20Betreff=20enth=C3=A4lt=20auch=20einen=20Umlaut!?=
+EOF
+
+test_expect_success '--8bit-encoding also treats subject' '
+	clean_fake_sendmail &&
+	echo bogus |
+	git send-email --from=author@example.com --to=nobody@example.com \
+			--smtp-server="$(pwd)/fake.sendmail" \
+			--8bit-encoding=UTF-8 \
+			email-using-8bit >stdout &&
+	grep "Subject" msgtxt1 >actual &&
+	test_cmp expected actual
+'
+
 test_done
-- 
1.7.1.557.gd161

```

## Junio C Hamano, 2010-06-12 16:28

Subject: Re: [PATCH] send-email: ask about and declare 8bit mails
Message-ID: <7vljakfc64.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vljakfc64.fsf%40alter.siamese.dyndns.org
In-Reply-To: <cebe57bb68b5e8ea445e560bbe6305c915ce8a1c.1276354971.git.trast@student.ethz.ch>

```
Thomas Rast <trast@student.ethz.ch> writes:

> git-send-email passes on an 8bit mail as-is even if it does not
> declare a content-type.  Because the user can edit email between
> format-patch and send-email, such invalid mails are unfortunately not
> very hard to come by.
>
> Make git-send-email stop and ask about the encoding to use if it
> encounters any such mail.  Also provide a configuration setting to
> permanently configure an encoding.
>
> Signed-off-by: Thomas Rast <trast@student.ethz.ch>
> ---
>
> This takes care of what I ran into earlier today.  However, there's
> another problem: format-patch doesn't even mark the patch 8bit if its
> patch contents (not log message) are non-ASCII.  I'm really not sure
> what to do there.

A project won't have uniform file encoding anyway, so even if we were to
do something clever about this, it has to be per-patch.  Perhaps

 (0) use the attributes mechanism to allow projects to mark paths with
     encoding.  E.g.

	# everything in UTF-8 unless otherwise specified...
        * encoding=UTF-8
        Documentation/zh_CN/* encoding=big5

 (1) for each patch, find the paths involved, and if their encodings are
     the same, perhaps promote that as the encoding used for the entire
     message;

 (2) otherwise, if there is an 8-bit encoding involved in the paths,
     perhaps mark the entire message as 8-bit (binary???).

I have this suspicion that (2) is very rare (you cannot transmit such a
patch as a plain text message reliably afaict, so it is not done in
practice), and we would probably need to make a separate patchfile for
groups of paths in each encoding and attach them as MIME multiparts (ugh).

Just thinkning aloud, before morning caffeine sinks in, so please take
this with a grain of salt...

```

## Andrew Sayers, 2010-06-12 20:50

Subject: [PATCH] bash completion: Support "divergence from upstream" messages in __git_ps1
Message-ID: <4C13F32B.7060106@pileofstuff.org>
URL: https://gitlist.dev/e/4C13F32B.7060106%40pileofstuff.org
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

```

## Michael Witten, 2010-06-13 04:15

Subject: Re: vger doesn't like UTF-8 from send-email
Message-ID: <AANLkTim1QajqLOp4y6-oIMAGp8Tkf7z9uTH6bwIIFYkH@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTim1QajqLOp4y6-oIMAGp8Tkf7z9uTH6bwIIFYkH%40mail.gmail.com
In-Reply-To: <201006121211.12870.trast@student.ethz.ch>

```
On Sat, Jun 12, 2010 at 05:11, Thomas Rast <trast@student.ethz.ch> wrote:
> AFAICT the original message did not declare an encoding:
>
> Subject: [PATCH v2 0/2] bash completion: Support "divergence from upstream" warnings in __git_ps1
> Date: Sat, 12 Jun 2010 12:02:14 +0200
> Message-ID: <cover.1276336602.git.trast@student.ethz.ch>
> X-Mailer: git-send-email 1.7.1.561.g94582
> In-Reply-To: <20100612000002.GA30196@neumann>
> References: <20100612000002.GA30196@neumann>
> MIME-Version: 1.0
> Content-Type: text/plain
> Return-Path: trast@student.ethz.ch
> ...
> Is this a new vger policy, or am I hitting a send-email bug?

Let's assume the headers themselves are already properly encoded.

According to:

    http://www.faqs.org/rfcs/rfc2045.html

we have:

    The proper Content-Transfer-Encoding
    label must always be used.

and:

    An encoding type of 7BIT requires that
    the body is already in a 7bit mail-ready
    representation.  This is the default value
    -- that is, "Content-Transfer-Encoding: 7BIT"
    is assumed if the Content-Transfer-Encoding
    header field is not present.

Moreover, according to:

    http://www.faqs.org/rfcs/rfc2046.html

we have:

    4.1.2    Charset Parameter
    ...
    The default character set, which must be
    assumed in the absence of a charset parameter,
    is US-ASCII.

So, your email is indeed incorrect in 2 ways if the body contains
UTF-8 encoded data.

>From what I've skimmed, the mail user agent (MUA)---such as
send-email---could send your unmodified message body by producing
these headers:

    MIME-Version: 1.0
    Content-type: text/plain; charset=utf-8
    Content-transfer-encoding: 8bit

but only 7bit transfer encodings are guaranteed to make it intact to
the destination; consequently, it would probably be a good idea for
the MUA to transform your message into some 7bit encoding, preferably
a human-readable one such as the 'quoted-printable' encoding; after
such a transformation, the headers could be:

    MIME-Version: 1.0
    Content-type: text/plain; charset=utf-8
    Content-transfer-encoding: quoted-printable

Sincerely,
Michael Witten

```

## Thomas Rast, 2010-06-13 15:09

Subject: Re: [PATCH] send-email: ask about and declare 8bit mails
Message-ID: <201006131709.55335.trast@student.ethz.ch>
URL: https://gitlist.dev/e/201006131709.55335.trast%40student.ethz.ch
In-Reply-To: <7vljakfc64.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano wrote:
>  (2) otherwise, if there is an 8-bit encoding involved in the paths,
>      perhaps mark the entire message as 8-bit (binary???).
> 
> I have this suspicion that (2) is very rare (you cannot transmit such a
> patch as a plain text message reliably afaict, so it is not done in
> practice), and we would probably need to make a separate patchfile for
> groups of paths in each encoding and attach them as MIME multiparts (ugh).

So IIUC this would be the main/first obstacle?  Seeing as we seem to
do fine here but you both say 8bit is not reliable.  (According to
Wikipedia[*] all the big names support it though...)

Perhaps Quoted-Printable would work with minimal effort?  We could
leave it to send-email to do all the quoting, mailsplit or am all the
unquoting and we retain (mostly) the readability of the original
patches.

That still doesn't solve the problem that we might send (invalid utf8)
binary data declared as utf8.  I suppose to work around that, a more
elaborate approach like

>  (0) use the attributes mechanism to allow projects to mark paths with
>      encoding.  E.g.
> 
> 	# everything in UTF-8 unless otherwise specified...
>         * encoding=UTF-8
>         Documentation/zh_CN/* encoding=big5
> 
>  (1) for each patch, find the paths involved, and if their encodings are
>      the same, perhaps promote that as the encoding used for the entire
>      message;

is needed.


[*] http://en.wikipedia.org/wiki/8BITMIME#8BITMIME

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

```

## Junio C Hamano, 2010-06-14 03:13

Subject: Re: [PATCH v2 2/2] bash completion: Support "divergence from upstream" warnings in __git_ps1
Message-ID: <7v7hm2e27z.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v7hm2e27z.fsf%40alter.siamese.dyndns.org
In-Reply-To: <93842467ca22405712cab23a9b3920c106df0f17.1276336602.git.trast@student.ethz.ch>

```
Thomas Rast <trast@student.ethz.ch> writes:

> +#       If you would like to see the difference between HEAD and its upstream,
> +#       set GIT_PS1_SHOWUPSTREAM to one of the following:
> +#           git          use @{upstream}
> +#           svn          attempt to DWIM svn upstream for normal and --stdlayout
> +#           ref <ref>    unconditionally use <ref>
> +#           eval <code>  evaluate <code> which should print the commit to use

This looks somewhat overengineered, although "git" and "svn" are probably
useful in real life.  I especially wonder if a fixed <ref> is useful at
all.  Wouldn't the choice of "other" branch always depend on the current
branch?

```

## Thomas Rast, 2010-06-14 07:42

Subject: Re: [PATCH] bash completion: Support "divergence from upstream" messages in __git_ps1
Message-ID: <201006140942.43099.trast@student.ethz.ch>
URL: https://gitlist.dev/e/201006140942.43099.trast%40student.ethz.ch
In-Reply-To: <4C13F32B.7060106@pileofstuff.org>

```
Andrew Sayers wrote:
> 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 omitted it because I thought it would be too cluttery, but then my
branches seem to rarely agree with their upstream.

> +       local cfg=( $( git config --get-regexp '^bash\.showUpstream$|^svn-remote\..*\.url$' 2>/dev/null ) )

Doesn't this break if the config value contains spaces?  I don't know
enough about bash arrays but in my simple tests, the array elements
are split between words.

And with the new design, you practically *expect* the config key to
contain spaces.

Along the same lines, I think
> +                               GIT_PS1_SHOWUPSTREAM="${cfg[$((n+1))]}"
> +                               if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
can never trigger because bash will never see the empty config string.

Slightly more robust would be to use

  git config --get-regexp '^bash\.showUpstream$|^svn-remote\..*\.url$' \
    2>/dev/null |
  while read key value, do
    # stuff
  done

That still breaks in the case of values containing newlines, though.

> 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.
[...]
> +#           cmd=<command> compare HEAD to the output of <command>
[...]
> +		cmd\=*) upstream=$( "${option:4}" ) ;;

"Encourage" is a mild understatement; AFAICS the code doesn't work
with more than single-word command any more.

The original intent was that the user could put a (very small) shell
script directly in the configuration if the normal DWIMming doesn't
fit his neds, perhaps most likely in the case of git-svn (do other
remote helpers have the same problem?).

Having to wrap it in a script defeats that point, as it becomes almost
as easy to edit the completion script.  So I think if it can't eval,
you might as well remove it.

BTW, please spell $(command) substitution without the spaces.  Your
current style does not match what is already in the file.

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

```

## Thomas Rast, 2010-06-14 07:44

Subject: Re: [PATCH v2 2/2] bash completion: Support "divergence from upstream" warnings in __git_ps1
Message-ID: <201006140944.20737.trast@student.ethz.ch>
URL: https://gitlist.dev/e/201006140944.20737.trast%40student.ethz.ch
In-Reply-To: <7v7hm2e27z.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano wrote:
> Thomas Rast <trast@student.ethz.ch> writes:
> 
> > +#       If you would like to see the difference between HEAD and its upstream,
> > +#       set GIT_PS1_SHOWUPSTREAM to one of the following:
> > +#           git          use @{upstream}
> > +#           svn          attempt to DWIM svn upstream for normal and --stdlayout
> > +#           ref <ref>    unconditionally use <ref>
> > +#           eval <code>  evaluate <code> which should print the commit to use
> 
> This looks somewhat overengineered, although "git" and "svn" are probably
> useful in real life.  I especially wonder if a fixed <ref> is useful at
> all.  Wouldn't the choice of "other" branch always depend on the current
> branch?

You're probably right.  I had 'ref' early on to test around, and then
made 'eval' to allow for arcane git-svn setups, but now that it seems
Andrew has a nice way of matching those, we can also just drop it.

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

```

## Erik Faye-Lund, 2010-06-14 11:57

Subject: Re: vger doesn't like UTF-8 from send-email
Message-ID: <AANLkTinH7p6_WV1FK7UTZLg-8OGINZFnYMA1kENb6PkW@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTinH7p6_WV1FK7UTZLg-8OGINZFnYMA1kENb6PkW%40mail.gmail.com
In-Reply-To: <AANLkTim1QajqLOp4y6-oIMAGp8Tkf7z9uTH6bwIIFYkH@mail.gmail.com>

```
On Sun, Jun 13, 2010 at 6:15 AM, Michael Witten <mfwitten@gmail.com> wrote:
> On Sat, Jun 12, 2010 at 05:11, Thomas Rast <trast@student.ethz.ch> wrote:
>> AFAICT the original message did not declare an encoding:
>>
>> Subject: [PATCH v2 0/2] bash completion: Support "divergence from upstream" warnings in __git_ps1
>> Date: Sat, 12 Jun 2010 12:02:14 +0200
>> Message-ID: <cover.1276336602.git.trast@student.ethz.ch>
>> X-Mailer: git-send-email 1.7.1.561.g94582
>> In-Reply-To: <20100612000002.GA30196@neumann>
>> References: <20100612000002.GA30196@neumann>
>> MIME-Version: 1.0
>> Content-Type: text/plain
>> Return-Path: trast@student.ethz.ch
>> ...
>> Is this a new vger policy, or am I hitting a send-email bug?
>
> Let's assume the headers themselves are already properly encoded.
>
> According to:
>
>    http://www.faqs.org/rfcs/rfc2045.html
>
> we have:
>
>    The proper Content-Transfer-Encoding
>    label must always be used.
>
> and:
>
>    An encoding type of 7BIT requires that
>    the body is already in a 7bit mail-ready
>    representation.  This is the default value
>    -- that is, "Content-Transfer-Encoding: 7BIT"
>    is assumed if the Content-Transfer-Encoding
>    header field is not present.
>
> Moreover, according to:
>
>    http://www.faqs.org/rfcs/rfc2046.html
>
> we have:
>
>    4.1.2    Charset Parameter
>    ...
>    The default character set, which must be
>    assumed in the absence of a charset parameter,
>    is US-ASCII.
>
> So, your email is indeed incorrect in 2 ways if the body contains
> UTF-8 encoded data.
>
> From what I've skimmed, the mail user agent (MUA)---such as
> send-email---could send your unmodified message body by producing
> these headers:
>
>    MIME-Version: 1.0
>    Content-type: text/plain; charset=utf-8
>    Content-transfer-encoding: 8bit
>
> but only 7bit transfer encodings are guaranteed to make it intact to
> the destination; consequently, it would probably be a good idea for
> the MUA to transform your message into some 7bit encoding, preferably
> a human-readable one such as the 'quoted-printable' encoding; after
> such a transformation, the headers could be:
>
>    MIME-Version: 1.0
>    Content-type: text/plain; charset=utf-8
>    Content-transfer-encoding: quoted-printable
>

QP-encoding is sometimes destructive, and as such not recommended for
patches - in fact, Documentation/SubmittingPatches forbid it. For the
cover-letter the destruction might not be an issue (IIRC it's some
line-feeds that might be added because QP can a line longer than the
maximum line-length), but special casing the encoding for
cover-letters doesn't strike me as The Right Thing To Do(tm).

I think the only real alternative to 8-bit encoding is Base64, and it
sacrifices human-readability. Dunno how bad that is, though.

-- 
Erik "kusma" Faye-Lund

```

## SZEDER Gábor, 2010-06-14 12:36

Subject: Re: [PATCH v2 2/2] bash completion: Support "divergence from upstream" warnings in __git_ps1
Message-ID: <20100614123633.GN4640@neumann>
URL: https://gitlist.dev/e/20100614123633.GN4640%40neumann
In-Reply-To: <93842467ca22405712cab23a9b3920c106df0f17.1276336602.git.trast@student.ethz.ch>

```
Hi,

On Sat, Jun 12, 2010 at 11:59:11AM +0200, Thomas Rast wrote:
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 57245a8..a6cb435 100755
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -42,6 +42,17 @@
>  #       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 one of the following:
> +#           git          use @{upstream}
> +#           svn          attempt to DWIM svn upstream for normal and --stdlayout
> +#           ref <ref>    unconditionally use <ref>
> +#           eval <code>  evaluate <code> which should print the commit to use
> +#       Any other value DWIMs either svn or git, preferring svn if configured.

Something like this should go in there somewhere:

  The bash.showUpstream config variable can be used to override the 
  value of GIT_PS1_SHOWUPSTREAM on a per-repository basis.

> +#
> +#       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.
> +#
>  # To submit patches:
>  #
>  #    *) Read Documentation/SubmittingPatches

```

## Andrew Sayers, 2010-06-15 21:50

Subject: [PATCHv4] bash completion: Support "divergence from upstream" messages in __git_ps1
Message-ID: <4C17F5B3.4070907@pileofstuff.org>
URL: https://gitlist.dev/e/4C17F5B3.4070907%40pileofstuff.org
In-Reply-To: <201006140942.43099.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.

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

This version removes ref= and cmd=/eval entirely, adds documentation
and reaches once again into the forbidden bash bag to fix Thomas'
issues.

I've used process substitution <(git config) instead of a simple pipe
because it's the only way I know to maintain the value of a local
variable.  To demonstrate, this prints a blank line:

foo() {
	local FOO
	echo foo | while read ; do FOO=$REPLY ; done
	echo $FOO
}
foo

Whereas this prints 'foo':

foo() {
	local FOO
	while read ; do FOO=$REPLY ; done < <( echo foo )
	echo $FOO
}
foo

While working on this patch, I found the following bug in 1.7.0.4:

$ git config --get-regexp '^(bash\.showUpstream)$'
bash.showupstream legacy verbose
$ git config --get-regexp '^(bash\.showUpstream|x)$'
bash.showupstream legacy verbose
$ git config --get-regexp '^(bash\.showupstream|\.)$'
bash.showupstream legacy verbose
$ git config --get-regexp '^(bash\.showUpstream|\.)$'

The last line should print the same value as all the others.  This
seems to be some weird issue with handling uppercase characters, but
I've not yet had time to create a minimal test case or check it on
master - I'll try to make some time tomorrow if it isn't a known
issue.

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

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 57245a8..dabcdaa 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
+#       behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated
+#       list of values:
+#           git           compare HEAD to @{upstream}
+#           svn           compare HEAD to your SVN upstream
+#           verbose       show number of commits ahead/behind (+/-) upstream
+#           legacy        don't use the '--count' option available in recent
+#                         versions of git-rev-list
+#       By default, __git_ps1 will compare HEAD to your SVN upstream
+#       if it can find one, or @{upstream} otherwise.  You can
+#       override the value of GIT_PS1_SHOWUPSTREAM on a per-repository
+#       basis by setting the bash.showUpstream config variable.
+#
+#
 # To submit patches:
 #
 #    *) Read Documentation/SubmittingPatches
@@ -78,6 +95,124 @@ __gitdir ()
 	fi
 }
 
+# stores the divergence from upstream in $p
+# used by GIT_PS1_SHOWUPSTREAM
+__git_ps1_show_upstream ()
+{
+	local key value
+	local svn_remote=() svn_url_pattern count n
+	local upstream=git legacy verbose
+
+	# get some config options from git-config
+	while read key value; do
+		case "$key" in
+			bash.showupstream)
+				GIT_PS1_SHOWUPSTREAM="$value"
+				if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
+					p=""
+					return
+				fi
+				;;
+			svn-remote.*.url)
+				svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
+				svn_url_pattern+="\\|$value"
+				upstream=svn # default upstream is SVN if available
+				;;
+		esac
+	done < <(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')
+
+	# parse configuration values
+	for option in ${GIT_PS1_SHOWUPSTREAM}; do
+		case "$option" in
+			git|svn) upstream="$option" ;;
+			verbose) verbose=1 ;;
+			legacy)  legacy=1  ;;
+		esac
+	done
+
+	# Find our upstream
+	case "$upstream" in
+		git)    upstream="@{upstream}" ;;
+		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 +267,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 +295,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

```

## Junio C Hamano, 2010-06-16 19:05

Subject: Re: [PATCHv4] bash completion: Support "divergence from upstream" messages in __git_ps1
Message-ID: <7v7hlyg5nh.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v7hlyg5nh.fsf%40alter.siamese.dyndns.org
In-Reply-To: <4C17F5B3.4070907@pileofstuff.org>

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

> 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.
>
> Support for other types of upstream than SVN should be easy to add if anyone is
> so inclined.
> ---

Sign-off?

> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 57245a8..dabcdaa 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
> +#       behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated
> +#       list of values:
> +#           git           compare HEAD to @{upstream}
> +#           svn           compare HEAD to your SVN upstream
> +#           verbose       show number of commits ahead/behind (+/-) upstream
> +#           legacy        don't use the '--count' option available in recent
> +#                         versions of git-rev-list
> +#       By default, __git_ps1 will compare HEAD to your SVN upstream
> +#       if it can find one, or @{upstream} otherwise.

This feels somewhat weird.

I can sort-of read from the above that I can set the variable to a random
string, e.g. "garbage", if I only want a simple show-upstream feature
without frills (i.e. I don't want it to be verbose, I don't want it to
restrict the comparison only to "git" upstream nor "svn" upstream, and I
don't think I would ever use ancient git that lack "rev-list --count").
But the description does not assure me that the random string I happened
to choose (in this case "garbage") is a safe one.  Perhaps list (and
implement) "default" as a safe, otherwise-no-op value?

How much overhead are we shaving if you specify "git" (without "svn") or
"svn" (without "git") to the variable?  I suspect that the bulk of the
time is spent by reading from "git config" to look for svn-remote.*.url,
which you seem to unconditionally do even when "git" was asked for
anyway.

> +#       You can
> +#       override the value of GIT_PS1_SHOWUPSTREAM on a per-repository
> +#       basis by setting the bash.showUpstream config variable.

That's totally backwards from it should be, isn't it?

Usually configuration variables are used to give you the default, and
you use environment variables to override them.

> +# stores the divergence from upstream in $p
> +# used by GIT_PS1_SHOWUPSTREAM
> +__git_ps1_show_upstream ()
> +{
> +	local key value
> +	local svn_remote=() svn_url_pattern count n
> +	local upstream=git legacy verbose
> +
> +	# get some config options from git-config
> +	while read key value; do
> +		case "$key" in
> +			bash.showupstream)
> +				GIT_PS1_SHOWUPSTREAM="$value"
> +				if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
> +					p=""
> +					return
> +				fi

This is the "backwards" part.

> +				;;
> +			svn-remote.*.url)
> +				svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
> +				svn_url_pattern+="\\|$value"
> +				upstream=svn # default upstream is SVN if available
> +				;;

I expected that (1) when on a branch that is a fork of a svn upstream, you
would use the svn magic; (2) otherwise when on a branch that is a fork of
a git upstream, you would use "@{upstream}".  That way, the users do not
even have to say "git" or "svn" in GIT_PS1_SHOWUPSTREAM at all, no?

But that does not seem to be what is happening here.  Your loop seems to
force "upstream=svn" if I have one branch that is a fork from svn
upstream, even if my current branch does not have anything to do with that
branch nor svn upstream.  Is that what was intended?

Oh, also, all of your case arms are one-indent too deep.  Please write
them like this:

	case foo in
        arm1)
        	stmt1
                ;;
	esac

> +		esac
> +	done < <(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')

If you "tr" to trash "\0" anyway, do you need to run "config -z"?

> +	# parse configuration values
> +	for option in ${GIT_PS1_SHOWUPSTREAM}; do

Is this safe under "set -u"?  See 25a31f8 (bash-completion: Support
running when set -u is enabled, 2009-01-15).

```

## Thomas Rast, 2010-06-16 19:11

Subject: Re: [PATCHv4] bash completion: Support "divergence from upstream" messages in __git_ps1
Message-ID: <201006162111.09557.trast@student.ethz.ch>
URL: https://gitlist.dev/e/201006162111.09557.trast%40student.ethz.ch
In-Reply-To: <7v7hlyg5nh.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano wrote:
> Andrew Sayers <andrew-git@pileofstuff.org> writes:
> > +#       You can
> > +#       override the value of GIT_PS1_SHOWUPSTREAM on a per-repository
> > +#       basis by setting the bash.showUpstream config variable.
> 
> That's totally backwards from it should be, isn't it?
> 
> Usually configuration variables are used to give you the default, and
> you use environment variables to override them.

Not in the bash completion.  The test for the environment variable is
cheap, so you use that to enable the feature and can then use configs
to tweak them at a per-repo level.  There is precedent with
GIT_PS1_SHOWDIRTYSTATE and bash.showDirtyState.

The comment above should state that this override only works if the
environment variable is also enabled, though.

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

```

## Andrew Sayers, 2010-06-17 21:31

Subject: [PATCHv5 0/2] bash completion: Support "divergence from upstream" messages in __git_ps1
Message-ID: <4C1A9442.7080304@pileofstuff.org>
URL: https://gitlist.dev/e/4C1A9442.7080304%40pileofstuff.org
In-Reply-To: <7v7hlyg5nh.fsf@alter.siamese.dyndns.org>

```
I agree with all the points I haven't specifically replied to.  The
first patch makes the appropriate changes.  The second patch fixes
largely unrelated "set -u" issues I stumbled over while running tests.

On 16/06/10 20:05, Junio C Hamano wrote:
>> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
>> index 57245a8..dabcdaa 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
>> +#       behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated
>> +#       list of values:
>> +#           git           compare HEAD to @{upstream}
>> +#           svn           compare HEAD to your SVN upstream
>> +#           verbose       show number of commits ahead/behind (+/-) upstream
>> +#           legacy        don't use the '--count' option available in recent
>> +#                         versions of git-rev-list
>> +#       By default, __git_ps1 will compare HEAD to your SVN upstream
>> +#       if it can find one, or @{upstream} otherwise.
> 
> This feels somewhat weird.
> 
> I can sort-of read from the above that I can set the variable to a random
> string, e.g. "garbage", if I only want a simple show-upstream feature
> without frills (i.e. I don't want it to be verbose, I don't want it to
> restrict the comparison only to "git" upstream nor "svn" upstream, and I
> don't think I would ever use ancient git that lack "rev-list --count").
> But the description does not assure me that the random string I happened
> to choose (in this case "garbage") is a safe one.  Perhaps list (and
> implement) "default" as a safe, otherwise-no-op value?

I agree this would improve the documentation, but I've used "auto"
instead of "default", to give a hint that that the code is being a bit
automagical.  I don't see how adding code would help though -
"GIT_PS1_SHOWUPSTREAM=auto" is already covered in the default case of an
unrecognised string, and adding code to make "GIT_PS1_SHOWUPSTREAM=1" do
nothing or print a warning would just confuse people that skip-read the
documentation and set the value to see what happened.

> How much overhead are we shaving if you specify "git" (without "svn") or
> "svn" (without "git") to the variable?  I suspect that the bulk of the
> time is spent by reading from "git config" to look for svn-remote.*.url,
> which you seem to unconditionally do even when "git" was asked for
> anyway.

In my tests, a single invocation of git-config took an average of
roughly 0.005s with a 30-line .git/config, and roughly 0.030s with a
.git/config that contained about 17,600 extra nonsense lines (aaa = aaa,
aab = aab, etc.).  In both cases, the extra test for svn-remote.*.url
made no significant difference to the time taken, whereas a second
invocation of `git config` (obviously) doubled the time taken.

Checking the SVN upstream with `git log --first-parent -1
--grep="^git-svn-id: \(${svn_url_pattern:2}\)"` is actually quite a
serious time issue, especially if you have made many commits since your
upstream.  A test with 100 empty commits since the SVN upstream took
roughly 0.012 seconds on average.  A test on git itself (>22,000
commits) took roughly 0.29 seconds to determine there was no SVN upstream.

Speed (and user confidence in speed) isn't the main reason to allow the
user to force "git" or "svn".  If someone had e.g. imported their old
SVN history into a git project, or did clever git tricks on a branch
they regularly merged into SVN, they would want to override the default
behaviour.  This is probably quite rare now I think about it, and I've
rejigged the documentation a bit to reflect that.

> 
>> +#       You can
>> +#       override the value of GIT_PS1_SHOWUPSTREAM on a per-repository
>> +#       basis by setting the bash.showUpstream config variable.
> 
> That's totally backwards from it should be, isn't it?
> 
> Usually configuration variables are used to give you the default, and
> you use environment variables to override them.
> 

I basically agree with Thomas here.  Going down that route without
parsing all the options in one big `--get-regexp` mess would take O(n)
time, where n consists mostly of config options I don't care about.

>> +				;;
>> +			svn-remote.*.url)
>> +				svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
>> +				svn_url_pattern+="\\|$value"
>> +				upstream=svn # default upstream is SVN if available
>> +				;;
> 
> I expected that (1) when on a branch that is a fork of a svn upstream, you
> would use the svn magic; (2) otherwise when on a branch that is a fork of
> a git upstream, you would use "@{upstream}".  That way, the users do not
> even have to say "git" or "svn" in GIT_PS1_SHOWUPSTREAM at all, no?
> 
> But that does not seem to be what is happening here.  Your loop seems to
> force "upstream=svn" if I have one branch that is a fork from svn
> upstream, even if my current branch does not have anything to do with that
> branch nor svn upstream.  Is that what was intended?

I'm not sure I understand how you would detect if something is a fork of
an SVN/git upstream.  It's certainly deliberate not to do a `git log` if
it's avoidable, for the efficiency reasons I mentioned above.  But it
seems like a good idea to use @{upstream} in the "auto" case if no SVN
upstream was found, so I've changed the patch to do that.  Please let me
know if there's some applicable magic I'm not aware of :)

>> +		esac
>> +	done < <(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')
> 
> If you "tr" to trash "\0" anyway, do you need to run "config -z"?

The `tr` is there to work around issues like this:

	git config bash.showUpstream $'svn\nlegacy'
	git config bash.showUpstream | tr '\0\n' '\n '

The end result is a format that can be easily parsed with `read`.
Without the -z, I'd have no way to tell the difference between the end
of a config item and a literal newline.

Having said that, I have no strong opinion about which is the more
appropriate thing to do here - slow down everyone's prompt to deal with
an edge case, or break otherwise-valid behaviour because the solution is
ugly.  The latest patch still has it in - let me know if you'd prefer it
out.

> 
>> +	# parse configuration values
>> +	for option in ${GIT_PS1_SHOWUPSTREAM}; do
> 
> Is this safe under "set -u"?  See 25a31f8 (bash-completion: Support
> running when set -u is enabled, 2009-01-15).

This is safe under "set -u", as this function is only called if
$GIT_PS1_SHOWUPSTREAM is defined.  But testing showed several issues
that make me suspect __git_ps1 never worked under "set -u".  My second
patch fixes those issues.

	- Andrew

```

## Andrew Sayers, 2010-06-17 21:32

Subject: [PATCHv5 1/2] bash completion: Support "divergence from upstream" messages in __git_ps1
Message-ID: <4C1A9452.1090900@pileofstuff.org>
URL: https://gitlist.dev/e/4C1A9452.1090900%40pileofstuff.org
In-Reply-To: <7v7hlyg5nh.fsf@alter.siamese.dyndns.org>

```
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.

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>
---
 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..6e6f458 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -42,6 +42,24 @@
 #       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="auto".  A "<" indicates
+#       you are behind, ">" indicates you are ahead, and "<>"
+#       indicates you have diverged.  You can further control
+#       behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated
+#       list of values:
+#           verbose       show number of commits ahead/behind (+/-) upstream
+#           legacy        don't use the '--count' option available in recent
+#                         versions of git-rev-list
+#           git           always compare HEAD to @{upstream}
+#           svn           always compare HEAD to your SVN upstream
+#       By default, __git_ps1 will compare HEAD to your SVN upstream
+#       if it can find one, or @{upstream} otherwise.  Once you have
+#       set GIT_PS1_SHOWUPSTREAM, you can override it on a
+#       per-repository basis by setting the bash.showUpstream config
+#       variable.
+#
+#
 # To submit patches:
 #
 #    *) Read Documentation/SubmittingPatches
@@ -78,6 +96,125 @@ __gitdir ()
 	fi
 }
 
+# stores the divergence from upstream in $p
+# used by GIT_PS1_SHOWUPSTREAM
+__git_ps1_show_upstream ()
+{
+	local key value
+	local svn_remote=() svn_url_pattern count n
+	local upstream=git legacy="" verbose=""
+
+	# get some config options from git-config
+	while read key value; do
+		case "$key" in
+		bash.showupstream)
+			GIT_PS1_SHOWUPSTREAM="$value"
+			if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
+				p=""
+				return
+			fi
+			;;
+		svn-remote.*.url)
+			svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
+			svn_url_pattern+="\\|$value"
+			upstream=svn+git # default upstream is SVN if available, else git
+			;;
+		esac
+	done < <(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')
+
+	# parse configuration values
+	for option in ${GIT_PS1_SHOWUPSTREAM}; do
+		case "$option" in
+		git|svn) upstream="$option" ;;
+		verbose) verbose=1 ;;
+		legacy)  legacy=1  ;;
+		esac
+	done
+
+	# Find our upstream
+	case "$upstream" in
+	git)    upstream="@{upstream}" ;;
+	svn*)
+		# get the upstream from the "git-svn-id: ..." in a commit message
+		# (git-svn uses essentially the same procedure internally)
+		local svn_upstream=($(git log --first-parent -1 \
+					--grep="^git-svn-id: \(${svn_url_pattern:2}\)" 2>/dev/null))
+		if [[ 0 -ne ${#svn_upstream[@]} ]]; then
+			svn_upstream=${svn_upstream[ ${#svn_upstream[@]} - 2 ]}
+			svn_upstream=${svn_upstream%@*}
+			for ((n=1; "$n" <= "${#svn_remote[@]}"; ++n)); do
+				svn_upstream=${svn_upstream#${svn_remote[$n]}}
+			done
+
+			if [[ -z "$svn_upstream" ]]; then
+				# default branch name for checkouts with no layout:
+				upstream=${GIT_SVN_ID:-git-svn}
+			else
+				upstream=${svn_upstream#/}
+			fi
+		elif [[ "svn+git" = "$upstream" ]]; then
+			upstream="@{upstream}"
+		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

```

## Andrew Sayers, 2010-06-17 21:32

Subject: [PATCHv5 2/2] bash-completion: Fix __git_ps1 to work with "set -u"
Message-ID: <4C1A9460.6080905@pileofstuff.org>
URL: https://gitlist.dev/e/4C1A9460.6080905%40pileofstuff.org
In-Reply-To: <7v7hlyg5nh.fsf@alter.siamese.dyndns.org>

```
Define several variables in __git_ps1 to avoid errors under "set -u" semantics.

__git_ps1 seems to have been missed when the rest of the file was fixed in
25a31f8.

Signed-off-by: Andrew Sayers <andrew-git@pileofstuff.org>
---
 contrib/completion/git-completion.bash |   16 ++++++++--------
 1 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 6e6f458..337e4c9 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -221,8 +221,8 @@ __git_ps1 ()
 {
 	local g="$(__gitdir)"
 	if [ -n "$g" ]; then
-		local r
-		local b
+		local r=""
+		local b=""
 		if [ -f "$g/rebase-merge/interactive" ]; then
 			r="|REBASE-i"
 			b="$(cat "$g/rebase-merge/head-name")"
@@ -264,12 +264,12 @@ __git_ps1 ()
 			}
 		fi
 
-		local w
-		local i
-		local s
-		local u
-		local c
-		local p
+		local w=""
+		local i=""
+		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
-- 
1.7.0.4

```

## Junio C Hamano, 2010-06-18 16:10

Subject: Re: [PATCHv5 0/2] bash completion: Support "divergence from upstream" messages in __git_ps1
Message-ID: <7vljacxqwc.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vljacxqwc.fsf%40alter.siamese.dyndns.org
In-Reply-To: <4C1A9442.7080304@pileofstuff.org>

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

>>> +#       You can
>>> +#       override the value of GIT_PS1_SHOWUPSTREAM on a per-repository
>>> +#       basis by setting the bash.showUpstream config variable.
>> 
>> That's totally backwards from it should be, isn't it?
>> 
>> Usually configuration variables are used to give you the default, and
>> you use environment variables to override them.
>
> I basically agree with Thomas here.

Ok.

> ...  If someone had e.g. imported their old
> SVN history into a git project, or did clever git tricks on a branch
> they regularly merged into SVN, they would want to override the default
> behaviour.  This is probably quite rare now I think about it, and I've
> rejigged the documentation a bit to reflect that.

Yeah, I see.

But doesn't all of the above suggest the decision should be per branch?
It is not too implausible to have a branch that is actively interacting
with SVN upstream and another branch whose upstream has migrated from SVN
and now managed by git.  Say you and your pal are working with a project
that is managed by SVN, and you use one of your branches to interact
directly with SVN upstream.  Your pal has a branch forked from the same
SVN upstream, and one of your other branches is building on top of her
work.  When you are on the former branch, you would want to know how your
work diverged from the SVN upstream; when you are on the latter branch,
you would want to know how your work diverged from your pal's git branch
that you are using as its upstream.  No?

Which led me to this expectation:

>>> +			svn-remote.*.url)
>>> +				svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
>>> +				svn_url_pattern+="\\|$value"
>>> +				upstream=svn # default upstream is SVN if available
>>> +				;;
>> 
>> I expected that (1) when on a branch that is a fork of a svn upstream, you
>> would use the svn magic; (2) otherwise when on a branch that is a fork of
>> a git upstream, you would use "@{upstream}".  That way, the users do not
>> even have to say "git" or "svn" in GIT_PS1_SHOWUPSTREAM at all, no?

I wonder if looking for "git-svn-id:" in the past log is the best you can
do to see if a branch is forked from a remote that is managed by git-svn;
for one thing, that would not work for "noMetadata" setting.

>> If you "tr" to trash "\0" anyway, do you need to run "config -z"?
>
> The `tr` is there to work around issues like this:
>
> 	git config bash.showUpstream $'svn\nlegacy'
> 	git config bash.showUpstream | tr '\0\n' '\n '

Is that even an issue?  Why should there be a LF in the value?  I thought
you defined it as a string with space separated magic tokens...  Perhaps I
am missing something?

```

## Andrew Sayers, 2010-06-18 21:02

Subject: Re: [PATCHv5 0/2] bash completion: Support "divergence from upstream" messages in __git_ps1
Message-ID: <4C1BDED3.2090002@pileofstuff.org>
URL: https://gitlist.dev/e/4C1BDED3.2090002%40pileofstuff.org
In-Reply-To: <7vljacxqwc.fsf@alter.siamese.dyndns.org>

```
On 18/06/10 17:10, Junio C Hamano wrote:
> 
> But doesn't all of the above suggest the decision should be per branch?
> It is not too implausible to have a branch that is actively interacting
> with SVN upstream and another branch whose upstream has migrated from SVN
> and now managed by git.  Say you and your pal are working with a project
> that is managed by SVN, and you use one of your branches to interact
> directly with SVN upstream.  Your pal has a branch forked from the same
> SVN upstream, and one of your other branches is building on top of her
> work.  When you are on the former branch, you would want to know how your
> work diverged from the SVN upstream; when you are on the latter branch,
> you would want to know how your work diverged from your pal's git branch
> that you are using as its upstream.  No?
> 

It sounds like you're asking for git-svn to set
git.<branch>.{remote|upstream}, and for this script to ditch the
SVN-specific workarounds.  I have no problem with such a solution, but I
also have no idea where to begin with it.  Is there some reason we don't
do this already?

A simpler 90% solution would be to switch the defaults around, so you
always use @{upstream} if defined, or otherwise search for the SVN
upstream.  This enables every use case except noMetadata, and I suspect
any solution to that one would be at least as complex as setting
git.<branch>.{remote|upstream}.

>>> If you "tr" to trash "\0" anyway, do you need to run "config -z"?
>>
>> The `tr` is there to work around issues like this:
>>
>> 	git config bash.showUpstream $'svn\nlegacy'
>> 	git config bash.showUpstream | tr '\0\n' '\n '
> 
> Is that even an issue?  Why should there be a LF in the value?  I thought
> you defined it as a string with space separated magic tokens...  Perhaps I
> am missing something?

My concern was more with the robustness principle than anything - LFs
aren't part of the format defined in the docs, and I can't think of a
reason why people would need them, but there's no mechanical way to stop
people putting them in there.  If you're saying that git users can be
trusted not to do anything so stupid (and/or that it's their problem if
they do), then I'm happy to get rid of this.

	- Andrew

```
