
    v-j1                    X   d dl mZ d dlZd dlmZ d dlmZ  G d d          Zedk    r~d dl	Z	 e            Z
e
j        rk e	j                    Z ede
j         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 )    )annotationsN)cached_property)Pathc                  T   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ed!d            Zd"dZd#dZe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.
        refdir (Path | None): Directory containing shared refs, objects, and config; 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.
        message (str | None): Current commit subject from a loose object; 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, config, and objects; 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| _        |                     | j                  | _        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_refdirrefdir)selfr   s     U/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/ultralytics/utils/git.py__init__zGitRepo.__init__+   sQ     OOD))	15Ddll49---ll4;//    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>8   s7      OO1!f*9L9L9N9NOQOOOOOOr   N)nextlistparentsr   s    r   r
   zGitRepo._find_root5   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._gitdir:   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   r   c                    | r| dz  nd}t                               |          x}r<t          |          }|                                s| |z                                  n|S | S )z7Resolve directory containing refs, objects, and config.	commondirN)r   _readr   is_absoluter.   )r   r   sr   s       r   r   zGitRepo._refdirF   sj     %+4F[  a   1 	HQA12GFQJ'')))aGr   
str | Nonec                    | r<|                                  r(|                     d                                          ndS )zRead and strip file if exists.r#   r$   N)r   r*   r+   r!   s    r   r3   zGitRepo._readO   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)r3   r   r   s    r   headzGitRepo.headT   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   r3   r   
read_bytes
splitlinesencoder-   r+   decode)
r   r<   rfr5   pfbtgtlineshanames
             r   _ref_commitzGitRepo._ref_commitY   s    [3

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

4++ICzz||s""zz||### #tr   commitc                   | j         dz  |dd         z  |dd         z  }|                                sdS t          j        |                                          }d|vrdS |                    dd          \  }}|                    d          rd|vrdS |                    dd          d                                         d                             d	
          	                                }|pdS )z)Commit subject from loose object or None.objectsN       r'   s   commit s   

r   replacer$   )
r   r   zlib
decompressrB   r-   r,   rC   rE   r+   )r   rN   objdatakindbodysubjects          r   _commit_subjectzGitRepo._commit_subjecti   s    kI%rr
2VABBZ?zz|| 	4s~~//004ZZq))
dz** 	gT.A.A4**Wa((+6688;BB)BTTZZ\\$r   boolc                    | j         duS )zTrue if inside a git repo.N)r   r:   s    r   is_repozGitRepo.is_repow   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/)r^   r;   r,   r+   len)r   r<   s     r   branchzGitRepo.branch|   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.Nr`   ra   )r^   r;   r,   rM   r+   r:   s    r   rN   zGitRepo.commit   sd     | 	49 	4:>):N:Nw:W:Wft	!"" 3 3 5 5666]a]ffr   c                V    | j         r| j        sdS |                     | j                  S )zCurrent commit subject or None.N)r^   rN   r[   r:   s    r   messagezGitRepo.message   s2     | 	4; 	4##DK000r   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'   )	r^   r   r3   rC   r+   r,   endswithlowerr-   )r   cfgremoteurlr5   r0   s         r   originzGitRepo.origin   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   r   r6   )r   r6   )r<   r=   r   r6   )rN   r=   r   r6   )r   r\   )__name__
__module____qualname____doc__r   __file__r.   r   staticmethodr
   r   r   r3   r   r;   rM   r[   propertyr^   rc   rN   rf   rr    r   r   r   r   
   s        @ %)DNN$:$:$<$< 0 0 0 0 0 W W W \W 	 	 	 \	    \ R R R \R I I I _I        ' ' ' X' S S S _S g g g _g 1 1 1 _1    _  r   r   __main__zrepo=z
branch=z
commit=z	
message=z
origin=i  u   
⏱️ Profiling: total z.3fz ms)
__future__r   rT   	functoolsr   pathlibr   r   rs   timer/   r^   perf_countert0printr   rc   rN   rf   rr   dtrz   r   r   <module>r      sH   # " " " " "  % % % % % %      V V V V V V V Vr zKKK		Ay 8T  lafllqxll!(llaillbcbjllmmmd!!B&$.62666677777 8 8r   