#!/usr/bin/env python
+# -*- coding: utf-8 -*-
# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
# Try to keep this small as it may be invoked frequently for each message
return _os_path.join(hash[:2], hash[2:])
+def _decode_header(string):
+ """Wrap email.header.decode_header to assemble a string.
+
+ >>> _decode_header(string='hello')
+ 'hello'
+ >>> _decode_header(string='=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=')
+ 'Keld Jørn Simonsen'
+ """
+ chunks = []
+ for decoded, charset in _email_header.decode_header(string):
+ if charset:
+ decoded = str(decoded, charset)
+ chunks.append(decoded)
+ return ''.join(chunks)
+
+
def get_commit_message(message):
- """Unwrap and decode the message subject for use as a commit message."""
+ r"""Unwrap and decode the message subject for use as a commit message.
+
+ >>> get_commit_message(
+ ... message={'Subject': '=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\n\t=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?='})
+ 'If you can read this you understand the example.'
+ """
commit_message = message.get('Subject', '<no subject>')
commit_message = _FOLDING_WHITESPACE_REGEX.sub(' ', commit_message)
- commit_message = ''.join(
- str(decoded, charset) for decoded, charset
- in _email_header.decode_header(commit_message))
- return commit_message
+ return _decode_header(string=commit_message)
def get_author(message):