hgext/releasenotes.py
author Augie Fackler <augie@google.com>
Sat, 13 Oct 2018 04:00:47 -0400
changeset 40235 a7cdd81f191b
parent 40234 3fc2ef49959c
child 40243 96e50dfd8c94
permissions -rw-r--r--
releasenotes: fix remaining bytes/unicode issues caught by tests All tests now pass. Differential Revision: https://phab.mercurial-scm.org/D5054
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
33881
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
    16
import difflib
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    17
import errno
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    18
import re
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,
34811
a542ad320adb releasenotes: don't abort is there is a bad formatted entry for releasenotes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34810
diff changeset
    26
    node,
35003
e68dd1909af3 py3: handle keyword arguments in hgext/releasenotes.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34813
diff changeset
    27
    pycompat,
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
    registrar,
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    29
    scmutil,
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
    30
    util,
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
cmdtable = {}
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
command = registrar.command(cmdtable)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
34812
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
    36
try:
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
    37
    import fuzzywuzzy.fuzz as fuzz
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
    38
    fuzz.token_set_ratio
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
    39
except ImportError:
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
    40
    fuzz = None
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
    41
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
# 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
    43
# 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
    44
# 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
    45
# leave the attribute unspecified.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    46
testedwith = 'ships-with-hg-core'
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
DEFAULT_SECTIONS = [
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
    ('feature', _('New Features')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
    ('bc', _('Backwards Compatibility Changes')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
    ('fix', _('Bug Fixes')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    52
    ('perf', _('Performance Improvements')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    53
    ('api', _('API Changes')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    56
RE_DIRECTIVE = re.compile('^\.\. ([a-zA-Z0-9_]+)::\s*([^$]+)?$')
40235
a7cdd81f191b releasenotes: fix remaining bytes/unicode issues caught by tests
Augie Fackler <augie@google.com>
parents: 40234
diff changeset
    57
RE_ISSUE = br'\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
    58
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    59
BULLET_SECTION = _('Other Changes')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    60
40234
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    61
if pycompat.ispy3:
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    62
    class byteswrapper(object):
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    63
        def __init__(self, **kwargs):
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    64
            for k in kwargs:
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    65
                v = kwargs[k]
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    66
                if not isinstance(v, str) and isinstance(v, bytes):
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    67
                    kwargs[k] = v.decode('utf8')
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    68
            self._tw = textwrap.TextWrapper(**kwargs)
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    69
        def wrap(self, data):
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    70
            return [
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    71
                l.encode('utf8') for l in self._tw.wrap(data.decode('utf8'))]
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    72
else:
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    73
    byteswrapper = textwrap.TextWrapper
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
    74
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    75
class parsedreleasenotes(object):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    76
    def __init__(self):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    77
        self.sections = {}
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
    def __contains__(self, section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    80
        return section in self.sections
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    81
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    82
    def __iter__(self):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    83
        return iter(sorted(self.sections))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    84
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    85
    def addtitleditem(self, section, title, paragraphs):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    86
        """Add a titled release note entry."""
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    87
        self.sections.setdefault(section, ([], []))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    88
        self.sections[section][0].append((title, paragraphs))
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 addnontitleditem(self, section, paragraphs):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    91
        """Adds a non-titled release note entry.
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
        Will be rendered as a bullet point.
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
        self.sections.setdefault(section, ([], []))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    96
        self.sections[section][1].append(paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    97
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    98
    def titledforsection(self, section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    99
        """Returns titled entries in a section.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   100
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   101
        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
   102
        """
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   103
        return self.sections.get(section, ([], []))[0]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   104
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   105
    def nontitledforsection(self, section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   106
        """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
   107
        return self.sections.get(section, ([], []))[1]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   108
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   109
    def hastitledinsection(self, section, title):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   110
        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
   111
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   112
    def merge(self, ui, other):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   113
        """Merge another instance into this one.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   114
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   115
        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
   116
        """
34813
288fad8c55f9 releasenotes: show a warning if fuzzywuzzy is not present
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34812
diff changeset
   117
        if not fuzz:
288fad8c55f9 releasenotes: show a warning if fuzzywuzzy is not present
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34812
diff changeset
   118
            ui.warn(_("module 'fuzzywuzzy' not found, merging of similar "
288fad8c55f9 releasenotes: show a warning if fuzzywuzzy is not present
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34812
diff changeset
   119
                      "releasenotes is disabled\n"))
288fad8c55f9 releasenotes: show a warning if fuzzywuzzy is not present
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34812
diff changeset
   120
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   121
        for section in other:
33698
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   122
            existingnotes = converttitled(self.titledforsection(section)) + \
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   123
                convertnontitled(self.nontitledforsection(section))
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   124
            for title, paragraphs in other.titledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   125
                if self.hastitledinsection(section, title):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   126
                    # 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
   127
                    # interactive mode.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   128
                    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
   129
                             (title, section))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   130
                    continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   131
33698
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   132
                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
   133
                if section == 'fix':
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   134
                    issue = getissuenum(incoming_str)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   135
                    if issue:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   136
                        if findissue(ui, existingnotes, issue):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   137
                            continue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   138
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   139
                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
   140
                    continue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   141
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   142
                self.addtitleditem(section, title, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   143
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   144
            for paragraphs in other.nontitledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   145
                if paragraphs in self.nontitledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   146
                    continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   147
33698
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   148
                incoming_str = convertnontitled([paragraphs])[0]
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   149
                if section == 'fix':
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   150
                    issue = getissuenum(incoming_str)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   151
                    if issue:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   152
                        if findissue(ui, existingnotes, issue):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   153
                            continue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   154
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   155
                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
   156
                    continue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   157
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   158
                self.addnontitleditem(section, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   159
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   160
class releasenotessections(object):
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   161
    def __init__(self, ui, repo=None):
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   162
        if repo:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   163
            sections = util.sortdict(DEFAULT_SECTIONS)
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   164
            custom_sections = getcustomadmonitions(repo)
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   165
            if custom_sections:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   166
                sections.update(custom_sections)
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   167
            self._sections = list(sections.iteritems())
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   168
        else:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   169
            self._sections = list(DEFAULT_SECTIONS)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   170
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   171
    def __iter__(self):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   172
        return iter(self._sections)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   173
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   174
    def names(self):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   175
        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
   176
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   177
    def sectionfromtitle(self, title):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   178
        for name, value in self._sections:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   179
            if value == title:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   180
                return name
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   181
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   182
        return None
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   183
33698
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   184
def converttitled(titledparagraphs):
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
    Convert titled paragraphs to strings
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   187
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   188
    string_list = []
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   189
    for title, paragraphs in titledparagraphs:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   190
        lines = []
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   191
        for para in paragraphs:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   192
            lines.extend(para)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   193
        string_list.append(' '.join(lines))
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   194
    return string_list
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   195
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   196
def convertnontitled(nontitledparagraphs):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   197
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   198
    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
   199
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   200
    string_list = []
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   201
    for paragraphs in nontitledparagraphs:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   202
        lines = []
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   203
        for para in paragraphs:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   204
            lines.extend(para)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   205
        string_list.append(' '.join(lines))
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   206
    return string_list
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   207
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   208
def getissuenum(incoming_str):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   209
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   210
    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
   211
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   212
    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
   213
    if issue:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   214
        issue = issue.group()
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   215
    return issue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   216
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   217
def findissue(ui, existing, issue):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   218
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   219
    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
   220
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   221
    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
   222
        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
   223
        return True
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   224
    else:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   225
        return False
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   226
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   227
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
   228
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   229
    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
   230
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   231
    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
   232
        merge = similaritycheck(incoming_str, existing)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   233
        if not merge:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   234
            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
   235
                     % incoming_str)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   236
            return True
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   237
        else:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   238
            return False
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   239
    else:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   240
        return False
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   241
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   242
def similaritycheck(incoming_str, existingnotes):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   243
    """
34780
070ba789f4d0 releasenotes: fix documentation of similaritycheck()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34736
diff changeset
   244
    Returns false when note fragment can be merged to existing notes.
33698
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   245
    """
34812
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
   246
    # fuzzywuzzy not present
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
   247
    if not fuzz:
34810
44bd29168d14 releasenotes: make the import of fuzzywuzzy optional
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34780
diff changeset
   248
        return True
44bd29168d14 releasenotes: make the import of fuzzywuzzy optional
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34780
diff changeset
   249
33698
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   250
    merge = True
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   251
    for bullet in existingnotes:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   252
        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
   253
        if score > 75:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   254
            merge = False
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   255
            break
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   256
    return merge
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   257
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   258
def getcustomadmonitions(repo):
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   259
    ctx = repo['.']
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   260
    p = config.config()
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   261
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   262
    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
   263
        if f in ctx:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   264
            data = ctx[f].data()
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   265
            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
   266
        else:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   267
            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
   268
                              repo.pathto(f))
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   269
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   270
    if '.hgreleasenotes' in ctx:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   271
        read('.hgreleasenotes')
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   272
    return p['sections']
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   273
33881
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   274
def checkadmonitions(ui, repo, directives, revs):
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   275
    """
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   276
    Checks the commit messages for admonitions and their validity.
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   277
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   278
    .. abcd::
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   279
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   280
       First paragraph under this admonition
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   281
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   282
    For this commit message, using `hg releasenotes -r . --check`
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   283
    returns: Invalid admonition 'abcd' present in changeset 3ea92981e103
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   284
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   285
    As admonition 'abcd' is neither present in default nor custom admonitions
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   286
    """
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   287
    for rev in revs:
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   288
        ctx = repo[rev]
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   289
        admonition = re.search(RE_DIRECTIVE, ctx.description())
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   290
        if admonition:
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   291
            if admonition.group(1) in directives:
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   292
                continue
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   293
            else:
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   294
                ui.write(_("Invalid admonition '%s' present in changeset %s"
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   295
                           "\n") % (admonition.group(1), ctx.hex()[:12]))
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   296
                sim = lambda x: difflib.SequenceMatcher(None,
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   297
                    admonition.group(1), x).ratio()
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   298
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   299
                similar = [s for s in directives if sim(s) > 0.6]
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   300
                if len(similar) == 1:
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   301
                    ui.write(_("(did you mean %s?)\n") % similar[0])
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   302
                elif similar:
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   303
                    ss = ", ".join(sorted(similar))
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   304
                    ui.write(_("(did you mean one of %s?)\n") % ss)
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   305
33940
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33881
diff changeset
   306
def _getadmonitionlist(ui, sections):
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33881
diff changeset
   307
    for section in sections:
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33881
diff changeset
   308
        ui.write("%s: %s\n" % (section[0], section[1]))
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33881
diff changeset
   309
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   310
def parsenotesfromrevisions(repo, directives, revs):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   311
    notes = parsedreleasenotes()
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   312
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   313
    for rev in revs:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   314
        ctx = repo[rev]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   315
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   316
        blocks, pruned = minirst.parse(ctx.description(),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   317
                                       admonitions=directives)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   318
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   319
        for i, block in enumerate(blocks):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   320
            if block['type'] != 'admonition':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   321
                continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   322
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   323
            directive = block['admonitiontitle']
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   324
            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
   325
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   326
            if i + 1 == len(blocks):
36769
3fff6f30bd7f releasenotes: mention changeset with warning and abort
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 36768
diff changeset
   327
                raise error.Abort(_('changeset %s: release notes directive %s '
3fff6f30bd7f releasenotes: mention changeset with warning and abort
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 36768
diff changeset
   328
                        'lacks content') % (ctx, directive))
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   329
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   330
            # 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
   331
            # admonition.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   332
            paragraphs = []
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   333
            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
   334
                pblock = blocks[j]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   335
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   336
                # 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
   337
                if pblock['type'] == 'margin':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   338
                    continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   339
36770
a5891e94bfe1 releasenotes: allow notes for multiple directives in a single changeset
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 36769
diff changeset
   340
                if pblock['type'] == 'admonition':
a5891e94bfe1 releasenotes: allow notes for multiple directives in a single changeset
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 36769
diff changeset
   341
                    break
a5891e94bfe1 releasenotes: allow notes for multiple directives in a single changeset
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 36769
diff changeset
   342
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   343
                if pblock['type'] != 'paragraph':
36769
3fff6f30bd7f releasenotes: mention changeset with warning and abort
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 36768
diff changeset
   344
                    repo.ui.warn(_('changeset %s: unexpected block in release '
3fff6f30bd7f releasenotes: mention changeset with warning and abort
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 36768
diff changeset
   345
                        'notes directive %s\n') % (ctx, directive))
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   346
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   347
                if pblock['indent'] > 0:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   348
                    paragraphs.append(pblock['lines'])
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   349
                else:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   350
                    break
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
            # 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
   353
            if not paragraphs:
34811
a542ad320adb releasenotes: don't abort is there is a bad formatted entry for releasenotes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34810
diff changeset
   354
                repo.ui.warn(_("error parsing releasenotes for revision: "
a542ad320adb releasenotes: don't abort is there is a bad formatted entry for releasenotes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34810
diff changeset
   355
                               "'%s'\n") % node.hex(ctx.node()))
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   356
            if title:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   357
                notes.addtitleditem(directive, title, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   358
            else:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   359
                notes.addnontitleditem(directive, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   360
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   361
    return notes
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   362
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   363
def parsereleasenotesfile(sections, text):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   364
    """Parse text content containing generated release notes."""
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   365
    notes = parsedreleasenotes()
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   366
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   367
    blocks = minirst.parse(text)[0]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   368
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   369
    def gatherparagraphsbullets(offset, title=False):
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   370
        notefragment = []
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   371
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   372
        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
   373
            block = blocks[i]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   374
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   375
            if block['type'] == 'margin':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   376
                continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   377
            elif block['type'] == 'section':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   378
                break
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   379
            elif block['type'] == 'bullet':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   380
                if block['indent'] != 0:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   381
                    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
   382
                if title:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   383
                    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
   384
                    notefragment.append(lines)
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   385
                    continue
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   386
                else:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   387
                    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
   388
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   389
                    for block in blocks[i + 1:]:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   390
                        if block['type'] in ('bullet', 'section'):
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   391
                            break
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   392
                        if block['type'] == 'paragraph':
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   393
                            lines.append(block['lines'])
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   394
                    notefragment.append(lines)
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   395
                    continue
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   396
            elif block['type'] != 'paragraph':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   397
                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
   398
                                    '%s') % block['type'])
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   399
            if title:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   400
                notefragment.append(block['lines'])
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   401
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   402
        return notefragment
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   403
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   404
    currentsection = None
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   405
    for i, block in enumerate(blocks):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   406
        if block['type'] != 'section':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   407
            continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   408
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   409
        title = block['lines'][0]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   410
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   411
        # 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
   412
        # work.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   413
        if block['underline'] == '=':  # main section
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   414
            name = sections.sectionfromtitle(title)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   415
            if not name:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   416
                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
   417
                                  title)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   418
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   419
            currentsection = name
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   420
            bullet_points = gatherparagraphsbullets(i)
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   421
            if bullet_points:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   422
                for para in bullet_points:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   423
                    notes.addnontitleditem(currentsection, para)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   424
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   425
        elif block['underline'] == '-':  # sub-section
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   426
            if title == BULLET_SECTION:
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   427
                bullet_points = gatherparagraphsbullets(i)
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   428
                for para in bullet_points:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   429
                    notes.addnontitleditem(currentsection, para)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   430
            else:
33012
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32778
diff changeset
   431
                paragraphs = gatherparagraphsbullets(i, True)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   432
                notes.addtitleditem(currentsection, title, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   433
        else:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   434
            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
   435
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   436
    return notes
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   437
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   438
def serializenotes(sections, notes):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   439
    """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
   440
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   441
    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
   442
    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
   443
    """
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   444
    lines = []
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
    for sectionname, sectiontitle in sections:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   447
        if sectionname not in notes:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   448
            continue
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
        lines.append(sectiontitle)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   451
        lines.append('=' * len(sectiontitle))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   452
        lines.append('')
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
        # First pass to emit sub-sections.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   455
        for title, paragraphs in notes.titledforsection(sectionname):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   456
            lines.append(title)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   457
            lines.append('-' * len(title))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   458
            lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   459
40234
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
   460
            wrapper = byteswrapper(width=78)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   461
            for i, para in enumerate(paragraphs):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   462
                if i:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   463
                    lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   464
                lines.extend(wrapper.wrap(' '.join(para)))
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
            lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   467
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   468
        # Second pass to emit bullet list items.
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
        # 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
   471
        # 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
   472
        # 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
   473
        # for the non-titled items.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   474
        nontitled = notes.nontitledforsection(sectionname)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   475
        if notes.titledforsection(sectionname) and nontitled:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   476
            # TODO make configurable.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   477
            lines.append(BULLET_SECTION)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   478
            lines.append('-' * len(BULLET_SECTION))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   479
            lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   480
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   481
        for paragraphs in nontitled:
40234
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
   482
            wrapper = byteswrapper(initial_indent='* ',
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
   483
                                   subsequent_indent='  ',
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
   484
                                   width=78)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   485
            lines.extend(wrapper.wrap(' '.join(paragraphs[0])))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   486
40234
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
   487
            wrapper = byteswrapper(initial_indent='  ',
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
   488
                                   subsequent_indent='  ',
3fc2ef49959c relnotes: port to Python 3
Augie Fackler <augie@google.com>
parents: 36770
diff changeset
   489
                                   width=78)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   490
            for para in paragraphs[1:]:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   491
                lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   492
                lines.extend(wrapper.wrap(' '.join(para)))
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
            lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   495
33784
589fda7895da releasenotes: minor bug fix for index error while serializing
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33698
diff changeset
   496
    if lines and lines[-1]:
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   497
        lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   498
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   499
    return '\n'.join(lines)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   500
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   501
@command('releasenotes',
33881
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   502
    [('r', 'rev', '', _('revisions to process for release notes'), _('REV')),
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   503
    ('c', 'check', False, _('checks for validity of admonitions (if any)'),
33940
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33881
diff changeset
   504
        _('REV')),
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33881
diff changeset
   505
    ('l', 'list', False, _('list the available admonitions with their title'),
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33881
diff changeset
   506
        None)],
33881
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   507
    _('hg releasenotes [-r REV] [-c] FILE'))
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   508
def releasenotes(ui, repo, file_=None, **opts):
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   509
    """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
   510
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   511
    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
   512
    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
   513
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   514
    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
   515
    directives. These have the form::
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   516
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   517
       .. directive:: title
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   518
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   519
          content
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   520
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   521
    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
   522
    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
   523
    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
   524
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   525
    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
   526
    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
   527
    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
   528
    section.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   529
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   530
    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
   531
    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
   532
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   533
       Section 1
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   534
       =========
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   535
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   536
       ...
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   537
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   538
       Section 2
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   539
       =========
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   540
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   541
       ...
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
    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
   544
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   545
    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
   546
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   547
       Section
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   548
       =======
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   549
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   550
       * Release note 1
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   551
       * Release note 2
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   552
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   553
    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
   554
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   555
       Section
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   556
       =======
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   557
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   558
       Note 1 Title
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   559
       ------------
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   560
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   561
       Description of the first long-form note.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   562
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   563
       Note 2 Title
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   564
       ------------
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   565
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   566
       Description of the second long-form note.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   567
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   568
    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
   569
    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
   570
    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
   571
    into this parsed set.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   572
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   573
    During release notes merging:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   574
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   575
    * Duplicate items are automatically ignored
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   576
    * 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
   577
      greater than a threshold.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   578
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   579
    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
   580
    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
   581
    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
   582
    release note after it has been added to the release notes file.
34341
01e8ab4b6573 releasenotes: update docstrings with information on additional flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34340
diff changeset
   583
01e8ab4b6573 releasenotes: update docstrings with information on additional flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34340
diff changeset
   584
    The -c/--check option checks the commit message for invalid admonitions.
01e8ab4b6573 releasenotes: update docstrings with information on additional flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34340
diff changeset
   585
01e8ab4b6573 releasenotes: update docstrings with information on additional flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34340
diff changeset
   586
    The -l/--list option, presents the user with a list of existing available
01e8ab4b6573 releasenotes: update docstrings with information on additional flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34340
diff changeset
   587
    admonitions along with their title. This also includes the custom
01e8ab4b6573 releasenotes: update docstrings with information on additional flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34340
diff changeset
   588
    admonitions (if any).
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   589
    """
35003
e68dd1909af3 py3: handle keyword arguments in hgext/releasenotes.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34813
diff changeset
   590
e68dd1909af3 py3: handle keyword arguments in hgext/releasenotes.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34813
diff changeset
   591
    opts = pycompat.byteskwargs(opts)
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   592
    sections = releasenotessections(ui, repo)
34340
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33940
diff changeset
   593
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33940
diff changeset
   594
    listflag = opts.get('list')
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33940
diff changeset
   595
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33940
diff changeset
   596
    if listflag and opts.get('rev'):
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33940
diff changeset
   597
        raise error.Abort(_('cannot use both \'--list\' and \'--rev\''))
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33940
diff changeset
   598
    if listflag and opts.get('check'):
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33940
diff changeset
   599
        raise error.Abort(_('cannot use both \'--list\' and \'--check\''))
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33940
diff changeset
   600
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33940
diff changeset
   601
    if listflag:
33940
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33881
diff changeset
   602
        return _getadmonitionlist(ui, sections)
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33881
diff changeset
   603
33881
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   604
    rev = opts.get('rev')
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   605
    revs = scmutil.revrange(repo, [rev or 'not public()'])
33881
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   606
    if opts.get('check'):
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   607
        return checkadmonitions(ui, repo, sections.names(), revs)
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33784
diff changeset
   608
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   609
    incoming = parsenotesfromrevisions(repo, sections.names(), revs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   610
34404
159a6f7e09a9 releasenotes: display release notes when no filename is specified
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34341
diff changeset
   611
    if file_ is None:
34736
25b5787e8dde releasenotes: add pager support when printing to the ui
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34404
diff changeset
   612
        ui.pager('releasenotes')
34404
159a6f7e09a9 releasenotes: display release notes when no filename is specified
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34341
diff changeset
   613
        return ui.write(serializenotes(sections, incoming))
159a6f7e09a9 releasenotes: display release notes when no filename is specified
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34341
diff changeset
   614
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   615
    try:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   616
        with open(file_, 'rb') as fh:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   617
            notes = parsereleasenotesfile(sections, fh.read())
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   618
    except IOError as e:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   619
        if e.errno != errno.ENOENT:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   620
            raise
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   621
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   622
        notes = parsedreleasenotes()
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   623
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   624
    notes.merge(ui, incoming)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   625
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   626
    with open(file_, 'wb') as fh:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   627
        fh.write(serializenotes(sections, notes))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   628
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   629
@command('debugparsereleasenotes', norepo=True)
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   630
def debugparsereleasenotes(ui, path, repo=None):
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   631
    """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
   632
    if path == '-':
40235
a7cdd81f191b releasenotes: fix remaining bytes/unicode issues caught by tests
Augie Fackler <augie@google.com>
parents: 40234
diff changeset
   633
        text = pycompat.stdin.read()
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   634
    else:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   635
        with open(path, 'rb') as fh:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   636
            text = fh.read()
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   637
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33012
diff changeset
   638
    sections = releasenotessections(ui, repo)
32778
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   639
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   640
    notes = parsereleasenotesfile(sections, text)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   641
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   642
    for section in notes:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   643
        ui.write(_('section: %s\n') % section)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   644
        for title, paragraphs in notes.titledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   645
            ui.write(_('  subsection: %s\n') % title)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   646
            for para in paragraphs:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   647
                ui.write(_('    paragraph: %s\n') % ' '.join(para))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   648
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   649
        for paragraphs in notes.nontitledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   650
            ui.write(_('  bullet point:\n'))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   651
            for para in paragraphs:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   652
                ui.write(_('    paragraph: %s\n') % ' '.join(para))