hgext/releasenotes.py
author Augie Fackler <augie@google.com>
Sun, 06 Oct 2019 09:45:02 -0400
changeset 43076 2372284d9457
parent 41768 aaad36b88298
child 43077 687b865b95ad
permissions -rw-r--r--
formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32798
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
33900
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
    16
import difflib
32798
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
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    20
from mercurial.i18n import _
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    21
from mercurial import (
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
    22
    config,
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    23
    error,
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
    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
    25
    node,
35044
e68dd1909af3 py3: handle keyword arguments in hgext/releasenotes.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34813
diff changeset
    26
    pycompat,
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    27
    registrar,
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
    scmutil,
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
    29
    util,
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    31
from mercurial.utils import stringutil
32798
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
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    38
34812
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
    39
    fuzz.token_set_ratio
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
    40
except ImportError:
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
    41
    fuzz = None
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
    42
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
# 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
    44
# 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
    45
# 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
    46
# leave the attribute unspecified.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    47
testedwith = 'ships-with-hg-core'
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    48
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
DEFAULT_SECTIONS = [
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
    ('feature', _('New Features')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
    ('bc', _('Backwards Compatibility Changes')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    52
    ('fix', _('Bug Fixes')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    53
    ('perf', _('Performance Improvements')),
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
    ('api', _('API Changes')),
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
41546
bd3f03d8cc9f global: use raw strings for regular expressions with escapes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40293
diff changeset
    57
RE_DIRECTIVE = re.compile(br'^\.\. ([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
    58
RE_ISSUE = br'\bissue ?[0-9]{4,6}(?![0-9])\b'
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    59
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    60
BULLET_SECTION = _('Other Changes')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    61
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
    62
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    63
class parsedreleasenotes(object):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    64
    def __init__(self):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    65
        self.sections = {}
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    66
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    67
    def __contains__(self, section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    68
        return section in self.sections
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    69
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    70
    def __iter__(self):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    71
        return iter(sorted(self.sections))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    72
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    73
    def addtitleditem(self, section, title, paragraphs):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    74
        """Add a titled release note entry."""
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    75
        self.sections.setdefault(section, ([], []))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    76
        self.sections[section][0].append((title, paragraphs))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    77
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    78
    def addnontitleditem(self, section, paragraphs):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    79
        """Adds a non-titled release note entry.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    80
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    81
        Will be rendered as a bullet point.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    82
        """
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    83
        self.sections.setdefault(section, ([], []))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    84
        self.sections[section][1].append(paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    85
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    86
    def titledforsection(self, section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    87
        """Returns titled entries in a section.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    88
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    89
        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
    90
        """
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    91
        return self.sections.get(section, ([], []))[0]
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
    def nontitledforsection(self, section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    94
        """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
    95
        return self.sections.get(section, ([], []))[1]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    96
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    97
    def hastitledinsection(self, section, title):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    98
        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
    99
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   100
    def merge(self, ui, other):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   101
        """Merge another instance into this one.
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
        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
   104
        """
34813
288fad8c55f9 releasenotes: show a warning if fuzzywuzzy is not present
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34812
diff changeset
   105
        if not fuzz:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   106
            ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   107
                _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   108
                    "module 'fuzzywuzzy' not found, merging of similar "
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   109
                    "releasenotes is disabled\n"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   110
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   111
            )
34813
288fad8c55f9 releasenotes: show a warning if fuzzywuzzy is not present
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34812
diff changeset
   112
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   113
        for section in other:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   114
            existingnotes = converttitled(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   115
                self.titledforsection(section)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   116
            ) + convertnontitled(self.nontitledforsection(section))
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   117
            for title, paragraphs in other.titledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   118
                if self.hastitledinsection(section, title):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   119
                    # 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
   120
                    # interactive mode.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   121
                    ui.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   122
                        _('%s already exists in %s section; ignoring\n')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   123
                        % (title, section)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   124
                    )
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   125
                    continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   126
33724
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   127
                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
   128
                if section == 'fix':
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   129
                    issue = getissuenum(incoming_str)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   130
                    if issue:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   131
                        if findissue(ui, existingnotes, issue):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   132
                            continue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   133
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   134
                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
   135
                    continue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   136
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   137
                self.addtitleditem(section, title, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   138
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   139
            for paragraphs in other.nontitledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   140
                if paragraphs in self.nontitledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   141
                    continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   142
33724
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   143
                incoming_str = convertnontitled([paragraphs])[0]
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   144
                if section == 'fix':
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   145
                    issue = getissuenum(incoming_str)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   146
                    if issue:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   147
                        if findissue(ui, existingnotes, issue):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   148
                            continue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   149
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   150
                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
   151
                    continue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   152
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   153
                self.addnontitleditem(section, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   154
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   155
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   156
class releasenotessections(object):
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   157
    def __init__(self, ui, repo=None):
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   158
        if repo:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   159
            sections = util.sortdict(DEFAULT_SECTIONS)
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   160
            custom_sections = getcustomadmonitions(repo)
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   161
            if custom_sections:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   162
                sections.update(custom_sections)
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   163
            self._sections = list(sections.iteritems())
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   164
        else:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   165
            self._sections = list(DEFAULT_SECTIONS)
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   166
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   167
    def __iter__(self):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   168
        return iter(self._sections)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   169
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   170
    def names(self):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   171
        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
   172
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   173
    def sectionfromtitle(self, title):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   174
        for name, value in self._sections:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   175
            if value == title:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   176
                return name
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   177
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   178
        return None
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   179
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   180
33724
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   181
def converttitled(titledparagraphs):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   182
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   183
    Convert titled paragraphs to strings
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   184
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   185
    string_list = []
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   186
    for title, paragraphs in titledparagraphs:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   187
        lines = []
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   188
        for para in paragraphs:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   189
            lines.extend(para)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   190
        string_list.append(' '.join(lines))
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   191
    return string_list
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   192
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   193
33724
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   194
def convertnontitled(nontitledparagraphs):
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
    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
   197
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   198
    string_list = []
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   199
    for paragraphs in nontitledparagraphs:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   200
        lines = []
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   201
        for para in paragraphs:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   202
            lines.extend(para)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   203
        string_list.append(' '.join(lines))
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   204
    return string_list
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   205
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   206
33724
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   207
def getissuenum(incoming_str):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   208
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   209
    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
   210
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   211
    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
   212
    if issue:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   213
        issue = issue.group()
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   214
    return issue
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   215
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   216
33724
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
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   227
33724
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   228
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
   229
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   230
    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
   231
    """
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   232
    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
   233
        merge = similaritycheck(incoming_str, existing)
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   234
        if not merge:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   235
            ui.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   236
                _('"%s" already exists in notes file; ignoring\n')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   237
                % incoming_str
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   238
            )
33724
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   239
            return True
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   240
        else:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   241
            return False
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   242
    else:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   243
        return False
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   244
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   245
33724
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   246
def similaritycheck(incoming_str, existingnotes):
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   247
    """
34780
070ba789f4d0 releasenotes: fix documentation of similaritycheck()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34736
diff changeset
   248
    Returns false when note fragment can be merged to existing notes.
33724
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   249
    """
34812
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
   250
    # fuzzywuzzy not present
bc2caa4b4480 releasenotes: move import of fuzzywuzzy to import level
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34811
diff changeset
   251
    if not fuzz:
34810
44bd29168d14 releasenotes: make the import of fuzzywuzzy optional
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34780
diff changeset
   252
        return True
44bd29168d14 releasenotes: make the import of fuzzywuzzy optional
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34780
diff changeset
   253
33724
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   254
    merge = True
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   255
    for bullet in existingnotes:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   256
        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
   257
        if score > 75:
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   258
            merge = False
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   259
            break
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   260
    return merge
3748098d072a releasenotes: add similarity check function to compare incoming notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33571
diff changeset
   261
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   262
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   263
def getcustomadmonitions(repo):
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   264
    ctx = repo['.']
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   265
    p = config.config()
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   266
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   267
    def read(f, sections=None, remap=None):
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   268
        if f in ctx:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   269
            data = ctx[f].data()
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   270
            p.parse(f, data, sections, remap, read)
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   271
        else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   272
            raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   273
                _(".hgreleasenotes file \'%s\' not found") % repo.pathto(f)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   274
            )
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   275
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   276
    if '.hgreleasenotes' in ctx:
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   277
        read('.hgreleasenotes')
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   278
    return p['sections']
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   279
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   280
33900
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   281
def checkadmonitions(ui, repo, directives, revs):
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   282
    """
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   283
    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: 33805
diff changeset
   284
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   285
    .. abcd::
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   286
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   287
       First paragraph under this admonition
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   288
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   289
    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: 33805
diff changeset
   290
    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: 33805
diff changeset
   291
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   292
    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: 33805
diff changeset
   293
    """
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   294
    for rev in revs:
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   295
        ctx = repo[rev]
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   296
        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: 33805
diff changeset
   297
        if admonition:
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   298
            if admonition.group(1) in directives:
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   299
                continue
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   300
            else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   301
                ui.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   302
                    _("Invalid admonition '%s' present in changeset %s" "\n")
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   303
                    % (admonition.group(1), ctx.hex()[:12])
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   304
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   305
                sim = lambda x: difflib.SequenceMatcher(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   306
                    None, admonition.group(1), x
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   307
                ).ratio()
33900
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   308
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   309
                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: 33805
diff changeset
   310
                if len(similar) == 1:
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   311
                    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: 33805
diff changeset
   312
                elif similar:
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   313
                    ss = ", ".join(sorted(similar))
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   314
                    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: 33805
diff changeset
   315
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   316
33958
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33900
diff changeset
   317
def _getadmonitionlist(ui, sections):
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33900
diff changeset
   318
    for section in sections:
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33900
diff changeset
   319
        ui.write("%s: %s\n" % (section[0], section[1]))
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33900
diff changeset
   320
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   321
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   322
def parsenotesfromrevisions(repo, directives, revs):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   323
    notes = parsedreleasenotes()
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   324
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   325
    for rev in revs:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   326
        ctx = repo[rev]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   327
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   328
        blocks, pruned = minirst.parse(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   329
            ctx.description(), admonitions=directives
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   330
        )
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   331
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   332
        for i, block in enumerate(blocks):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   333
            if block['type'] != 'admonition':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   334
                continue
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
            directive = block['admonitiontitle']
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   337
            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
   338
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   339
            if i + 1 == len(blocks):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   340
                raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   341
                    _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   342
                        'changeset %s: release notes directive %s '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   343
                        'lacks content'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   344
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   345
                    % (ctx, directive)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   346
                )
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   347
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   348
            # 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
   349
            # admonition.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   350
            paragraphs = []
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   351
            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
   352
                pblock = blocks[j]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   353
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   354
                # 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
   355
                if pblock['type'] == 'margin':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   356
                    continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   357
36778
a5891e94bfe1 releasenotes: allow notes for multiple directives in a single changeset
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 36777
diff changeset
   358
                if pblock['type'] == 'admonition':
a5891e94bfe1 releasenotes: allow notes for multiple directives in a single changeset
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 36777
diff changeset
   359
                    break
a5891e94bfe1 releasenotes: allow notes for multiple directives in a single changeset
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 36777
diff changeset
   360
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   361
                if pblock['type'] != 'paragraph':
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   362
                    repo.ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   363
                        _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   364
                            'changeset %s: unexpected block in release '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   365
                            'notes directive %s\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   366
                        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   367
                        % (ctx, directive)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   368
                    )
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   369
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   370
                if pblock['indent'] > 0:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   371
                    paragraphs.append(pblock['lines'])
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   372
                else:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   373
                    break
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
            # 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
   376
            if not paragraphs:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   377
                repo.ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   378
                    _("error parsing releasenotes for revision: " "'%s'\n")
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   379
                    % node.hex(ctx.node())
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   380
                )
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   381
            if title:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   382
                notes.addtitleditem(directive, title, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   383
            else:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   384
                notes.addnontitleditem(directive, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   385
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   386
    return notes
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   387
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   388
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   389
def parsereleasenotesfile(sections, text):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   390
    """Parse text content containing generated release notes."""
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   391
    notes = parsedreleasenotes()
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   392
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   393
    blocks = minirst.parse(text)[0]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   394
33028
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   395
    def gatherparagraphsbullets(offset, title=False):
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   396
        notefragment = []
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   397
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   398
        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
   399
            block = blocks[i]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   400
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   401
            if block['type'] == 'margin':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   402
                continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   403
            elif block['type'] == 'section':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   404
                break
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   405
            elif block['type'] == 'bullet':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   406
                if block['indent'] != 0:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   407
                    raise error.Abort(_('indented bullet lists not supported'))
33028
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   408
                if title:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   409
                    lines = [l[1:].strip() for l in block['lines']]
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   410
                    notefragment.append(lines)
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   411
                    continue
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   412
                else:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   413
                    lines = [[l[1:].strip() for l in block['lines']]]
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   414
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   415
                    for block in blocks[i + 1 :]:
33028
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   416
                        if block['type'] in ('bullet', 'section'):
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   417
                            break
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   418
                        if block['type'] == 'paragraph':
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   419
                            lines.append(block['lines'])
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   420
                    notefragment.append(lines)
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   421
                    continue
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   422
            elif block['type'] != 'paragraph':
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   423
                raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   424
                    _('unexpected block type in release notes: ' '%s')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   425
                    % block['type']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   426
                )
33028
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   427
            if title:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   428
                notefragment.append(block['lines'])
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   429
33028
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   430
        return notefragment
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   431
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   432
    currentsection = None
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   433
    for i, block in enumerate(blocks):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   434
        if block['type'] != 'section':
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   435
            continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   436
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   437
        title = block['lines'][0]
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   438
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   439
        # 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
   440
        # work.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   441
        if block['underline'] == '=':  # main section
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   442
            name = sections.sectionfromtitle(title)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   443
            if not name:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   444
                raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   445
                    _('unknown release notes section: %s') % title
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   446
                )
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   447
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   448
            currentsection = name
33028
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   449
            bullet_points = gatherparagraphsbullets(i)
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   450
            if bullet_points:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   451
                for para in bullet_points:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   452
                    notes.addnontitleditem(currentsection, para)
32798
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
        elif block['underline'] == '-':  # sub-section
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   455
            if title == BULLET_SECTION:
33028
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   456
                bullet_points = gatherparagraphsbullets(i)
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   457
                for para in bullet_points:
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   458
                    notes.addnontitleditem(currentsection, para)
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   459
            else:
33028
5814db57941c releasenotes: improve parsing around bullet points
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 32798
diff changeset
   460
                paragraphs = gatherparagraphsbullets(i, True)
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   461
                notes.addtitleditem(currentsection, title, paragraphs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   462
        else:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   463
            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
   464
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   465
    return notes
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   466
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   467
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   468
def serializenotes(sections, notes):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   469
    """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
   470
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   471
    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
   472
    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
   473
    """
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   474
    lines = []
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   475
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   476
    for sectionname, sectiontitle in sections:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   477
        if sectionname not in notes:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   478
            continue
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   479
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   480
        lines.append(sectiontitle)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   481
        lines.append('=' * len(sectiontitle))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   482
        lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   483
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   484
        # First pass to emit sub-sections.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   485
        for title, paragraphs in notes.titledforsection(sectionname):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   486
            lines.append(title)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   487
            lines.append('-' * len(title))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   488
            lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   489
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   490
            for i, para in enumerate(paragraphs):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   491
                if i:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   492
                    lines.append('')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   493
                lines.extend(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   494
                    stringutil.wrap(' '.join(para), width=78).splitlines()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   495
                )
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   496
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   497
            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
        # Second pass to emit bullet list items.
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
        # 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
   502
        # 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
   503
        # 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
   504
        # for the non-titled items.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   505
        nontitled = notes.nontitledforsection(sectionname)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   506
        if notes.titledforsection(sectionname) and nontitled:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   507
            # TODO make configurable.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   508
            lines.append(BULLET_SECTION)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   509
            lines.append('-' * len(BULLET_SECTION))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   510
            lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   511
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   512
        for paragraphs in nontitled:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   513
            lines.extend(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   514
                stringutil.wrap(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   515
                    ' '.join(paragraphs[0]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   516
                    width=78,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   517
                    initindent='* ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   518
                    hangindent='  ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   519
                ).splitlines()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   520
            )
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   521
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   522
            for para in paragraphs[1:]:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   523
                lines.append('')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   524
                lines.extend(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   525
                    stringutil.wrap(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   526
                        ' '.join(para),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   527
                        width=78,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   528
                        initindent='  ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   529
                        hangindent='  ',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   530
                    ).splitlines()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   531
                )
32798
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
            lines.append('')
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   534
33805
589fda7895da releasenotes: minor bug fix for index error while serializing
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33724
diff changeset
   535
    if lines and lines[-1]:
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   536
        lines.append('')
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
    return '\n'.join(lines)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   539
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   540
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   541
@command(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   542
    'releasenotes',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   543
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   544
        ('r', 'rev', '', _('revisions to process for release notes'), _('REV')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   545
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   546
            'c',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   547
            'check',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   548
            False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   549
            _('checks for validity of admonitions (if any)'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   550
            _('REV'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   551
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   552
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   553
            'l',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   554
            'list',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   555
            False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   556
            _('list the available admonitions with their title'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   557
            None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   558
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   559
    ],
40293
c303d65d2e34 help: assigning categories to existing commands
rdamazio@google.com
parents: 40243
diff changeset
   560
    _('hg releasenotes [-r REV] [-c] FILE'),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   561
    helpcategory=command.CATEGORY_CHANGE_NAVIGATION,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   562
)
33900
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   563
def releasenotes(ui, repo, file_=None, **opts):
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   564
    """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
   565
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   566
    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
   567
    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
   568
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   569
    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
   570
    directives. These have the form::
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   571
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   572
       .. directive:: title
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   573
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   574
          content
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   575
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   576
    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
   577
    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
   578
    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
   579
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   580
    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
   581
    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
   582
    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
   583
    section.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   584
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   585
    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
   586
    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
   587
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   588
       Section 1
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   589
       =========
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   590
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   591
       ...
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   592
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   593
       Section 2
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   594
       =========
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   595
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   596
       ...
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   597
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   598
    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
   599
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   600
    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
   601
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   602
       Section
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   603
       =======
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   604
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   605
       * Release note 1
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   606
       * Release note 2
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   607
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   608
    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
   609
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   610
       Section
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   611
       =======
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   612
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   613
       Note 1 Title
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   614
       ------------
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   615
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   616
       Description of the first long-form note.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   617
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   618
       Note 2 Title
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   619
       ------------
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   620
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   621
       Description of the second long-form note.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   622
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   623
    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
   624
    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
   625
    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
   626
    into this parsed set.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   627
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   628
    During release notes merging:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   629
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   630
    * Duplicate items are automatically ignored
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   631
    * 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
   632
      greater than a threshold.
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   633
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   634
    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
   635
    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
   636
    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
   637
    release note after it has been added to the release notes file.
34348
01e8ab4b6573 releasenotes: update docstrings with information on additional flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34347
diff changeset
   638
01e8ab4b6573 releasenotes: update docstrings with information on additional flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34347
diff changeset
   639
    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: 34347
diff changeset
   640
01e8ab4b6573 releasenotes: update docstrings with information on additional flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34347
diff changeset
   641
    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: 34347
diff changeset
   642
    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: 34347
diff changeset
   643
    admonitions (if any).
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   644
    """
35044
e68dd1909af3 py3: handle keyword arguments in hgext/releasenotes.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34813
diff changeset
   645
e68dd1909af3 py3: handle keyword arguments in hgext/releasenotes.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34813
diff changeset
   646
    opts = pycompat.byteskwargs(opts)
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   647
    sections = releasenotessections(ui, repo)
34347
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33958
diff changeset
   648
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33958
diff changeset
   649
    listflag = opts.get('list')
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33958
diff changeset
   650
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33958
diff changeset
   651
    if listflag and opts.get('rev'):
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33958
diff changeset
   652
        raise error.Abort(_('cannot use both \'--list\' and \'--rev\''))
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33958
diff changeset
   653
    if listflag and opts.get('check'):
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33958
diff changeset
   654
        raise error.Abort(_('cannot use both \'--list\' and \'--check\''))
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33958
diff changeset
   655
741a511492d3 releasenotes: raise error on simultaneous usage of flags
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33958
diff changeset
   656
    if listflag:
33958
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33900
diff changeset
   657
        return _getadmonitionlist(ui, sections)
2a37459aedf2 releasenotes: view admonition titles using -l flag
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33900
diff changeset
   658
33900
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   659
    rev = opts.get('rev')
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   660
    revs = scmutil.revrange(repo, [rev or 'not public()'])
33900
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   661
    if opts.get('check'):
6a49c74b1e8f releasenotes: add check flag for use of admonitions and its validity
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33805
diff changeset
   662
        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: 33805
diff changeset
   663
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   664
    incoming = parsenotesfromrevisions(repo, sections.names(), revs)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   665
34405
159a6f7e09a9 releasenotes: display release notes when no filename is specified
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34348
diff changeset
   666
    if file_ is None:
34736
25b5787e8dde releasenotes: add pager support when printing to the ui
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34405
diff changeset
   667
        ui.pager('releasenotes')
34405
159a6f7e09a9 releasenotes: display release notes when no filename is specified
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34348
diff changeset
   668
        return ui.write(serializenotes(sections, incoming))
159a6f7e09a9 releasenotes: display release notes when no filename is specified
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 34348
diff changeset
   669
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   670
    try:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   671
        with open(file_, 'rb') as fh:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   672
            notes = parsereleasenotesfile(sections, fh.read())
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   673
    except IOError as e:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   674
        if e.errno != errno.ENOENT:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   675
            raise
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   676
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   677
        notes = parsedreleasenotes()
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   678
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   679
    notes.merge(ui, incoming)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   680
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   681
    with open(file_, 'wb') as fh:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   682
        fh.write(serializenotes(sections, notes))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   683
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41768
diff changeset
   684
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   685
@command('debugparsereleasenotes', norepo=True)
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   686
def debugparsereleasenotes(ui, path, repo=None):
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   687
    """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
   688
    if path == '-':
40235
a7cdd81f191b releasenotes: fix remaining bytes/unicode issues caught by tests
Augie Fackler <augie@google.com>
parents: 40234
diff changeset
   689
        text = pycompat.stdin.read()
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   690
    else:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   691
        with open(path, 'rb') as fh:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   692
            text = fh.read()
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   693
33571
9a944e908ecf releasenotes: add custom admonitions support for release notes
Rishabh Madan <rishabhmadan96@gmail.com>
parents: 33028
diff changeset
   694
    sections = releasenotessections(ui, repo)
32798
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   695
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   696
    notes = parsereleasenotesfile(sections, text)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   697
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   698
    for section in notes:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   699
        ui.write(_('section: %s\n') % section)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   700
        for title, paragraphs in notes.titledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   701
            ui.write(_('  subsection: %s\n') % title)
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   702
            for para in paragraphs:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   703
                ui.write(_('    paragraph: %s\n') % ' '.join(para))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   704
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   705
        for paragraphs in notes.nontitledforsection(section):
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   706
            ui.write(_('  bullet point:\n'))
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   707
            for para in paragraphs:
91e355a0408b releasenotes: command to manage release notes files
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   708
                ui.write(_('    paragraph: %s\n') % ' '.join(para))