
    X              	       n   S r SSKJrJr  SSKJr  SSKJr  SSKrSSK	J
r
Jr  SrSrS	rS
rSrSr\\4r\" SSS5      rSrSrSrSr " S S\" S/ SQ5      5      rS rS rS rS$S jrS r    S%S jrS rS r S&S jr!Sr"S r#S r$S&S jr%S  r& " S! S"\'5      r(\r)\r*\#r+ SS#K,JrJrJ#r#  g! \- a     gf = f)'z&Utilities for diffing files and trees.    )defaultdict
namedtuple)BytesIO)chainN)S_ISGITLINK	TreeEntryaddmodifydeleterenamecopy	unchangedd   <      c                   8    \ rS rSrSr\S 5       r\S 5       rSrg)
TreeChange8   z.Named tuple a single change between two trees.c                 &    U " [         [        U5      $ N)
CHANGE_ADD_NULL_ENTRY)clsnews     $lib/third_party/dulwich/diff_tree.pyr	   TreeChange.add;   s    :{C00    c                 &    U " [         U[        5      $ r   )CHANGE_DELETEr   )r   olds     r   r   TreeChange.delete?   s    =#{33r    N)	__name__
__module____qualname____firstlineno____doc__classmethodr	   r   __static_attributes__r"   r   r   r   r   8   s+    81 1 4 4r   r   )typer    r   c                     / nU(       d  U$ UR                  SS9 H#  nUR                  UR                  U 5      5        M%     U$ )NT)
name_order)	iteritemsappendin_path)pathtreeresultentrys       r   _tree_entriesr4   D   s>    F40emmD)* 1Mr   c                 d   [        X5      n[        X5      nS=pV[        U5      n[        U5      n/ n	XW:  a  Xh:  a  X5   n
XF   nU
R                  UR                  :  a  U	R                  U
[        45        US-  nOSU
R                  UR                  :  a  U	R                  [        U45        US-  nOU	R                  X45        US-  nUS-  nXW:  a  Xh:  a  M  [        XW5       H  nU	R                  X<   [        45        M     [        Xh5       H  nU	R                  [        XL   45        M     U	$ )a  Merge the entries of two trees.

Args:
  path: A path to prepend to all tree entry names.
  tree1: The first Tree object to iterate, or None.
  tree2: The second Tree object to iterate, or None.
Returns:
  A list of pairs of TreeEntry objects for each pair of entries in
    the trees. If an entry exists in one tree but not the other, the other
    entry will have all attributes set to None. If neither entry's path is
    None, they are guaranteed to match.
r      )r4   lenr0   r.   r   range)r0   tree1tree2entries1entries2i1i2len1len2r2   entry1entry2is                r   _merge_entriesrD   M   s    T)HT)HKBx=Dx=DF
)	;;$MM6;/0!GB[[6;;&MM;/0!GBMM6*+!GB!GB )	 2_x{K01 2_{HK01 Mr   c                 N    U R                   nUc  g[        R                  " U5      $ NF)modestatS_ISDIR)r3   rG   s     r   _is_treerJ   u   s"    ::D|<<r   c           	   #     #    U=(       a    [         R                  =(       d    SnU=(       a    [         R                  =(       d    Sn[        SXA5      [        SXR5      4/nU(       a  UR                  5       u  px[	        U5      n	[	        U5      n
U(       a  U	(       a  U
(       a  Xx:X  a  MK  U	=(       a    XR
                     =(       d    SnU
=(       a    XR
                     =(       d    SnUR                  =(       d    UR                  nUR                  [        [        XU5      5      5        Xx4v   U(       a  M  gg7f)a  Recursively walk all the entries of two trees.

Iteration is depth-first pre-order, as in e.g. os.walk.

Args:
  store: An ObjectStore for looking up objects.
  tree1_id: The SHA of the first Tree object to iterate, or None.
  tree2_id: The SHA of the second Tree object to iterate, or None.
  param prune_identical: If True, identical subtrees will not be walked.
Returns:
  Iterator over Pairs of TreeEntry objects for each pair of entries
    in the trees and their subtrees recursively. If an entry exists in one
    tree but not the other, the other entry will have all attributes set
    to None. If neither entry's path is None, they are guaranteed to
    match.
Nr   )
rH   S_IFDIRr   poprJ   shar0   extendreversedrD   )storetree1_idtree2_idprune_identicalmode1mode2todorA   rB   is_tree1is_tree2r9   r:   r0   s                 r   
walk_treesrZ   |   s     & %-E%-EsE,iU.MNOD
F#F#xH9I.U::.6$.U::.6${{)fkkH^D?@An $s   D8D><D>c                     U R                   b,  U(       d+  [        R                  " U R                   5      (       a  [        $ U $ r   )rG   rH   rI   r   )r3   include_treess     r   
_skip_treer]      s+    zz-DLL4L4LLr   c              #   b  #    Ub$  Ub!  Ub  UR                  UUUUS9 H  nUv   M	     g[        XX#(       + S9nU H  u  pX:X  a	  U(       d  M  [        X5      n	[        X5      n
U	[        :w  a  U
[        :w  a  [        R
                  " U	R                  5      [        R
                  " U
R                  5      :w  a+  U(       d$  [        R                  U	5      v   [        n	[        nO7X:X  a  [        nO+[        nO$U	[        :w  a  [        nOU
[        :w  a  [        nOM  [        XU
5      v   M     g7f)a=  Find the differences between the contents of two trees.

Args:
  store: An ObjectStore for looking up objects.
  tree1_id: The SHA of the source tree.
  tree2_id: The SHA of the target tree.
  want_unchanged: If True, include TreeChanges for unmodified entries
    as well.
  include_trees: Whether to include trees
  rename_detector: RenameDetector object for detecting renames.
  change_type_same: Whether to report change types in the same
    entry or as delete+add.
Returns:
  Iterator over TreeChange instances for each change between the
    source and target tree.
Nwant_unchangedr\   )rT   )changes_with_renamesrZ   r]   r   rH   S_IFMTrG   r   r   r   CHANGE_UNCHANGEDCHANGE_MODIFYr   )rQ   rR   rS   r`   rename_detectorr\   change_type_samechangeentriesrA   rB   change_types               r   tree_changesrj      s    2 "x';@T%::)'	 ; 
F L
 	4FG "N F2F2[ V{%:FKK(DKK,DD( !''//$(!.+{"'K{"$K f559 "s   D-D/c                 2    U  H  nU" U5      U:w  d  M    g   g)NFTr"   )seqkeyvaluees       r   _all_eqrp      s    q6U?  r   c                 2    [        U SS  X" U S   5      5      $ )Nr6   r   )rp   )rl   rm   s     r   	_all_samerr      s    3qr7CSV--r   c           
   #     ^#    U Vs/ s H  n[        XX#S9PM     nn[        U5      m[        U4S j5      n[        U5       HV  u  pxU HK  n	U	R                  [
        :X  a  U	R                  R                  n
OU	R                  R                  n
XU
   U'   MM     MX     S nS n[        UR                  5       5       H~  u  p[        U5      T:X  d   eU Vs/ s H	  oc  M  UPM     nn[        UU[
        5      (       a  [        UU5      (       d  Uv   MY  M[  [        UU5      (       d  Uv   Mr  SU;  d  Mz  Uv   M     gs  snf s  snf 7f)a  Get the tree changes for a merge tree relative to all its parents.

Args:
  store: An ObjectStore for looking up objects.
  parent_tree_ids: An iterable of the SHAs of the parent trees.
  tree_id: The SHA of the merge tree.
  rename_detector: RenameDetector object for detecting renames.

Returns:
  Iterator over lists of TreeChange objects, one per conflicted path
  in the merge.

  Each list contains one element per parent, with the TreeChange for that
  path relative to that parent. An element may be None if it never
  existed in one parent and was deleted in two others.

  A path is only included in the output if it is a conflict, i.e. its SHA
  in the merge tree is not found in any of the parents, or in the case of
  deletes, if not all of the old SHAs match.
)re   c                     > S /T -  $ r   r"   )num_parentss   r   <lambda>(tree_changes_for_merge.<locals>.<lambda>  s    4&;*>r   c                 .    U R                   R                  $ r   )r    rN   cs    r   old_sha'tree_changes_for_merge.<locals>.old_sha  s    uuyyr   c                     U R                   $ r   )r*   ry   s    r   ri   +tree_changes_for_merge.<locals>.change_type  s    vvr   N)rj   r7   r   	enumerater*   r   r    r0   r   sorteditemsrp   rr   )rQ   parent_tree_idstree_idre   tall_parent_changeschanges_by_pathrC   parent_changesrg   r0   r{   ri   _changesrz   haveru   s                    @r   tree_changes_for_merger      s5    . ! A 	UwH    o&K!">?O ''9:$F{{m+zzzz'-D!!$ % ; _2245
7|{***"47a744m44T7++ ,4--M  M 6/2 5s)   E	D?B:E	E E&AE	6E	@   c                    [        [        5      n[        5       nSnUR                  nUR                  nUR
                  nUR                  n[        R                  " U R                  5       5       Hk  nUR                  SS5      nU" U5        US-  nUS:X  d  U[        :X  d  M4  U" 5       n	U[        U	5      ==   [        U	5      -  ss'   U" S5        U" 5         SnMm     US:  a&  U" 5       n
U[        U
5      ==   [        U
5      -  ss'   U$ )zCount the blocks in an object.

Splits the data into blocks either on lines or <=64-byte chunks of lines.

Args:
  obj: The object to count blocks for.
Returns:
  A dict of block hashcode -> total bytes occurring.
r   r6   big   
)r   intr   writeseektruncategetvaluer   from_iterableas_raw_chunksto_bytes_BLOCK_SIZEhashr7   )objblock_countsblocknblock_write
block_seekblock_truncateblock_getvaluerz   rn   
last_blocks              r   _count_blocksr   4  s     s#LIE	A ++KJ^^N^^N  !2!2!45JJq% A	Q:k)"$Ee%U3%qMA 6 	1u#%
T*%&#j/9&r   c                     [        U 5      [        U5      :  a  XpSnU R                  5        H-  u  p4UR                  U5      nU(       d  M  U[        XE5      -  nM/     U$ )a;  Count the number of common bytes in two block count dicts.

Args:
  block1: The first dict of block hashcode -> total bytes.
  block2: The second dict of block hashcode -> total bytes.
Returns:
  The number of bytes in common between blocks1 and blocks2. This is
  only approximate due to possible hash collisions.
r   )r7   r   getmin)blocks1blocks2scorer   count1count2s         r   _common_bytesr   X  sY     7|c'l""E U#6S((E ) Lr   c                    Uc  0 nU R                   U;  a  [        U 5      X R                   '   UR                   U;  a  [        U5      X!R                   '   [        X R                      X!R                      5      n[        U R	                  5       UR	                  5       5      nU(       d  [
        $ [        [        U5      [
        -  U-  5      $ )a  Compute a similarity score for two objects.

Args:
  obj1: The first object to score.
  obj2: The second object to score.
  block_cache: An optional dict of SHA to block counts to cache
    results between calls.
Returns:
  The similarity score between the two objects, defined as the
    number of bytes in common between the two objects divided by the
    maximum size, scaled to the range 0-100.
)idr   r   max
raw_length
_MAX_SCOREr   float)obj1obj2block_cachecommon_bytesmax_sizes        r   _similarity_scorer   m  s     wwk!,T2GGwwk!,T2GG WW!5{777KLL4??$doo&78Hu\"Z/(:;;r   c                 t    U R                   R                  nU R                  R                  nUc  UnUc  UnX4$ r   )r    r0   r   )r3   path1path2s      r   _tree_change_keyr     s8    IINNEIINNE}}>r   c                       \ rS rSrSr\\\S4S jrS r	S r
S rS rS	 rS
 rS rS rS rS rS rS rS r SS jrSrg)RenameDetectori  z7Object for handling rename detection between two trees.Fc                 N    Xl         X l        X@l        X0l        XPl        SU l        g)a0  Initialize the rename detector.

Args:
  store: An ObjectStore for looking up objects.
  rename_threshold: The threshold similarity score for considering
    an add/delete pair to be a rename/copy; see _similarity_score.
  max_files: The maximum number of adds and deletes to consider,
    or None for no limit. The detector is guaranteed to compare no more
    than max_files ** 2 add/delete pairs. This limit is provided
    because rename detection can be quadratic in the project size. If
    the limit is exceeded, no content rename detection is attempted.
  rewrite_threshold: The threshold similarity score below which a
    modify should be considered a delete/add, or None to not break
    modifies; see _similarity_score.
  find_copies_harder: If True, consider unmodified files when
    detecting copies.
FN)_store_rename_threshold_rewrite_threshold
_max_files_find_copies_harder_want_unchanged)selfrQ   rename_threshold	max_filesrewrite_thresholdfind_copies_harders         r   __init__RenameDetector.__init__  s(    2 !1"3##5 $r   c                 .    / U l         / U l        / U l        g r   )_adds_deletes_changesr   s    r   _resetRenameDetector._reset  s    
r   c                 ^   U R                   bB  UR                  [        :w  d.  UR                  R                  UR
                  R                  :X  a  gU R                  UR                  R                     nU R                  UR
                  R                     n[        X#5      U R                   :  $ rF   )r   r*   rd   r    rN   r   r   r   )r   rg   old_objnew_objs       r   _should_splitRenameDetector._should_split  sw    ##+{{m+zz~~/++fjjnn-++fjjnn- 2T5L5LLLr   c                    UR                   [        :X  a  U R                  R                  U5        g UR                   [        :X  a  U R
                  R                  U5        g U R                  U5      (       aq  U R
                  R                  [        R                  UR                  5      5        U R                  R                  [        R                  UR                  5      5        g U R                  (       a  UR                   [        :X  d  UR                   [        :X  a  U R
                  R                  U5        g U R                  R                  U5        g r   )r*   r   r   r.   r   r   r   r   r   r    r	   r   r   rc   rd   r   )r   rg   s     r   _add_changeRenameDetector._add_change  s    ;;*$JJf%[[M)MM  (''MM  !2!26::!>?JJjnnVZZ89$$8H)H[[M)
 MM  (MM  (r   c                     U R                   =(       d    U R                  n[        U R                  UUUU R                  S9 H  nU R                  U5        M     g )Nr_   )r   r   rj   r   _include_treesr   )r   rR   rS   r`   rg   s        r   _collect_changesRenameDetector._collect_changes  sP    11IT5I5I"KK)--
F V$
r   c                    U R                    Vs/ s H   o3R                  R                  U;  d  M  UPM"     snU l         U R                   Vs/ s H   oDR                  R                  U;  d  M  UPM"     snU l        g s  snf s  snf r   )r   r   r0   r   r    )r   	add_pathsdelete_pathsads        r   _pruneRenameDetector._prune  sY    !%KAuuzz/JaK
$(MMTMqUUZZ|5SMT LTs   A<A<B-Bc           	         [        [        5      nU R                   H4  nXR                  R                     R                  UR                  5        M6     [        [        5      nU R                   HI  nUR                  [        :H  nX4R                  R                     R                  UR                  U45        MK     [        5       n[        5       nUR                  5        GH[  u  pX   n
[        X5       H  u  u  pn[        R                  " UR                  5      [        R                  " UR                  5      :w  a  ML  U(       a  UR!                  UR"                  5        UR!                  UR"                  5        U=(       a    [$        =(       d    [&        nU R(                  R                  [+        XU5      5        M     [-        U
5      [-        U	5      -
  nU	S   S   nUS:  d  GM  X* S   HG  nUR!                  UR"                  5        U R(                  R                  [+        [&        X5      5        MI     GM^     U R/                  Xg5        g )Nr   )r   listr   r   rN   r.   r   r*   r   r    setr   ziprH   rb   rG   r	   r0   CHANGE_RENAMECHANGE_COPYr   r   r7   r   )r   add_mapr	   
delete_mapr   	is_deleter   r   rN   sha_deletessha_addsr    r   new_typenum_extra_addss                  r   _find_exact_renames"RenameDetector._find_exact_renames  s   d#::CGGKK ''0  &
mmF }4Izz~~&--vzz9.EF	 $ E	u * 0 0 2C|H),[)C% #;;sxx(DKK,AA $$SXX.chh'$6E+$$Zs%CD *D !]S-==Na.#C!#O$45CMM#((+MM((K)JK 6 !3$ 	I,r   c                 x    [        U R                  5      [        U R                  5      -  U R                  S-  :*  $ )N   )r7   r   r   r   r   s    r   _should_find_content_renames+RenameDetector._should_find_content_renames	  s,    4::T]]!33t!7KKKr   c                     U(       a4  UR                   R                  UR                  R                  :X  a  [        $ UR                  [
        :w  a  [        $ [        $ r   )r    r0   r   rd   r*   r   r   r   )r   check_pathsr   r	   s       r   _rename_typeRenameDetector._rename_type  s>    6::??cggll: ! [[M) r   c                    / =ol         U R                  5       (       d  g 0 nU R                  S LnU R                   GHK  n[	        UR
                  R                  5      (       a  M*  UR
                  R                  nU R                  U   n[        U5      X%'   U R                   H  n[        R                  " UR
                  R                  5      [        R                  " UR                  R                  5      :w  a  M[  U R                  UR                  R                     n[        XhUS9n	XR                  :  d  M  U R                  X4U5      n
[!        XR
                  UR                  5      nUR#                  U	* U45        M     GMN     g )N)r   )_candidatesr   r   r   r   r    rG   rN   r   r   r   rH   rb   r   r   r   r   r.   )r   
candidatesr   r   r   r{   r   r	   r   r   r   r   s               r   _find_content_rename_candidates.RenameDetector._find_content_rename_candidates  s&   (**
% 0022,,D8mmF6::??++jjnnGkk'*G#0#9K zz;;vzz/4;;sww||3LL++cggkk2)'T111#00cJH'**cggFF%%vv&67 " $r   c                    U R                   R                  5         [        5       n[        5       nU R                    H  u  p4UR                  R                  nXR;   a  M"  UR
                  R                  nUR                  nXa;   a%  [        [        UR
                  UR                  5      nU[        :w  a  UR                  U5        UR                  U5        U R                  R                  U5        M     U R                  X!5        g r   )r   sortr   r   r0   r    r*   r   r   r	   r   r.   r   )r   r   r   r   rg   new_pathold_path	orig_types           r   _choose_content_renames&RenameDetector._choose_content_renames5  s     	uE	))IAzzH$zzHI'#KVZZH K'  *MM(#MM  ( * 	I,r   c                 &   U R                   c  g 0 n[        S U R                   5       5      nU R                   H  nUR                  R
                  nUR                  U5      nUc  M/  [        R                  " UR                  R                  5      [        R                  " UR                  R                  5      :X  d  M  [        [        UR                  UR                  5      X'   M     U R                   Vs/ s H   ofR                  R
                  U;  d  M  UPM"     snU l        U R                   Vs/ s H   ofR                  R
                  U;  d  M  UPM"     snU l        U =R                  UR                  5       -  sl        g s  snf s  snf )Nc              3   P   #    U  H  oR                   R                  U4v   M     g 7fr   )r    r0   ).0r   s     r   	<genexpr>0RenameDetector._join_modifies.<locals>.<genexpr>R  s     A=a55::q/=s   $&)r   dictr   r   r   r0   r   rH   rb   r    rG   r   rd   r   values)r   modifiesr   r	   r0   r   r   s          r   _join_modifiesRenameDetector._join_modifiesM  s   ""*A4==AA
::C77<<D^^D)F!dkk&**//&BdkkG ' ",M6::sww!O  "&JAuuzz/IaJ
$(MMPMqUUZZx5OMP** KPs   9F	F	6FFc                     / nUR                  U R                  5        UR                  U R                  5        UR                  U R                  5        UR	                  [
        S9  U$ )N)rm   )rO   r   r   r   r  r   )r   r2   s     r   _sorted_changesRenameDetector._sorted_changes_  sK    djj!dmm$dmm$()r   c                     U R                   (       a  g U R                   Vs/ s H  oR                  [        :w  d  M  UPM     snU l        g s  snf r   )r   r   r*   rc   )r   r   s     r   _prune_unchangedRenameDetector._prune_unchangedg  s5    $(MMPMqVV?O5OMPPs
   AAc                    U R                  5         X0l        X@l        U R                  X5        U R	                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  5       $ )zAIterate TreeChanges between two tree SHAs, with rename detection.)
r   r   r   r   r   r  r  r  r  r  )r   rR   rS   r`   r\   s        r   ra   #RenameDetector.changes_with_renamesl  sr     	-+h1  ",,.$$&##%%r   )r   r   r   r   r   r   r   r   r   r   r   N)FF)r#   r$   r%   r&   r'   RENAME_THRESHOLD	MAX_FILESREWRITE_THRESHOLDr   r   r   r   r   r   r   r   r   r  r  r  r  r  ra   r)   r"   r   r   r   r     sk    A
 *+ %@
	M)&	%U-BL
8:-0+$Q GL&r   r   )rJ   rD   r   )F)FNFFr   ).r'   collectionsr   r   ior   	itertoolsr   rH   dulwich.objectsr   r   r   rd   r   r   r   rc   RENAME_CHANGE_TYPESr   r   r  r  r  r   r4   rD   rJ   rZ   r]   rj   rp   rr   r   r   r   r   r   r   objectr   _is_tree_py_merge_entries_py_count_blocks_pydulwich._diff_treeImportErrorr"   r   r   <module>r*     s$  * -
    
 $k2 dD)
 	 	4L*@A 	4%P!H B6J.8v !H*<6f&V f&T "   	 
  		s    
B+ +B43B4