hgext/releasenotes.py
author Jun Wu <quark@fb.com>
Fri, 11 Aug 2017 01:08:07 -0700
changeset 33849 3ae2eaecb49e
parent 33784 589fda7895da
child 33881 6a49c74b1e8f
permissions -rw-r--r--
rebase: optimize "source" calculation in adjustdest The "source" variable is calculated inside a loop but it does not depend on loop variables. Therefore move it outside the loop. Differential Revision: https://phab.mercurial-scm.org/D345
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
# Copyright 2017-present Gregory Szorc <gregory.szorc@gmail.com>
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
#
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
# This software may be used and distributed according to the terms of the
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
# GNU General Public License version 2 or any later version.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
"""generate release notes from commit messages (EXPERIMENTAL)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
It is common to maintain files detailing changes in a project between
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
releases. Maintaining these files can be difficult and time consuming.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
The :hg:`releasenotes` command provided by this extension makes the
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    11
process simpler by automating it.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    12
"""
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    13
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
from __future__ import absolute_import
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    15
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    16
import errno
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    17
import re
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    18
import sys
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    19
import textwrap
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    20
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    21
from mercurial.i18n import _
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    22
from mercurial import (
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
    23
    config,
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
    error,
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    25
    minirst,
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    26
    registrar,
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    27
    scmutil,
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
    28
    util,
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    29
)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
cmdtable = {}
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
command = registrar.command(cmdtable)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
# be specifying the version(s) of Mercurial they are tested with, or
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
# leave the attribute unspecified.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
testedwith = 'ships-with-hg-core'
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    40
DEFAULT_SECTIONS = [
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
    ('feature', _('New Features')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
    ('bc', _('Backwards Compatibility Changes')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
    ('fix', _('Bug Fixes')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    44
    ('perf', _('Performance Improvements')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
    ('api', _('API Changes')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    46
]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    47
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    48
RE_DIRECTIVE = re.compile('^\.\. ([a-zA-Z0-9_]+)::\s*([^$]+)?$')
33698
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
    49
RE_ISSUE = r'\bissue ?[0-9]{4,6}(?![0-9])\b'
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
BULLET_SECTION = _('Other Changes')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    52
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    53
class parsedreleasenotes(object):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
    def __init__(self):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
        self.sections = {}
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    56
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    57
    def __contains__(self, section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    58
        return section in self.sections
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    59
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    60
    def __iter__(self):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    61
        return iter(sorted(self.sections))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    62
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    63
    def addtitleditem(self, section, title, paragraphs):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    64
        """Add a titled release note entry."""
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    65
        self.sections.setdefault(section, ([], []))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    66
        self.sections[section][0].append((title, paragraphs))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    67
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    68
    def addnontitleditem(self, section, paragraphs):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    69
        """Adds a non-titled release note entry.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    70
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    71
        Will be rendered as a bullet point.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    72
        """
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    73
        self.sections.setdefault(section, ([], []))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    74
        self.sections[section][1].append(paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    75
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    76
    def titledforsection(self, section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    77
        """Returns titled entries in a section.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    78
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    79
        Returns a list of (title, paragraphs) tuples describing sub-sections.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    80
        """
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    81
        return self.sections.get(section, ([], []))[0]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    82
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    83
    def nontitledforsection(self, section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    84
        """Returns non-titled, bulleted paragraphs in a section."""
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    85
        return self.sections.get(section, ([], []))[1]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    86
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    87
    def hastitledinsection(self, section, title):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    88
        return any(t[0] == title for t in self.titledforsection(section))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    89
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    90
    def merge(self, ui, other):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    91
        """Merge another instance into this one.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    92
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    93
        This is used to combine multiple sources of release notes together.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    94
        """
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    95
        for section in other:
33698
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
    96
            existingnotes = converttitled(self.titledforsection(section)) + \
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
    97
                convertnontitled(self.nontitledforsection(section))
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    98
            for title, paragraphs in other.titledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    99
                if self.hastitledinsection(section, title):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   100
                    # TODO prompt for resolution if different and running in
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   101
                    # interactive mode.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   102
                    ui.write(_('%s already exists in %s section; ignoring\n') %
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   103
                             (title, section))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   104
                    continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   105
33698
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   106
                incoming_str = converttitled([(title, paragraphs)])[0]
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   107
                if section == 'fix':
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   108
                    issue = getissuenum(incoming_str)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   109
                    if issue:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   110
                        if findissue(ui, existingnotes, issue):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   111
                            continue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   112
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   113
                if similar(ui, existingnotes, incoming_str):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   114
                    continue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   115
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   116
                self.addtitleditem(section, title, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   117
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   118
            for paragraphs in other.nontitledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   119
                if paragraphs in self.nontitledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   120
                    continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   121
33698
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   122
                incoming_str = convertnontitled([paragraphs])[0]
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   123
                if section == 'fix':
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   124
                    issue = getissuenum(incoming_str)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   125
                    if issue:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   126
                        if findissue(ui, existingnotes, issue):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   127
                            continue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   128
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   129
                if similar(ui, existingnotes, incoming_str):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   130
                    continue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   131
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   132
                self.addnontitleditem(section, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   133
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   134
class releasenotessections(object):
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   135
    def __init__(self, ui, repo=None):
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   136
        if repo:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   137
            sections = util.sortdict(DEFAULT_SECTIONS)
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   138
            custom_sections = getcustomadmonitions(repo)
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   139
            if custom_sections:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   140
                sections.update(custom_sections)
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   141
            self._sections = list(sections.iteritems())
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   142
        else:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   143
            self._sections = list(DEFAULT_SECTIONS)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   144
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   145
    def __iter__(self):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   146
        return iter(self._sections)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   147
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   148
    def names(self):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   149
        return [t[0] for t in self._sections]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   150
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   151
    def sectionfromtitle(self, title):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   152
        for name, value in self._sections:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   153
            if value == title:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   154
                return name
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   155
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   156
        return None
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   157
33698
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   158
def converttitled(titledparagraphs):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   159
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   160
    Convert titled paragraphs to strings
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   161
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   162
    string_list = []
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   163
    for title, paragraphs in titledparagraphs:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   164
        lines = []
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   165
        for para in paragraphs:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   166
            lines.extend(para)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   167
        string_list.append(' '.join(lines))
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   168
    return string_list
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   169
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   170
def convertnontitled(nontitledparagraphs):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   171
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   172
    Convert non-titled bullets to strings
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   173
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   174
    string_list = []
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   175
    for paragraphs in nontitledparagraphs:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   176
        lines = []
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   177
        for para in paragraphs:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   178
            lines.extend(para)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   179
        string_list.append(' '.join(lines))
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   180
    return string_list
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   181
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   182
def getissuenum(incoming_str):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   183
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   184
    Returns issue number from the incoming string if it exists
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   185
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   186
    issue = re.search(RE_ISSUE, incoming_str, re.IGNORECASE)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   187
    if issue:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   188
        issue = issue.group()
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   189
    return issue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   190
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   191
def findissue(ui, existing, issue):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   192
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   193
    Returns true if issue number already exists in notes.
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   194
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   195
    if any(issue in s for s in existing):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   196
        ui.write(_('"%s" already exists in notes; ignoring\n') % issue)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   197
        return True
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   198
    else:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   199
        return False
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   200
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   201
def similar(ui, existing, incoming_str):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   202
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   203
    Returns true if similar note found in existing notes.
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   204
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   205
    if len(incoming_str.split()) > 10:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   206
        merge = similaritycheck(incoming_str, existing)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   207
        if not merge:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   208
            ui.write(_('"%s" already exists in notes file; ignoring\n')
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   209
                     % incoming_str)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   210
            return True
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   211
        else:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   212
            return False
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   213
    else:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   214
        return False
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   215
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   216
def similaritycheck(incoming_str, existingnotes):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   217
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   218
    Returns true when note fragment can be merged to existing notes.
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   219
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   220
    import fuzzywuzzy.fuzz as fuzz
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   221
    merge = True
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   222
    for bullet in existingnotes:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   223
        score = fuzz.token_set_ratio(incoming_str, bullet)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   224
        if score > 75:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   225
            merge = False
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   226
            break
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   227
    return merge
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   228
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   229
def getcustomadmonitions(repo):
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   230
    ctx = repo['.']
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   231
    p = config.config()
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   232
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   233
    def read(f, sections=None, remap=None):
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   234
        if f in ctx:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   235
            data = ctx[f].data()
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   236
            p.parse(f, data, sections, remap, read)
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   237
        else:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   238
            raise error.Abort(_(".hgreleasenotes file \'%s\' not found") %
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   239
                              repo.pathto(f))
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   240
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   241
    if '.hgreleasenotes' in ctx:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   242
        read('.hgreleasenotes')
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   243
    return p['sections']
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   244
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   245
def parsenotesfromrevisions(repo, directives, revs):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   246
    notes = parsedreleasenotes()
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   247
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   248
    for rev in revs:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   249
        ctx = repo[rev]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   250
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   251
        blocks, pruned = minirst.parse(ctx.description(),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   252
                                       admonitions=directives)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   253
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   254
        for i, block in enumerate(blocks):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   255
            if block['type'] != 'admonition':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   256
                continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   257
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   258
            directive = block['admonitiontitle']
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   259
            title = block['lines'][0].strip() if block['lines'] else None
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   260
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   261
            if i + 1 == len(blocks):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   262
                raise error.Abort(_('release notes directive %s lacks content')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   263
                                  % directive)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   264
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   265
            # Now search ahead and find all paragraphs attached to this
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   266
            # admonition.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   267
            paragraphs = []
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   268
            for j in range(i + 1, len(blocks)):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   269
                pblock = blocks[j]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   270
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   271
                # Margin blocks may appear between paragraphs. Ignore them.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   272
                if pblock['type'] == 'margin':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   273
                    continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   274
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   275
                if pblock['type'] != 'paragraph':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   276
                    raise error.Abort(_('unexpected block in release notes '
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   277
                                        'directive %s') % directive)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   278
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   279
                if pblock['indent'] > 0:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   280
                    paragraphs.append(pblock['lines'])
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   281
                else:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   282
                    break
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   283
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   284
            # TODO consider using title as paragraph for more concise notes.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   285
            if not paragraphs:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   286
                raise error.Abort(_('could not find content for release note '
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   287
                                    '%s') % directive)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   288
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   289
            if title:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   290
                notes.addtitleditem(directive, title, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   291
            else:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   292
                notes.addnontitleditem(directive, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   293
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   294
    return notes
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   295
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   296
def parsereleasenotesfile(sections, text):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   297
    """Parse text content containing generated release notes."""
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   298
    notes = parsedreleasenotes()
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   299
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   300
    blocks = minirst.parse(text)[0]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   301
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   302
    def gatherparagraphsbullets(offset, title=False):
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   303
        notefragment = []
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   304
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   305
        for i in range(offset + 1, len(blocks)):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   306
            block = blocks[i]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   307
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   308
            if block['type'] == 'margin':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   309
                continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   310
            elif block['type'] == 'section':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   311
                break
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   312
            elif block['type'] == 'bullet':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   313
                if block['indent'] != 0:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   314
                    raise error.Abort(_('indented bullet lists not supported'))
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   315
                if title:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   316
                    lines = [l[1:].strip() for l in block['lines']]
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   317
                    notefragment.append(lines)
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   318
                    continue
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   319
                else:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   320
                    lines = [[l[1:].strip() for l in block['lines']]]
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   321
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   322
                    for block in blocks[i + 1:]:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   323
                        if block['type'] in ('bullet', 'section'):
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   324
                            break
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   325
                        if block['type'] == 'paragraph':
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   326
                            lines.append(block['lines'])
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   327
                    notefragment.append(lines)
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   328
                    continue
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   329
            elif block['type'] != 'paragraph':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   330
                raise error.Abort(_('unexpected block type in release notes: '
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   331
                                    '%s') % block['type'])
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   332
            if title:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   333
                notefragment.append(block['lines'])
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   334
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   335
        return notefragment
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   336
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   337
    currentsection = None
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   338
    for i, block in enumerate(blocks):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   339
        if block['type'] != 'section':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   340
            continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   341
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   342
        title = block['lines'][0]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   343
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   344
        # TODO the parsing around paragraphs and bullet points needs some
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   345
        # work.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   346
        if block['underline'] == '=':  # main section
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   347
            name = sections.sectionfromtitle(title)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   348
            if not name:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   349
                raise error.Abort(_('unknown release notes section: %s') %
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   350
                                  title)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   351
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   352
            currentsection = name
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   353
            bullet_points = gatherparagraphsbullets(i)
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   354
            if bullet_points:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   355
                for para in bullet_points:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   356
                    notes.addnontitleditem(currentsection, para)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   357
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   358
        elif block['underline'] == '-':  # sub-section
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   359
            if title == BULLET_SECTION:
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   360
                bullet_points = gatherparagraphsbullets(i)
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   361
                for para in bullet_points:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   362
                    notes.addnontitleditem(currentsection, para)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   363
            else:
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   364
                paragraphs = gatherparagraphsbullets(i, True)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   365
                notes.addtitleditem(currentsection, title, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   366
        else:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   367
            raise error.Abort(_('unsupported section type for %s') % title)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   368
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   369
    return notes
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   370
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   371
def serializenotes(sections, notes):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   372
    """Serialize release notes from parsed fragments and notes.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   373
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   374
    This function essentially takes the output of ``parsenotesfromrevisions()``
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   375
    and ``parserelnotesfile()`` and produces output combining the 2.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   376
    """
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   377
    lines = []
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   378
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   379
    for sectionname, sectiontitle in sections:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   380
        if sectionname not in notes:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   381
            continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   382
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   383
        lines.append(sectiontitle)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   384
        lines.append('=' * len(sectiontitle))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   385
        lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   386
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   387
        # First pass to emit sub-sections.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   388
        for title, paragraphs in notes.titledforsection(sectionname):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   389
            lines.append(title)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   390
            lines.append('-' * len(title))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   391
            lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   392
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   393
            wrapper = textwrap.TextWrapper(width=78)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   394
            for i, para in enumerate(paragraphs):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   395
                if i:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   396
                    lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   397
                lines.extend(wrapper.wrap(' '.join(para)))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   398
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   399
            lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   400
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   401
        # Second pass to emit bullet list items.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   402
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   403
        # If the section has titled and non-titled items, we can't
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   404
        # simply emit the bullet list because it would appear to come
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   405
        # from the last title/section. So, we emit a new sub-section
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   406
        # for the non-titled items.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   407
        nontitled = notes.nontitledforsection(sectionname)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   408
        if notes.titledforsection(sectionname) and nontitled:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   409
            # TODO make configurable.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   410
            lines.append(BULLET_SECTION)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   411
            lines.append('-' * len(BULLET_SECTION))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   412
            lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   413
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   414
        for paragraphs in nontitled:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   415
            wrapper = textwrap.TextWrapper(initial_indent='* ',
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   416
                                           subsequent_indent='  ',
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   417
                                           width=78)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   418
            lines.extend(wrapper.wrap(' '.join(paragraphs[0])))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   419
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   420
            wrapper = textwrap.TextWrapper(initial_indent='  ',
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   421
                                           subsequent_indent='  ',
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   422
                                           width=78)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   423
            for para in paragraphs[1:]:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   424
                lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   425
                lines.extend(wrapper.wrap(' '.join(para)))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   426
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   427
            lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   428
33784
589fda7895da releasenotes: minor bug fix for index error while serializing
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33698
diff changeset
   429
    if lines and lines[-1]:
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   430
        lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   431
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   432
    return '\n'.join(lines)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   433
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   434
@command('releasenotes',
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   435
    [('r', 'rev', '', _('revisions to process for release notes'), _('REV'))],
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   436
    _('[-r REV] FILE'))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   437
def releasenotes(ui, repo, file_, rev=None):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   438
    """parse release notes from commit messages into an output file
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   439
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   440
    Given an output file and set of revisions, this command will parse commit
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   441
    messages for release notes then add them to the output file.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   442
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   443
    Release notes are defined in commit messages as ReStructuredText
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   444
    directives. These have the form::
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   445
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   446
       .. directive:: title
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   447
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   448
          content
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   449
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   450
    Each ``directive`` maps to an output section in a generated release notes
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   451
    file, which itself is ReStructuredText. For example, the ``.. feature::``
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   452
    directive would map to a ``New Features`` section.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   453
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   454
    Release note directives can be either short-form or long-form. In short-
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   455
    form, ``title`` is omitted and the release note is rendered as a bullet
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   456
    list. In long form, a sub-section with the title ``title`` is added to the
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   457
    section.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   458
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   459
    The ``FILE`` argument controls the output file to write gathered release
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   460
    notes to. The format of the file is::
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   461
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   462
       Section 1
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   463
       =========
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   464
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   465
       ...
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   466
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   467
       Section 2
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   468
       =========
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   469
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   470
       ...
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   471
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   472
    Only sections with defined release notes are emitted.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   473
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   474
    If a section only has short-form notes, it will consist of bullet list::
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   475
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   476
       Section
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   477
       =======
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   478
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   479
       * Release note 1
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   480
       * Release note 2
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   481
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   482
    If a section has long-form notes, sub-sections will be emitted::
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   483
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   484
       Section
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   485
       =======
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   486
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   487
       Note 1 Title
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   488
       ------------
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   489
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   490
       Description of the first long-form note.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   491
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   492
       Note 2 Title
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   493
       ------------
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   494
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   495
       Description of the second long-form note.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   496
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   497
    If the ``FILE`` argument points to an existing file, that file will be
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   498
    parsed for release notes having the format that would be generated by this
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   499
    command. The notes from the processed commit messages will be *merged*
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   500
    into this parsed set.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   501
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   502
    During release notes merging:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   503
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   504
    * Duplicate items are automatically ignored
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   505
    * Items that are different are automatically ignored if the similarity is
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   506
      greater than a threshold.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   507
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   508
    This means that the release notes file can be updated independently from
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   509
    this command and changes should not be lost when running this command on
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   510
    that file. A particular use case for this is to tweak the wording of a
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   511
    release note after it has been added to the release notes file.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   512
    """
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   513
    sections = releasenotessections(ui, repo)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   514
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   515
    revs = scmutil.revrange(repo, [rev or 'not public()'])
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   516
    incoming = parsenotesfromrevisions(repo, sections.names(), revs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   517
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   518
    try:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   519
        with open(file_, 'rb') as fh:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   520
            notes = parsereleasenotesfile(sections, fh.read())
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   521
    except IOError as e:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   522
        if e.errno != errno.ENOENT:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   523
            raise
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   524
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   525
        notes = parsedreleasenotes()
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   526
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   527
    notes.merge(ui, incoming)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   528
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   529
    with open(file_, 'wb') as fh:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   530
        fh.write(serializenotes(sections, notes))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   531
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   532
@command('debugparsereleasenotes', norepo=True)
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   533
def debugparsereleasenotes(ui, path, repo=None):
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   534
    """parse release notes and print resulting data structure"""
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   535
    if path == '-':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   536
        text = sys.stdin.read()
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   537
    else:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   538
        with open(path, 'rb') as fh:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   539
            text = fh.read()
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   540
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   541
    sections = releasenotessections(ui, repo)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   542
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   543
    notes = parsereleasenotesfile(sections, text)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   544
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   545
    for section in notes:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   546
        ui.write(_('section: %s\n') % section)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   547
        for title, paragraphs in notes.titledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   548
            ui.write(_('  subsection: %s\n') % title)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   549
            for para in paragraphs:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   550
                ui.write(_('    paragraph: %s\n') % ' '.join(para))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   551
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   552
        for paragraphs in notes.nontitledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   553
            ui.write(_('  bullet point:\n'))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   554
            for para in paragraphs:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   555
                ui.write(_('    paragraph: %s\n') % ' '.join(para))