o
    Uh-                  	   @   s  d Z ddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZ e	r*ddlmZ ddlmZmZ d	ed
efddZded
efddZde
d
ee fddZd$dededed
efddZG dd dZG dd dZG dd dZded
efd d!ZG d"d# d#ZdS )%zhParsing of gitignore files.

For details for the matching rules, see https://git-scm.com/docs/gitignore
    N)Iterable)suppress)TYPE_CHECKINGBinaryIOOptionalUnion   )Repo)Configget_xdg_config_home_pathsegmentreturnc                 C   s  | dkrdS d}dt | }}||k r| ||d  }|d }|dkr(|d7 }n|dkr1|d7 }n|d	krG|t| ||d  7 }|d7 }n|d
kr|}||k r_| ||d  dkr_|d }||k rq| ||d  dkrq|d }||k r| ||d  dkr|d }||k r| ||d  dks||kr|d7 }n5| || d	d}|d }|drd|dd   }n	|drd	| }|d
| d 7 }n|t|7 }||k s|S )N   *s   [^/]+    r   r   s   [^/]*   ?s   [^/]   \   [   !   ]s   \[s   \\   ^)lenreescapereplace
startswith)r   resincjstuff r!   b/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/dulwich/ignore.py_translate_segment&   sF   





r#   patc                 C   s   d}d| dd vr|d7 }|  dr| dd } |d7 }|  dr(| dd } t| dD ]\}}|d	kr<|d
7 }q/||dkrFtdndt| 7 }q/| dsW|d7 }|d S )zTranslate a shell PATTERN to a regular expression.

    There is no way to quote meta-characters.

    Originally copied from fnmatch in Python 2.7, but modified for Dulwich
    to cope with features in Git ignore patterns.
    s   (?ms)   /Ns   (.*/)?s   **/   r   s   **s   (/.*)?r   r   s   /?s   \Z)r   	enumeratesplitr   r   r#   endswith)r$   r   r   r   r!   r!   r"   	translateL   s    

$
r+   fc                 c   sv    | D ]5}| d}| sq|drq|dr/|ds/|dd }|dr/|dr|dd}|V  qdS )zlRead a git ignore file.

    Args:
      f: File-like object to read from
    Returns: List of patterns
    s   
   #    s   \ Nr&   )rstripstripr   r*   r   )r,   liner!   r!   r"   read_ignore_patternso   s   

r2   Fpathpattern
ignorecasec                 C   s   t ||| S )zMatch a gitignore-style pattern against a path.

    Args:
      path: Path to match
      pattern: Pattern to match
      ignorecase: Whether to do case-sensitive matching
    Returns:
      bool indicating whether the pattern matched
    )Patternmatch)r3   r4   r5   r!   r!   r"   match_pattern   s   
r8   c                   @   sv   e Zd ZdZddededdfddZdefd	d
ZdefddZ	de
defddZdefddZdedefddZdS )r6   zA single ignore pattern.Fr4   r5   r   Nc                 C   sx   || _ || _|dd dkrd| _|dd  }n|dd dkr&|dd  }d| _d}| jr1tj}tt||| _d S )Nr   r   r   Fr   T)r4   r5   
is_excluder   
IGNORECASEcompiler+   _re)selfr4   r5   flagsr!   r!   r"   __init__   s   zPattern.__init__c                 C   s   | j S N)r4   r=   r!   r!   r"   	__bytes__   s   zPattern.__bytes__c                 C   s   t | jS r@   )osfsdecoder4   rA   r!   r!   r"   __str__   s   zPattern.__str__otherc                 C   s&   t |t| o| j|jko| j|jkS r@   )
isinstancetyper4   r5   )r=   rF   r!   r!   r"   __eq__   s
   

zPattern.__eq__c                 C   s    t | j d| jd| jdS N(z, ))rH   __name__r4   r5   rA   r!   r!   r"   __repr__   s    zPattern.__repr__r3   c                 C   s   t | j|S )zTry to match a path against this ignore pattern.

        Args:
          path: Path to match (relative to ignore location)
        Returns: boolean
        )boolr<   r7   r=   r3   r!   r!   r"   r7      s   zPattern.matchF)rM   
__module____qualname____doc__bytesrO   r?   rB   strrE   objectrI   rN   r7   r!   r!   r!   r"   r6      s    r6   c                   @   s   e Zd Z	ddee deddfddZdeddfd	d
Zdeee	f dee
 fddZdedee fddZeddedd fddZde	fddZdS )IgnoreFilterFNpatternsr5   r   c                 C   s*   g | _ || _|| _|D ]}| | qd S r@   )	_patterns_ignorecase_pathappend_pattern)r=   rY   r5   r3   r4   r!   r!   r"   r?      s   zIgnoreFilter.__init__r4   c                 C   s   | j t|| j dS )zAdd a pattern to the set.N)rZ   appendr6   r[   )r=   r4   r!   r!   r"   r]      s   zIgnoreFilter.append_patternr3   c                 c   s6    t |tst|}| jD ]
}||r|V  qdS )zYield all matching patterns for path.

        Args:
          path: Path to match
        Returns:
          Iterator over iterators
        N)rG   rU   rC   fsencoderZ   r7   )r=   r3   r4   r!   r!   r"   find_matching   s   



zIgnoreFilter.find_matchingc                 C   s   d}|  |D ]}|j}q|S )zCheck whether a path is ignored.

        For directories, include a trailing slash.

        Returns: status is None if file is not mentioned, True if it is
            included, False if it is explicitly excluded.
        N)r`   r9   )r=   r3   statusr4   r!   r!   r"   
is_ignored   s   zIgnoreFilter.is_ignoredc                 C   s@   t |d}| t|||dW  d    S 1 sw   Y  d S )Nrb)r3   )openr2   )clsr3   r5   r,   r!   r!   r"   	from_path   s   $zIgnoreFilter.from_pathc                 C   s<   t | dd }|d urt| j d|dS dt| j dS )Nr\   z.from_path(rL   <>)getattrrH   rM   rP   r!   r!   r"   rN      s   zIgnoreFilter.__repr__)FNrQ   )rM   rR   rS   r   rU   rO   r?   r]   r   rV   r6   r`   r   rb   classmethodrf   rN   r!   r!   r!   r"   rX      s    
	rX   c                   @   s0   e Zd ZdZd	ddZdedee fddZdS )
IgnoreFilterStackz,Check for ignore status in multiple filters.r   Nc                 C   s
   || _ d S r@   )_filters)r=   filtersr!   r!   r"   r?      s   
zIgnoreFilterStack.__init__r3   c                 C   s.   d}| j D ]}||}|dur|  S q|S )Check whether a path is explicitly included or excluded in ignores.

        Args:
          path: Path to check
        Returns:
          None if the file is not mentioned, True if it is included,
          False if it is explicitly excluded.
        N)rl   rb   )r=   r3   ra   filterr!   r!   r"   rb      s   	

zIgnoreFilterStack.is_ignored)r   N)	rM   rR   rS   rT   r?   rV   r   rO   rb   r!   r!   r!   r"   rk      s    
rk   configc                 C   sF   z|  dd}t|tsJ |jddW S  ty   Y nw tddS )zReturn default user ignore filter path.

    Args:
      config: A Config object
    Returns:
      Path to a global ignore file
    )   cores   excludesFilezutf-8)encodinggitignore)getrG   rU   decodeKeyErrorr   )rp   valuer!   r!   r"   default_user_ignore_filter_path  s   
ry   c                   @   s   e Zd ZdZdedee deddfddZdefd	d
Z	dede
e fddZdedee fddZdede
e fddZedddZdS )IgnoreFilterManagerzIgnore file manager.top_pathglobal_filtersr5   r   Nc                 C   s   i | _ || _|| _|| _d S r@   )_path_filters	_top_path_global_filtersr[   )r=   r{   r|   r5   r!   r!   r"   r?   $  s   
zIgnoreFilterManager.__init__c                 C   s(   t | j d| j d| jd| jdS rJ   )rH   rM   r~   r   r[   rA   r!   r!   r"   rN   /  s   (zIgnoreFilterManager.__repr__r3   c                 C   sr   z| j | W S  ty   Y nw tj| j|d}zt|| j| j |< W n t	y3   d | j |< Y nw | j | S )Nz
.gitignore)
r}   rw   rC   r3   joinr~   rX   rf   r[   OSError)r=   r3   pr!   r!   r"   
_load_path2  s   
zIgnoreFilterManager._load_pathc                 C   s   t j|rt| ddd | jD }t jjdkr#|t jjd}|d}g }tt	|d D ]?}d
|d| }|D ] \}}d
||| }	|t	|k rV|	d7 }	|t||	7 }q?| |}
|
durq|d||
f q2t|S )zFind matching patterns for path.

        Args:
          path: Path to check
        Returns:
          Iterator over Pattern instances
        z is an absolute pathc                 S   s   g | ]}d |fqS )r   r!   ).0r,   r!   r!   r"   
<listcomp>I  s    z5IgnoreFilterManager.find_matching.<locals>.<listcomp>/r   Nr   )rC   r3   isabs
ValueErrorr   sepr   r)   ranger   r   listr`   r   insertiter)r=   r3   rm   partsmatchesr   dirnamesr,   relpathignore_filterr!   r!   r"   r`   ?  s&   

z!IgnoreFilterManager.find_matchingc                 C   s    t | |}|r|d jS dS )rn   r&   N)r   r`   r9   )r=   r3   r   r!   r!   r"   rb   \  s   	
zIgnoreFilterManager.is_ignoredrepor	   c              	   C   s   g }t j| ddt| fD ]"}tt |t	
t j| W d   n1 s/w   Y  q| }|ddd}| |j||S )zCreate a IgnoreFilterManager from a repository.

        Args:
          repo: Repository object
        Returns:
          A `IgnoreFilterManager` object
        infoexcludeNrq   s
   ignorecaseF)rC   r3   r   
controldirry   get_config_stackr   r   r^   rX   rf   
expanduserget_boolean)re   r   r|   r   rp   r5   r!   r!   r"   	from_repoj  s   	

zIgnoreFilterManager.from_repo)r   r	   r   rz   )rM   rR   rS   rT   rV   r   rX   rO   r?   rN   r   r   r   r6   r`   rb   rj   r   r!   r!   r!   r"   rz   !  s"    
rz   rQ   )rT   os.pathrC   r   collections.abcr   
contextlibr   typingr   r   r   r   r   r	   rp   r
   r   rU   r#   r+   r2   rO   r8   r6   rX   rk   rV   ry   rz   r!   r!   r!   r"   <module>   s$   &#,6