Skip to contentSkip to conversation

format-patch: Use --mbox by default.

PATCH In review Andreas Ericsson 5 participants 14 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 format-patch: Use --mbox by default.

This patch enables a slightly modified and more correct --mbox output by default. The --date, --author and --mbox options are obsoleted and removed.

Previously, author-info and date was by default only printed if the commit was made by someone else, irrespective of the obsoleted options. Since it does no harm having ones own info there too it shouldn't really matter much, and should lessen the confusion regarding how to use git-format-patch with git-am.

The patches generated also had bad mail headers for sendmail (of postfix 2.2.2 at least) to use directly (From: line must be on top,
Subject: last of header), which isn't really an issue but annoying just the same. I haven't actually tried this new format, but at least 'file' now recognize it as "news or mail text", rather than "ASCII English text".

Note that this reverts commit aa66c7ec77d474b737da607d6cb2d07f56628def, "format-patch/commit: Quote single quote in the author name properly" which is no longer needed since the whosepatchScript now prints the headers directly rather than passing them to eval.

These changes allow us to remove two conditional statements and the invokation of a sub-shell in the process_one function, so it should be a bit faster too.

---

 Documentation/git-format-patch.txt |   18 ++-----------
 git-format-patch.sh                |   49 +++++++-----------------------------
 2 files changed, 12 insertions(+), 55 deletions(-)

b38d1ffeb27ba32de849f33a9c416fda4cd8eecb

Documentation/git-format-patch.txt +3 -15
index 47705de..73e1358 100644
@@ -9,7 +9,7 @@ git-format-patch - Prepare patches for e
 SYNOPSIS
 --------
 [verse]
-'git-format-patch' [-n | -k] [-o <dir> | --stdout] [-s] [-c] [--mbox]
+'git-format-patch' [-n | -k] [-o <dir> | --stdout] [-s] [-c]
 		 [--diff-options] <his> [<mine>]
 
 DESCRIPTION
@@ -27,9 +27,8 @@ When -n is specified, instead of "[PATCH
 line is formatted as "[PATCH N/M] Subject", unless you have only
 one patch.
 
-When --mbox is specified, the output is formatted to resemble
-UNIX mailbox format, and can be concatenated together for
-processing with applymbox.
+The output is formatted to resemble UNIX mailbox format, and can
+be concatenated together for processing with gitlink::git-am[1].
 
 
 OPTIONS
@@ -45,11 +44,6 @@ OPTIONS
 	Do not strip/add '[PATCH]' from the first line of the
 	commit log message.
 
--a|--author, -d|--date::
-	Output From: and Date: headers for commits made by
-	yourself as well.  Usually these are output only for
-	commits made by people other than yourself.
-
 -s|--signoff::
 	Add `Signed-off-by:` line to the commit message, using
 	the committer identity of yourself.
@@ -61,12 +55,6 @@ OPTIONS
         has a SP character immediately followed by a TAB
         character.
 
--m|--mbox::
-	Format the output files for closer to mbox format by
-	adding a phony Unix "From " line, so they can be
-	concatenated together and fed to `git-applymbox`.
-	Implies --author and --date.
-
 --stdout::
 	This flag generates the mbox formatted output to the
 	standard output, instead of saving them into a file per
git-format-patch.sh +9 -40

103 lines not on this page · raw message

Signature · 1 line
1.0.7-gb38d1
#1 lore raw
Quoted 4 lines · Andreas Ericsson

The patches generated also had bad mail headers for sendmail (of postfix 2.2.2 at least) to use directly (From: line must be on top,
Subject: last of header)

Sounds like a bug report against postfix is in order.

-hpa

#2 lore raw
Quoted 10 lines · H. Peter Anvin
Quoted 4 lines · Andreas Ericsson

The patches generated also had bad mail headers for sendmail (of postfix 2.2.2 at least) to use directly (From: line must be on top,
Subject: last of header)

Sounds like a bug report against postfix is in order.

No need. It works with 2.2.8. The 2.2.2 it failed with is the default for FC4. I have no interest investigating all the possibilities, and I doubt Vietse will look for bugs in old versions unless they're security related.

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#3 lore raw

PATCH 1/1 format-patch: always --mbox and show sane Date:

In reply to Andreas Ericsson

Make --mbox, --author, and --date options a no-op, and always use --mbox output, and rewrite the commit log formatting in Perl. This makes it easier to output Date: header in RFC 2822 format, so do that as well.

Inspiration for this patch came from Andreas Ericsson's earlier patch.

---

* I admit that trying to avoid Perl and sticking to multiple sed invocations in this script was a lost battle. This patch is still minimalistic and it does not go all the way to rewrite the main loop of the program, which probably should happen someday.

I would not mind if someday somebody rewrote most of the barebone Porcelainish in Python or Perl, but the rewrite should not be for the sake of using that language. It should have other justifications: "there is too much text processing there it would not make sense to do that in shell". I think this patch is borderline to qualify to be in that camp.

As the version string at the end shows, this was prepared with the script source itself before building, as a test case.

 git-format-patch.sh |  143 +++++++++++++++++++++++++++------------------------
 1 files changed, 75 insertions(+), 68 deletions(-)

8b4271ef24818e6d52926bc615e5ce78ba0ed01b

git-format-patch.sh +75 -68
index d3979d7..7e67c4e 100755
@@ -34,14 +34,12 @@ outdir=./
 while case "$#" in 0) break;; esac
 do
     case "$1" in
-    -a|--a|--au|--aut|--auth|--autho|--author)
-    author=t ;;
     -c|--c|--ch|--che|--chec|--check)
     check=t ;;
-    -d|--d|--da|--dat|--date)
-    date=t ;;
-    -m|--m|--mb|--mbo|--mbox)
-    date=t author=t mbox=t ;;
+    -a|--a|--au|--aut|--auth|--autho|--author|\
+    -d|--d|--da|--dat|--date|\
+    -m|--m|--mb|--mbo|--mbox) # now noop
+    ;;
     -k|--k|--ke|--kee|--keep|--keep-|--keep-s|--keep-su|--keep-sub|\
     --keep-subj|--keep-subje|--keep-subjec|--keep-subject)
     keep_subject=t ;;
@@ -173,80 +171,89 @@ titleScript='
 	q
 '
 
-whosepatchScript='
-/^author /{
-	s/'\''/'\''\\'\'\''/g
-	s/author \(.*>\) \(.*\)$/au='\''\1'\'' ad='\''\2'\''/p
-	q
-}'
-
 process_one () {
-	mailScript='
-	/./d
-	/^$/n'
-	case "$keep_subject" in
-	t)  ;;
-	*)
-	    mailScript="$mailScript"'
-	    s|^\[PATCH[^]]*\] *||
-	    s|^|[PATCH'"$num"'] |'
-	    ;;
-	esac
-	mailScript="$mailScript"'
-	s|^|Subject: |'
-	case "$mbox" in
-	t)
-	    echo 'From nobody Mon Sep 17 00:00:00 2001' ;# UNIX "From" line
-	    ;;
-	esac
+	perl -w -e '
+my ($keep_subject, $num, $signoff, $commsg) = @ARGV;
+my ($signoff_pattern, $done_header, $done_subject, $signoff_seen,
+    $last_was_signoff);
+
+if ($signoff) {
+	$signoff = `git-var GIT_COMMITTER_IDENT`;
+	$signoff =~ s/>.*/>/;
+	$signoff_pattern = quotemeta($signoff);
+}
 
-	eval "$(sed -ne "$whosepatchScript" $commsg)"
-	test "$author,$au" = ",$me" || {
-		mailScript="$mailScript"'
-	a\
-From: '"$au"
-	}
-	test "$date,$au" = ",$me" || {
-		mailScript="$mailScript"'
-	a\
-Date: '"$ad"
-	}
+my @weekday_names = qw(Sun Mon Tue Wed Thu Fri Sat);
+my @month_names = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
 
-	mailScript="$mailScript"'
-	a\
+sub show_date {
+    my ($time, $tz) = @_;
+    my $minutes = abs($tz);
+    $minutes = ($minutes / 100) * 60 + ($minutes % 100);
+    if ($tz < 0) {
+        $minutes = -$minutes;
+    }
+    my $t = $time + $minutes * 60;
+    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime($t);
+    return sprintf("%s %s %d %02d:%02d:%02d %d %+05d",
+		   $weekday_names[$wday],
+		   $month_names[$mon],
+		   $mday, $hour, $min, $sec,
+		   $year+1900, $tz);
+}
 
-	: body
-	p
-	n
-	b body'
+print "From nobody Mon Sep 17 00:00:00 2001\n";
+open FH, "git stripspace <$commsg |" or die "open $commsg pipe";
+while (<FH>) {
+    unless ($done_header) {
+	if (/^$/) {
+	    $done_header = 1;
+	}
+	elsif (/^author (.*>) (.*)$/) {
+	    my ($author_ident, $author_date) = ($1, $2);
+	    my ($utc, $off) = ($author_date =~ /^(\d+) ([-+]?\d+)$/);
+	    $author_date = show_date($utc, $off);
 
-	(cat $commsg ; echo; echo) |
-	sed -ne "$mailScript" |
-	git-stripspace
-
-	test "$signoff" = "t" && {
-		offsigner=`git-var GIT_COMMITTER_IDENT | sed -e 's/>.*/>/'`
-		line="Signed-off-by: $offsigner"
-		grep -q "^$line\$" $commsg || {
-			echo
-			echo "$line"
-			echo
-		}
+	    print "From: $author_ident\n";
+	    print "Date: $author_date\n";
 	}
-	echo
-	echo '---'
-	echo
+	next;
+    }
+    unless ($done_subject) {
+	unless ($keep_subject) {
+	    s/^\[PATCH[^]]*\]\s*//;
+	    s/^/[PATCH$num] /;
+	}
+        print "Subject: $_";
+	$done_subject = 1;
+	next;
+    }
+
+    $last_was_signoff = 0;
+    if (/Signed-off-by:/i) {
+        if ($signoff ne "" && /Signed-off-by:\s*$signoff_pattern$/i) {
+	    $signoff_seen = 1;
+	}
+    }
+    print $_;
+}
+if (!$signoff_seen && $signoff ne "") {
+    if (!$last_was_signoff) {
+        print "\n";
+    }
+    print "$signoff\n";
+}
+print "\n---\n\n";
+close FH or die "close $commsg pipe";
+' "$keep_subject" "$num" "$signoff" $commsg
+
 	git-diff-tree -p $diff_opts "$commit" | git-apply --stat --summary
 	echo
 	git-diff-tree -p $diff_opts "$commit"
 	echo "-- "
 	echo "@@GIT_VERSION@@"
 
-	case "$mbox" in
-	t)
-		echo
-		;;
-	esac
+	echo
 }
 
 total=`wc -l <$series | tr -dc "[0-9]"`
Signature · 1 line
@@GIT_VERSION@@
#4 lore raw
In reply to Andreas Ericsson
Quoted 3 lines · Andreas Ericsson

This patch enables a slightly modified and more correct --mbox output by default. The --date, --author and --mbox options are obsoleted and removed.

I always use format-patch with -k -m (or -k --stdout), so I have no objections to making --mbox the default. I do not think anybody uses --date or --author in their scripts, but removing the --mbox option _might_ break things for people's scripts. Maybe it is worthwhile to keep it as a no-op option for a while.

Quoted 5 lines

Previously, author-info and date was by default only printed if the commit was made by someone else, irrespective of the obsoleted options. Since it does no harm having ones own info there too it shouldn't really matter much, and should lessen the confusion regarding how to use git-format-patch with git-am.

True. Originally it was really "format my changes to submission" tool (--signoff option is a sure sign to tell you that) and propagating authorship info was added as afterthought.

Quoted 3 lines

The patches generated also had bad mail headers for sendmail (of postfix 2.2.2 at least) to use directly (From: line must be on top,
Subject: last of header),...

I do not think RFC2822 specifies that. See "3.6 Field definitions", third paragraph, first two sentences.

In that sense, technically they are not "bad mail headers", but at the same time, since it is valid in any order, if _one_ popular MTA is unconfortable with a particular ordering, we could order things differently to make it happier (mind you, this would not work for more than one such broken MTAs).

Another thing I've been considering is to handle UTF-8 (or i18n.commitencoding in general) a bit better. We currently do not have Content-Type: header, so if you pipe the output to "am", it must be invoked without "-u" to make sure that it takes things as-is ("am -u" assumes things are in latin1 without Content-Type:). If we start to tackle that issue, we might also want to do RFC2047 in headers.

#5 lore raw
Quoted 8 lines · Junio C Hamano
Quoted 3 lines · Andreas Ericsson

This patch enables a slightly modified and more correct --mbox output by default. The --date, --author and --mbox options are obsoleted and removed.

Quoted 6 lines

I always use format-patch with -k -m (or -k --stdout), so I have no objections to making --mbox the default. I do not think anybody uses --date or --author in their scripts, but removing the --mbox option _might_ break things for people's scripts. Maybe it is worthwhile to keep it as a no-op option for a while.

The reworked patch (which works for sending patches to myself, so far), prints a warning to stderr that a deprecated option is used and then silently ignores it.

Quoted 6 lines
Quoted 4 lines

The patches generated also had bad mail headers for sendmail (of postfix 2.2.2 at least) to use directly (From: line must be on top,
Subject: last of header),...

Quoted 9 lines

I do not think RFC2822 specifies that. See "3.6 Field definitions", third paragraph, first two sentences.

In that sense, technically they are not "bad mail headers", but at the same time, since it is valid in any order, if _one_ popular MTA is unconfortable with a particular ordering, we could order things differently to make it happier (mind you, this would not work for more than one such broken MTAs).

The only thing I've seen break is if Subject: is placed on top. It's easier, script-wise, to put it last and that seems to work with qmail and postfix both.

Quoted 9 lines

Another thing I've been considering is to handle UTF-8 (or i18n.commitencoding in general) a bit better. We currently do not have Content-Type: header, so if you pipe the output to "am", it must be invoked without "-u" to make sure that it takes things as-is ("am -u" assumes things are in latin1 without Content-Type:). If we start to tackle that issue, we might also want to do RFC2047 in headers.

You'll have to ask someone else for that. I have no idea how it works and I wouldn't know how to begin doing it in shell.

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#6 lore raw

PATCH 1/1 .gitignore git-describe

---

 .gitignore |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

265df952d4c61f60eae42261aeea90e9b6d8e414

.gitignore +1 -0
index 1a9090b..4335061 100644
@@ -23,6 +23,7 @@ git-count-objects
 git-cvsexportcommit
 git-cvsimport
 git-daemon
+git-describe
 git-diff
 git-diff-files
 git-diff-index
Signature · 1 line
1.0.7-gb38d1
#7 lore raw

git-format-patch Date header

git-format-patch uses "date '+%s %z'" format for the Date header. Sylpheed does not understand this format. How common this convention is? Should we change git-format-patch to use more standard date format?

OTOH, I like the %s format because it is so simple and easy to parse :)

Signature · 1 line
http://onion.dynserv.net/~timo/
#8 lore raw

Re: git-format-patch Date header

Quoted 6 lines · Timo Hirvonen

git-format-patch uses "date '+%s %z'" format for the Date header. Sylpheed does not understand this format. How common this convention is? Should we change git-format-patch to use more standard date format?

OTOH, I like the %s format because it is so simple and easy to parse :)

Actually, that's what's printed in the commit message, so any change would have to be put there and that would break backwards compatibility for new tools that might want to use it.

On an unrelated note, please don't start a new thread by replying to an old message. It's very confusing to follow and people may not see your message. This time it was doubly so because the next mail in my inbox is a patch for git-format-patch, to which I thought you replied.

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#9 lore raw

Re: git-format-patch Date header

Quoted 11 lines · On Sun, 08 Jan 2006 16:21:13 +0100 Andreas Ericsson <[email protected]> wrote:
Quoted 6 lines · Timo Hirvonen

git-format-patch uses "date '+%s %z'" format for the Date header. Sylpheed does not understand this format. How common this convention is? Should we change git-format-patch to use more standard date format?

OTOH, I like the %s format because it is so simple and easy to parse :)

Actually, that's what's printed in the commit message, so any change would have to be put there and that would break backwards compatibility for new tools that might want to use it.

So fixing Sylpheed (and maybe other email clients too) is the right solution...

Quoted 4 lines

On an unrelated note, please don't start a new thread by replying to an old message. It's very confusing to follow and people may not see your message. This time it was doubly so because the next mail in my inbox is a patch for git-format-patch, to which I thought you replied.

Sorry. I replied to your message because it was the message having Date header in the "%s %z" format.

Signature · 1 line
http://onion.dynserv.net/~timo/
#10 lore raw

Re: git-format-patch Date header

Quoted 10 lines · Timo Hirvonen
Quoted 6 lines · On Sun, 08 Jan 2006 16:21:13 +0100 Andreas Ericsson <[email protected]> wrote:
Quoted 4 lines · Timo Hirvonen

git-format-patch uses "date '+%s %z'" format for the Date header. Sylpheed does not understand this format. How common this convention is? Should we change git-format-patch to use more standard date format?

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

OTOH, I like the %s format because it is so simple and easy to parse :)

Actually, that's what's printed in the commit message, so any change would have to be put there and that would break backwards compatibility for new tools that might want to use it.

So fixing Sylpheed (and maybe other email clients too) is the right solution...

Nopes. In this case fixing my home-hacked git-send-patch script is the right thing to do. git-format-patch has always printed that format, though, so the error lies with me and the patch I sent. I shall have to re-visit it after reading rfc 822 a bit more closely.

Quoted 7 lines
Quoted 2 lines

On an unrelated note, please don't start a new thread by replying to an old message. It's very confusing to follow and people may not see your message. This time it was doubly so because the next mail in my inbox is a patch for git-format-patch, to which I thought you replied.

Sorry. I replied to your message because it was the message having Date header in the "%s %z" format.

I just noticed. Thanks, and sorry.

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#11 lore raw

Re: git-format-patch Date header

Quoted 3 lines · Andreas Ericsson

Nopes. In this case fixing my home-hacked git-send-patch script is the right thing to do. git-format-patch has always printed that format, though, so the error lies with me and the patch I sent. I shall have to re-visit it after reading rfc 822 a bit more closely.

RFC 2822, please.

-hpa

#12 lore raw

Re: git-format-patch Date header

Quoted 8 lines · H. Peter Anvin
Quoted 3 lines · Andreas Ericsson

Nopes. In this case fixing my home-hacked git-send-patch script is the right thing to do. git-format-patch has always printed that format, though, so the error lies with me and the patch I sent. I shall have to re-visit it after reading rfc 822 a bit more closely.

RFC 2822, please.

Right you are.

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#13 lore raw

Re: git-format-patch Date header

In reply to Andreas Ericsson
Quoted 4 lines · Andreas Ericsson

Actually, that's what's printed in the commit message, so any change would have to be put there and that would break backwards compatibility for new tools that might want to use it.

No, git should always take the date in any of a million different formats, and always turn it into "seconds + timezone" internally.

The fact that git-format-patch also prints it out in the internal format is unambiguous (nice) but human-unreadable (bad).

There are other unambiguous formats it could use, notably standard rfc2822 format ("date -R").

The git "show_date()" function hopefully does exactly that rfc2822 format, but you'd have to make a helper program to do the conversion in git-format-patch.sh.

Linus