
    >                         d Z ddlZddlZddlmZ ddlmZmZmZm	Z	 ddl
mZ ddlmZ dZdZeefZd	Z G d
 de      Z G d de      Z G d de      Zd fdZy)z=General implementation of walking commits and their contents.    N)chain)RENAME_CHANGE_TYPEStree_changestree_changes_for_mergeRenameDetector)MissingCommitError)Tagdatetopo   c                   $    e Zd ZdZd ZddZd Zy)	WalkEntryz1Object encapsulating a single result from a walk.c                     || _         |j                  | _        |j                  | _        i | _        |j                  | _        y N)commitstore_storeget_parents_get_parents_changesrename_detector_rename_detector)selfwalkerr   s      lib/third_party/dulwich/walk.py__init__zWalkEntry.__init__5   s7    ll".. & 6 6    Nc                 $   | j                   j                  |      }|| j                  }| j                  |      s
t        }d}n&t        | j                  |            dk(  rlt        }| j                  | j                  |      d      j                  }|r|j                  | j                  j                  |      \  }}| j                  |   }nt        }| j                  |      D cg c]  }| j                  |   j                   }}|r`|D cg c]  }| j                  |    }	}g }|	D ]=  }	 |j                  | j                  j                  |      \  }}
|j                  |
       ? |j                  }|r8| j                  |   }|j                  | j                  j                  |      \  }}t         || j                  ||| j                              }|| j                   |<   | j                   |   S c c}w c c}w # t        $ r Y w xY w)a-  Get the tree changes for this entry.

        Args:
          path_prefix: Portion of the path in the repository to
            use to filter changes. Must be a directory name. Must be
            a full, valid, path reference (no partial names or wildcards).
        Returns: For commits with up to one parent, a list of TreeChange
            objects; if the commit has no parents, these will be relative to
            the empty tree. For merge commits, a list of lists of TreeChange
            objects; see dulwich.diff.tree_changes_for_merge.
        N   r   )r   )r   getr   r   r   lenr   treelookup_path__getitem__r   appendKeyErrorlistr   )r   path_prefixcachedr   changes_funcparentmodesubtree_shapparent_treesstcommit_tree_shacommit_trees                r   changeszWalkEntry.changes<   s    "";/>[[F$$V,+T&&v./14+T%6%6v%>q%ABGG(.(:(://#)%D+ "[[5F57;7H7H7PQ7P!$++a.--7PQ<B#CFqDKKNFL#CF).'(}} $ 7 7 +(HD" #MM"- * %kkO"kk/:(3(?(?KK++)%o KK#$($9$9	F *0DMM+&}}[))= R#C  ( ! !s   ( G9G>0)H	HHc                 X    d| j                   j                  d| j                         dS )Nz<WalkEntry commit=z
, changes=>)r   idr3   r   s    r   __repr__zWalkEntry.__repr__y   s     KKNNLLN
 	
r   r   )__name__
__module____qualname____doc__r   r3   r8    r   r   r   r   2   s    ;7;*z
r   r   c                   ,    e Zd ZdZd Zd Zd Zd ZeZy)_CommitTimeQueuez3Priority queue of WalkEntry objects by commit time.c                    || _         |j                  | _        |j                  | _        |j
                  | _        g | _        t               | _	        t               | _
        t               | _        |j                  | _        d | _        t        | _        d| _        t%        |j&                  |j
                        D ]  }| j)                  |        y )NF)_walkerr   r   r   r   excluded	_excluded_pqset_pq_set_seen_donesince	_min_time_last_MAX_EXTRA_COMMITS_extra_commits_left_is_finishedr   include_push)r   r   	commit_ids      r   r   z_CommitTimeQueue.__init__   s    ll"..uU
U

#5 !v~~v?IJJy! @r   c                    	 | j                   |   }t        |t              r| j                  |j                  d          y |}|j                  | j                  vr|j                  | j                  vrxt        j                  | j                  |j                   |f       | j                  j                  |j                         | j                  j                  |j                         y y y # t        $ r t        |      w xY wNr   )r   r&   r   
isinstancer	   rP   objectr6   rF   rH   heapqheappushrD   commit_timeaddrG   )r   	object_idobjr   s       r   rP   z_CommitTimeQueue._push   s    	0++i(C c3JJszz!}%99DLL(VYYdjj-HNN488v'9'9&96%BCLLVYY'JJNN699% .I(  	0$Y//	0s   C, ,Dc                     | j                   }| j                  }|g}|ra|j                         }| j                  |      D ]9  }||vr"||v r|j	                  | j
                  |          |j                  |       ; |r`y y r   )rC   rG   popr   r%   r   rY   )r   r   rB   seentodor+   s         r   _exclude_parentsz!_CommitTimeQueue._exclude_parents   st    >>zzxXXZF++F3)fn
 KKF 34V$ 4 r   c                      j                   ry  j                  rt        j                   j                        \  }}|j                  } j
                  j                  |       | j                  v re j                  j                  |        j                  |      D ]  } j                  |        d}| j                  v }|r j                  |        j                  rdt         fd j                  D              rF j                  d   \  }} j                  r&|j                   j                  j                  k\  rd}nd} j                   |j                   j                   k  rd}|rt"         _        n* xj$                  dz  c_         j$                  s		 d _         y |s| _        t'         j(                  |      S  j                  rd _         y )NTc              3   T   K   | ]  \  }}|j                   j                  v  ! y wr   )r6   rC   ).0_cr   s      r   	<genexpr>z(_CommitTimeQueue.next.<locals>.<genexpr>   s#     #Pxtq!ADDDNN$:xs   %(r   Fr   )rN   rD   rV   heappopr6   rF   removerH   rY   r   rP   rC   r`   allrK   rX   rJ   rL   rM   r   rA   )r   rd   r   sha	parent_idreset_extra_commitsis_excludedns   `       r   nextz_CommitTimeQueue.next   s   hhdhh/IAv))CLL$djj JJNN3!..v6	

9% 7 #'/K%%f-88#Ptxx#P P88A;DAqzzammtzz7M7M&M /3+.3+~~)f.@.@4>>.Q
 ',#"+=(((A-(//
 !	 #
 v66Y hhZ !r   N)	r9   r:   r;   r<   r   rP   r`   ro   __next__r=   r   r   r?   r?      s!    =""&%1f Hr   r?   c                   T    e Zd ZdZdedddddddd efdZd Zd Zd Z	d	 Z
d
 Zd Zy)WalkerzObject for performing a walk of commits in a store.

    Walker objects are initialized with a store and other options and can then
    be treated as iterators of Commit objects.
    NFc                     | j                   S r   parentsr   s    r   <lambda>zWalker.<lambda>   s    6>>r   c                    |t         vrt        d|z        || _        t        |t              r|g}|| _        t        |xs g       | _        || _        || _	        || _
        |xr t        |      xs d| _        |	r|st        |      }|| _        || _        |	| _        |
| _        || _        d| _         ||       | _        t)        j*                         | _        y)a  Constructor.

        Args:
          store: ObjectStore instance for looking up objects.
          include: Iterable of SHAs of commits to include along with their
            ancestors.
          exclude: Iterable of SHAs of commits to exclude along with their
            ancestors, overriding includes.
          order: ORDER_* constant specifying the order of results.
            Anything other than ORDER_DATE may result in O(n) memory usage.
          reverse: If True, reverse the order of output, requiring O(n)
            memory.
          max_entries: The maximum number of entries to yield, or None for
            no limit.
          paths: Iterable of file or subtree paths to show entries for.
          rename_detector: diff.RenameDetector object for detecting
            renames.
          follow: If True, follow path across renames/copies. Forces a
            default rename_detector.
          since: Timestamp to list commits after.
          until: Timestamp to list commits before.
          get_parents: Method to retrieve the parents of a commit
          queue_cls: A class to use for a queue of commits, supporting the
            iterator protocol. The constructor takes a single argument, the
            Walker.
        zUnknown walk order %sNr   )
ALL_ORDERS
ValueErrorr   rT   bytesrO   rE   rB   orderreversemax_entriespathsr   r   r   followrI   until_num_entries_queuecollectionsdeque
_out_queue)r   r   rO   excluder|   r}   r~   r   r   r   rI   r   r   	queue_clss                 r   r   zWalker.__init__   s    X 
"4u<==
gu% iGGMr*
&)s5z1T
/,U3O.&

o%++-r   c                     |y| j                   D ].  }||k(  r y|j                  |      s|t        |         dk(  s. y y)NFT/   )r   
startswithr!   )r   changed_pathfollowed_paths      r   _path_matcheszWalker._path_matches4  sL    !ZZM},''6 ]!34? ( r   c                 R   |sy|j                   j                  }|j                  j                  }| j                  |      rU| j                  rH|j
                  t        v r6| j                  j                  |       | j                  j                  |       y| j                  |      ryy)NFT)
oldpathnewr   r   typer   r   rY   rh   )r   changeold_pathnew_paths       r   _change_matcheszWalker._change_matchesA  s|    ::??::??h'{{v{{.AA

x(

!!(+)r   c                    |j                   }| j                  |j                  | j                  k  ry| j                  |j                  | j                  kD  ry|j                  | j
                  v ry| j                  yt        | j                  |            dkD  r1|j                         D ]  }|D ]  }| j                  |      s  y  y|j                         D ]  }| j                  |      s y y)a	  Determine if a walk entry should be returned..

        Args:
          entry: The WalkEntry to consider.
        Returns: True if the WalkEntry should be returned by this walk, or
            False otherwise (e.g. if it doesn't match any requested paths).
        NFTr   )r   rI   rX   r   r6   rB   r   r!   r   r3   r   )r   entryr   path_changesr   s        r   _should_returnzWalker._should_returnP  s     ::!f&8&84::&E::!f&8&84::&E99%::t'(1, % +F++F3# +	 !0   --/''/ * r   c                    | j                   }|| j                  |k  rt        | j                        }|| j                  j                  |       |t        | j                        t        kD  rO| j                  sy | j                  j                         }| j                  |      r| xj                  dz  c_        |S || j                  |k  ry rS   )
r~   r   ro   r   r   r%   r!   rL   popleftr   )r   r~   r   s      r   _nextzWalker._nextq  s    &&!T%6%6%D%E &&u-}DOO 47I I//1&&u-%%*% L !T%6%6%D r   c                     | j                   t        k(  rt        || j                        }| j                  rt        t        |            }|S )a  Possibly reorder a results iterator.

        Args:
          results: An iterator of WalkEntry objects, in the order returned
            from the queue_cls.
        Returns: An iterator or list of WalkEntry objects, in the order
            required by the Walker.
        )r|   
ORDER_TOPO_topo_reorderr   r}   reversedr'   )r   resultss     r   _reorderzWalker._reorder  s<     ::##GT-=-=>G<<tG}-Gr   c                 ^    t        | j                  t        | j                  d                   S r   )iterr   r   r7   s    r   __iter__zWalker.__iter__  s!    DMM$tzz4"89::r   )r9   r:   r;   r<   
ORDER_DATEr?   r   r   r   r   r   r   r   r=   r   r   rr   rr      sP     1"C.JB;r   rr   c                     | j                   S r   rt   rv   s    r   rw   rw     s    fnnr   c              #     K   t        j                         }i }t        j                  t              }| D ]7  }|j	                  |        ||j
                        D ]  }||xx   dz  cc<    9 |r|j                         }|j
                  }|j                  }||   r|||<   5 ||      D ];  }	||	xx   dz  cc<   ||	   r|j                  |	d      }
|
s+|j                  |
       = | |ryyw)a  Reorder an iterable of entries topologically.

    This works best assuming the entries are already in almost-topological
    order, e.g. in commit time order.

    Args:
      entries: An iterable of WalkEntry objects.
      get_parents: Optional function for getting the parents of a commit.
    Returns: iterator over WalkEntry objects from entries in FIFO order, except
        where a parent would be yielded before any of its children.
    r   N)
r   r   defaultdictintr%   r   r   r6   r]   
appendleft)entriesr   r_   pendingnum_childrenr   r.   r   rQ   rk   parent_entrys              r   r   r     s      DG**3/LEU\\*AOq O + 
 II		"!&GI$V,I#q(#	*&{{9d;OOL1 -  s   B>C2C2C20C2)r<   r   rV   	itertoolsr   dulwich.diff_treer   r   r   r   dulwich.errorsr   dulwich.objectsr	   r   r   ry   rL   rU   r   r?   rr   r   r=   r   r   <module>r      s   * D     

*%
  K
 K
\ev ePh;V h;V (E !r   