U
    楡c[                     @   s`  d dl Z d dlmZ d dlmZ d dlmZm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mZmZ d d	lmZmZ erdd
lmZ ddlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z& ed Z'dZ(e) Z*e +dZ,ee-dddZ.d"e-e/ee- dddZ0G dd de)Z1edddZ2G dd  d ee2 Z3G d!d de)Z4dS )#    N)handle_process_output)defenc)finalize_process
hex_to_bin   )Blob)mode_str_to_int)AnyIteratorListMatchOptionalTupleTypeTypeVarUnionTYPE_CHECKINGcast)PathLikeLiteral)Tree)Commit)Repo)IndexObject)Popen)Git)ADCMRTU)Diffable	DiffIndexDiff	NULL_TREEs   \\([0-9]{3}))matchobjreturnc                 C   s&   |  d}t|d}tt|f}|S )Nr      )groupintbytes	bytearray)r'   value r/   ,/tmp/pip-unpacked-wheel-_pbxsds5/git/diff.py_octal_repl;   s    

r1   T)pathhas_ab_prefixr(   c                 C   s   | dkrd S |  drL| drL| dd ddddd	dd
d} tt| } |r|  dst|  dstt| dd  } | S )Ns	   /dev/null   "r   s   \n   
s   \t   	s   \"s   \\   \s   a/s   b/   )
startswithendswithreplace_octal_byte_resubr1   AssertionError)r2   r3   r/   r/   r0   decode_pathB   s    ,r@   c                   @   s   e Zd ZdZdZG dd deZeee	d e
d ef  eee	d e
d ef  dddZed	d
fee
d ddd	e	ef eeee eedf d	f eeddddZd	S )r#   zCommon interface for all object that can be diffed against another object of compatible type.

    :note:
        Subclasses require a repo member as it is the case for Object instances, for practical
        reasons we do not derive from Object.r/   c                   @   s   e Zd ZdS )Diffable.IndexN)__name__
__module____qualname__r/   r/   r/   r0   Index]   s   rE   rA   )argsr(   c                 C   s   |S )z
        :return:
            possibly altered version of the given args list.
            Method is called right before git command execution.
            Subclasses can use it to alter the behaviour of the superclassr/   )selfrF   r/   r/   r0   _process_diff_args`   s    zDiffable._process_diff_argsNFr   r   .r$   )otherpathscreate_patchkwargsr(   c           
      K   s^  g }| d | d | d |r2| d n| d | d | d |dk	rlt|ttfsl|g}t| d	r~| j| _| jjj}|| jkr|	d
d nV|t
kr|	d
d |	d
d | jjj}n*|dk	r|	d
d |	d
| | jjj}|	d
|  |r| d || d|d< || ||}|r@tjntj}|| j|}	|  |	S )a  Creates diffs between two items being trees, trees and index or an
        index and the working tree. It will detect renames automatically.

        :param other:
            Is the item to compare us with.
            If None, we will be compared to the working tree.
            If Treeish, it will be compared against the respective tree
            If Index ( type ), it will be compared against the index.
            If git.NULL_TREE, it will compare against the empty tree.
            It defaults to Index to assure the method will not by-default fail
            on bare repositories.

        :param paths:
            is a list of paths or a single path to limit the diff to.
            It will only include at least one of the given path or paths.

        :param create_patch:
            If True, the returned Diff contains a detailed patch that if applied
            makes the self to other. Patches are somewhat costly as blobs have to be read
            and diffed.

        :param kwargs:
            Additional arguments passed to git-diff, such as
            R=True to swap both sides of the diff.

        :return: git.DiffIndex

        :note:
            On a bare repository, 'other' needs to be provided as Index or as
            as Tree/Commit, or a git command error will occurz--abbrev=40z--full-indexz-Mz-pz--rawz-zz
--no-colorNZHas_Repor   z--cachedz-rz--rootz--TZ
as_process)append
isinstancetuplelisthasattrrepogitdiffrE   insertr&   Z	diff_treeextendrH   r%   _index_from_patch_format_index_from_raw_formatwait)
rG   rI   rJ   rK   rL   rF   Zdiff_cmdprocZdiff_methodindexr/   r/   r0   rT   j   sD    %











zDiffable.diff)rB   rC   rD   __doc__	__slots__objectrE   r   r   strr   rH   r   r   boolr	   rT   r/   r/   r/   r0   r#   R   s    r#   T_Diffr%   )boundc                   @   s(   e Zd ZdZdZeee dddZdS )r$   zImplements an Index for diffs, allowing a list of Diffs to be queried by
    the diff properties.

    The class improves the diff handling convenience)r   r   r   r    r   r!   )change_typer(   c                 c   s   || j krtd| | D ]}|j |kr0|V  q|dkrF|jrF|V  q|dkr\|jr\|V  q|dkrr|jrr|V  q|dkr|jr|V  q|dkr|jr|jr|j|jkr|V  qdS )a  
        :return:
            iterator yielding Diff instances that match the given change_type

        :param change_type:
            Member of DiffIndex.change_type, namely:

            * 'A' for added paths
            * 'D' for deleted paths
            * 'R' for renamed paths
            * 'M' for paths with modified data
            * 'T' for changed in the type paths
        zInvalid change type: %sr   r   r   r    r   N)rc   
ValueErrornew_filedeleted_filecopied_filerenameda_blobb_blob)rG   rc   Zdiffidxr/   r/   r0   iter_change_type   s    

 zDiffIndex.iter_change_typeN)	rB   rC   rD   r\   rc   Lit_change_typer
   ra   rk   r/   r/   r/   r0   r$      s   r$   c                   @   s  e Zd ZdZedejejB ZdZ	dZ
dZdee ee eeedf eeedf eeedf eeedf eeeee ee eeedf ee ee ddd	d
ZeedddZeedddZedddZedddZeee dddZeee dddZeee dddZeee dddZeedddZeeddd Ze eeeee d!d"d#Z!e ded$ e"d%d&d'Z#e$ede"dd(d)d*Z%e dd+d,d%d-d.Z&dS )/r%   a  A Diff contains diff information between two Trees.

    It contains two sides a and b of the diff, members are prefixed with
    "a" and "b" respectively to inidcate that.

    Diffs keep information about the changed blob objects, the file mode, renames,
    deletions and new files.

    There are a few cases where None has to be expected as member variable value:

    ``New File``::

        a_mode is None
        a_blob is None
        a_path is None

    ``Deleted File``::

        b_mode is None
        b_blob is None
        b_path is None

    ``Working Tree Blobs``

        When comparing to working trees, the working tree blob will have a null hexsha
        as a corresponding object does not yet exist. The mode will be null as well.
        But the path will be available though.
        If it is listed in a diff the working tree version of the file must
        be different to the version in the index or tree, and hence has been modified.s  
                                ^diff[ ]--git
                                    [ ](?P<a_path_fallback>"?[ab]/.+?"?)[ ](?P<b_path_fallback>"?[ab]/.+?"?)\n
                                (?:^old[ ]mode[ ](?P<old_mode>\d+)\n
                                   ^new[ ]mode[ ](?P<new_mode>\d+)(?:\n|$))?
                                (?:^similarity[ ]index[ ]\d+%\n
                                   ^rename[ ]from[ ](?P<rename_from>.*)\n
                                   ^rename[ ]to[ ](?P<rename_to>.*)(?:\n|$))?
                                (?:^new[ ]file[ ]mode[ ](?P<new_file_mode>.+)(?:\n|$))?
                                (?:^deleted[ ]file[ ]mode[ ](?P<deleted_file_mode>.+)(?:\n|$))?
                                (?:^similarity[ ]index[ ]\d+%\n
                                   ^copy[ ]from[ ].*\n
                                   ^copy[ ]to[ ](?P<copied_file_name>.*)(?:\n|$))?
                                (?:^index[ ](?P<a_blob_id>[0-9A-Fa-f]+)
                                    \.\.(?P<b_blob_id>[0-9A-Fa-f]+)[ ]?(?P<b_mode>.+)?(?:\n|$))?
                                (?:^---[ ](?P<a_path>[^\t\n\r\f\v]*)[\t\r\f\v]*(?:\n|$))?
                                (?:^\+\+\+[ ](?P<b_path>[^\t\n\r\f\v]*)[\t\r\f\v]*(?:\n|$))?
                            Z(0000000000000000000000000000000000000000s                       )ri   rj   a_modeb_mode	a_rawpath	b_rawpathre   rf   rg   raw_rename_fromraw_rename_torT   rc   scorer   N)rR   ro   rp   	a_blob_id	b_blob_idrm   rn   re   rf   rg   rq   rr   rT   rc   rs   r(   c                 C   sz  |d kst |tst|d ks,t |ts,t|| _|| _|rDt|nd | _|rVt|nd | _|r|r|jD ]*}|j	|
tdkrj| r| } qqj|  |d ks|| jkrd | _nt|t|| j| jd| _|  |d ks|| jkrd | _nt|t|| j| jd| _|| _|	| _|
| _|d ks2t |ts2t|d ksLt |tsLt|pTd | _|p`d | _|| _|| _|| _d S )Nr<   )moder2   )rN   r,   r?   ro   rp   r   rm   rn   Z
submodulesr2   decoder   Zmodule_existsmoduleNULL_HEX_SHAri   r   r   a_pathrj   b_pathre   rf   rg   rq   rr   rT   rc   rs   )rG   rR   ro   rp   rt   ru   rm   rn   re   rf   rg   rq   rr   rT   rc   rs   	submoduler/   r/   r0   __init__@  s<    
zDiff.__init__)rI   r(   c                 C   s*   | j D ]}t| |t||kr dS qdS )NFT)r]   getattr)rG   rI   namer/   r/   r0   __eq__~  s    
zDiff.__eq__c                 C   s
   | |k S Nr/   )rG   rI   r/   r/   r0   __ne__  s    zDiff.__ne__)r(   c                    s   t t fdd jD S )Nc                 3   s   | ]}t  |V  qd S r   )r~   ).0nrG   r/   r0   	<genexpr>  s     z Diff.__hash__.<locals>.<genexpr>)hashrO   r]   r   r/   r   r0   __hash__  s    zDiff.__hash__c                 C   sb  d}| j r|| j j; }n| jr*|| jj; }d}d }d}t| j | jfdD ]>\}}|rhd||j|jf }nd| }tt||}||7 }qH|dd|  7 }| jr|d	7 }| j	r|d
7 }| j
r|d| j| jf 7 }| jr|d| j 7 }| jr|d| j 7 }| jrV|d7 }z(|t| jtr$| jtn| j7 }W n tk
rL   |d7 }Y nX |d7 }|| }|S )Nz%s r   )lhsrhsz
%s: %o | %sz	
%s: None
=z
file deleted in rhsz
file added in rhsz
file %r copied from %rz
file renamed from %rz
file renamed to %rz
---zOMITTED BINARY DATA)ri   r2   rj   ziprv   Zhexshamaxlenrf   re   rg   r{   rz   rename_from	rename_torT   rN   r,   rw   r   UnicodeDecodeError)rG   hmsglineZline_lengthbr   resr/   r/   r0   __str__  sD    
(zDiff.__str__c                 C   s   | j r| j tdS d S Nr<   )ro   rw   r   r   r/   r/   r0   rz     s    zDiff.a_pathc                 C   s   | j r| j tdS d S r   )rp   rw   r   r   r/   r/   r0   r{     s    zDiff.b_pathc                 C   s   | j r| j tdS d S r   )rq   rw   r   r   r/   r/   r0   r     s    zDiff.rename_fromc                 C   s   | j r| j tdS d S r   )rr   rw   r   r   r/   r/   r0   r     s    zDiff.rename_toc                 C   s   | j S )z:returns: True if the blob of our diff has been renamed
        :note: This property is deprecated, please use ``renamed_file`` instead.
        )renamed_filer   r/   r/   r0   rh     s    zDiff.renamedc                 C   s   | j | jkS )z7:returns: True if the blob of our diff has been renamed)r   r   r   r/   r/   r0   r     s    zDiff.renamed_file)
path_matchrename_matchpath_fallback_matchr(   c                 C   s,   |rt |S |rt |ddS |r(t |S d S )NF)r3   )r@   )clsr   r   r   r/   r/   r0   _pick_best_path  s    zDiff._pick_best_path)r   zGit.AutoInterrupt)rR   rZ   r(   c                 C   s  g }t ||jdtdd d|}t }d}d}d\}}	d\}
}| j|D ]}| \}}}}}}}}}}}}}}	t|t|t|  }}}| 	|||}| 	|	||}	|dk	r||
 |  |d _|p|p|o|p|p|}
|p|p|p|	o|
}|t|||	|o|t|o$|t|
o2|
t|o@|t|||||ddd |}|}qL|r|r||
 d |d _|S )a  Create a new DiffIndex from the given text which must be in patch format
        :param repo: is the repository we are operating on - it is required
        :param stream: result of 'git diff' as a stream (supporting file protocol)
        :return: git.DiffIndexNFZdecode_streams    )NNr5   )r   rM   r   joinr$   	re_headerfinditergroupsr`   r   endstartrT   r%   rw   r   )r   rR   rZ   Z	text_listtextr[   Zprevious_headerheaderrz   r{   rm   rn   Z_headerZa_path_fallbackZb_path_fallbackold_modenew_moder   r   Znew_file_modeZdeleted_file_modeZcopied_file_namert   ru   re   rf   rg   r/   r/   r0   rW     sv    

zDiff._index_from_patch_format)lines_bytesrR   r[   r(   c                 C   s  |  t}|d\}}}|dD ]h}|s0q$|d\}}}|d}|d d\}}	}
}}tt|d }d|dd  }| rt	|nd }|
 }|t}|t}d}d}d}d }d }|d	krd }d
}n|dkrd }
d
}nx|dkrd
}|dd\}}|t}|t}nD|dkrV|dd\}}|t}|t}|| }}n
|dkr`t||||
|||	|||||d||}|| q$d S )N:z :    r   r   r   Fr   Tr   r   r    r!   )rw   r   	partitionsplitrstripr   rl   r   isdigitr+   stripencoder%   rM   )r   rR   r[   lines_r   metar2   r   r   rt   ru   Z_change_typerc   Z	score_strrs   rz   r{   rf   re   rg   r   r   Z
a_path_strZ
b_path_strrT   r/   r/   r0   _handle_diff_line9  sl    









zDiff._handle_diff_liner   r$   c                    s(   t  t| fdddtdd S )zhCreate a new DiffIndex from the given stream which must be in raw format.
        :return: git.DiffIndexc                    s     | S r   )r   )Zbytr   r[   rR   r/   r0   <lambda>  r   z-Diff._index_from_raw_format.<locals>.<lambda>NFr   )r$   r   r   )r   rR   rZ   r/   r   r0   rX     s    zDiff._index_from_raw_format)'rB   rC   rD   r\   recompileVERBOSE	MULTILINEr   ry   ZNULL_BIN_SHAr]   r   r,   r   r_   r`   rl   r+   r}   r^   r   r   r   r   propertyrz   r{   r   r   rh   r   classmethodr   r$   rW   staticmethodr   rX   r/   r/   r/   r0   r%      sb   
>1SI)T)5r   Zgit.cmdr   Z
git.compatr   Zgit.utilr   r   Zobjects.blobr   Zobjects.utilr   typingr	   r
   r   r   r   r   r   r   r   r   r   Z	git.typesr   r   Zobjects.treer   Zobjectsr   Zgit.repo.baser   Zgit.objects.baser   
subprocessr   rS   r   rl   __all__r^   r&   r   r=   r,   r1   r`   r@   r#   ra   r$   r%   r/   r/   r/   r0   <module>   s0   4

p0