o
    Wi                     @  sn  d dl mZ d dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZ d dl
mZ 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 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 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" G dd deZ#G dd de$Z%d<ddZ&d=d&d'Z'e(d(Z)	)d>d?d0d1Z*d@d2d3Z+	4	5dAdBd:d;Z,dS )C    )annotationsN)Sequence)Any)
NamedTuple)git)output)xargs)InvalidManifestError)load_config)load_manifest)LOCAL)META)migrate_config)CalledProcessError)
cmd_output)cmd_output_b)	yaml_dump)	yaml_loadc                   @  sP   e Zd ZU ded< ded< dZded< e Zded< edddZdddZ	dS )RevInfostrreporevNz
str | Nonefrozenzfrozenset[str]hook_idsconfigdict[str, Any]returnc                 C  s   | |d |d S )Nr   r    )clsr   r   r   R/home/ubuntu/.local/lib/python3.10/site-packages/pre_commit/commands/autoupdate.pyfrom_config"   s   zRevInfo.from_config	tags_onlyboolfreezec                 C  s  t  }dgtjd|R }|rd}nd}g |ddd|R }t|| j tg |dd	d
R   tg |ddddddR   z
t| d  }W n t	ym   tg |ddR  d  }Y n	w |rvt
||}d }|rtg |d|R  d  }	|	|kr|	|}}z#tg |d| dtj R   tg |d|dtjR   W n	 t	y   Y nw zttj|tj}
W n ty } ztd| j d| d }~ww tdd |
D }W d    n1 sw   Y  | j|||dS )Nr   z-Cz
--abbrev=0z--exactdescribe
FETCH_HEADz--tagsr   zextensions.partialClonetruefetchoriginHEADz--quietz--filter=blob:none   z	rev-parseshow:checkoutz--[] c                 s  s    | ]}|d  V  qdS )idNr   .0hookr   r   r   	<genexpr>P   s    z!RevInfo.update.<locals>.<genexpr>)r   r   r   )tempfileTemporaryDirectoryr   NO_FS_MONITOR	init_repor   r   r   stripr   get_best_candidate_tagCMANIFEST_FILEr   ospathjoinr	   RepositoryCannotBeUpdatedError	frozenset_replace)selfr!   r#   tmp_gittag_opttag_cmdr   r   exactmanifester   r   r   r   update&   sd   
"
"$+zRevInfo.update)r   r   r   r   )r!   r"   r#   r"   r   r   )
__name__
__module____qualname____annotations__r   rA   r   classmethodr    rK   r   r   r   r   r      s   
 r   c                   @  s   e Zd ZdS )r@   N)rL   rM   rN   r   r   r   r   r@   U   s    r@   repo_configr   infor   Nonec                 C  sD   dd | d D }||j  }|r td|j ddt| d S )Nc                 S  s   h | ]}|d  qS )r0   r   r1   r   r   r   	<setcomp>^   s    z2_check_hooks_still_exist_at_rev.<locals>.<setcomp>hooksr.   zB] Cannot update because the update target is missing these hooks: z, )r   r@   r   r?   sorted)rQ   rR   rU   hooks_missingr   r   r   _check_hooks_still_exist_at_revY   s   

rX   iintr   r!   r"   r#   tuple[int, RevInfo, RevInfo]c                C  s,   t |}|j||d}t|| | ||fS )Nr!   r#   )r   r    rK   rX   )rY   r   r!   r#   oldnewr   r   r   _update_oneg   s   


r_   z,^(\s+)rev:(\s*)([\'"]?)([^\s#]+)(.*)(\r?\n)$Fr>   r   	rev_infoslist[RevInfo | None]retrytuple[list[str], list[int]]c                 C  s   t | dd}| }W d   n1 sw   Y  |d}dd t|D }t|t|kr4||fS |r:tdt | d}|tt| W d   n1 sSw   Y  t	| |dd	S )
z(detect `rev:` lines or reformat the file newlineNTc                 S  s   g | ]\}}t |r|qS r   )REV_LINE_REmatch)r2   rY   liner   r   r   
<listcomp>   s    z#_original_lines.<locals>.<listcomp>zcould not find rev linesw)rb   )
openread
splitlines	enumeratelenAssertionErrorwriter   r   _original_lines)r>   r`   rb   foriginallinesidxsr   r   r   rs   w   s   

rs   c                 C  s  t | |\}}t||D ]\\}}|d u rqt|| }|d us"J td|ji|d d}|ddd  }|jd urCd|j }	n|d  	drOd	}	n|d }	|d  d
|d  | |	 |d  ||< qt
| dd	d}
|
d	| W d    d S 1 sw   Y  d S )Nr      )default_styler,   r*   z  # frozen:    z	# frozen:rd   zrev:      rk   re   )rs   ziprg   rh   r   r   splitr9   r   
startswithrl   rr   r?   )r>   r`   rv   rw   idxrev_inforh   	new_rev_snew_revcommentrt   r   r   r   _write_new_config   s"   
,"r   r   r*   config_filereposSequence[str]jobsc                   s|  t | dd d}d}dd t| d D }dgt| }|p!t }t|tp+t|}t|d	}tj	|u  fd
dt
|D }	tj|	D ]X}
z	|
 \}}}W n tys } ztt| d	}W Y d}~qMd}~ww |j|jkrd}|jr|j d}n|j}d|j d| }|||< nd}td|j d|  qMW d   n1 sw   Y  |rt| | |S )zBAuto-update the pre-commit config to the latest versions of repos.T)quietFr   c                 S  s    g | ]}|d  t thvr|qS )r   )r   r   )r2   r   r   r   r   rj      s
    zautoupdate.<locals>.<listcomp>r   Nr*   c              	     s4   g | ]\}}r|d  v r j t||dqS )r   r\   )submitr_   )r2   rY   r   exer#   r   r!   r   r   rj      s    z	 (frozen)z	updating z -> zalready up to date!r.   r/   )r   r
   rp   r   	cpu_countminmax
concurrentfuturesThreadPoolExecutorro   as_completedresultr@   r   
write_liner   r   r   r   r   )r   r!   r#   r   r   changedretvconfig_reposr`   r   futurerY   r]   r^   rJ   new_smsgr   r   r   
autoupdate   sH   



r   )rQ   r   rR   r   r   rS   )
rY   rZ   r   r   r!   r"   r#   r"   r   r[   )F)r>   r   r`   ra   rb   r"   r   rc   )r>   r   r`   ra   r   rS   )r   r*   )r   r   r!   r"   r#   r"   r   r   r   rZ   r   rZ   )-
__future__r   concurrent.futuresr   os.pathr=   rer5   collections.abcr   typingr   r   pre_commit.constants	constantsr;   
pre_commitr   r   r   pre_commit.clientlibr	   r
   r   r   r   "pre_commit.commands.migrate_configr   pre_commit.utilr   r   r   pre_commit.yamlr   r   r   RuntimeErrorr@   rX   r_   compilerg   rs   r   r   r   r   r   r   <module>   sD    9



