git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] mailinfo: allow individual e-mail files as input

From
Brandon Casey <casey@nrlssc.navy.mil>
Date
Aug 7, 2009, 01:08 UTC
Message-ID
<COrzR9ThNBy5SQ7chsXyUB30jVGIijxZQ3LI9L_y7Ab5vWcDcy_HolvjjuHTC7DHI9ntV-eR_v0@cipher.nrlssc.navy.mil>
In-Reply-To
<COrzR9ThNBy5SQ7chsXyUOUuBmX-VWMCz3MUVwvBOlIZzlIXRRMP6EMS7BRy_6uJvxt5H-FbtdY@cipher.nrlssc.navy.mil>
From: Junio C Hamano <gitster@pobox.com>

We traditionally allowed a mbox file or a directory name of a maildir (but never an individual file inside a maildir) to be given to "git am". Even though an individual file in a maildir (or more generally, a piece of RFC2822 e-mail) is not a mbox file, it contains enough information to create a commit out of it, so there is no reason to reject one. Running mailsplit on such a file feels stupid, but it does not hurt.

This builds on top of a5a6755 (git-am foreign patch support: introduce patch_format, 2009-05-27) that introduced mailbox format detection. The codepath to deal with a mbox requires it to begin with "From " line and also allows it to begin with "From: ", but a random piece of e-mail can and often do begin with any valid RFC2822 header lines.

Instead of checking the first line, we extract all the lines up to the first empty line, and make sure they look like e-mail headers.

This fixes the test in t4150-am.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Brandon Casey <drafnel@gmail.com>
---
Junio,

You'll notice that I changed your grep -E to an egrep and dropped the -e. I do not see any other grep which uses -e, and I seem to recall Jeff King actively removing -e claiming that some greps do not recognize it. I do not have a perfect memory though, so apologies to Jeff if I am mistaken.

-brandon
 git-am.sh     |   14 ++++++++++++++
 t/t4150-am.sh |    2 +-
 2 files changed, 15 insertions(+), 1 deletions(-)
diff --git a/git-am.sh b/git-am.sh
index d64d997..dd60f5d 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -191,6 +191,20 @@ check_patch_format () {
 			esac
 			;;
 		esac
+		if test -z "$patch_format" &&
+			test -n "$l1" &&
+			test -n "$l2" &&
+			test -n "$l3"
+		then
+			# This begins with three non-empty lines.  Is this a
+			# piece of e-mail a-la RFC2822?  Grab all the headers,
+			# discarding the indented remainder of folded lines,
+			# and see if it looks like that they all begin with the
+			# header field names...
+			sed -n -e '/^$/q' -e '/^[ 	]/d' -e p "$1" |
+			egrep -v '^[A-Za-z]+(-[A-Za-z]+)*:' >/dev/null ||
+			patch_format=mbox
+		fi
 	} < "$1" || clean_abort
 }
 
diff --git a/t/t4150-am.sh b/t/t4150-am.sh
index ad2a85f..4e8e176 100755
--- a/t/t4150-am.sh
+++ b/t/t4150-am.sh
@@ -114,7 +114,7 @@ test_expect_success 'am applies patch correctly' '
 	test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
 '
 
-test_expect_failure 'am correctly applies patch from email lacking "From" in first 3 lines' '
+test_expect_success 'am correctly applies patch from email lacking "From" in first 3 lines' '
 	git checkout first &&
 	git am patch1.eml &&
 	! test -d .git/rebase-apply &&
-- 
1.6.4
Previous: Brandon CaseyNext: Brandon Casey
Message 6 of 24 in “What's in git.git (Aug 2009, #01; Wed, 05)”
  1. Junio C HamanoAug 5, 2009
  2. Brandon CaseyAug 6, 2009
  3. Nicolas SebrechtAug 6, 2009
  4. Nicolas SebrechtAug 6, 2009
  5. 1/3 t4150-am: demonstrate git-am's failure to handle some patch emailsBrandon Casey, Aug 7, 2009
  6. 2/3 mailinfo: allow individual e-mail files as inputBrandon Casey, Aug 7, 2009
  7. 3/3 git-am: print fair error message when format detection failsBrandon Casey, Aug 7, 2009
  8. Jeff KingAug 7, 2009
  9. Brandon CaseyAug 7, 2009
  10. mailinfo: allow individual e-mail files as inputBrandon Casey, Aug 7, 2009
  11. Re: mailinfo: allow individual e-mail files as inputNicolas Sebrecht, Aug 7, 2009
  12. Re: mailinfo: allow individual e-mail files as inputNicolas Sebrecht, Aug 7, 2009
  13. Brandon CaseyAug 7, 2009
  14. Re: mailinfo: allow individual e-mail files as inputNicolas Sebrecht, Aug 7, 2009
  15. Brandon CaseyAug 7, 2009
  16. Re: mailinfo: allow individual e-mail files as inputNicolas Sebrecht, Aug 7, 2009
  17. Junio C HamanoAug 7, 2009
  18. Brandon CaseyAug 7, 2009
  19. 2/3 Re: mailinfo: allow individual e-mail files as inputNicolas Sebrecht, Aug 7, 2009
  20. Brandon CaseyAug 7, 2009
  21. Junio C HamanoAug 7, 2009
  22. Nanako ShiraishiAug 7, 2009
  23. Junio C HamanoAug 7, 2009
  24. Brandon CaseyAug 7, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.