
    /j}              
      @   d dl mZ d dlmZ d dlmZ  G d d          Zedk    rvd dlZ e            Z	e	j
        rc ej                    Z ede	j         d	e	j         d
e	j         de	j                     ej                    ez
  dz  Z ededd           dS dS dS )    )annotations)cached_property)Pathc                     e Zd ZdZ ee                                          fddZedd            Z	edd
            Z
edd            Zedd            ZddZedd            Zedd            Zedd            Zedd            ZdS )GitRepoa  Represent a local Git repository and expose branch, commit, and remote metadata.

    This class discovers the repository root by searching for a .git entry from the given path upward, resolves the
    actual .git directory (including worktrees), and reads Git metadata directly from on-disk files. It does not invoke
    the git binary and therefore works in restricted environments. All metadata properties are resolved lazily and
    cached; construct a new instance to refresh state.

    Attributes:
        root (Path | None): Repository root directory containing the .git entry; None if not in a repository.
        gitdir (Path | None): Resolved .git directory path; handles worktrees; None if unresolved.
        head (str | None): Raw contents of HEAD; a SHA for detached HEAD or "ref: <refname>" for branch heads.
        is_repo (bool): Whether the provided path resides inside a Git repository.
        branch (str | None): Current branch name when HEAD points to a branch; None for detached HEAD or non-repo.
        commit (str | None): Current commit SHA for HEAD; None if not determinable.
        origin (str | None): URL of the "origin" remote as read from gitdir/config; None if unset or unavailable.

    Examples:
        Initialize from the current working directory and read metadata
        >>> from pathlib import Path
        >>> repo = GitRepo(Path.cwd())
        >>> repo.is_repo
        True
        >>> repo.branch, repo.commit[:7], repo.origin
        ('main', '1a2b3c4', 'https://example.com/owner/repo.git')

    Notes:
        - Resolves metadata by reading files: HEAD, packed-refs, and config; no subprocess calls are used.
        - Caches properties on first access using cached_property; recreate the object to reflect repository changes.
    pathr   c                    |                      |          | _        | j        r|                     | j                  nd| _        dS )zInitialize a Git repository context by discovering the repository root from a starting path.

        Args:
            path (Path, optional): File or directory path used as the starting point to locate the repository root.
        N)
_find_rootroot_gitdirgitdir)selfr   s     Z/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/utils/git.py__init__zGitRepo.__init__(   s<     OOD))	15Ddll49---    preturnPath | Nonec                `    t          d | gt          | j                  D             d          S )zReturn repo root or None.c              3  H   K   | ]}|d z                                   |V  dS ).gitN)exists).0ds     r   	<genexpr>z%GitRepo._find_root.<locals>.<genexpr>4   s7      OO1!f*9L9L9N9NOQOOOOOOr   N)nextlistparentsr   s    r   r
   zGitRepo._find_root1   s4     OO 5T!)__ 5OOOQUVVVr   r   c                d   | dz  }|                                 r|S |                                r|                    d                                          }|                    d          rC| |                    dd          d                                         z                                  S dS )z2Resolve actual .git directory (handles worktrees).r   ignoreerrorszgitdir::   N)is_diris_file	read_textstrip
startswithsplitresolve)r   gts      r   r   zGitRepo._gitdir6   s     6M88:: 	H99;; 	E8,,2244A||I&& EqwwsAq177999BBDDDtr   
str | Nonec                    | r<|                                  r(|                     d                                          ndS )zRead and strip file if exists.r!   r"   N)r   r(   r)   r   s    r   _readzGitRepo._readB   s;     89QQXXZZQq{{({++11333TQr   c                N    |                      | j        r
| j        dz  nd          S )zHEAD file contents.HEADN)r1   r   r   s    r   headzGitRepo.headG   s)     zz$+G$+..4HHHr   refstrc                   | j         |z  }|                     |          x}r|S | j         dz  }|                                r&|                                                                ng }|                                }|D ]Z}|dd         dv sd|vr|                    dd          \  }}	|	                                |k    r|                                c S [dS )z%Commit for ref (handles packed-refs).zpacked-refsNr%   )   #   ^    )	r   r1   r   
read_bytes
splitlinesencoder+   r)   decode)
r   r6   rfspfbtgtlineshanames
             r   _ref_commitzGitRepo._ref_commitL   s    [3

21 	H[=(,.IIKK?BMMOO&&(((Rjjll 	$ 	$DBQBx<''4t+;+;

4++ICzz||s""zz||### #tr   boolc                    | j         duS )zTrue if inside a git repo.N)r   r4   s    r   is_repozGitRepo.is_repo\   s     {$&&r   c                    | j         r!| j        r| j                            d          sdS | j        dd                                         }|                    d          r|t	          d          d         n|S )zCurrent branch or None.ref: N   zrefs/heads/)rK   r5   r*   r)   len)r   r6   s     r   branchzGitRepo.brancha   s}     | 	49 	DI4H4H4Q4Q 	4im!!##,/NN=,I,IRs3}%%''((sRr   c                    | j         r| j        sdS | j                            d          r4|                     | j        dd                                                   n| j        S )zCurrent commit SHA or None.NrM   rN   )rK   r5   r*   rH   r)   r4   s    r   commitzGitRepo.commiti   sd     | 	49 	4:>):N:Nw:W:Wft	!"" 3 3 5 5666]a]ffr   c                   | j         sdS | j        dz  }d\  }}|                     |          pd                                D ]}|                                }|                    d          r*|                    d          r|                                }U|                                                    d          r6|dk    r0|                    d	d
          d
                                         } n|S )zOrigin URL or None.Nconfig)NN []zurl =z[remote "origin"]=r%   )	rK   r   r1   r=   r)   r*   endswithlowerr+   )r   cfgremoteurlrA   r.   s         r   originzGitRepo.originp   s     | 	4kH$ **S//'R3355 	 	A		A||C   QZZ__ %%g.. 6=P3P3Pggc1ooa(..00
r   N)r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r/   )r   r/   )r6   r7   r   r/   )r   rI   )__name__
__module____qualname____doc__r   __file__r,   r   staticmethodr
   r   r1   r   r5   rH   propertyrK   rP   rR   r^    r   r   r   r   	   s}        < %)DNN$:$:$<$< E E E E E W W W \W 	 	 	 \	 R R R \R I I I _I     ' ' ' X' S S S _S g g g _g    _  r   r   __main__Nzrepo=z
branch=z
commit=z
origin=i  u   
⏱️ Profiling: total z.3fz ms)
__future__r   	functoolsr   pathlibr   r   r_   timer-   rK   perf_countert0printr   rP   rR   r^   dtrf   r   r   <module>rp      s3   # " " " " " % % % % % %      u u u u u u u up zKKK		Ay 8T  WafWWqxWW!(WWQXWWXXXd!!B&$.62666677777 8 8r   