From: Junio C Hamano Subject: Re: [PATCH] git-am: Handle "git show" output correctly Date: Wed, 12 Sep 2012 13:06:13 -0700 Message-ID: <7vtxv3atvu.fsf@alter.siamese.dyndns.org> References: <1347472823.12986.3.camel@eddie.install.bos.redhat.com> <1347473304-21418-1-git-send-email-pjones@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: git@vger.kernel.org To: Peter Jones X-From: git-owner@vger.kernel.org Wed Sep 12 22:06:32 2012 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1TBtCd-0002SV-5e for gcvg-git-2@plane.gmane.org; Wed, 12 Sep 2012 22:06:27 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752577Ab2ILUGS (ORCPT ); Wed, 12 Sep 2012 16:06:18 -0400 Received: from b-pb-sasl-quonix.pobox.com ([208.72.237.35]:41284 "EHLO smtp.pobox.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752132Ab2ILUGR (ORCPT ); Wed, 12 Sep 2012 16:06:17 -0400 Received: from smtp.pobox.com (unknown [127.0.0.1]) by b-sasl-quonix.pobox.com (Postfix) with ESMTP id 3C791991F; Wed, 12 Sep 2012 16:06:16 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=pobox.com; h=from:to:cc :subject:references:date:in-reply-to:message-id:mime-version :content-type; s=sasl; bh=urVorKUg6apkUryLIEdf1/si6Rs=; b=GIUwMB gLRvOmHOw42aV9m/nr8DQKHdg+r4xP86Kka1RAPxk1EGKDdnI1XdiaiiR/MLfCzq G9BPTLyY1i9i6xI6Bmxqqrwv1WWR7LJ62JyxHDj2LcHcit4YGYUeJGUmmJAluy0k /WLj1gIfO76lSzoNZUgGrGKDCcd/BInzaSo1Y= DomainKey-Signature: a=rsa-sha1; c=nofws; d=pobox.com; h=from:to:cc :subject:references:date:in-reply-to:message-id:mime-version :content-type; q=dns; s=sasl; b=xHiBdYAwQS4IdKIIUhEVZRzW7PL865Wr jm6t94IZweEXK1nCGpwfGH/bpm22VE/P/3vCelr7BwWhfrzVmLhTWqrZ7oQx2qf1 tOFQT4Wwea1ExgC01iAJT4ZDq9nyKoFVaC5j06e1zrDAebtdlq60PnUdw4Xd/84t xmhYAfNpm9c= Received: from b-pb-sasl-quonix.pobox.com (unknown [127.0.0.1]) by b-sasl-quonix.pobox.com (Postfix) with ESMTP id 29D9D991E; Wed, 12 Sep 2012 16:06:16 -0400 (EDT) Received: from pobox.com (unknown [98.234.214.94]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by b-sasl-quonix.pobox.com (Postfix) with ESMTPSA id 40FD19919; Wed, 12 Sep 2012 16:06:15 -0400 (EDT) In-Reply-To: <1347473304-21418-1-git-send-email-pjones@redhat.com> (Peter Jones's message of "Wed, 12 Sep 2012 14:08:24 -0400") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) X-Pobox-Relay-ID: 4E9AF27E-FD15-11E1-A067-BAB72E706CDE-77302942!b-pb-sasl-quonix.pobox.com Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: Peter Jones writes: > This patch adds the ability for "git am" to accept patches in the format > generated by "git show". Some people erroneously use "git show" instead > of "git format-patch", and it's nice as a maintainer to be able to > easily take their patch rather than going back and forth with them to > get a "correctly" formatted patch containing exactly the same actual > information. > > Signed-off-by: Peter Jones > --- > git-am.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 60 insertions(+) > > diff --git a/git-am.sh b/git-am.sh > index c682d34..210e9fe 100755 > --- a/git-am.sh > +++ b/git-am.sh > @@ -216,6 +216,21 @@ check_patch_format () { > read l2 > read l3 > case "$l1" in > + "commit "*) > + case "$l2" in > + "Author: "*) > + case "$l3" in > + "Date: "*) > + patch_format=gitshow > + ;; > + *) > + ;; > + esac > + ;; > + *) > + ;; > + esac > + ;; At least the inner one could become easier to read by losing one level of nesting, e.g. case "$l2,,$l3" in "Author: *",,"Date: ") found it ;; esac I wonder what the severity of the damage if we misidentify the patch format in this function would be? If it is severe enough, the check for the first line may want to become a bit more strict to avoid misidentification (e.g. expr "$l1" : 'commit [0-9a-f]\{40\}$'). Perhaps we don't care. I dunno. > @@ -321,6 +336,51 @@ split_patches () { > this= > msgnum= > ;; > + gitshow) > + this=0 > + for patch in "$@" > + do So each input file is expected to be nothing but an output from "git show" for a single commit; in other words, not concatenation of them, nor just an e-mail message that has "git show" output copy&pasted in the body with some other cruft, but plausibly was delibered as a separate attachment file. I somehow was visualizing that you were trying to accept mails I sometimes see here like: From: somebody Date: someday Hi, a long winded discussion that talks about the motivation behind the patch comes here. commit 4d8c4db13c8c4c79b6fc0a38ff52d85d3543aa7a Author: A U Thor Date: Tue Sep 11 12:34:56 2012 +0900 a one liner that just says "bugfix" and nothing else diff --git .... and that was one of the reasons I thought (but didn't say in my responses) "Why bother? When running 'am' on such a message you will have to edit the message to move things around anyway". If the target is a stand-alone "git show" output, at least we do not have to worry about such a case. > + this=`expr "$this" + 1` > + msgnum=`printf "%0${prec}d" $this` > + # The first nonemptyline after an empty line is the > + # subject, and the body starts with the next nonempty > + # line. > + perl -ne 'BEGIN { > + $diff = 0; $subject = 0; $subjtext=""; > + } > + if ($diff == 1 || /^diff/ || /^---$/) { > + $diff = 1 ; > + print ; > + } elsif ($subject > 1) { > + s/^ // ; > + print ; > + } elsif ($subject == 1 && !/^\s+$/) { > + s/^ // ; > + $subjtext = "$subjtext $_"; > + } elsif ($subject == 1) { > + $subject = 2 ; > + print "Subject: ", $subjtext ; > + s/^ // ; > + print ; > + } elsif ($subject) { > + print "\n" ; > + s/^ // ; > + print ; > + } elsif (/^\s+$/) { next ; } > + elsif (/^Author:/) { s/Author/From/ ; print ;} > + elsif (/^(From|Date)/) { print ; } Where does "^From" come from? Should this be /^Date: / instead? > + elsif (/^commit/) { next ; } > + else { > + s/^ // ; > + $subjtext = $_; > + $subject = 1; > + } > + ' < "$patch" > "$dotest/$msgnum" || clean_abort > + done This reminds me of another reason why I am hesitant to make "am" take output from "git show". Unlike format-patch output, "Author:" and "Date:" in its output, because it is meant for human consumption, might be a fair game for i18n/l10n (the first line "commit [0-9][a-f]{40}" is not likely to change, though). > + echo "$this" > "$dotest/last" > + this= > + msgnum= > + ;; > hg) > this=0 > for hg in "$@"