
    5pj                        S r SSKJr  SSKrSSKrSSKrSSKJr  SSKJ	r	J
r
  SSKJr  SSKJr  \" S	5      r " S
 S\5      r\R"                  " S5      rSS jr " S S5      rg)zMinimal Git CLI wrapper for repository operations.

This module provides thin wrappers around ``git`` subprocess calls,
used by :class:`~._repository.Repository` for clone/pull/push/lfs ops.
    )annotationsN)Path)urlparse
urlunparse   )HubError)
get_loggergitc                  $    \ rS rSrSrSrSrSrSrg)GitError   z8Raised when a git subprocess exits with a non-zero code.E1024Fz@Git operation failed. Please check network and repo permissions. N)	__name__
__module____qualname____firstlineno____doc__
error_code	retryable
suggestion__static_attributes__r       O/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/modelscope_hub/_git.pyr   r      s    BJISJr   r   zhttps?://\S+c                    [        U 5      nUR                  (       d  UR                  (       aH  [        UR	                  SUR
                   3UR                  (       a  SUR                   3OS-   S95      $ U $ )zGStrip userinfo from a single URL using urlparse (handles `@` in creds).z***@: netloc)r   usernamepasswordr   _replacehostnameport)urlparseds     r   _redact_git_urlr'       s_    c]F&//&//47H1IbhbmbmqQWQ\Q\P]M^su1v/wxxJr   c                     \ rS rSr% SrSrS\S'   \SS j5       r\	SS j5       r
\SS	.SS
 jj5       r\   S           SS jj5       r\SSS jj5       r\SSS jj5       r\S S j5       r\S!S j5       r\S"S j5       r\S#S j5       r\S$S j5       r\S%S&S jj5       r\S%S'S jj5       r\S(S j5       r\	S)S j5       r\	S*S j5       rSrg)+
GitCommand(   uz   Minimal Git CLI wrapper for repository operations.

All methods are static/class-level — no instance state is required.
r
   str	_git_pathc                    Xl         g)z0Override the git binary path (e.g. for testing).N)r,   )clspaths     r   set_git_pathGitCommand.set_git_path0   s	     r   c                0    [         R                  S U 5      $ )z/Strip embedded credentials from arbitrary text.c                4    [        U R                  5       5      $ )N)r'   group)ms    r   <lambda>$GitCommand._redact.<locals>.<lambda>;   s    _QWWY%?r   )_URL_REsub)texts    r   _redactGitCommand._redact8   s     {{?FFr   Ncwdc               h   U R                   /UQn[        R                  R                  5       nSUS'   [        R                  SU R                  SR                  U5      5      U=(       d    S5        [        R                  " UUSSUS9nUR                  S:w  a  UR                  R                  5       nUR                  R                  5       nS	U;   a  [        R                  S
5        U$ U R                  U=(       d    U5      n[        R                  SUS   U5        [        SUS    SU 35      eU$ )zExecute a git command and return the CompletedProcess.

Raises :class:`GitError` on non-zero exit (unless it's a benign warning).
0GIT_TERMINAL_PROMPTzgit %s (cwd=%s) .T)r>   capture_outputr:   envr   znothing to commitu(   Nothing to commit — repo is up to datezgit %s failed: %szgit z	 failed: )r,   osenvironcopyloggerdebugr;   join
subprocessrun
returncodestderrstripstdouterrorr   )	r.   r>   argscmdrE   resultrO   rQ   details	            r   _runGitCommand._run=   s
    }}$t$jjoo%(!"&CHHTN(CSZCP
 !]]((*F]]((*F"f,GH[[!162FLL,d1gv>T$q')F8<==r   c                |   U R                  X5      nS/nU(       a  UR                  SU/5        U(       a  UR                  S[        U5      /5        UR                  U[        U5      /5         U R                  " U6   g! [         a2    US-  R                  5       (       a  [        R                  SU5         ge f = f)a  Clone a repository.

Parameters
----------
url:
    Remote repository URL.
target_dir:
    Local directory to clone into.
branch:
    Optional branch/tag to checkout.
token:
    Optional OAuth2 token injected into the URL.
depth:
    Shallow clone depth (``None`` for full clone).
clonez--branchz--depthz.gitzVClone exited non-zero but repository exists at %s. Likely caused by a post-clone hook.N)_inject_tokenextendr+   rW   r   is_dirrI   warning)r.   r%   
target_dirbranchtokendepthauth_urlrS   s           r   rZ   GitCommand.clone`   s    0 $$S0")KKV,-KKCJ/0Xs:/0
	HHdO 	V#++--l
 	s   /A? ?8B;9B;c                `    SU/nU(       a  UR                  U5        U R                  " USU06  g)z Pull latest changes from remote.pullr>   NappendrW   r.   repo_dirremoter`   rS   s        r   rf   GitCommand.pull   .     KK$%H%r   c                `    SU/nU(       a  UR                  U5        U R                  " USU06  g)zPush local commits to remote.pushr>   Nrg   ri   s        r   ro   GitCommand.push   rm   r   c                F    S/U=(       d    S/QnU R                   " USU06  g)zStage files for commit.addrC   r>   NrW   )r.   rj   pathsrS   s       r   rr   GitCommand.add   s(     )3%)$%H%r   c                &    U R                  SSX!S9  g)z'Create a commit with the given message.commitz-mr=   Nrs   )r.   rj   messages      r   rw   GitCommand.commit   s     	47r   c                (    U R                  SSSUS9  g)z*Run ``git lfs install`` in the repository.lfsinstallz--forcer=   Nrs   )r.   rj   s     r   lfs_installGitCommand.lfs_install   s     		9(;r   c                &    U R                  SSX!S9  g)zTrack a pattern with Git LFS.r{   trackr=   Nrs   )r.   rj   patterns      r   	lfs_trackGitCommand.lfs_track   s     	7r   c                V     U R                  SS5        g! [        [        4 a     gf = f)z,Check if Git LFS is installed and available.r{   rE   TF)rW   r   FileNotFoundError)r.   s    r   is_lfs_availableGitCommand.is_lfs_available   s0    	HHUE"+, 		s    ((c                (    U R                  SSX2US9  g)zUpdate the remote URL.rk   zset-urlr=   Nrs   )r.   rj   r%   rk   s       r   set_remote_urlGitCommand.set_remote_url   s     	9fx@r   c                X    U R                  SSX!S9nUR                  R                  5       $ )zGet the current remote URL.rk   zget-urlr=   )rW   rQ   rP   )r.   rj   rk   rU   s       r   get_remote_urlGitCommand.get_remote_url   s+     (IvD}}""$$r   c                &    U R                  SX#US9  g)zSet a local git config value.configr=   Nrs   )r.   rj   keyvalues       r   r   GitCommand.config   s     	384r   c                v   U(       d  U $ U R                  S5      (       a  U $  [        U 5      nUR                  S;  a  U $ UR                  S:X  a  U $ UR
                  =(       d    SnUR                  (       a  U SUR                   3nSU SU 3n[        UR                  US95      $ ! [         a    U s $ f = f)	zkInject an OAuth2 token into an HTTP(S) URL.

Returns the URL unchanged for SSH URLs or when token is None.
zgit@)httphttpsoauth2r   r   zoauth2:@r   )	
startswithr   	Exceptionschemer    r#   r$   r   r"   )r%   ra   r&   hostr   s        r   r[   GitCommand._inject_token   s     J>>&!!J	c]F == 11J??h&J$";;V1V[[M*D5'4&)&///899  	J	s   B) )B87B8c                    U (       a  SU ;  a  U $  [        U 5      nUR                  =(       d    SnUR                  (       a  U SUR                   3n[        UR	                  US95      $ ! [
         a    U s $ f = f)z6Remove OAuth2 credentials from a URL for safe logging.z//oauth2r   r   r   )r   r#   r$   r   r"   r   )r%   r&   r   s      r   strip_token_from_urlGitCommand.strip_token_from_url   st     j+J	c]F??(bD{{q.fooTo:;; 	J	s   AA+ +A:9A:r   )r/   r+   returnNone)r:   r+   r   r+   )rS   r+   r>   zPath | str | Noner   z subprocess.CompletedProcess[str])NNN)r%   r+   r_   r   r`   
str | Nonera   r   rb   z
int | Noner   r   )originN)rj   r   rk   r+   r`   r   r   r   )rj   r   rt   r+   r   r   )rj   r   rx   r+   r   r   )rj   r   r   r   )rj   r   r   r+   r   r   )r   bool)r   )rj   r   r%   r+   rk   r+   r   r   )rj   r   rk   r+   r   r+   )rj   r   r   r+   r   r+   r   r   )r%   r+   ra   r   r   r+   r%   r+   r   r+   )r   r   r   r   r   r,   __annotations__classmethodr0   staticmethodr;   rW   rZ   rf   ro   rr   rw   r}   r   r   r   r   r   r[   r   r   r   r   r   r)   r)   (   s   
 Is  G G 7;  D 
 "  )) ) 	)
 ) ) 
) )V & & & & & &
 8 8 < < 8 8   A A % %
 5 5 : :4  r   r)   r   )r   
__future__r   rF   rerL   pathlibr   urllib.parser   r   errorsr   utils.loggerr	   rI   r   compiler8   r'   r)   r   r   r   <module>r      s]    # 	 	   -  $	E	Tx T **_
%L Lr   