Пример #1
0
def makepatch(ui, repo, patch, opts, _charsets, idx, total, patchname=None):

    desc = []
    node = None
    body = ''

    for line in patch:
        if line.startswith('#'):
            if line.startswith('# Node ID'):
                node = line.split()[-1]
            continue
        if line.startswith('diff -r') or line.startswith('diff --git'):
            break
        desc.append(line)

    if not patchname and not node:
        raise ValueError

    if opts.get('attach'):
        body = ('\n'.join(desc[1:]).strip() or
                'Patch subject is complete summary.')
        body += '\n\n\n'

    if opts.get('plain'):
        while patch and patch[0].startswith('# '):
            patch.pop(0)
        if patch:
            patch.pop(0)
        while patch and not patch[0].strip():
            patch.pop(0)

    if opts.get('diffstat'):
        body += cdiffstat(ui, '\n'.join(desc), patch) + '\n\n'

    if opts.get('attach') or opts.get('inline'):
        msg = email.MIMEMultipart.MIMEMultipart()
        if body:
            msg.attach(mail.mimeencode(ui, body, _charsets, opts.get('test')))
        p = mail.mimetextpatch('\n'.join(patch), 'x-patch', opts.get('test'))
        binnode = bin(node)
        # if node is mq patch, it will have the patch file's name as a tag
        if not patchname:
            patchtags = [t for t in repo.nodetags(binnode)
                         if t.endswith('.patch') or t.endswith('.diff')]
            if patchtags:
                patchname = patchtags[0]
            elif total > 1:
                patchname = cmdutil.make_filename(repo, '%b-%n.patch',
                                                  binnode, seqno=idx, total=total)
            else:
                patchname = cmdutil.make_filename(repo, '%b.patch', binnode)
        disposition = 'inline'
        if opts.get('attach'):
            disposition = 'attachment'
        p['Content-Disposition'] = disposition + '; filename=' + patchname
        msg.attach(p)
    else:
        body += '\n'.join(patch)
        msg = mail.mimetextpatch(body, display=opts.get('test'))

    flag = ' '.join(opts.get('flag'))
    if flag:
        flag = ' ' + flag

    subj = desc[0].strip().rstrip('. ')
    if total == 1 and not opts.get('intro'):
        subj = '[PATCH%s] %s' % (flag, opts.get('subject') or subj)
    else:
        tlen = len(str(total))
        subj = '[PATCH %0*d of %d%s] %s' % (tlen, idx, total, flag, subj)
    msg['Subject'] = mail.headencode(ui, subj, _charsets, opts.get('test'))
    msg['X-Mercurial-Node'] = node
    return msg, subj
Пример #2
0
def makepatch(ui,
              repo,
              patchlines,
              opts,
              _charsets,
              idx,
              total,
              numbered,
              patchname=None):

    desc = []
    node = None
    body = ''

    for line in patchlines:
        if line.startswith('#'):
            if line.startswith('# Node ID'):
                node = line.split()[-1]
            continue
        if line.startswith('diff -r') or line.startswith('diff --git'):
            break
        desc.append(line)

    if not patchname and not node:
        raise ValueError

    if opts.get('attach'):
        body = ('\n'.join(desc[1:]).strip()
                or 'Patch subject is complete summary.')
        body += '\n\n\n'

    if opts.get('plain'):
        while patchlines and patchlines[0].startswith('# '):
            patchlines.pop(0)
        if patchlines:
            patchlines.pop(0)
        while patchlines and not patchlines[0].strip():
            patchlines.pop(0)

    ds = patch.diffstat(patchlines, git=opts.get('git'))
    if opts.get('diffstat'):
        body += ds + '\n\n'

    if opts.get('attach') or opts.get('inline'):
        msg = email.MIMEMultipart.MIMEMultipart()
        if body:
            msg.attach(mail.mimeencode(ui, body, _charsets, opts.get('test')))
        p = mail.mimetextpatch('\n'.join(patchlines), 'x-patch',
                               opts.get('test'))
        binnode = bin(node)
        # if node is mq patch, it will have the patch file's name as a tag
        if not patchname:
            patchtags = [
                t for t in repo.nodetags(binnode)
                if t.endswith('.patch') or t.endswith('.diff')
            ]
            if patchtags:
                patchname = patchtags[0]
            elif total > 1:
                patchname = cmdutil.makefilename(repo,
                                                 '%b-%n.patch',
                                                 binnode,
                                                 seqno=idx,
                                                 total=total)
            else:
                patchname = cmdutil.makefilename(repo, '%b.patch', binnode)
        disposition = 'inline'
        if opts.get('attach'):
            disposition = 'attachment'
        p['Content-Disposition'] = disposition + '; filename=' + patchname
        msg.attach(p)
    else:
        body += '\n'.join(patchlines)
        msg = mail.mimetextpatch(body, display=opts.get('test'))

    flag = ' '.join(opts.get('flag'))
    if flag:
        flag = ' ' + flag

    subj = desc[0].strip().rstrip('. ')
    if not numbered:
        subj = '[PATCH%s] %s' % (flag, opts.get('subject') or subj)
    else:
        tlen = len(str(total))
        subj = '[PATCH %0*d of %d%s] %s' % (tlen, idx, total, flag, subj)
    msg['Subject'] = mail.headencode(ui, subj, _charsets, opts.get('test'))
    msg['X-Mercurial-Node'] = node
    return msg, subj, ds
Пример #3
0
def makepatch(
    ui,
    repo,
    rev,
    patchlines,
    opts,
    _charsets,
    idx,
    total,
    numbered,
    patchname=None,
):

    desc = []
    node = None
    body = b''

    for line in patchlines:
        if line.startswith(b'#'):
            if line.startswith(b'# Node ID'):
                node = line.split()[-1]
            continue
        if line.startswith(b'diff -r') or line.startswith(b'diff --git'):
            break
        desc.append(line)

    if not patchname and not node:
        raise ValueError

    if opts.get(b'attach') and not opts.get(b'body'):
        body = (b'\n'.join(desc[1:]).strip()
                or b'Patch subject is complete summary.')
        body += b'\n\n\n'

    if opts.get(b'plain'):
        while patchlines and patchlines[0].startswith(b'# '):
            patchlines.pop(0)
        if patchlines:
            patchlines.pop(0)
        while patchlines and not patchlines[0].strip():
            patchlines.pop(0)

    ds = patch.diffstat(patchlines)
    if opts.get(b'diffstat'):
        body += ds + b'\n\n'

    addattachment = opts.get(b'attach') or opts.get(b'inline')
    if not addattachment or opts.get(b'body'):
        body += b'\n'.join(patchlines)

    if addattachment:
        msg = emimemultipart.MIMEMultipart()
        if body:
            msg.attach(mail.mimeencode(ui, body, _charsets, opts.get(b'test')))
        p = mail.mimetextpatch(b'\n'.join(patchlines), 'x-patch',
                               opts.get(b'test'))
        binnode = bin(node)
        # if node is mq patch, it will have the patch file's name as a tag
        if not patchname:
            patchtags = [
                t for t in repo.nodetags(binnode)
                if t.endswith(b'.patch') or t.endswith(b'.diff')
            ]
            if patchtags:
                patchname = patchtags[0]
            elif total > 1:
                patchname = cmdutil.makefilename(repo[node],
                                                 b'%b-%n.patch',
                                                 seqno=idx,
                                                 total=total)
            else:
                patchname = cmdutil.makefilename(repo[node], b'%b.patch')
        disposition = r'inline'
        if opts.get(b'attach'):
            disposition = r'attachment'
        p['Content-Disposition'] = (disposition + '; filename=' +
                                    encoding.strfromlocal(patchname))
        msg.attach(p)
    else:
        msg = mail.mimetextpatch(body, display=opts.get(b'test'))

    prefix = _formatprefix(ui, repo, rev, opts.get(b'flag'), idx, total,
                           numbered)
    subj = desc[0].strip().rstrip(b'. ')
    if not numbered:
        subj = b' '.join([prefix, opts.get(b'subject') or subj])
    else:
        subj = b' '.join([prefix, subj])
    msg['Subject'] = mail.headencode(ui, subj, _charsets, opts.get(b'test'))
    msg['X-Mercurial-Node'] = pycompat.sysstr(node)
    msg['X-Mercurial-Series-Index'] = '%i' % idx
    msg['X-Mercurial-Series-Total'] = '%i' % total
    return msg, subj, ds