
    dԝ                     
   d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZmZmZmZmZ dd	lmZ eZd
ZdZdZdZdZ ed          ZdZeZ  G d de!          Z"d Z#defdZ$ G d d          Z% G d de%          Z& G d de%          Z' G d de%          Z(d Z)d Z*d  Z+d8d!Z,d" Z-d#efd$Z.d% Z/d& Z0d' Z1d(e%d)ed*ed+ed,ee         d-efd.Z2d/ Z3	 	 	 d9d1e%d2e4d(ee4e4f         d3ee         d4e5d5e5fd6Z6d7 Z7dS ):zRef handling.

    N)suppress)DictOptional   )PackedRefsExceptionRefFormatError)GitFileensure_dir_exists)ZERO_SHAObjectIDTaggit_linevalid_hexsha)ObjectContainers   HEADs   ref: s   refs/heads/s
   refs/tags/   refs/remotes/s    ~^:?*[s   ^{}c                       e Zd ZdZd ZdS )
SymrefLoopz,There is a loop between one or more symrefs.c                 "    || _         || _        d S N)refdepth)selfr   r   s      P/home/feoh/.local/pipx/venvs/poetry/lib/python3.11/site-packages/dulwich/refs.py__init__zSymrefLoop.__init__4   s    


    N)__name__
__module____qualname____doc__r    r   r   r   r   1   s)        66    r   r   c                     |                      t                    r/| t          t                    d                             d          S t	          |           )z`Parse a symref value.

    Args:
      contents: Contents to parse
    Returns: Destination
    N   
)
startswithSYMREFlenrstrip
ValueError)contentss    r   parse_symref_valuer)   9   sK     6"" 7F&--g666
X

r   refnamec                 0   d| v s|                      d          rdS d| vrdS d| v rdS t          |           D ]/\  }}t          | ||dz                      dk     s	|t          v r dS 0| d         dv rdS |                     d	          rdS d
| v rdS d| v rdS dS )a+  Check if a refname is correctly formatted.

    Implements all the same rules as git-check-ref-format[1].

    [1]
    http://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html

    Args:
      refname: The refname to check
    Returns: True if refname is valid, False otherwise
    s   /.   .F   /s   ..r       s   .locks   @{   \T)r#   	enumerateordBAD_REF_CHARSendswith)r*   ics      r   check_ref_formatr7   E   s     7--d33u7uu'""  1wq1q5y!""T))Q--?-?55 .@r{eu!! uuu4r   c                   b   e Zd ZdZd$dZ	 	 	 	 d%dZ	 	 	 	 d%dZd Zdee	e
e         f         fdZd	 Z	 	 	 	 	 d&de	dee	ef         de
e         de
e         de
e         de
e         defdZd Zd Zd$dZd Zd$dZd Zd Zd Zd Zd Zd Z	 	 	 	 d%dZ	 	 d%dZd  Z	 	 	 	 d%d!Zd" Z d# Z!dS )'RefsContainerzA container for refs.Nc                     || _         d S r   _logger)r   loggers     r   r   zRefsContainer.__init__j   s    r   c           	      V    | j         d S |d S |                      |||||||           d S r   r;   )r   r   old_shanew_sha	committer	timestamptimezonemessages           r   _logzRefsContainer._logm   s?     <F?FS'7Iy(GTTTTTr   c                 *    t          | j                  )zMake a ref point at another ref.

        Args:
          name: Name of the ref to set
          other: Name of the ref to point at
          message: Optional message
        )NotImplementedErrorset_symbolic_ref)r   nameotherrA   rB   rC   rD   s          r   rH   zRefsContainer.set_symbolic_ref}   s      "$"7888r   c                 *    t          | j                  )Get contents of the packed-refs file.

        Returns: Dictionary mapping ref names to SHA1s

        Note: Will return an empty dictionary when no packed-refs file is
            present.
        )rG   get_packed_refsr   s    r   rM   zRefsContainer.get_packed_refs   s     "$"6777r   new_refsc                 *    t          | j                  )Add the given refs as packed refs.

        Args:
          new_refs: A mapping of ref names to targets; if a target is None that
            means remove the ref
        )rG   add_packed_refs)r   rO   s     r   rR   zRefsContainer.add_packed_refs   s     "$"6777r   c                     dS ae  Return the cached peeled value of a ref, if available.

        Args:
          name: Name of the ref to peel
        Returns: The peeled value of the ref. If the ref is known not point to
            a tag, this will be the SHA the ref refers to. If the ref may point
            to a tag, but no cached information is available, None is returned.
        Nr    r   rI   s     r   
get_peeledzRefsContainer.get_peeled   s	     tr   FbaserJ   rA   rB   rC   rD   prunec                    |r#t          |                     |                    }nt                      }|                                D ]t\  }	}
|
|                    |	           n.|                     d                    ||	f          d |
|           |r'	 |                    |	           d# t          $ r Y pw xY wu|D ]/}|                     d                    ||f          d |           0d S )Nr-   rD   )	setsubkeysitemsaddset_if_equalsjoinremoveKeyErrorremove_if_equals)r   rW   rJ   rA   rB   rC   rD   rX   	to_deleterI   valuer   s               r   import_refszRefsContainer.import_refs   s3     	DLL..//III ;;== 	 	KD%}d####""IItTl++T5' #     $$T****   D
  	Q 	QC!!$))T3K"8"8$!PPPP	Q 	Qs   B,,
B98B9c                 *    t          | j                  )z#All refs present in this container.)rG   allkeysrN   s    r   rh   zRefsContainer.allkeys   s    !$,///r   c                 D    t          |                                           S r   )iterrh   rN   s    r   __iter__zRefsContainer.__iter__   s    DLLNN###r   c                 X    ||                      |          S |                                 S )zRefs present in this container.

        Args:
          base: An optional base to return refs under.
        Returns: An unsorted set of valid refs in this container, including
            packed refs.
        )r\   rh   )r   rW   s     r   keyszRefsContainer.keys   s*     <<%%%<<>>!r   c                     t                      }t          |          dz   }|                                 D ]4}|                    |          r|                    ||d                    5|S )zRefs present in this container under a base.

        Args:
          base: The base to return refs under.
        Returns: A set of valid refs in this container under the base; the base
            prefix is stripped from the ref names returned.
        r   N)r[   r%   rh   r#   r^   )r   rW   rm   base_lenr*   s        r   r\   zRefsContainer.subkeys   sj     uut99q=||~~ 	- 	-G!!$'' -+,,,r   c                     i }|                      |          }|d}n|                    d          }|D ]>}	 | |dz   |z                       d                   ||<   (# t          t          f$ r Y ;w xY w|S )z6Return the contents of this container as a dictionary.Nr   r-   )rm   r&   stripr   rb   )r   rW   retrm   keys        r   as_dictzRefsContainer.as_dict   s    yy<DD;;t$$D 	 	Cs!2 9 9$ ? ?@C)    
s   $AA0/A0c                     |t           dfv rdS |                    d          rt          |dd                   st          |          dS )a  Ensure a refname is valid and lives in refs or is HEAD.

        HEAD is not a valid refname according to git-check-ref-format, but this
        class needs to be able to touch HEAD. Also, check_ref_format expects
        refnames without the leading 'refs/', but this class requires that
        so it cannot touch anything outside the refs dir (or HEAD).

        Args:
          name: The name of the reference.
        Raises:
          KeyError: if a refname is not HEAD or is otherwise not valid.
        s
   refs/stashNs   refs/   )HEADREFr#   r7   r   rU   s     r   _check_refnamezRefsContainer._check_refname   s]     G]+++Fx(( 	'0@abb0J0J 	' &&&	' 	'r   c                     |                      |          }|s(|                                                     |d          }|S )zRead a reference without following any references.

        Args:
          refname: The name of the reference
        Returns: The contents of the ref file, or None if it does
            not exist.
        N)read_loose_refrM   get)r   r*   r(   s      r   read_refzRefsContainer.read_ref  sF     &&w// 	A++--11'4@@Hr   c                 *    t          | j                  )zRead a loose reference and return its contents.

        Args:
          name: the refname to read
        Returns: The contents of the ref file, or None if it does
            not exist.
        )rG   rz   rU   s     r   rz   zRefsContainer.read_loose_ref  s     "$"5666r   c                 V   t           |z   }d}g }|                    t                     r~|t          t                     d         }|                    |           |                     |          }|sn5|dz  }|dk    rt          ||          |                    t                     ~||fS )zFollow a reference name.

        Returns: a tuple of (refnames, sha), wheres refnames are the names of
            references in the chain
        r   Nr   rv   )r$   r#   r%   appendr|   r   )r   rI   r(   r   refnamesr*   s         r   followzRefsContainer.follow&  s     D=!!&)) 	.s6{{}}-GOOG$$$}}W--H QJEqyy u--- !!&)) 	. !!r   c                 4    |                      |          rdS dS )NTF)r|   )r   r*   s     r   __contains__zRefsContainer.__contains__:  s     ==!! 	4ur   c                 X    |                      |          \  }}|t          |          |S )zaGet the SHA1 for a reference name.

        This method follows all symbolic references.
        )r   rb   )r   rI   _shas       r   __getitem__zRefsContainer.__getitem__?  s/    
 T""3;4.. 
r   c                 *    t          | j                  )a'  Set a refname to new_ref only if it currently equals old_ref.

        This method follows all symbolic references if applicable for the
        subclass, and can be used to perform an atomic compare-and-swap
        operation.

        Args:
          name: The refname to set.
          old_ref: The old sha the refname must refer to, or None to set
            unconditionally.
          new_ref: The new sha the refname will refer to.
          message: Message for reflog
        Returns: True if the set was successful, False otherwise.
        )rG   r_   )r   rI   old_refnew_refrA   rB   rC   rD   s           r   r_   zRefsContainer.set_if_equalsI  s    0 "$"4555r   c                 *    t          | j                  )zAdd a new reference only if it does not already exist.

        Args:
          name: Ref name
          ref: Ref value
        )rG   
add_if_newr   rI   r   rA   rB   rC   rD   s          r   r   zRefsContainer.add_if_newc  s     "$/222r   c                 4    |                      |d|           dS )a  Set a reference name to point to the given SHA1.

        This method follows all symbolic references if applicable for the
        subclass.

        Note: This method unconditionally overwrites the contents of a
            reference. To update atomically only if the reference has not
            changed, use set_if_equals().

        Args:
          name: The refname to set.
          ref: The new sha the refname will refer to.
        N)r_   )r   rI   r   s      r   __setitem__zRefsContainer.__setitem__m  s"     	4s+++++r   c                 *    t          | j                  )a  Remove a refname only if it currently equals old_ref.

        This method does not follow symbolic references, even if applicable for
        the subclass. It can be used to perform an atomic compare-and-delete
        operation.

        Args:
          name: The refname to delete.
          old_ref: The old sha the refname must refer to, or None to
            delete unconditionally.
          message: Message for reflog
        Returns: True if the delete was successful, False otherwise.
        )rG   rc   )r   rI   r   rA   rB   rC   rD   s          r   rc   zRefsContainer.remove_if_equals}  s    , "$"7888r   c                 2    |                      |d           dS )as  Remove a refname.

        This method does not follow symbolic references, even if applicable for
        the subclass.

        Note: This method unconditionally deletes the contents of a reference.
            To delete atomically only if the reference has not changed, use
            remove_if_equals().

        Args:
          name: The refname to delete.
        N)rc   rU   s     r   __delitem__zRefsContainer.__delitem__  s      	dD)))))r   c                     i }|                                  D ]:}	 t          |                     |                    }|||<   +# t          $ r Y 7w xY w|S )zuGet a dict with all symrefs in this container.

        Returns: Dictionary mapping source ref to target ref
        )rh   r)   r|   r'   )r   rr   srcdsts       r   get_symrefszRefsContainer.get_symrefs  st    
 <<>> 	 	C(s););<< C     
s   "A
AAr   NNNN)NNNNF)"r   r   r   r   r   rE   rH   rM   r   Refr   r   rR   rV   bytesboolrf   rh   rk   rm   r\   rt   rx   r|   rz   r   r   r   r_   r   r   rc   r   r   r    r   r   r9   r9   g   s           U U U U( 9 9 9 9$8 8 88S(82D-D(E 8 8 8 8	 	 	 &*%)$(#'Q QQ CM"Q E?	Q
 E?Q 5/Q %Q Q Q Q Q:0 0 0$ $ $" " " "      ' ' '$  7 7 7" " "(  
   6 6 6 64 ?C*.3 3 3 3, , ,( 9 9 9 90* * *    r   r9   c                        e Zd ZdZd fd	Zd Zd Zd Zd Z	 	 	 	 dde	d	e	fd
Z
	 	 	 	 ddZ	 	 	 	 dde	dedee         fdZ	 	 	 	 ddZd Zd Zd Z xZS )DictRefsContainerzRefsContainer backed by a simple dict.

    This container does not support symbolic or packed references and is not
    threadsafe.
    Nc                     t                                          |           || _        i | _        t	                      | _        d S )Nr=   )superr   _refs_peeledr[   	_watchers)r   refsr=   	__class__s      r   r   zDictRefsContainer.__init__  s;    '''
r   c                 4    | j                                         S r   r   rm   rN   s    r   rh   zDictRefsContainer.allkeys      z   r   c                 8    | j                             |d           S r   r   r{   rU   s     r   rz   z DictRefsContainer.read_loose_ref      z~~dD)))r   c                     i S r   r    rN   s    r   rM   z!DictRefsContainer.get_packed_refs      	r   c                 H    | j         D ]}|                    ||f           d S r   )r   _notify)r   r   newshawatchers       r   r   zDictRefsContainer._notify  s6    ~ 	+ 	+GOOS&M****	+ 	+r   rI   rJ   c           	          |                      |          d         }t          |z   }|| j        |<   |                     ||           |                     |||||||           d S )Nr/   rA   rB   rC   rD   )r   r$   r   r   rE   )	r   rI   rJ   rA   rB   rC   rD   oldnews	            r   rH   z"DictRefsContainer.set_symbolic_ref  s}     kk$#un
4T3		 	 	
 	
 	
 	
 	
r   c           
      f   |&| j                             |t                    |k    rdS |                     |          \  }}	|D ]m}
|                     |
           | j                             |
          }|| j         |
<   |                     |
|           |                     |
||||||           ndS NFr   T)r   r{   r   r   rx   r   rE   )r   rI   r   r   rA   rB   rC   rD   	realnamesr   realnamer   s               r   r_   zDictRefsContainer.set_if_equals  s     4:>>$#A#AW#L#L5{{4((	1! 	 	H)))*..**C#*DJx LL7+++II##!      tr   r   rD   c           	          || j         v rdS || j         |<   |                     ||           |                     |d |||||           dS r   )r   r   rE   r   s          r   r   zDictRefsContainer.add_if_new  sm     4:5
4T3		 	 	
 	
 	
 tr   c           	         |&| j                             |t                    |k    rdS 	 | j                             |          }|                     |d            |                     ||d ||||           n# t          $ r Y nw xY wdS r   )r   r{   r   popr   rE   rb   )r   rI   r   rA   rB   rC   rD   r   s           r   rc   z"DictRefsContainer.remove_if_equals  s     4:>>$#A#AW#L#L5	*..&&C LLt$$$II##!     	  	 	 	D	 ts   A7 7
BBc                 6    | j                             |          S r   )r   r{   rU   s     r   rV   zDictRefsContainer.get_peeled6  s    |%%%r   c                 h    |                                 D ]\  }}|                     |d|           dS )z0Update multiple refs; intended only for testing.N)r]   r_   )r   r   r   r   s       r   _updatezDictRefsContainer._update9  sD     

 	/ 	/HCsD#....	/ 	/r   c                 :    | j                             |           dS )z5Update cached peeled refs; intended only for testing.N)r   update)r   peeleds     r   _update_peeledz DictRefsContainer._update_peeled@  s    F#####r   r   r   )r   r   r   r   r   rh   rz   rM   r   r   rH   r_   r   r   r   r   rc   rV   r   r   __classcell__r   s   @r   r   r     su             ! ! !* * *  + + + 
 

 
 
 
 
8    B #'   %   8    8& & &/ / /$ $ $ $ $ $ $r   r   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )InfoRefsContainerz5Refs container that reads refs from a info/refs file.c                    i | _         i | _        |                                D ]}|                    d                              d          \  }}|                    t                    r6|d d         }t          |          st          d|z            || j        |<   }t          |          st          d|z            || j         |<   d S )N   
   	invalid ref name %r)	r   r   	readlinesr&   splitr4   PEELED_TAG_SUFFIXr7   r'   )r   fliner   rI   s        r   r   zInfoRefsContainer.__init__H  s    
KKMM 
	' 
	'DE**0077IC}}.// 'CRCy'-- C$%:T%ABBB%(T""'-- C$%:T%ABBB#&
4  
	' 
	'r   c                 4    | j                                         S r   r   rN   s    r   rh   zInfoRefsContainer.allkeysW  r   r   c                 8    | j                             |d           S r   r   rU   s     r   rz   z InfoRefsContainer.read_loose_refZ  r   r   c                     i S r   r    rN   s    r   rM   z!InfoRefsContainer.get_packed_refs]  r   r   c                 X    	 | j         |         S # t          $ r | j        |         cY S w xY wr   )r   rb   r   rU   s     r   rV   zInfoRefsContainer.get_peeled`  sB    	$<%% 	$ 	$ 	$:d####	$s    ))N)	r   r   r   r   r   rh   rz   rM   rV   r    r   r   r   r   E  se        ??' ' '! ! !* * *  $ $ $ $ $r   r   c                        e Zd ZdZd fd	Zd Zd Zd Zd Zd Z	d	e
eee         f         fd
Zd Zd Zd Z	 	 	 	 ddZ	 	 	 	 ddZ	 	 	 	 ddededee         fdZ	 	 	 	 ddZ xZS )DiskRefsContainerz)Refs container that reads refs from disk.Nc                     t                                          |           t          |dd           t          j        |          }|| _        ||}t          |dd           t          j        |          }|| _        d | _        d | _        d S )Nr   encode)	r   r   getattrosfsencodepathworktree_path_packed_refs_peeled_refs)r   r   r   r=   r   s       r   r   zDiskRefsContainer.__init__j  s    '''44((4;t$$D	  M=(D11=K66M*  r   c                 L    d                     | j        j        | j                  S )Nz{}({!r}))formatr   r   r   rN   s    r   __repr__zDiskRefsContainer.__repr__w  s      !8$)DDDr   c                    t                      }|                     |          }t          j        |          D ]\  }}}|t	          |          d          }t          j        j        dk    r7|                    t          j        t          j        j                  d          }|	                    d          }|D ]J}d
                    |r|gng |gz             }	t          |dz   |	z             r|                    |	           K|                                 D ]T}
|
                    |          r=|                    |
t	          |          d          	                    d                     U|S )N/r-   )r[   refpathr   walkr%   r   sepreplacer   rq   r`   r7   r^   rM   r#   )r   rW   r\   r   rootunused_dirsfilesdirfilenamer*   rs   s              r   r\   zDiskRefsContainer.subkeysz  sX   %%||D!!(* 
	) 
	)$D+us4yy{{#Cw{c!!kk"+bgk":":DAA))D//C! ) )))c%9cUUrhZ$GHH $D4K'$9:: )KK((() '')) 	: 	:C~~d## :CD		,22488999r   c                    t                      }t          j                            |                     t
                              r|                    t
                     |                     d          }|                     d          }t          j        |          D ]\  }}}|t          |          d          }t          j        j	        dk    r7|
                    t          j        t          j        j	                  d          }|D ]=}d                    ||g          }	t          |	          r|                    |	           >|                    |                                            |S )Nr      refsr   r-   )r[   r   r   existsr   rw   r^   r   r%   r   r   r   r`   r7   r   rM   )
r   rh   r   refspathr   r   r   r   r   r*   s
             r   rh   zDiskRefsContainer.allkeys  s6   %%7>>$,,w//00 	!KK   ||C  <<(((*(9(9 	) 	)$D+us4yy{{#Cw{c!!kk"+bgk":":DAA! ) )))S(O44#G,, )KK((() 	t++--...r   c                 D   t           j        j        dk    r7|                    dt          j        t           j        j                            }|t
          k    r%t           j                            | j        |          S t           j                            | j        |          S )zReturn the disk path of a ref.r   r-   )r   r   r   r   r   rw   r`   r   rU   s     r   r   zDiskRefsContainer.refpath  sn    7;#<<bk"'+&>&>??D 7??7<< 2D9997<<	4000r   c                 R   | j         i | _         i | _        t          j                            | j        d          }	 t          |d          }n# t          $ r i cY S w xY w|5  t          t          |                    	                                }|
                    d          r1d|v r-t          |          D ]\  }}}|| j         |<   |r
|| j        |<   n4|                    d           t          |          D ]\  }}|| j         |<   ddd           n# 1 swxY w Y   | j         S )rL   N   packed-refsrbs   # pack-refss    peeledr   )r   r   r   r   r`   r	   FileNotFoundErrornextrj   r&   r#   read_packed_refs_with_peeledseekread_packed_refs)r   r   r   
first_liner   rI   r   s          r   rM   z!DiskRefsContainer.get_packed_refs  s    $ !#D "D7<<	>::DD$''$   			 
6 
6!$q'']]1133
((88 6Z:=U=U-I!-L-L = =)T625)$/! =6<D-d3=
 FF1III%5a%8%8 6 6	T25)$//
6 
6 
6 
6 
6 
6 
6 
6 
6 
6 
6 
6 
6 
6 
6   s$   A AA"B)DDDrO   c                 z   |sdS t           j                            | j        d          }t          |d          5 }|                                                                 }|                                D ]\  }}|t          k    rt          d          t          t                    5  t          j        |                     |                     ddd           n# 1 swxY w Y   ||||<   z|                    |d           t          ||| j                   || _        ddd           dS # 1 swxY w Y   dS )rQ   Nr   wbzcannot pack HEAD)r   r   r`   r	   rM   copyr]   rw   r'   r   OSErrorra   r   r   write_packed_refsr   r   )r   rO   r   r   packed_refsr   targets          r   rR   z!DiskRefsContainer.add_packed_refs  s     	Fw||DI~66T4   	,A..005577K'~~// / /V'>>$%7888
 g&& 1 1Idll3//0001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 %'-K$$OOC....ad.?@@@ +D-	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s6   A.D0((CD0C  D0#C $?D00D47D4c                     |                                   | j        	|| j        vrdS || j        v r| j        |         S | |         S rT   )rM   r   r   rU   s     r   rV   zDiskRefsContainer.get_peeled  sY     	$D4E(E(E44$$$$T** :r   c                    |                      |          }	 t          |d          5 }|                    t          t                              }|t          k    r>|t          t          |                                        d          z   cddd           S ||                    dt          t                    z
            z   cddd           S # 1 swxY w Y   dS # t          t          f$ r Y dS w xY w)a  Read a reference file and return its contents.

        If the reference file a symbolic reference, only read the first line of
        the file. Otherwise, only read the first 40 bytes.

        Args:
          name: the refname to read, relative to refpath
        Returns: The contents of the ref file, or None if the file does not
            exist.
        Raises:
          IOError: if any other error occurs
        r   r"   N(   )
r   r	   readr%   r$   r   rj   r&   r   UnicodeError)r   rI   r   r   headers        r   rz   z DiskRefsContainer.read_loose_ref   sC    <<%%	4(( =AF,,V##!DaMM$8$8$A$AA	= = = = = = = = "AFF2F+;$<$<<= = = = = = = = = = = = = = = = = = & 	 	 	 44		sA   C A$CC ,CC CC CC C32C3c                 *   | j         d S t          j                            | j        d          }t	          |d          }	 d | _         |                                  || j         vr	 |                                 d S | j         |= t          t                    5  | j	        |= d d d            n# 1 swxY w Y   t          || j         | j	                   |                                 |                                 d S # |                                 w xY w)Nr   r   )r   r   r   r`   r	   rM   abortr   rb   r   r   close)r   rI   r   r   s       r   _remove_packed_refz$DiskRefsContainer._remove_packed_ref  s<   $F7<<	>::Hd##	 $D  """4,,, GGIIIII !$'(## , ,%d+, , , , , , , , , , , , , , ,a!2D4EFFFGGIIIGGIIIIIAGGIIIIs6    $C< ;C< 	B, C< ,B00C< 3B042C< <Dc           	         |                      |           |                      |           |                     |          }t          |d          }	 |                    t          |z   dz              |                     |          d         }	|                     ||	|	||||           |                                 dS # t          $ r |	                                  w xY w)zMake a ref point at another ref.

        Args:
          name: Name of the ref to set
          other: Name of the ref to point at
          message: Optional message to describe the change
        r   r   r/   r   N)
rx   r   r	   writer$   r   rE   r  BaseExceptionr
  )
r   rI   rJ   rA   rB   rC   rD   r   r   r   s
             r   rH   z"DiskRefsContainer.set_symbolic_ref2  s      	D!!!E"""<<%%Hd##	GGFUNU*+++++d##B'CII##!     GGIIIII	  	 	 	GGIII	s   AB> > Cc           
      R   |                      |           	 |                     |          \  }}	|d         }
n# t          t          t          f$ r |}
Y nw xY w|                     |
          }t          j                            |
          }| 	                                }|rF|
                    |d          t          |          t          j                            |          }|Ft          t          j                            |                     t          |d          5 }|	 |                     |
          }|-| 	                                
                    |
t                    }||k    r"|                                 	 ddd           dS n## t"          $ r |                                  w xY w	 |                    |dz              n## t"          $ r |                                  w xY w|                     |
||||||           ddd           n# 1 swxY w Y   dS )a  Set a refname to new_ref only if it currently equals old_ref.

        This method follows all symbolic references, and can be used to perform
        an atomic compare-and-swap operation.

        Args:
          name: The refname to set.
          old_ref: The old sha the refname must refer to, or None to set
            unconditionally.
          new_ref: The new sha the refname will refer to.
          message: Set message for reflog
        Returns: True if the set was successful, False otherwise.
        r/   Nr   Fr   r   T)rx   r   rb   
IndexErrorr   r   r   r   dirnamerM   r{   NotADirectoryErrorr
   r	   rz   r   r
  r   r  rE   )r   rI   r   r   rA   rB   rC   rD   r   r   r   r   	probe_refr  r   orig_refs                   r   r_   zDiskRefsContainer.set_if_equalsX  s   . 	D!!!	;;t,,LIq }HH*j1 	 	 	HHH	<<)) GOOH--	**,, 	3y$//;(222	22I  	3
 	"'//(33444Xt$$ 	"
#228<<H'#'#7#7#9#9#=#=h#Q#Q7**			$	 	 	 	 	 	 	 	 +    GGIII%((((   			 II##!    %	 	 	 	 	 	 	 	 	 	 	 	 	 	 	6 tsR    8 AA#H'AFH F44H8GH G11HH #H rI   r   rD   c           
         	 |                      |          \  }}|dS |d         }	n# t          t          f$ r |}	Y nw xY w|                     |	           |                     |	          }
t          t          j                            |
                     t          |
d          5 }t          j        
                    |
          s||                                 v r"|                                 	 ddd           dS 	 |                    |dz              |                     |d|||||           n## t          $ r |                                  w xY w	 ddd           n# 1 swxY w Y   dS )a  Add a new reference only if it does not already exist.

        This method follows symrefs, and only ensures that the last ref in the
        chain does not exist.

        Args:
          name: The refname to set.
          ref: The new sha the refname will refer to.
          message: Optional message for reflog
        Returns: True if the add was successful, False otherwise.
        NFr/   r   r   r   T)r   rb   r  rx   r   r
   r   r   r  r	   r   rM   r
  r  rE   r   )r   rI   r   rA   rB   rC   rD   r   r(   r   r   r   s               r   r   zDiskRefsContainer.add_if_new  s   (	"&++d"3"3Ix#u }HH*% 	 	 	HHH	H%%%<<))"'//(33444Xt$$ 	w~~h'' 443G3G3I3I+I+I				 	 	 	 	 	 	 	e$$$
 		''%#     	    				 	 	 	 	 	 	 	 	 	 	 	 	 	 	& ts;   ' ' ==&AE$?D4E$4 EE$$E(+E(c           	         |                      |           |                     |          }t          t          j                            |                     t          |d          }	 |a|                     |          }	|	-|                                 	                    |t                    }	|	|k    r	 |                                 dS 	 t          j                            |          }
n# t          $ r d}
Y nw xY w|
rt          j        |           |                     |           |                     ||d||||           |                                 n# |                                 w xY w|}	 	 |                    dd          \  }}n# t$          $ r Y nGw xY w|dk    rn<|                     |          }	 t          j        |           n# t          $ r Y nw xY wndS )	a  Remove a refname only if it currently equals old_ref.

        This method does not follow symbolic references. It can be used to
        perform an atomic compare-and-delete operation.

        Args:
          name: The refname to delete.
          old_ref: The old sha the refname must refer to, or None to
            delete unconditionally.
          message: Optional message
        Returns: True if the delete was successful, False otherwise.
        r   NFr   Tr-   r   r   )rx   r   r
   r   r   r  r	   rz   rM   r{   r   r
  lexistsr   ra   r  rE   rsplitr'   rmdir)r   rI   r   rA   rB   rC   rD   r   r   r  foundparentr   parent_filenames                 r   rc   z"DiskRefsContainer.remove_if_equals  s*   * 	D!!!<<%%"'//(33444Hd##	"..t44##335599$IIHw&& 0 GGIIIII+11     $	(#####D)))II##!     GGIIIIAGGIIII
 	"MM$22	      "ll622O))))   
 	$ tsV   (AE C, +E ,C;8E :C;;A
E E07F 
FF>G 
G G )NNr   )r   r   r   r   r   r   r\   rh   r   rM   r   r   r   r   rR   rV   rz   r  rH   r_   r   r   rc   r   r   s   @r   r   r   g  s       33! ! ! ! ! !E E E  &  "	1 	1 	1! ! !@",S(82D-D(E ", ", ", ",H  &  :  2 $ $ $ $V C C C CR #'1 11 1 %1 1 1 1n O O O O O O O Or   r   c                 2   |                      d                              d          }t          |          dk    rt          d| z            |\  }}t	          |          st          d|z            t          |          st          d|z            ||fS )z6Split a single ref line into a tuple of SHA1 and name.s   
       zinvalid ref line %rInvalid hex sha %rr   )r&   r   r%   r   r   r7   )r   fieldsr   rI   s       r   _split_ref_liner#  "  s    [[!!''--F
6{{a!"7$">???IC >!"6"<===D!! @!"7$">???;r   c              #      K   | D ]M}|                     d          r|                     d          rt          d          t          |          V  NdS )zRead a packed refs file.

    Args:
      f: file-like object to read from
    Returns: Iterator over tuples with SHA1s and ref names.
       #   ^z.found peeled ref in packed-refs without peeledN)r#   r   r#  )r   r   s     r   r   r   /  ss        $ $??4   	??4   	X%&VWWWd######$ $r   c              #     K   d}| D ]}|d         dk    r|                     d          }|                    d          rf|st          d          t          |dd                   st          d|dd         z            t	          |          \  }}d}|||dd         fV  |rt	          |          \  }}||dfV  |}|rt	          |          \  }}||dfV  dS dS )	a	  Read a packed refs file including peeled refs.

    Assumes the "# pack-refs with: peeled" line was already read. Yields tuples
    with ref names, SHA1s, and peeled SHA1s (or None).

    Args:
      f: file-like object to read from, seek'ed to the second line
    Nr   r%  r"   r&  zunexpected peeled ref liner   r!  )r&   r#   r   r   r#  )r   lastr   r   rI   s        r   r   r   ?  sA      D  7d??{{7##??4   	 H)*FGGGQRR)) K)*>abb*IJJJ'--ICDd122h''''' (+D11	TD$''''DD  #D))	TD$   r   c                    |i }n|                      d           t          |                                          D ]P}|                      t          ||         |                     ||v r!|                      d||         z   dz              QdS )zWrite a packed refs file.

    Args:
      f: empty file-like object to write to
      packed_refs: dict of refname to sha of packed refs to write
      peeled_refs: dict of refname to peeled value of sha
    Ns   # pack-refs with: peeled
r&  r   )r  sortedrm   r   )r   r  peeled_refsr*   s       r   r   r   _  s     	-...+**,,-- 9 9	W-w77888k!!GGD;w//%78889 9r   c                     i }|                                  D ]3}|                    d                              dd          \  }}|||<   4|S )Nr"   r   r   )r   r&   r   )r   rr   r   r   rI   s        r   read_info_refsr-  q  sS    
C  kk'**00::dD		Jr   storec              #   F  K   ddl m} t          |                                           D ]v\  }}|t          k    r	 ||         }n# t
          $ r Y 'w xY w |||          \  }}|j        dz   |z   dz   V  |j        |j        k    r|j        dz   |z   t          z   dz   V  wdS )zGenerate info refs.r   peel_shar   r   N)object_storer1  r*  r]   rw   rb   idr   )r   r.  r1  rI   r   ounpeeledr   s           r   write_info_refsr6  y  s       '&&&&&DJJLL)) G G	c 7??	c
AA 	 	 	H	#8E3//&dUlT!E))))469)e#d*->>FFFFG Gs   A
AAc                 6    |                      t                    S r   )r#   LOCAL_BRANCH_PREFIX)xs    r   is_local_branchr:    s    <<+,,,r   c                 >    d |                                  D             S )zRemove all peeled refsc                 N    i | ]"\  }}|                     t                    ||#S r    )r4   r   ).0r   r   s      r   
<dictcomp>z%strip_peeled_refs.<locals>.<dictcomp>  sA       S#||-..S  r   )r]   )r   s    r   strip_peeled_refsr?    s+     **,,   r   c                     d|z   dz   }|r_|                     t                    rG|t          z   }||t          t                    d          z   }|| v r|                     ||           d S d S d S d S )Nr   r-   )r#   r8  rw   r%   rH   )r   originorigin_headorigin_base
origin_ref
target_refs         r   _set_origin_headrF    s    "V+d2K :{--.ABB : 7*
 ;s3F/G/G/I/I#JJ
!!*j99999	: : : : r   r   rA  rB  branchref_messagereturnc                    d|z   dz   }|rn||z   }|| v r*t           |z   }|                     || |         |           |}nt          |z   | v rt          |z   }nt          dt	          j        |          z            |rm|}|                    t                     r ||t          t                     d         z   }n|}	 |                     || |         |           n# t          $ r Y nw xY wt          d          |S )zSet the default branch.
    r   r-   z%r is not a valid branch or tagNz+neither origin_head nor branch are provided)	r8  r   LOCAL_TAG_PREFIXr'   r   r   r#   r%   rb   )	r   rA  rB  rG  rH  rC  rD  	local_refhead_refs	            r   _set_default_branchrN    sV   
 #V+d2K H 6)
+f4IOO4
+[   !HH&$..'&0HH1BK4G4GG   
 H!!"566 	%${37J3K3K3M3M'NNJJ$J	OO$z*K     	 	 	D	 FGGGOs   :C 
C%$C%c                    |                     t                    rh| |         }t          |t                    r$|j        \  }}|                    |          j        }| t          = |                     t          d ||           nU	 | |         }| 	                    t          |           |                     t          d ||           n# t          $ r d }Y nw xY w|S )NrZ   )r#   rK  
isinstancer   object
get_objectr3  rw   r_   rH   rb   )r   rM  rH  head_clsobjs         r   	_set_headrV    s    +,, H~dC   	*ID#>>#&&)DMT4 	 	
 	
 	
 	

	>D!!'8444wdKHHHH 	 	 	DDD	Ks   AC CCFrefs_containerremote_namerD   rX   
prune_tagsc                 4   t          |          }d |                                D             }|                     d|                                z   |||           d |                                D             }|                     t          |||           d S )Nc                     i | ]<\  }}|                     t                    |t          t                    d          |=S r   )r#   r8  r%   r=  nvs      r   r>  z'_import_remote_refs.<locals>.<dictcomp>  sU       Q<<+,,	#!
"
"
$
$%q  r   r   )rD   rX   c                     i | ]V\  }}|                     t                    |                    t                    9|t	          t                    d          |WS r   )r#   rK  r4   r   r%   r\  s      r   r>  z'_import_remote_refs.<locals>.<dictcomp>  sl       Q<<()) 34**=N2O2O	#


!
!"A  r   )r?  r]   rf   r   rK  )	rW  rX  r   rD   rX   rY  stripped_refsbranchestagss	            r   _import_remote_refsrc    s     &d++M #))++  H
 ;--///	     #))++  D
 /wjYYYYYr   c           
      z   ddl m} i }|                                D ]\  }}	  || |          \  }}t          |t                    r|j        ||t          z   <   |j        ||<   G# t          $ rJ t          j	        d|
                    dd          d|
                    d          t                     Y w xY w|S )Nr   r0  zref zutf-8r   z points at non-present sha ascii)r2  r1  r]   rP  r   r3  r   rb   warningswarndecodeUserWarning)r.  r   r1  rr   r   r   r5  r   s           r   serialize_refsrj    s    &&&&&&
CJJLL # #S	#'xs33Hf (C(( 9/5yC++,{CHH  	 	 	MM::gy11113::g3F3F3FH  
 H	 Js   A$$AB87B8r   )NFF)8r   r   rf  
contextlibr   typingr   r   errorsr   r   filer	   r
   objectsr   r   r   r   r   packr   r   r   rw   r$   r8  rK  LOCAL_REMOTE_PREFIXr[   r3   r   ANNOTATED_TAG_SUFFIX	Exceptionr   r)   r7   r9   r   r   r   r#  r   r   r   r-  r6  r:  r?  rF  rN  rV  strr   rc  rj  r    r   r   <module>ru     s  ,  
			        ! ! ! ! ! ! ! ! 7 7 7 7 7 7 7 7 , , , , , , , , D D D D D D D D D D D D D D ! ! ! ! ! !
	$   & N##  )        	 	 	c    DJ J J J J J J JZ
N$ N$ N$ N$ N$ N$ N$ N$b$ $ $ $ $ $ $ $Dx x x x x x x xv
 
 
$ $ $      @9 9 9 9$  G G G G G&- - -  : : :""%*"9>"HM"e_")." " " "J  4  $Z Z!ZZ sCx.Z e_	Z
 Z Z Z Z Z8    r   