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NamedTuple)Optional)Union)compat)
get_logger)cached)	StopWatchz
git.branchzgit.commit.shazgit.commit.head.shazgit.commit.head.messagezgit.commit.head.author.datezgit.commit.head.author.emailzgit.commit.head.author.namezgit.commit.head.committer.datezgit.commit.head.committer.emailzgit.commit.head.committer.namezgit.repository_urlzgit.tagzgit.commit.author.namezgit.commit.author.emailzgit.commit.author.datezgit.commit.committer.namezgit.commit.committer.emailzgit.commit.committer.datezgit.commit.messagepython_main_packagez^refs/(heads/)?z^origin/z^tags/_GitSubprocessDetailsstdoutstderrduration
returncodenamereturnc              
   C   s(   | d urt dtdtd| S d S )N )_RE_TAGSsub
_RE_ORIGIN_RE_REFS)r    r   C/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/ext/git.pynormalize_refb   s   (r   refc                 C   s   | rd| v S dS )Nztags/Fr   )r   r   r   r   is_ref_a_tagf      r   executable_namec                 C   s   t | tjdS )zReturn the path to an executable.

    NOTE: cached() requires an argument which is why executable_name is passed in, even though it's really only ever
    used to find the git executable at this point.
    )mode)r   osX_OK)r    r   r   r   _get_executable_pathj   s   r$   cwdstd_incmdr&   r'   c                 G   s   t d}|du rtd|g}|tt||  td| t }tj	|tj
tj
tj
| d}|j|d\}}W d   n1 sBw   Y  tt| t| | d |jS )zHelper for invoking the git CLI binary

    Returns a tuple containing:
        - a str representation of stdout
        - a str representation of stderr
        - the time it took to execute the command, in milliseconds
        - the exit code
    gitNzGit executable not foundzExecuting git command: %s)r   r   stdinr&   )inputi  )r$   FileNotFoundErrorextend_add_safe_directory_overridelistlogdebugr   
subprocessPopenPIPEcommunicater   r	   ensure_textstripelapsedr   )r&   r'   r(   git_cmd	stopwatchprocessr   r   r   r   r    _git_subprocess_cmd_with_detailst   s$   
r<   c                 C   s@   t | tr
| d} t| |dd\}}}}|dkr|S t|)z'Helper for invoking the git CLI binary. Nr%   r   )
isinstancestrsplitr<   
ValueError)r(   r&   r'   r   r   _r   r   r   r   _git_subprocess_cmd   s   

rC   pathc                 C   sB   t jt j| do t jt j| do t jt j| dS )NHEADobjectsrefs)r"   rD   isfilejoinisdirrD   r   r   r   _is_bare_git_root   s
   rL      )maxsize	start_dirc                 C   s   t j| }td| |rNt j|d}t j|s!t j|r)td| |S t|r5td| |S t j	|}||krJtd t j| S |}|st j| S )zCached implementation of git root resolution.

    Args:
        start_dir: Absolute path to start searching from

    Returns:
        The git repository root path
    z"Finding git repository root for %sz.gitzGit repository root found as %sz$Bare git repository root found as %szLNo .git found for repository root, defaulting to original starting directory)
r"   rD   realpathr0   r1   rI   rJ   rH   rL   dirname)rO   current
git_markerparentr   r   r   _resolve_git_root_cached   s"   

rU   c                 C   s   t j| pt  }t|S )zResolve the git repository root from a given directory.

    Args:
        cwd: Directory to start searching from, or None to use current directory

    Returns:
        The git repository root path
    )r"   rD   abspathgetcwdrU   )r&   rO   r   r   r   _resolve_git_root   s   
rX   c                 C   s   t |}dd|g| S )Nz-czsafe.directory={0})rX   format)r(   r&   rootr   r   r   r.      s   r.   c                 C      t ddddd| dS )Nconfigz	--defaultorigin--getzclone.defaultRemoteNamer&   r<   r_   r   r   r   -_extract_clone_defaultremotename_with_details   s   ra   c                 C      t d| d}|S )Nzrev-parse @{upstream}r_   rC   )r&   outputr   r   r   _extract_upstream_sha   s   re   c                 C   s,   t dd| d\}}}}| dk}|||fS )Nz	rev-parsez--is-shallow-repositoryr_   true)r<   r7   )r&   r   rB   r   r   
is_shallowr   r   r   #_is_shallow_repository_with_details   s   
rh   c                 C   s   t | jS N)r"   statst_devrK   r   r   r   _get_device_for_path   s   rl   Freporefspecparent_onlyc                 C   sJ   d|rdndddddg}|d ur| | |d ur| | t|d| iS )	Nfetchz
--deepen=1z--shallow-since="1 month ago"z--update-shallow--filter=blob:nonez--recurse-submodules=noz	--no-tagsr&   )appendr<   )r&   rm   rn   ro   r(   r   r   r   "_unshallow_repository_with_details   s   


rs   c                 C   s   t | ||| d S ri   )rs   r&   rm   rn   ro   r   r   r   _unshallow_repository   s   ru   
commit_shac           
      C   sL   d}|r
|d| 7 }t || d}|d\}}}}}}	|||f|||	fdS )zhExtract commit author info from the git repository in the current directory or one specified by ``cwd``.zTshow -s --format=%an|||%ae|||%ad|||%cn|||%ce|||%cd --date=format:%Y-%m-%dT%H:%M:%S%zr=   )r(   r&   z|||)author	committer)rC   r@   )
r&   rv   r(   r   author_nameauthor_emailauthor_datecommitter_namecommitter_emailcommitter_dater   r   r   extract_user_info	  s   r   c                 C   sP   t d}ztdd | d dD }W |S  ty'   td| Y dS w )Nz	--versionc                 S   s   g | ]}t |qS r   )int).0partr   r   r   
<listcomp>      z'extract_git_version.<locals>.<listcomp>   .zIGit version not found, it is not following the desired version format: %s)r   r   r   )rC   tupler@   rA   r0   error)r&   rd   version_infor   r   r   extract_git_version  s   "r   c                 C   s   t ddd| dS )Nr\   r^   zremote.origin.urlr_   r`   r_   r   r   r    _extract_remote_url_with_details!  r   r   c                 C   &   t | d\}}}}|dkr|S t|)Nr_   r   )r   rA   )r&   
remote_urlr   rB   r   r   r   r   extract_remote_url%  s   r   c                 C   r[   )Nr0   z--format=%Hz-n1000--since="1 month ago"r_   r`   r_   r   r   r   $_extract_latest_commits_with_details,  s   r   c                 C   s4   t | d\}}}}|dkr|r|dS g S t|)Nr_   r   
)r   r@   rA   )r&   latest_commitsr   rB   r   r   r   r   extract_latest_commits0  s   r   c                 C   s   t | |dd S )N)excluded_commit_shasr&   r   _get_rev_list_with_details)commit_shasr&   r   r   r   get_rev_list_excluding_commits7  r   r   r   included_commit_shasc                 C   sz   g d}t |ddkr|d |d |d | r(dd | D }|| |r6d	d |D }|| t|d
|iS )N)zrev-listz	--objectsrq   r_   )r      r   r   z--no-object-namesrE   c                 S      g | ]}d | qS )z^%sr   r   shar   r   r   r   F  r   z._get_rev_list_with_details.<locals>.<listcomp>c                 S   r   )z%sr   r   r   r   r   r   I  r   r&   )r   rr   r-   r<   )r   r   r&   command
exclusions
inclusionsr   r   r   r   ;  s   




r   c                 C   s   t | ||dd S )Nr   r   r&   r   r   r   r   r   r   _get_rev_listN  s
   r   c                 C   s   t dd| dS )hExtract the repository url from the git repository in the current directory or one specified by ``cwd``.z	ls-remotez	--get-urlr_   r`   r_   r   r   r   $_extract_repository_url_with_detailsX  s   r   c                 C   r   )r   r_   r   )r   rA   )r&   r   r   rB   r   r   r   r   extract_repository_url^  s   r   c                 C   rb   )zhExtract git commit message from the git repository in the current directory or one specified by ``cwd``.zshow -s --format=%sr_   rc   )r&   commit_messager   r   r   extract_commit_messagef  s   r   c                 C   rb   )zmExtract the root directory path from the git repository in the current directory or one specified by ``cwd``.zrev-parse --show-toplevelr_   rc   )r&   workspace_pathr   r   r   extract_workspace_pathm     r   c                 C   rb   )z`Extract git branch from the git repository in the current directory or one specified by ``cwd``.zrev-parse --abbrev-ref HEADr_   rc   )r&   branchr   r   r   extract_branchs  r   r   c                 C   rb   )zdExtract git commit SHA from the git repository in the current directory or one specified by ``cwd``.zrev-parse HEADr_   rc   r&   rv   r   r   r   extract_commit_shay  r   r   head_commit_shac           	   
   C   s  i }t |d^}}|rt|d d dd zEt|| d}|d d |t< |d d |t< |d d |t< |d	 d |t< |d	 d |t< |d	 d |t< t	d

d| f||t< W |S  tyg   td Y |S  ty } zttj}t|}tjd||d W Y d }~|S d }~ww )Nr_   Trt   r   rw   r      r   rx   r=   zlog -n 1 --format=%B6Git executable not found, cannot extract git metadata.!Error extracting git metadata: %sexc_info)rh   ru   r   COMMIT_HEAD_AUTHOR_NAMECOMMIT_HEAD_AUTHOR_EMAILCOMMIT_HEAD_AUTHOR_DATECOMMIT_HEAD_COMMITTER_NAMECOMMIT_HEAD_COMMITTER_EMAILCOMMIT_HEAD_COMMITTER_DATErC   rI   COMMIT_HEAD_MESSAGEGitNotFoundErrorr0   r   rA   isEnabledForloggingDEBUGr?   )	r   r&   tagsrg   rB   userse
debug_moder   r   r   r   extract_git_head_metadata  s0   r   c              
   C   s  i }zTt | d|t< t| d|t< t| d}|d d |t< |d d |t< |d d |t< |d d |t< |d d |t	< |d d |t
< t| d|t< t| d|t< W |S  tye   td Y |S  ty } zttj}t|}tjd||d	 W Y d
}~|S d
}~ww )zExtract git commit metadata.r_   rw   r   r   r   rx   r   r   r   N)r   REPOSITORY_URLr   COMMIT_MESSAGEr   COMMIT_AUTHOR_NAMECOMMIT_AUTHOR_EMAILCOMMIT_AUTHOR_DATECOMMIT_COMMITTER_NAMECOMMIT_COMMITTER_EMAILCOMMIT_COMMITTER_DATEr   BRANCHr   
COMMIT_SHAr   r0   r   rA   r   r   r   r?   )r&   r   r   r   r   r   r   r   r   extract_git_metadata  s0   
r   envc                 C   s   | du rt jn| } t| d}t| d}t| dr"|}d}i }| d|t< | d|t< ||t< ||t< | d|t	< | d|t
< | d|t< | d	|t< | d
|t< | d|t< | d|t< |S )z8Extract git commit metadata from user-provided env vars.NDD_GIT_BRANCH
DD_GIT_TAGDD_GIT_REPOSITORY_URLDD_GIT_COMMIT_SHADD_GIT_COMMIT_MESSAGEDD_GIT_COMMIT_AUTHOR_DATEDD_GIT_COMMIT_AUTHOR_EMAILDD_GIT_COMMIT_AUTHOR_NAMEDD_GIT_COMMIT_COMMITTER_DATEDD_GIT_COMMIT_COMMITTER_EMAILDD_GIT_COMMIT_COMMITTER_NAME)r"   environr   getr   r   r   r   TAGr   r   r   r   r   r   r   )r   r   tagr   r   r   r   extract_user_git_metadata  s&   r   T	revisionsuse_tempdirc              
   c   s    t tdd}|r|nt }t }t|t|jkr!|j}n
t	d|j| |}dj
||d}t	d| z&tddd	||| d
d}||fV  W t|tr]t	d| |  d S d S t|trot	d| |  w w )Nr   i@B z=tempdir %s and cwd %s are on different filesystems, using cwdz{basepath}/{basename})basepathbasenamez%Building packfiles in prefix path: %szpack-objectsz--compression=9z--max-pack-size=3mzutf-8r%   z#Cleaning up temporary directory: %s)r?   randomrandintr"   rW   r   rl   r   r0   r1   rY   r<   encoder>   cleanup)r   r&   r   r   tempdirr   prefixprocess_detailsr   r   r   !_build_git_packfiles_with_details  s6   

r   c                 c   sf    t | |d"\}}|jdkr|V  	 W d    d S td|j|j t|j1 s,w   Y  d S )Nr_   r   z:Failed to pack objects, command return code: %s, error: %s)r   r   r0   r1   r   rA   )r   r&   r   r   r   r   r   build_git_packfiles  s   

r   )NNri   )NNNF)NNN)NT)b__doc__
contextlibr   r"   r   reshutilr   r2   tempfiler   typingr   r   r   r   r   ddtrace.internalr	   ddtrace.internal.loggerr
   ddtrace.internal.utils.cacher   ddtrace.internal.utils.timer   r,   r   r   r   COMMIT_HEAD_SHAr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   MAIN_PACKAGEcompiler   r   r   __name__r0   r?   floatr   r   r   boolr   r$   bytesr<   r/   rC   rL   rU   rX   r.   ra   re   r   rh   rl   rs   ru   dictr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   contextmanagerr   r   r   r   r   r   <module>   s   




0!""

2	










($," "