a
    %0É_ÝU  ã                	   @   sú  d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZmZmZmZmZmZmZmZmZ ddlmZmZ i Ze e¡ZzddlmZ ej W n ey¶   dZY n0 dZd	ed
ƒfdedƒfdedƒfdedƒfdedƒfgZe d¡Z dZ!edƒZ"G dd„ de#ƒZ$G dd„ de#ƒZ%dd„ Z&dd„ Z'dd„ Z(d d!„ Z)d"d#„ Z*d$d%„ Z+d&d'„ Z,d(d)„ Z-d*d+„ Z.d,d-„ Z/d.d/„ Z0d0d1„ Z1ed2d3d4d5ed6ƒed7ƒfd8d9d:ed;ƒed7ƒfd<d=d:ed>ƒdfged?ƒej2d@dHdAdB„ƒZ3edCdDdEdIdFdG„ƒZ4dS )Ja/  generate release notes from commit messages (EXPERIMENTAL)

It is common to maintain files detailing changes in a project between
releases. Maintaining these files can be difficult and time consuming.
The :hg:`releasenotes` command provided by this extension makes the
process simpler by automating it.
é    )Úabsolute_importN)Ú_)Úopen)	ÚcmdutilÚconfigÚerrorÚminirstÚnodeÚpycompatÚ	registrarÚscmutilÚutil)ÚprocutilÚ
stringutils   ships-with-hg-cores   features   New Featuress   bcs   Backwards Compatibility Changesó   fixs	   Bug Fixess   perfs   Performance Improvementss   apis   API Changess#   ^\.\. ([a-zA-Z0-9_]+)::\s*([^$]+)?$s   \bissue ?[0-9]{4,6}(?![0-9])\bs   Other Changesc                   @   sT   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )Úparsedreleasenotesc                 C   s
   i | _ d S ©N©Úsections©Úself© r   ú4/usr/lib/python3/dist-packages/hgext/releasenotes.pyÚ__init__E   s    zparsedreleasenotes.__init__c                 C   s
   || j v S r   r   ©r   Úsectionr   r   r   Ú__contains__H   s    zparsedreleasenotes.__contains__c                 C   s   t t| jƒƒS r   )ÚiterÚsortedr   r   r   r   r   Ú__iter__K   s    zparsedreleasenotes.__iter__c                 C   s.   | j  |g g f¡ | j | d  ||f¡ dS )z Add a titled release note entry.r   N©r   Ú
setdefaultÚappend)r   r   ÚtitleÚ
paragraphsr   r   r   ÚaddtitleditemN   s    z parsedreleasenotes.addtitleditemc                 C   s*   | j  |g g f¡ | j | d  |¡ dS )z[Adds a non-titled release note entry.

        Will be rendered as a bullet point.
        é   Nr    )r   r   r$   r   r   r   ÚaddnontitleditemS   s    z#parsedreleasenotes.addnontitleditemc                 C   s   | j  |g g f¡d S )z|Returns titled entries in a section.

        Returns a list of (title, paragraphs) tuples describing sub-sections.
        r   ©r   Úgetr   r   r   r   Útitledforsection[   s    z#parsedreleasenotes.titledforsectionc                 C   s   | j  |g g f¡d S )z5Returns non-titled, bulleted paragraphs in a section.r&   r(   r   r   r   r   Únontitledforsectionb   s    z&parsedreleasenotes.nontitledforsectionc                    s   t ‡ fdd„|  |¡D ƒƒS )Nc                 3   s   | ]}|d  ˆ kV  qdS )r   Nr   ©Ú.0Út©r#   r   r   Ú	<genexpr>g   ó    z8parsedreleasenotes.hastitledinsection.<locals>.<genexpr>)Úanyr*   )r   r   r#   r   r/   r   Úhastitledinsectionf   s    z%parsedreleasenotes.hastitledinsectionc           	      C   s8  t s| tdƒ¡ |D ]}t|  |¡ƒt|  |¡ƒ }| |¡D ]|\}}|  ||¡rn| tdƒ||f ¡ qBt||fgƒd }|dkr¢t	|ƒ}|r¢t
|||ƒr¢qBt|||ƒr°qB|  |||¡ qB| |¡D ]f}||  |¡v rÞqÊt|gƒd }|dkrt	|ƒ}|rt
|||ƒrqÊt|||ƒr$qÊ|  ||¡ qÊqdS )z{Merge another instance into this one.

        This is used to combine multiple sources of release notes together.
        sK   module 'fuzzywuzzy' not found, merging of similar releasenotes is disabled
s*   %s already exists in %s section; ignoring
r   r   N)ÚfuzzÚwarnr   Úconverttitledr*   Úconvertnontitledr+   r3   ÚwriteÚgetissuenumÚ	findissueÚsimilarr%   r'   )	r   ÚuiÚotherr   Úexistingnotesr#   r$   Úincoming_strÚissuer   r   r   Úmergei   sR    ÿÿ
ÿþÿÿ
zparsedreleasenotes.mergeN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r%   r'   r*   r+   r3   rA   r   r   r   r   r   D   s   r   c                   @   s.   e Zd Zd
dd„Zdd„ Zdd„ Zdd	„ ZdS )ÚreleasenotessectionsNc                 C   sD   |r6t  t¡}t|ƒ}|r$| |¡ tt |¡ƒ| _n
ttƒ| _d S r   )	r   ZsortdictÚDEFAULT_SECTIONSÚgetcustomadmonitionsÚupdateÚlistr
   Z	iteritemsÚ	_sections)r   r<   Úrepor   Zcustom_sectionsr   r   r   r   ¢   s    

zreleasenotessections.__init__c                 C   s
   t | jƒS r   )r   rJ   r   r   r   r   r   ¬   s    zreleasenotessections.__iter__c                 C   s   dd„ | j D ƒS )Nc                 S   s   g | ]}|d  ‘qS )r   r   r,   r   r   r   Ú
<listcomp>°   r1   z.releasenotessections.names.<locals>.<listcomp>©rJ   r   r   r   r   Únames¯   s    zreleasenotessections.namesc                 C   s$   | j D ]\}}||kr|  S qd S r   rM   )r   r#   ÚnameÚvaluer   r   r   Úsectionfromtitle²   s    
z%releasenotessections.sectionfromtitle)N)rB   rC   rD   r   r   rN   rQ   r   r   r   r   rE   ¡   s   

rE   c                 C   s>   g }| D ]0\}}g }|D ]}|  |¡ q| d |¡¡ q|S )z.
    Convert titled paragraphs to strings
    ó    ©Úextendr"   Újoin)ZtitledparagraphsÚstring_listr#   r$   ÚlinesÚparar   r   r   r6   º   s    r6   c                 C   s:   g }| D ],}g }|D ]}|  |¡ q| d |¡¡ q|S )z/
    Convert non-titled bullets to strings
    rR   rS   )ZnontitledparagraphsrV   r$   rW   rX   r   r   r   r7   Ç   s    r7   c                 C   s    t  t| t j¡}|r| ¡ }|S )zD
    Returns issue number from the incoming string if it exists
    )ÚreÚsearchÚRE_ISSUEÚ
IGNORECASEÚgroup)r?   r@   r   r   r   r9   Ô   s    r9   c                    s4   t ‡ fdd„|D ƒƒr,|  tdƒˆ  ¡ dS dS dS )z?
    Returns true if issue number already exists in notes.
    c                 3   s   | ]}ˆ |v V  qd S r   r   ©r-   Ús©r@   r   r   r0   â   r1   zfindissue.<locals>.<genexpr>s'   "%s" already exists in notes; ignoring
TFN)r2   r8   r   )r<   Úexistingr@   r   r`   r   r:   Þ   s    r:   c                 C   sB   t | ¡ ƒdkr:t||ƒ}|s4|  tdƒ| ¡ dS dS ndS dS )z?
    Returns true if similar note found in existing notes.
    é
   s,   "%s" already exists in notes file; ignoring
TFN)ÚlenÚsplitÚsimilaritycheckr8   r   )r<   ra   r?   rA   r   r   r   r;   é   s    
ÿÿr;   c                 C   s6   t sdS d}|D ] }t  | |¡}|dkrd} q2q|S )zK
    Returns false when note fragment can be merged to existing notes.
    TéK   F)r4   Útoken_set_ratio)r?   r>   rA   ZbulletZscorer   r   r   re   û   s    re   c                    s<   ˆd ‰ t   ¡ ‰d‡ ‡‡‡fdd„	‰dˆ v r4ˆdƒ ˆd S )Nó   .c                    sD   | ˆ v r(ˆ |    ¡ }ˆ | |||ˆ¡ nt tdƒˆ | ¡ ¡‚d S )Ns#   .hgreleasenotes file '%s' not found)ÚdataÚparser   ÚAbortr   Zpathto)Úfr   Zremapri   ©ÚctxÚpÚreadrK   r   r   rp     s    ÿz"getcustomadmonitions.<locals>.reads   .hgreleasenotess   sections)NN)r   )rK   r   rm   r   rG     s    	rG   c                    sÊ   |D ]À}|| }t  t| ¡ ¡‰ ˆ rˆ  d¡|v r6qq|  tdƒˆ  d¡| ¡ dd… f ¡ ‡ fdd„‰‡fdd„|D ƒ}t|ƒdkr |  td	ƒ|d
  ¡ q|rd 	t
|ƒ¡}|  tdƒ| ¡ qdS )a`  
    Checks the commit messages for admonitions and their validity.

    .. abcd::

       First paragraph under this admonition

    For this commit message, using `hg releasenotes -r . --check`
    returns: Invalid admonition 'abcd' present in changeset 3ea92981e103

    As admonition 'abcd' is neither present in default nor custom admonitions
    r&   s0   Invalid admonition '%s' present in changeset %s
Né   c                    s   t  d ˆ  d¡| ¡ ¡ S )Nr&   )ÚdifflibZSequenceMatcherr]   Zratio)Úx)Ú
admonitionr   r   Ú<lambda>6  s   ÿz"checkadmonitions.<locals>.<lambda>c                    s   g | ]}ˆ |ƒd kr|‘qS )g333333ã?r   r^   )Úsimr   r   rL   :  r1   z$checkadmonitions.<locals>.<listcomp>s   (did you mean %s?)
r   s   , s   (did you mean one of %s?)
)rY   rZ   ÚRE_DIRECTIVEÚdescriptionr]   r8   r   Úhexrc   rU   r   )r<   rK   Ú
directivesÚrevsÚrevrn   r;   Zssr   )rt   rv   r   Úcheckadmonitions  s$    ÿÿr}   c                 C   s(   |D ]}|   d|d |d f ¡ qd S )Ns   %s: %s
r   r&   )r8   )r<   r   r   r   r   r   Ú_getadmonitionlistB  s    r~   c              	   C   sr  t ƒ }|D ]`}| | }tj| ¡ |d\}}t|ƒD ]2\}}	|	d dkrNq6|	d }
|	d rn|	d d  ¡ nd }|d t|ƒkr˜t t	dƒ||
f ¡‚g }t
|d t|ƒƒD ]t}|| }|d d	krÈq®|d dkrÚ q$|d d
krþ| j t	dƒ||
f ¡ |d dkr| |d ¡ q® q$q®|sH| j t	dƒt | ¡ ¡ ¡ |r^| |
||¡ q6| |
|¡ q6q
|S )N)Zadmonitionsó   types
   admonitions   admonitiontitleó   linesr   r&   s6   changeset %s: release notes directive %s lacks contentó   marginó	   paragraphs=   changeset %s: unexpected block in release notes directive %s
ó   indents.   error parsing releasenotes for revision: '%s'
)r   r   rj   rx   Ú	enumerateÚstriprc   r   rk   r   Úranger<   r5   r"   r	   ry   r%   r'   )rK   rz   r{   Únotesr|   rn   ÚblocksZprunedÚiÚblockZ	directiver#   r$   ÚjZpblockr   r   r   ÚparsenotesfromrevisionsG  s^    
ÿ
ÿüÿ
ÿüÿÿÿrŒ   c                    s
  t ƒ }t |¡d ‰ d‡ fdd„	}d}tˆ ƒD ]Ö\}}|d dkrDq.|d d }|d	 d
kr¤|  |¡}|s|t tdƒ| ¡‚|}||ƒ}	|	r¢|	D ]}
| ||
¡ qq.|d	 dkrò|t	krØ||ƒ}	|	D ]}
| ||
¡ qÄn||dƒ}| 
|||¡ q.t tdƒ| ¡‚q.|S )z6Parse text content containing generated release notes.r   Fc                    s0  g }t | d tˆ ƒƒD ]}ˆ | }|d dkr4qnä|d dkrH q,nÐ|d dkrô|d dkrnt tdƒ¡‚|r’d	d
„ |d D ƒ}| |¡ qn`dd
„ |d D ƒg}ˆ |d d … D ].}|d dv rÊ qæ|d dkr¶| |d ¡ q¶| |¡ qn$|d dkrt tdƒ|d  ¡‚|r| |d ¡ q|S )Nr&   r   r   ó   sectionó   bulletrƒ   r   s#   indented bullet lists not supportedc                 S   s   g | ]}|d d…   ¡ ‘qS ©r&   N©r…   ©r-   Úlr   r   r   rL   ž  r1   zJparsereleasenotesfile.<locals>.gatherparagraphsbullets.<locals>.<listcomp>r€   c                 S   s   g | ]}|d d…   ¡ ‘qS r   r   r‘   r   r   r   rL   ¢  r1   )rŽ   r   r‚   s*   unexpected block type in release notes: %s)r†   rc   r   rk   r   r"   )Úoffsetr#   Znotefragmentr‰   rŠ   rW   ©rˆ   r   r   Úgatherparagraphsbullets  s>    

ÿÿz6parsereleasenotesfile.<locals>.gatherparagraphsbulletsNr   r   r€   s	   underlineó   =s!   unknown release notes section: %só   -Ts   unsupported section type for %s)F)r   r   rj   r„   rQ   r   rk   r   r'   ÚBULLET_SECTIONr%   )r   Útextr‡   r•   Zcurrentsectionr‰   rŠ   r#   rO   Zbullet_pointsrX   r$   r   r”   r   ÚparsereleasenotesfileŠ  s8    %

ÿ
rš   c           
      C   s¨  g }| D ]x\}}||vrq|  |¡ |  dt|ƒ ¡ |  d¡ | |¡D ]v\}}|  |¡ |  dt|ƒ ¡ |  d¡ t|ƒD ]4\}}|r˜|  d¡ | tjd |¡dd ¡ ¡ q‚|  d¡ qL| 	|¡}	| |¡r|	r|  t
¡ |  dtt
ƒ ¡ |  d¡ |	D ]v}| tjd |d ¡ddd	d
 ¡ ¡ |dd… D ]2}|  d¡ | tjd |¡dd	d	d
 ¡ ¡ q@|  d¡ q
q|rž|d rž|  d¡ d |¡S )zÓSerialize release notes from parsed fragments and notes.

    This function essentially takes the output of ``parsenotesfromrevisions()``
    and ``parserelnotesfile()`` and produces output combining the 2.
    r–   r1   r—   rR   éN   )Úwidthr   s   * s     )rœ   Z
initindentZ
hangindentr&   Néÿÿÿÿó   
)r"   rc   r*   r„   rT   r   ZwraprU   Ú
splitlinesr+   r˜   )
r   r‡   rW   ZsectionnameZsectiontitler#   r$   r‰   rX   Z	nontitledr   r   r   ÚserializenotesÙ  s\    




ÿ


üÿ	
üÿ	
r    ó   releasenotesó   ró   revr1   s&   revisions to process for release notess   REVó   có   checkFs+   checks for validity of admonitions (if any)ó   ló   lists/   list the available admonitions with their titles"   hg releasenotes [-r REV] [-c] FILE)Zhelpcategoryc              
   K   sj  t  |¡}t| |ƒ}t |dddg¡ | d¡r:t| |ƒS | d¡}t ||pPdg¡}| d¡rrt	| || 
¡ |ƒS t|| 
¡ |ƒ}|du r¤|  d¡ |  t||ƒ¡S z<t|dƒ}t|| ¡ ƒ}	W d  ƒ n1 sÔ0    Y  W n< ty }
 z"|
jtjkr‚ tƒ }	W Y d}
~
n
d}
~
0 0 |	 | |¡ t|dƒ }| t||	ƒ¡ W d  ƒ n1 s\0    Y  dS )	aé	  parse release notes from commit messages into an output file

    Given an output file and set of revisions, this command will parse commit
    messages for release notes then add them to the output file.

    Release notes are defined in commit messages as ReStructuredText
    directives. These have the form::

       .. directive:: title

          content

    Each ``directive`` maps to an output section in a generated release notes
    file, which itself is ReStructuredText. For example, the ``.. feature::``
    directive would map to a ``New Features`` section.

    Release note directives can be either short-form or long-form. In short-
    form, ``title`` is omitted and the release note is rendered as a bullet
    list. In long form, a sub-section with the title ``title`` is added to the
    section.

    The ``FILE`` argument controls the output file to write gathered release
    notes to. The format of the file is::

       Section 1
       =========

       ...

       Section 2
       =========

       ...

    Only sections with defined release notes are emitted.

    If a section only has short-form notes, it will consist of bullet list::

       Section
       =======

       * Release note 1
       * Release note 2

    If a section has long-form notes, sub-sections will be emitted::

       Section
       =======

       Note 1 Title
       ------------

       Description of the first long-form note.

       Note 2 Title
       ------------

       Description of the second long-form note.

    If the ``FILE`` argument points to an existing file, that file will be
    parsed for release notes having the format that would be generated by this
    command. The notes from the processed commit messages will be *merged*
    into this parsed set.

    During release notes merging:

    * Duplicate items are automatically ignored
    * Items that are different are automatically ignored if the similarity is
      greater than a threshold.

    This means that the release notes file can be updated independently from
    this command and changes should not be lost when running this command on
    that file. A particular use case for this is to tweak the wording of a
    release note after it has been added to the release notes file.

    The -c/--check option checks the commit message for invalid admonitions.

    The -l/--list option, presents the user with a list of existing available
    admonitions along with their title. This also includes the custom
    admonitions (if any).
    r§   r£   r¥   s   not public()Nr¡   ó   rbs   wb)r
   ZbyteskwargsrE   r   Zcheck_incompatible_argumentsr)   r~   r   Zrevranger}   rN   rŒ   Zpagerr8   r    r   rš   rp   ÚIOErrorÚerrnoZENOENTr   rA   )r<   rK   Zfile_Zoptsr   r|   r{   ZincomingÚfhr‡   Úer   r   r   Úreleasenotes"  s.    o






0r­   s   debugparsereleasenotesT)Znorepoc              	   C   s  |dkrt j ¡ }n2t|dƒ}| ¡ }W d  ƒ n1 s<0    Y  t| |ƒ}t||ƒ}|D ]ž}|  tdƒ| ¡ | |¡D ]<\}}	|  tdƒ| ¡ |	D ]}
|  tdƒd 	|
¡ ¡ qœq~| 
|¡D ]4}	|  tdƒ¡ |	D ]}
|  tdƒd 	|
¡ ¡ qÜqÆq^dS )	z6parse release notes and print resulting data structurer—   r¨   Ns   section: %s
s     subsection: %s
s       paragraph: %s
rR   s     bullet point:
)r   Ústdinrp   r   rE   rš   r8   r   r*   rU   r+   )r<   ÚpathrK   r™   r«   r   r‡   r   r#   r$   rX   r   r   r   Údebugparsereleasenotes³  s     &

r°   )N)N)5Ú__doc__Z
__future__r   rr   rª   rY   Zmercurial.i18nr   Zmercurial.pycompatr   Z	mercurialr   r   r   r   r	   r
   r   r   r   Zmercurial.utilsr   r   ZcmdtableZcommandZfuzzywuzzy.fuzzr4   rg   ÚImportErrorZ
testedwithrF   Úcompilerw   r[   r˜   Úobjectr   rE   r6   r7   r9   r:   r;   re   rG   r}   r~   rŒ   rš   r    ZCATEGORY_CHANGE_NAVIGATIONr­   r°   r   r   r   r   Ú<module>   s„   ,







û
]
$COIûûûñæu
