Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address headers encoding #34

Merged
merged 3 commits into from Aug 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 31 additions & 5 deletions git-multimail/git_multimail.py
Expand Up @@ -73,6 +73,7 @@
LOGBEGIN = '- Log -----------------------------------------------------------------\n'
LOGEND = '-----------------------------------------------------------------------\n'

ADDR_HEADERS = set(['from', 'to', 'cc', 'bcc', 'reply-to', 'sender'])

# It is assumed in many places that the encoding is uniformly UTF-8,
# so changing these constants is unsupported. But define them here
Expand Down Expand Up @@ -297,6 +298,31 @@ def read_git_lines(args, keepends=False, **kw):
return read_git_output(args, keepends=True, **kw).splitlines(keepends)


def header_encode(text, header_name=None):
"""Encode and line-wrap the value of an email header field."""

try:
if isinstance(text, str):
text = text.decode(ENCODING, errors='replace')
return Header(text, header_name=header_name).encode()
except UnicodeEncodeError:
return Header(text, header_name=header_name, charset=CHARSET,
errors='replace').encode()


def addr_header_encode(text, header_name=None):
"""Encode and line-wrap the value of an email header field containing
email addresses."""

return Header(
', '.join(
formataddr((header_encode(name), emailaddr))
for name, emailaddr in getaddresses([text])
),
header_name=header_name
).encode()


class Config(object):
def __init__(self, section, git_config=None):
"""Represent a section of the git configuration.
Expand Down Expand Up @@ -578,11 +604,11 @@ def expand_header_lines(self, template, **extra_values):
% (e.args[0], line,)
)
else:
try:
h = Header(value, header_name=name)
except UnicodeDecodeError:
h = Header(value, header_name=name, charset=CHARSET, errors='replace')
for splitline in ('%s: %s\n' % (name, h.encode(),)).splitlines(True):
if name.lower() in ADDR_HEADERS:
value = addr_header_encode(value, name)
else:
value = header_encode(value, name)
for splitline in ('%s: %s\n' % (name, value)).splitlines(True):
yield splitline

def generate_email_header(self):
Expand Down
2 changes: 1 addition & 1 deletion t/create-test-repo
Expand Up @@ -19,7 +19,7 @@ git config multimailhook.reponame 'test-repo'

git config multimailhook.mailinglist 'Mailing List <mailinglist@example.com>'
git config multimailhook.refchangelist 'Refchange List <refchangelist@example.com>'
git config multimailhook.announcelist 'Announce List <announcelist@example.com>'
git config multimailhook.announcelist 'Announce List <announcelist@example.com>, Zébulon <zeb@example.com>'
git config multimailhook.commitlist 'Commit List <commitlist@example.com>'

git config multimailhook.announceshortlog 'true'
Expand Down
5 changes: 5 additions & 0 deletions t/generate-test-emails
Expand Up @@ -8,6 +8,11 @@ POST_RECEIVE="$D/../git-multimail/post-receive"

TESTREPO=$("$D/create-test-repo")

HOME="$D"
XDG_CONFIG_HOME="$D"
GIT_CONFIG_NOSYSTEM=1
export HOME XDG_CONFIG_HOME GIT_CONFIG_NOSYSTEM

cd $TESTREPO

test_email() {
Expand Down
100 changes: 56 additions & 44 deletions t/multimail.expect
Expand Up @@ -1532,10 +1532,11 @@ To stop receiving notification emails like this one, please contact
Administrator <administrator@example.com>.
EOF
######################################################################
Sending notification emails to: Announce List <announcelist@example.com>
Sending notification emails to: Announce List <announcelist@example.com>, Zébulon <zeb@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Announce List <announcelist@example.com>
To: Announce List <announcelist@example.com>,
=?utf-8?q?Z=C3=A9bulon?= <zeb@example.com>
Subject: *test-repo* annotated tag tag-annotated created (now bc4a4a6)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Expand Down Expand Up @@ -1577,10 +1578,11 @@ To stop receiving notification emails like this one, please contact
Administrator <administrator@example.com>.
EOF
######################################################################
Sending notification emails to: Announce List <announcelist@example.com>
Sending notification emails to: Announce List <announcelist@example.com>, Zébulon <zeb@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Announce List <announcelist@example.com>
To: Announce List <announcelist@example.com>,
=?utf-8?q?Z=C3=A9bulon?= <zeb@example.com>
Subject: *test-repo* annotated tag tag-annotated updated (902dfe1 -> bc4a4a6)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Expand Down Expand Up @@ -1657,10 +1659,11 @@ To stop receiving notification emails like this one, please contact
Administrator <administrator@example.com>.
EOF
######################################################################
Sending notification emails to: Announce List <announcelist@example.com>
Sending notification emails to: Announce List <announcelist@example.com>, Zébulon <zeb@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Announce List <announcelist@example.com>
To: Announce List <announcelist@example.com>,
=?utf-8?q?Z=C3=A9bulon?= <zeb@example.com>
Subject: *test-repo* annotated tag tag-annotated deleted (was bc4a4a6)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Expand Down Expand Up @@ -1693,10 +1696,11 @@ To stop receiving notification emails like this one, please contact
Administrator <administrator@example.com>.
EOF
######################################################################
Sending notification emails to: Announce List <announcelist@example.com>
Sending notification emails to: Announce List <announcelist@example.com>, Zébulon <zeb@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Announce List <announcelist@example.com>
To: Announce List <announcelist@example.com>,
=?utf-8?q?Z=C3=A9bulon?= <zeb@example.com>
Subject: *test-repo* annotated tag tag-annotated-new-content created (now
3fe4049)
MIME-Version: 1.0
Expand Down Expand Up @@ -1836,10 +1840,11 @@ To stop receiving notification emails like this one, please contact
Administrator <administrator@example.com>.
EOF
######################################################################
Sending notification emails to: Announce List <announcelist@example.com>
Sending notification emails to: Announce List <announcelist@example.com>, Zébulon <zeb@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Announce List <announcelist@example.com>
To: Announce List <announcelist@example.com>,
=?utf-8?q?Z=C3=A9bulon?= <zeb@example.com>
Subject: *test-repo* annotated tag tag-annotated-new-content updated
(902dfe1 -> 3fe4049)
MIME-Version: 1.0
Expand Down Expand Up @@ -2009,10 +2014,11 @@ To stop receiving notification emails like this one, please contact
Administrator <administrator@example.com>.
EOF
######################################################################
Sending notification emails to: Announce List <announcelist@example.com>
Sending notification emails to: Announce List <announcelist@example.com>, Zébulon <zeb@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Announce List <announcelist@example.com>
To: Announce List <announcelist@example.com>,
=?utf-8?q?Z=C3=A9bulon?= <zeb@example.com>
Subject: *test-repo* annotated tag tag-annotated-new-content deleted (was
3fe4049)
MIME-Version: 1.0
Expand Down Expand Up @@ -2047,10 +2053,11 @@ To stop receiving notification emails like this one, please contact
Administrator <administrator@example.com>.
EOF
######################################################################
Sending notification emails to: Announce List <announcelist@example.com>
Sending notification emails to: Announce List <announcelist@example.com>, Zébulon <zeb@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Announce List <announcelist@example.com>
To: Announce List <announcelist@example.com>,
=?utf-8?q?Z=C3=A9bulon?= <zeb@example.com>
Subject: *test-repo* annotated tag tree-tag created (now df5bc27)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Expand Down Expand Up @@ -2089,10 +2096,11 @@ To stop receiving notification emails like this one, please contact
Administrator <administrator@example.com>.
EOF
######################################################################
Sending notification emails to: Announce List <announcelist@example.com>
Sending notification emails to: Announce List <announcelist@example.com>, Zébulon <zeb@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Announce List <announcelist@example.com>
To: Announce List <announcelist@example.com>,
=?utf-8?q?Z=C3=A9bulon?= <zeb@example.com>
Subject: *test-repo* annotated tag tree-tag updated (902dfe1 -> df5bc27)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Expand Down Expand Up @@ -2134,10 +2142,11 @@ To stop receiving notification emails like this one, please contact
Administrator <administrator@example.com>.
EOF
######################################################################
Sending notification emails to: Announce List <announcelist@example.com>
Sending notification emails to: Announce List <announcelist@example.com>, Zébulon <zeb@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Announce List <announcelist@example.com>
To: Announce List <announcelist@example.com>,
=?utf-8?q?Z=C3=A9bulon?= <zeb@example.com>
Subject: *test-repo* annotated tag tree-tag deleted (was df5bc27)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Expand Down Expand Up @@ -2170,10 +2179,11 @@ To stop receiving notification emails like this one, please contact
Administrator <administrator@example.com>.
EOF
######################################################################
Sending notification emails to: Announce List <announcelist@example.com>
Sending notification emails to: Announce List <announcelist@example.com>, Zébulon <zeb@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Announce List <announcelist@example.com>
To: Announce List <announcelist@example.com>,
=?utf-8?q?Z=C3=A9bulon?= <zeb@example.com>
Subject: *test-repo* annotated tag recursive-tag created (now b56d4b8)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Expand Down Expand Up @@ -2210,10 +2220,11 @@ To stop receiving notification emails like this one, please contact
Administrator <administrator@example.com>.
EOF
######################################################################
Sending notification emails to: Announce List <announcelist@example.com>
Sending notification emails to: Announce List <announcelist@example.com>, Zébulon <zeb@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Announce List <announcelist@example.com>
To: Announce List <announcelist@example.com>,
=?utf-8?q?Z=C3=A9bulon?= <zeb@example.com>
Subject: *test-repo* annotated tag recursive-tag updated (902dfe1 -> b56d4b8)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Expand Down Expand Up @@ -2285,10 +2296,11 @@ To stop receiving notification emails like this one, please contact
Administrator <administrator@example.com>.
EOF
######################################################################
Sending notification emails to: Announce List <announcelist@example.com>
Sending notification emails to: Announce List <announcelist@example.com>, Zébulon <zeb@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Announce List <announcelist@example.com>
To: Announce List <announcelist@example.com>,
=?utf-8?q?Z=C3=A9bulon?= <zeb@example.com>
Subject: *test-repo* annotated tag recursive-tag deleted (was b56d4b8)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Expand Down Expand Up @@ -2652,7 +2664,7 @@ Sending notification emails to: Refchange List <refchangelist@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Refchange List <refchangelist@example.com>
Subject: *test-repo* reference refs/foo/bar created (now ab5a78a)
Subject: *test-repo* reference refs/foo/bar created (now ce88e42)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Expand All @@ -2663,19 +2675,19 @@ X-Git-Repo: test-repo
X-Git-Refname: refs/foo/bar
X-Git-Reftype: reference
X-Git-Oldrev: 0000000000000000000000000000000000000000
X-Git-Newrev: ab5a78a6d6540fc787daee288e2f29804430c7a9
X-Git-Newrev: ce88e4233b43b275d24695813d0bae1460a8f1b8
Auto-Submitted: auto-generated

This is an automated email from the git hooks/post-receive script.

pushuser pushed a change to reference refs/foo/bar
in repository test-repo.

at ab5a78a b1
at ce88e42 b1

This reference includes the following new commits:

new ab5a78a b1
new ce88e42 b1

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
Expand All @@ -2696,22 +2708,22 @@ MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
From: From <from@example.com>
Reply-To: Joe User <user@example.com>
Reply-To: =?utf-8?q?Jo=C3=ABl_User?= <user@example.com>
In-Reply-To: <...>
References: <...>
X-Git-Repo: test-repo
X-Git-Refname: refs/foo/bar
X-Git-Reftype: reference
X-Git-Rev: ab5a78a6d6540fc787daee288e2f29804430c7a9
X-Git-Rev: ce88e4233b43b275d24695813d0bae1460a8f1b8
Auto-Submitted: auto-generated

This is an automated email from the git hooks/post-receive script.

pushuser pushed a commit to reference refs/foo/bar
in repository test-repo.

commit ab5a78a6d6540fc787daee288e2f29804430c7a9
Author: Joe User <user@example.com>
commit ce88e4233b43b275d24695813d0bae1460a8f1b8
Author: Joël User <user@example.com>
Date: Sat Jan 26 07:43:14 2013 +0100

b1
Expand All @@ -2738,7 +2750,7 @@ Sending notification emails to: Refchange List <refchangelist@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Refchange List <refchangelist@example.com>
Subject: *test-repo* reference refs/foo/bar updated (902dfe1 -> ab5a78a)
Subject: *test-repo* reference refs/foo/bar updated (902dfe1 -> ce88e42)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Expand All @@ -2749,7 +2761,7 @@ X-Git-Repo: test-repo
X-Git-Refname: refs/foo/bar
X-Git-Reftype: reference
X-Git-Oldrev: 902dfe1c4025851d6b175c8f1efeee9ee1a0b74d
X-Git-Newrev: ab5a78a6d6540fc787daee288e2f29804430c7a9
X-Git-Newrev: ce88e4233b43b275d24695813d0bae1460a8f1b8
Auto-Submitted: auto-generated

This is an automated email from the git hooks/post-receive script.
Expand All @@ -2765,7 +2777,7 @@ in repository test-repo.
omits ebf40e1 a4
omits 012fc78 a3
omits 50d684a a2
new ab5a78a b1
new ce88e42 b1

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
Expand All @@ -2775,7 +2787,7 @@ containing something like this:

* -- * -- B -- O -- O -- O (902dfe1)
\
N -- N -- N refs/foo/bar (ab5a78a)
N -- N -- N refs/foo/bar (ce88e42)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
Expand Down Expand Up @@ -2807,22 +2819,22 @@ MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
From: From <from@example.com>
Reply-To: Joe User <user@example.com>
Reply-To: =?utf-8?q?Jo=C3=ABl_User?= <user@example.com>
In-Reply-To: <...>
References: <...>
X-Git-Repo: test-repo
X-Git-Refname: refs/foo/bar
X-Git-Reftype: reference
X-Git-Rev: ab5a78a6d6540fc787daee288e2f29804430c7a9
X-Git-Rev: ce88e4233b43b275d24695813d0bae1460a8f1b8
Auto-Submitted: auto-generated

This is an automated email from the git hooks/post-receive script.

pushuser pushed a commit to reference refs/foo/bar
in repository test-repo.

commit ab5a78a6d6540fc787daee288e2f29804430c7a9
Author: Joe User <user@example.com>
commit ce88e4233b43b275d24695813d0bae1460a8f1b8
Author: Joël User <user@example.com>
Date: Sat Jan 26 07:43:14 2013 +0100

b1
Expand All @@ -2849,7 +2861,7 @@ Sending notification emails to: Refchange List <refchangelist@example.com>
######################################################################
/usr/sbin/sendmail -t -f Sender <sender@example.com> <<EOF
To: Refchange List <refchangelist@example.com>
Subject: *test-repo* reference refs/foo/bar deleted (was ab5a78a)
Subject: *test-repo* reference refs/foo/bar deleted (was ce88e42)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Expand All @@ -2859,7 +2871,7 @@ Reply-To: pushuser@example.com
X-Git-Repo: test-repo
X-Git-Refname: refs/foo/bar
X-Git-Reftype: reference
X-Git-Oldrev: ab5a78a6d6540fc787daee288e2f29804430c7a9
X-Git-Oldrev: ce88e4233b43b275d24695813d0bae1460a8f1b8
X-Git-Newrev: 0000000000000000000000000000000000000000
Auto-Submitted: auto-generated

Expand All @@ -2868,11 +2880,11 @@ This is an automated email from the git hooks/post-receive script.
pushuser pushed a change to reference refs/foo/bar
in repository test-repo.

was ab5a78a b1
was ce88e42 b1

This change permanently discards the following revisions:

discards ab5a78a b1
discards ce88e42 b1

--
To stop receiving notification emails like this one, please contact
Expand Down