+ from email.Header import decode_header, make_header
+
+ def __decode_header(header):
+ """Decode a qp-encoded e-mail header as per rfc2047"""
+ try:
+ words_enc = decode_header(header)
+ hobj = make_header(words_enc)
+ except Exception, ex:
+ raise CmdException, 'header decoding error: %s' % str(ex)
+ return unicode(hobj).encode('utf-8')
+
+ # parse the headers
+ if msg.has_key('from'):
+ authname, authemail = name_email(__decode_header(msg['from']))
+ else:
+ authname = authemail = None
+
+ # '\n\t' can be found on multi-line headers
+ descr = __decode_header(msg['subject']).replace('\n\t', ' ')
+ authdate = msg['date']
+
+ # remove the '[*PATCH*]' expression in the subject
+ if descr:
+ descr = re.findall('^(\[.*?[Pp][Aa][Tt][Cc][Hh].*?\])?\s*(.*)$',
+ descr)[0][1]
+ else:
+ raise CmdException, 'Subject: line not found'
+
+ # the rest of the message
+ msg_text = ''
+ for part in msg.walk():
+ if part.get_content_type() == 'text/plain':
+ msg_text += part.get_payload(decode = True)
+
+ rem_descr, diff = __split_descr_diff(msg_text)
+ if rem_descr:
+ descr += '\n\n' + rem_descr
+
+ # parse the description for author information
+ descr, descr_authname, descr_authemail, descr_authdate = \
+ __parse_description(descr)
+ if descr_authname:
+ authname = descr_authname
+ if descr_authemail:
+ authemail = descr_authemail
+ if descr_authdate:
+ authdate = descr_authdate
+
+ return (descr, authname, authemail, authdate, diff)
+
+def parse_patch(text, contains_diff):
+ """Parse the input text and return (description, authname,
+ authemail, authdate, diff)
+ """
+ if contains_diff:
+ (text, diff) = __split_descr_diff(text)
+ else:
+ diff = None
+ (descr, authname, authemail, authdate) = __parse_description(text)
+
+ # we don't yet have an agreed place for the creation date.
+ # Just return None
+ return (descr, authname, authemail, authdate, diff)
+
+def readonly_constant_property(f):
+ """Decorator that converts a function that computes a value to an
+ attribute that returns the value. The value is computed only once,
+ the first time it is accessed."""
+ def new_f(self):
+ n = '__' + f.__name__
+ if not hasattr(self, n):
+ setattr(self, n, f(self))
+ return getattr(self, n)
+ return property(new_f)
+
+def update_commit_data(cd, options, allow_edit = False):
+ """Return a new CommitData object updated according to the command line
+ options."""
+ # Set the commit message from commandline.
+ if options.message != None:
+ cd = cd.set_message(options.message)
+
+ # Modify author data.
+ cd = cd.set_author(options.author(cd.author))
+
+ # Add Signed-off-by: or similar.
+ if options.sign_str != None:
+ sign_str = options.sign_str
+ else:
+ sign_str = config.get("stgit.autosign")
+ if sign_str != None:
+ cd = cd.set_message(
+ add_sign_line(cd.message, sign_str,
+ cd.committer.name, cd.committer.email))
+
+ # Let user edit the commit message manually.
+ if allow_edit and not options.message:
+ cd = cd.set_message(edit_string(cd.message, '.stgit-new.txt'))
+
+ return cd