Merge branch 'kd/mailinfo-quoted-string'
authorJunio C Hamano <gitster@pobox.com>
Mon, 3 Oct 2016 20:30:38 +0000 (13:30 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 3 Oct 2016 20:30:38 +0000 (13:30 -0700)
An author name, that spelled a backslash-quoted double quote in the
human readable part "My \"double quoted\" name", was not unquoted
correctly while applying a patch from a piece of e-mail.

* kd/mailinfo-quoted-string:
mailinfo: unescape quoted-pair in header fields
t5100-mailinfo: replace common path prefix with variable

mailinfo.c
t/t5100-mailinfo.sh
t/t5100/comment.expect [new file with mode: 0644]
t/t5100/comment.in [new file with mode: 0644]
t/t5100/quoted-string.expect [new file with mode: 0644]
t/t5100/quoted-string.in [new file with mode: 0644]
index 2275b285f097d632e38d7f1fe59692df1f807e28..2fb3877ee44e9cdc83c43fa23971f02a94b5a49f 100644 (file)
@@ -54,6 +54,86 @@ static void parse_bogus_from(struct mailinfo *mi, const struct strbuf *line)
        get_sane_name(&mi->name, &mi->name, &mi->email);
 }
 
+static const char *unquote_comment(struct strbuf *outbuf, const char *in)
+{
+       int c;
+       int take_next_litterally = 0;
+
+       strbuf_addch(outbuf, '(');
+
+       while ((c = *in++) != 0) {
+               if (take_next_litterally == 1) {
+                       take_next_litterally = 0;
+               } else {
+                       switch (c) {
+                       case '\\':
+                               take_next_litterally = 1;
+                               continue;
+                       case '(':
+                               in = unquote_comment(outbuf, in);
+                               continue;
+                       case ')':
+                               strbuf_addch(outbuf, ')');
+                               return in;
+                       }
+               }
+
+               strbuf_addch(outbuf, c);
+       }
+
+       return in;
+}
+
+static const char *unquote_quoted_string(struct strbuf *outbuf, const char *in)
+{
+       int c;
+       int take_next_litterally = 0;
+
+       while ((c = *in++) != 0) {
+               if (take_next_litterally == 1) {
+                       take_next_litterally = 0;
+               } else {
+                       switch (c) {
+                       case '\\':
+                               take_next_litterally = 1;
+                               continue;
+                       case '"':
+                               return in;
+                       }
+               }
+
+               strbuf_addch(outbuf, c);
+       }
+
+       return in;
+}
+
+static void unquote_quoted_pair(struct strbuf *line)
+{
+       struct strbuf outbuf;
+       const char *in = line->buf;
+       int c;
+
+       strbuf_init(&outbuf, line->len);
+
+       while ((c = *in++) != 0) {
+               switch (c) {
+               case '"':
+                       in = unquote_quoted_string(&outbuf, in);
+                       continue;
+               case '(':
+                       in = unquote_comment(&outbuf, in);
+                       continue;
+               }
+
+               strbuf_addch(&outbuf, c);
+       }
+
+       strbuf_swap(&outbuf, line);
+       strbuf_release(&outbuf);
+
+}
+
 static void handle_from(struct mailinfo *mi, const struct strbuf *from)
 {
        char *at;
@@ -63,6 +143,8 @@ static void handle_from(struct mailinfo *mi, const struct strbuf *from)
        strbuf_init(&f, from->len);
        strbuf_addbuf(&f, from);
 
+       unquote_quoted_pair(&f);
+
        at = strchr(f.buf, '@');
        if (!at) {
                parse_bogus_from(mi, from);
index e173c33f4b64480eaa948bc9f5e1178f82433f7f..e6b995161e0fd15493bdaf4e5a5df2a3c2990798 100755 (executable)
@@ -7,8 +7,10 @@ test_description='git mailinfo and git mailsplit test'
 
 . ./test-lib.sh
 
+DATA="$TEST_DIRECTORY/t5100"
+
 test_expect_success 'split sample box' \
-       'git mailsplit -o. "$TEST_DIRECTORY"/t5100/sample.mbox >last &&
+       'git mailsplit -o. "$DATA/sample.mbox" >last &&
        last=$(cat last) &&
        echo total is $last &&
        test $(cat last) = 18'
@@ -16,28 +18,28 @@ test_expect_success 'split sample box' \
 check_mailinfo () {
        mail=$1 opt=$2
        mo="$mail$opt"
-       git mailinfo -u $opt msg$mo patch$mo <$mail >info$mo &&
-       test_cmp "$TEST_DIRECTORY"/t5100/msg$mo msg$mo &&
-       test_cmp "$TEST_DIRECTORY"/t5100/patch$mo patch$mo &&
-       test_cmp "$TEST_DIRECTORY"/t5100/info$mo info$mo
+       git mailinfo -u $opt "msg$mo" "patch$mo" <"$mail" >"info$mo" &&
+       test_cmp "$DATA/msg$mo" "msg$mo" &&
+       test_cmp "$DATA/patch$mo" "patch$mo" &&
+       test_cmp "$DATA/info$mo" "info$mo"
 }
 
 
 for mail in 00*
 do
        test_expect_success "mailinfo $mail" '
-               check_mailinfo $mail "" &&
-               if test -f "$TEST_DIRECTORY"/t5100/msg$mail--scissors
+               check_mailinfo "$mail" "" &&
+               if test -f "$DATA/msg$mail--scissors"
                then
-                       check_mailinfo $mail --scissors
+                       check_mailinfo "$mail" --scissors
                fi &&
-               if test -f "$TEST_DIRECTORY"/t5100/msg$mail--no-inbody-headers
+               if test -f "$DATA/msg$mail--no-inbody-headers"
                then
-                       check_mailinfo $mail --no-inbody-headers
+                       check_mailinfo "$mail" --no-inbody-headers
                fi &&
-               if test -f "$TEST_DIRECTORY"/t5100/msg$mail--message-id
+               if test -f "$DATA/msg$mail--message-id"
                then
-                       check_mailinfo $mail --message-id
+                       check_mailinfo "$mail" --message-id
                fi
        '
 done
@@ -45,7 +47,7 @@ done
 
 test_expect_success 'split box with rfc2047 samples' \
        'mkdir rfc2047 &&
-       git mailsplit -orfc2047 "$TEST_DIRECTORY"/t5100/rfc2047-samples.mbox \
+       git mailsplit -orfc2047 "$DATA/rfc2047-samples.mbox" \
          >rfc2047/last &&
        last=$(cat rfc2047/last) &&
        echo total is $last &&
@@ -54,20 +56,20 @@ test_expect_success 'split box with rfc2047 samples' \
 for mail in rfc2047/00*
 do
        test_expect_success "mailinfo $mail" '
-               git mailinfo -u $mail-msg $mail-patch <$mail >$mail-info &&
+               git mailinfo -u "$mail-msg" "$mail-patch" <"$mail" >"$mail-info" &&
                echo msg &&
-               test_cmp "$TEST_DIRECTORY"/t5100/empty $mail-msg &&
+               test_cmp "$DATA/empty" "$mail-msg" &&
                echo patch &&
-               test_cmp "$TEST_DIRECTORY"/t5100/empty $mail-patch &&
+               test_cmp "$DATA/empty" "$mail-patch" &&
                echo info &&
-               test_cmp "$TEST_DIRECTORY"/t5100/rfc2047-info-$(basename $mail) $mail-info
+               test_cmp "$DATA/rfc2047-info-$(basename $mail)" "$mail-info"
        '
 done
 
 test_expect_success 'respect NULs' '
 
-       git mailsplit -d3 -o. "$TEST_DIRECTORY"/t5100/nul-plain &&
-       test_cmp "$TEST_DIRECTORY"/t5100/nul-plain 001 &&
+       git mailsplit -d3 -o. "$DATA/nul-plain" &&
+       test_cmp "$DATA/nul-plain" 001 &&
        (cat 001 | git mailinfo msg patch) &&
        test_line_count = 4 patch
 
@@ -75,52 +77,52 @@ test_expect_success 'respect NULs' '
 
 test_expect_success 'Preserve NULs out of MIME encoded message' '
 
-       git mailsplit -d5 -o. "$TEST_DIRECTORY"/t5100/nul-b64.in &&
-       test_cmp "$TEST_DIRECTORY"/t5100/nul-b64.in 00001 &&
+       git mailsplit -d5 -o. "$DATA/nul-b64.in" &&
+       test_cmp "$DATA/nul-b64.in" 00001 &&
        git mailinfo msg patch <00001 &&
-       test_cmp "$TEST_DIRECTORY"/t5100/nul-b64.expect patch
+       test_cmp "$DATA/nul-b64.expect" patch
 
 '
 
 test_expect_success 'mailinfo on from header without name works' '
 
        mkdir info-from &&
-       git mailsplit -oinfo-from "$TEST_DIRECTORY"/t5100/info-from.in &&
-       test_cmp "$TEST_DIRECTORY"/t5100/info-from.in info-from/0001 &&
+       git mailsplit -oinfo-from "$DATA/info-from.in" &&
+       test_cmp "$DATA/info-from.in" info-from/0001 &&
        git mailinfo info-from/msg info-from/patch \
          <info-from/0001 >info-from/out &&
-       test_cmp "$TEST_DIRECTORY"/t5100/info-from.expect info-from/out
+       test_cmp "$DATA/info-from.expect" info-from/out
 
 '
 
 test_expect_success 'mailinfo finds headers after embedded From line' '
        mkdir embed-from &&
-       git mailsplit -oembed-from "$TEST_DIRECTORY"/t5100/embed-from.in &&
-       test_cmp "$TEST_DIRECTORY"/t5100/embed-from.in embed-from/0001 &&
+       git mailsplit -oembed-from "$DATA/embed-from.in" &&
+       test_cmp "$DATA/embed-from.in" embed-from/0001 &&
        git mailinfo embed-from/msg embed-from/patch \
          <embed-from/0001 >embed-from/out &&
-       test_cmp "$TEST_DIRECTORY"/t5100/embed-from.expect embed-from/out
+       test_cmp "$DATA/embed-from.expect" embed-from/out
 '
 
 test_expect_success 'mailinfo on message with quoted >From' '
        mkdir quoted-from &&
-       git mailsplit -oquoted-from "$TEST_DIRECTORY"/t5100/quoted-from.in &&
-       test_cmp "$TEST_DIRECTORY"/t5100/quoted-from.in quoted-from/0001 &&
+       git mailsplit -oquoted-from "$DATA/quoted-from.in" &&
+       test_cmp "$DATA/quoted-from.in" quoted-from/0001 &&
        git mailinfo quoted-from/msg quoted-from/patch \
          <quoted-from/0001 >quoted-from/out &&
-       test_cmp "$TEST_DIRECTORY"/t5100/quoted-from.expect quoted-from/msg
+       test_cmp "$DATA/quoted-from.expect" quoted-from/msg
 '
 
 test_expect_success 'mailinfo unescapes with --mboxrd' '
        mkdir mboxrd &&
        git mailsplit -omboxrd --mboxrd \
-               "$TEST_DIRECTORY"/t5100/sample.mboxrd >last &&
+               "$DATA/sample.mboxrd" >last &&
        test x"$(cat last)" = x2 &&
        for i in 0001 0002
        do
                git mailinfo mboxrd/msg mboxrd/patch \
                  <mboxrd/$i >mboxrd/out &&
-               test_cmp "$TEST_DIRECTORY"/t5100/${i}mboxrd mboxrd/msg
+               test_cmp "$DATA/${i}mboxrd" mboxrd/msg
        done &&
        sp=" " &&
        echo "From " >expect &&
@@ -142,4 +144,18 @@ test_expect_success 'mailinfo unescapes with --mboxrd' '
        test_cmp expect mboxrd/msg
 '
 
+test_expect_success 'mailinfo handles rfc2822 quoted-string' '
+       mkdir quoted-string &&
+       git mailinfo /dev/null /dev/null <"$DATA/quoted-string.in" \
+               >quoted-string/info &&
+       test_cmp "$DATA/quoted-string.expect" quoted-string/info
+'
+
+test_expect_success 'mailinfo handles rfc2822 comment' '
+       mkdir comment &&
+       git mailinfo /dev/null /dev/null <"$DATA/comment.in" \
+               >comment/info &&
+       test_cmp "$DATA/comment.expect" comment/info
+'
+
 test_done
diff --git a/t/t5100/comment.expect b/t/t5100/comment.expect
new file mode 100644 (file)
index 0000000..7228177
--- /dev/null
@@ -0,0 +1,5 @@
+Author: A U Thor (this is (really) a comment (honestly))
+Email: somebody@example.com
+Subject: testing comments
+Date: Sun, 25 May 2008 00:38:18 -0700
+
diff --git a/t/t5100/comment.in b/t/t5100/comment.in
new file mode 100644 (file)
index 0000000..c53a192
--- /dev/null
@@ -0,0 +1,9 @@
+From 1234567890123456789012345678901234567890 Mon Sep 17 00:00:00 2001
+From: "A U Thor" <somebody@example.com> (this is \(really\) a comment (honestly))
+Date: Sun, 25 May 2008 00:38:18 -0700
+Subject: [PATCH] testing comments
+
+
+
+---
+patch
diff --git a/t/t5100/quoted-string.expect b/t/t5100/quoted-string.expect
new file mode 100644 (file)
index 0000000..cab1bce
--- /dev/null
@@ -0,0 +1,5 @@
+Author: Author "The Author" Name
+Email: somebody@example.com
+Subject: testing quoted-pair
+Date: Sun, 25 May 2008 00:38:18 -0700
+
diff --git a/t/t5100/quoted-string.in b/t/t5100/quoted-string.in
new file mode 100644 (file)
index 0000000..e2e627a
--- /dev/null
@@ -0,0 +1,9 @@
+From 1234567890123456789012345678901234567890 Mon Sep 17 00:00:00 2001
+From: "Author \"The Author\" Name" <somebody@example.com>
+Date: Sun, 25 May 2008 00:38:18 -0700
+Subject: [PATCH] testing quoted-pair
+
+
+
+---
+patch