# Path: pypasta/Repository/Mbox.py
# class PatchMail(MessageDiff):
#     REGEX_RAW_DIFF = re.compile(r'---.*\n\+\+\+.*\n.*', re.MULTILINE)
# 
#     @staticmethod
#     def extract_patch_mail(mail):
#         id = mail['message-id']
# 
#         # The easy case: Mail is a raw text mail, and contains the patch inline
#         payload = decode_payload(mail)
#         if payload:
#             return mail, payload
# 
#         # The complex case: mail consists of multiple parts:
#         payload = mail.get_payload()
#         if isinstance(payload, str):
#             return mail, payload
# 
#         if not isinstance(payload, list):
#             log.warning('IMPLEMENT ME! %s' % id)
#             raise NotImplementedError('impl me')
# 
#         # Try to decode all message parts
#         payload = [decode_payload(x) for x in payload]
#         payload = list(filter(None, payload))
#         # Let's test if one of the payloads can be converted to a
#         # mail object. In that case, it's very likely that a patch created by
#         # git format-patch was sent as attachment
#         for p in payload:
#             try:
#                 m = email.message_from_string(p)
#                 # If there's no from, we probably have no real mail
#                 if len(m.defects) or len(m.keys()) == 0 or 'From' not in m:
#                     continue
# 
#                 # Hey, we have a valid email as attachment. Use it.
#                 p = decode_payload(m)
#                 return m, p
#             except:
#                 pass
# 
#         if len(payload) >= 2:
#             msg = str()
#             diff = str()
#             for p in payload:
#                 if not isinstance(p, str):
#                     continue
# 
#                 s = p.split('\n')
#                 if any(map(lambda line: line.startswith('diff --'), s)) or \
#                        any(map(lambda line: line.startswith('Index: '), s)) or \
#                        PatchMail.REGEX_RAW_DIFF.match(p):
#                     diff += p + '\n'
#                 else:
#                     msg += p + '\n'
# 
#             # We need at least len(diff). len(msg) doesn't make sense: The
#             # commit message may hide inside the diff part. Later, let the
#             # splitting approach decide.
#             if len(diff):
#                 return mail, msg + diff + '\n'
# 
#         for p in payload:
#             if 'From: ' in p or 'diff --' in p:
#                 return mail, p
# 
#         raise ValueError('Unable to find suitable payload')
# 
#     def __init__(self, mail, identifier):
#         # Get informations on the author
#         date = mail_parse_date(mail['Date'], assume_epoch=True)
# 
#         mail, payload = self.extract_patch_mail(mail)
#         self.mail_subject = mail['Subject']
# 
#         author = str(mail['From'])
#         author_name, author_email = email.utils.parseaddr(author)
#         author = Signature(author_name, author_email, date)
# 
#         retval = parse_single_message(payload)
#         if retval is None:
#             raise TypeError('Unable to split mail to msg and diff')
# 
#         msg, annotation, diff = retval
# 
#         # reconstruct commit message
#         subject = self.mail_subject
#         match = PATCH_SUBJECT_REGEX.match(self.mail_subject)
#         if match:
#             subject = match.group(1)
#         msg = [subject, ''] + msg
# 
#         content = msg, annotation, diff
# 
#         super(PatchMail, self).__init__(identifier, content, author)
# 
#     def format_message(self):
#         custom = ['Mail Subject: %s' % self.subject]
#         return super(PatchMail, self).format_message(custom)


import os
import sys
from logging import getLogger
from pypasta import *
from pypasta.Repository import PatchMail
from flask import Flask, render_template, request, Response
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import Navbar, View
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import InputRequired, Length
        arguments = request.form
    else:
        arguments = request.args

    lookup_form = CommitForm(arguments, csrf_enabled=False)
    repo = config.repo

    def render_mbox(base=None, history=None):
        return render_template('mbox.html',
                               title='Mbox',
                               lookup_form=lookup_form,
                               project=config.project_name,
                               base=base,
                               history=history)

    if not lookup_form.validate():
        return render_mbox()

    patch = repo[lookup_form.id.data]
    id = patch.identifier

    if id not in cluster:
        return render_mbox(patch)

    elements = cluster.get_upstream(id) | cluster.get_downstream(id)

    # convert ids to commit objects
    elements = {repo.get_commit(x) for x in elements}
    # sort them by author_date
    elements = sorted(elements,

# Path: pypasta/Repository/Mbox.py
# class PatchMail(MessageDiff):
#     REGEX_RAW_DIFF = re.compile(r'---.*\n\+\+\+.*\n.*', re.MULTILINE)
# 
#     @staticmethod
#     def extract_patch_mail(mail):
#         id = mail['message-id']
# 
#         # The easy case: Mail is a raw text mail, and contains the patch inline
#         payload = decode_payload(mail)
#         if payload:
#             return mail, payload
# 
#         # The complex case: mail consists of multiple parts:
#         payload = mail.get_payload()
#         if isinstance(payload, str):
#             return mail, payload
# 
#         if not isinstance(payload, list):
#             log.warning('IMPLEMENT ME! %s' % id)
#             raise NotImplementedError('impl me')
# 
#         # Try to decode all message parts
#         payload = [decode_payload(x) for x in payload]
#         payload = list(filter(None, payload))
#         # Let's test if one of the payloads can be converted to a
#         # mail object. In that case, it's very likely that a patch created by
#         # git format-patch was sent as attachment
#         for p in payload:
#             try:
#                 m = email.message_from_string(p)
#                 # If there's no from, we probably have no real mail
#                 if len(m.defects) or len(m.keys()) == 0 or 'From' not in m:
#                     continue
# 
#                 # Hey, we have a valid email as attachment. Use it.
#                 p = decode_payload(m)
#                 return m, p
#             except:
#                 pass
# 
#         if len(payload) >= 2:
#             msg = str()
#             diff = str()
#             for p in payload:
#                 if not isinstance(p, str):
#                     continue
# 
#                 s = p.split('\n')
#                 if any(map(lambda line: line.startswith('diff --'), s)) or \
#                        any(map(lambda line: line.startswith('Index: '), s)) or \
#                        PatchMail.REGEX_RAW_DIFF.match(p):
#                     diff += p + '\n'
#                 else:
#                     msg += p + '\n'
# 
#             # We need at least len(diff). len(msg) doesn't make sense: The
#             # commit message may hide inside the diff part. Later, let the
#             # splitting approach decide.
#             if len(diff):
#                 return mail, msg + diff + '\n'
# 
#         for p in payload:
#             if 'From: ' in p or 'diff --' in p:
#                 return mail, p
# 
#         raise ValueError('Unable to find suitable payload')
# 
#     def __init__(self, mail, identifier):
#         # Get informations on the author
#         date = mail_parse_date(mail['Date'], assume_epoch=True)
# 
#         mail, payload = self.extract_patch_mail(mail)
#         self.mail_subject = mail['Subject']
# 
#         author = str(mail['From'])
#         author_name, author_email = email.utils.parseaddr(author)
#         author = Signature(author_name, author_email, date)
# 
#         retval = parse_single_message(payload)
#         if retval is None:
#             raise TypeError('Unable to split mail to msg and diff')
# 
#         msg, annotation, diff = retval
# 
#         # reconstruct commit message
#         subject = self.mail_subject
#         match = PATCH_SUBJECT_REGEX.match(self.mail_subject)
#         if match:
#             subject = match.group(1)
#         msg = [subject, ''] + msg
# 
#         content = msg, annotation, diff
# 
#         super(PatchMail, self).__init__(identifier, content, author)
# 
#     def format_message(self):
#         custom = ['Mail Subject: %s' % self.subject]
#         return super(PatchMail, self).format_message(custom)


# Path: bin/pasta_web.py
import os
import sys
from logging import getLogger
from pypasta import *
from pypasta.Repository import PatchMail
from flask import Flask, render_template, request, Response
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import Navbar, View
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import InputRequired, Length

        arguments = request.form
    else:
        arguments = request.args

    lookup_form = CommitForm(arguments, csrf_enabled=False)
    repo = config.repo

    def render_mbox(base=None, history=None):
        return render_template('mbox.html',
                               title='Mbox',
                               lookup_form=lookup_form,
                               project=config.project_name,
                               base=base,
                               history=history)

    if not lookup_form.validate():
        return render_mbox()

    patch = repo[lookup_form.id.data]
    id = patch.identifier

    if id not in cluster:
        return render_mbox(patch)

    elements = cluster.get_upstream(id) | cluster.get_downstream(id)

    # convert ids to commit objects
    elements = {repo.get_commit(x) for x in elements}
    # sort them by author_date
    elements = sorted(elements,

                      key=lambda x: x.author.date if isinstance(x, PatchMail)