a
    yn„fÂ  ã                   @   s<   d dl Z d dlZd dlmZ ddlmZ G dd„ deƒZdS )é    N)ÚAbstractFileSystemé   )Ú
MemoryFilec                       sl   e Zd ZdZdZdZd‡ fdd„	Ze‡ fdd„ƒZd	d
„ Z	e
dd„ ƒZddd„Zddd„Zddd„Z‡  ZS )ÚGitFileSystemz\Browse the files of a local git repo at any hash/tag/branch

    (experimental backend)
    Ú TNc                    s8   t ƒ jf i |¤Ž t |p$|p$t ¡ ¡| _|p0d| _dS )aM  

        Parameters
        ----------
        path: str (optional)
            Local location of the repo (uses current directory if not given).
            May be deprecated in favour of ``fo``. When used with a higher
            level function such as fsspec.open(), may be of the form
            "git://[path-to-repo[:]][ref@]path/to/file" (but the actual
            file path should not contain "@" or ":").
        fo: str (optional)
            Same as ``path``, but passed as part of a chained URL. This one
            takes precedence if both are given.
        ref: str (optional)
            Reference to work with, could be a hash, tag or branch name. Defaults
            to current working tree. Note that ``ls`` and ``open`` also take hash,
            so this becomes the default for those operations
        kwargs
        ZmasterN)ÚsuperÚ__init__Úpygit2Z
RepositoryÚosÚgetcwdÚrepoÚref)ÚselfÚpathÚfor   Úkwargs©Ú	__class__© ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/fsspec/implementations/git.pyr      s    zGitFileSystem.__init__c                    sL   t ƒ  |¡ d¡}d|v r*| dd¡d }d|v rB| dd¡d }| d¡S )Nú/ú:r   ú@)r   Ú_strip_protocolÚlstripÚsplit)Úclsr   r   r   r   r   +   s    zGitFileSystem._strip_protocolc                 C   sL   | j  |p| j¡\}}| d¡}|j}|D ]}|r*t|tjƒr*|| }q*|S )Nr   )r   Zresolve_refishr   r   ÚtreeÚ
isinstancer	   ÚTree)r   r   r   ZcommÚpartsr   Úpartr   r   r   Ú_path_to_object4   s    

zGitFileSystem._path_to_objectc                 C   sV   |   d¡r| dd … } i }d| v r6|  dd¡\|d< } d| v rR|  dd¡\|d< } |S )Nzgit://é   r   r   r   r   r   )Ú
startswithr   )r   Úoutr   r   r   Ú_get_kwargs_from_urls=   s    
z#GitFileSystem._get_kwargs_from_urlsc              	   K   sÚ   |   |¡}|  ||¡}t|tjƒr¢g }|D ]t}t|tjƒrl| dd ||jg¡ d¡|j	|j
d›ddœ¡ q*| dd ||jg¡ d¡|j	|j
d›|jdœ¡ q*n"|}d|j|j	|j
d›|jdœg}|rÌ|S dd„ |D ƒS )	NÚ	directoryr   Úor   )ÚtypeÚnameÚhexÚmodeÚsizeÚfilec                 S   s   g | ]}|d  ‘qS )r*   r   )Ú.0r(   r   r   r   Ú
<listcomp>o   ó    z$GitFileSystem.ls.<locals>.<listcomp>)r   r"   r   r	   r   ÚappendÚjoinr*   r   r+   Úfilemoder-   )r   r   Údetailr   r   r   r%   Úobjr   r   r   ÚlsH   sB    
ûÿ
ûÿ
ûÿ	zGitFileSystem.lsc                 C   s   | j ||d�d S )N)r   r+   )Úinfo)r   r   r   r   r   r   Úukeyq   s    zGitFileSystem.ukeyÚrbc           	      K   s   |   ||p| j¡}t|jd�S )N)Údata)r"   r   r   r;   )	r   r   r,   Ú
block_sizeZ
autocommitZcache_optionsr   r   r6   r   r   r   Ú_opent   s    
zGitFileSystem._open)NNN)TN)N)r:   NTNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zroot_markerZcachabler   Úclassmethodr   r"   Ústaticmethodr&   r7   r9   r=   Ú__classcell__r   r   r   r   r   
   s"   	


)
     ùr   )r
   r	   Zfsspec.specr   Zmemoryr   r   r   r   r   r   Ú<module>   s   