a
    a:                     @   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
 ee edddZee ee dd	d
ZdeeeeedddZdS )zDefines a git hook to allow pre-commit warnings and errors about import order.

usage:
    exit_code = git_hook(strict=True|False, modify=True|False)
    N)Path)List)Configapi
exceptions)commandreturnc                 C   s   t j| t jdd}|j S )zRun a command and return raw output

    :param str command: the command to run
    :returns: the stdout output of the command
    T)stdoutcheck)
subprocessrunPIPEr	   decode)r   result r   *lib/python3.9/site-packages/isort/hooks.py
get_output   s    r   c                 C   s   t | }dd | D S )zRun a command and return lines of output

    :param str command: the command to run
    :returns: list of whitespace-stripped lines output by command
    c                 S   s   g | ]}|  qS r   )strip).0liner   r   r   
<listcomp>       zget_lines.<locals>.<listcomp>)r   
splitlines)r   r	   r   r   r   	get_lines   s    r   F )strictmodifylazysettings_filer   c              	   C   s   g d}|r| d t|}|s&dS d}t|tjtj|d d}|D ]n}|drNddd| g}	t|	}
z2t	j
|
t||d	s|d
7 }|rt	j||d W qN tjy   Y qN0 qN| r|S dS )a  Git pre-commit hook to check staged files for isort errors

    :param bool strict - if True, return number of errors on exit,
        causing the hook to fail. If False, return zero so it will
        just act as a warning.
    :param bool modify - if True, fix the sources if they are not
        sorted properly. If False, only report result without
        modifying anything.
    :param bool lazy - if True, also check/fix unstaged files.
        This is useful if you frequently use ``git commit -a`` for example.
        If False, ony check/fix the staged files for isort errors.
    :param str settings_file - A path to a file to be used as
                               the configuration file for this run.
        When settings_file is the empty string, the configuration file
        will be searched starting at the directory containing the first
        staged file, if any, and going upward in the directory structure.

    :return number of errors if in strict mode, 0 otherwise.
    )gitz
diff-index--cachedz--name-onlyz--diff-filter=ACMRTUXBZHEADr    r   )r   Zsettings_pathz.pyr   Zshow:)Z	file_pathconfig   )r"   )remover   r   ospathdirnameabspathendswithr   r   Zcheck_code_stringr   Z	sort_filer   ZFileSkipped)r   r   r   r   Zdiff_cmdZfiles_modifiederrorsr"   filenameZ
staged_cmdZstaged_contentsr   r   r   git_hook"   s2    


r,   )FFFr   )__doc__r%   r   pathlibr   typingr   Zisortr   r   r   strr   r   boolintr,   r   r   r   r   <module>   s   
 