
    *                        S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	r	SSK
Jr  SSK
Jr  SSK
Jr  SS	KJr  SS
KJr  SSKJr  SSKrSSKrSrSr " S S\R,                  5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rS%S jr " S S\5      rS rS&S! jr S \4S" jr! " S# S$\"5      r#g)'z)Wrapper to manipulate GCP git repository.    )absolute_import)division)unicode_literalsN)
exceptions)log)
properties)encoding)files)	platforms)   r      )r      r   c                       \ rS rSrSrSrg)Error+   zExceptions for this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       (lib/googlecloudsdk/api_lib/source/git.pyr   r   +   s    #r   r   c                       \ rS rSrSrSrg)UnknownRepositoryAliasException/   zHException to be thrown when a repository alias provided cannot be found.r   Nr   r   r   r   r   r   /   s    Pr   r   c                       \ rS rSrSrSrg)CannotInitRepositoryException3   z;Exception to be thrown when a repository cannot be created.r   Nr   r   r   r   r    r    3       Cr   r    c                       \ rS rSrSrSrg)CannotFetchRepositoryException7   z;Exception to be thrown when a repository cannot be fetched.r   Nr   r   r   r   r$   r$   7   r"   r   r$   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )GitVersionException;   z+Exceptions for when git version is too old.c                 R   > X l         [        [        U ]  UR	                  X#S95        g )Ncur_versionmin_version)r+   superr'   __init__format)selffmtstrr+   r,   	__class__s       r   r.   GitVersionException.__init__>   s(    "	
t-+GIr   )r+   r   r   r   r   r   r.   r   __classcell__r2   s   @r   r'   r'   ;   s    3I Ir   r'   c                       \ rS rSrSrSrg)InvalidGitExceptionD   z4Exceptions for when git version is empty or invalid.r   Nr   r   r   r   r8   r8   D   s    <r   r8   c                       \ rS rSrSrSrg)GcloudIsNotInPathH   z.Exception for when the gcloud cannot be found.r   Nr   r   r   r   r;   r;   H   s    6r   r;   c                 ^    [         R                  " [        R                  " SS/5      5      nU(       d  [	        S5      eUR                  S5      (       d  [	        S5      e[        R                  " SU5      nU(       d  [	        S5      e[        UR                  SS	S
5       Vs/ s H  n[        U5      PM     sn5      nU (       a'  X@:  a"  SR                  S U  5       5      n[        SXS9egs  snf ! [         a.  nUR                  [        R                  :X  a
  [!        5       ee SnAff = f)a  Returns true when version of git is >= min_version.

Args:
  version_lower_bound: (int,int,int), The lowest allowed version, or None to
    just check for the presence of git.

Returns:
  True if version >= min_version.

Raises:
  GitVersionException: if `git` was found, but the version is incorrect.
  InvalidGitException: if `git` was found, but the output of `git version` is
    not as expected.
  NoGitException: if `git` was not found.
gitversionz The git version string is empty.zgit version z4The git version string must start with git version .z(\d+)\.(\d+)\.(\d+)z5The git version string must contain a version number.r   r      .c              3   N   #    U  H  n[         R                  " U5      v   M     g 7fNsix	text_type.0is     r   	<genexpr>"CheckGitVersion.<locals>.<genexpr>j   s     K7J!S]]1--7J   #%zvYour git version {cur_version} is older than the minimum version {min_version}. Please install a newer version of git.r*   NT)r	   Decode
subprocesscheck_outputr8   
startswithresearchtuplegroupintjoinr'   OSErrorerrnoENOENTNoGitException)version_lower_boundr+   matchitemcurrent_versionr,   es          r   CheckGitVersionr`   L   s    
//*"9"95):L"MNK BCC!!.11 "1 3 3II,k:E !2 3 3 5;;q!Q3GH3G4SY3GHIODHHK7JKKkB!< < 
 I 
 
ww%,,	
s*   B"C4 $C/95C4 /C4 4
D,>)D''D,c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )rZ   v   z)Exceptions for when git is not available.c                 T   > [         [        U ]  [        R                  " S5      5        g )Na          Cannot find git. Please install git and try again.

        You can find git installers at [http://git-scm.com/downloads], or use
        your favorite package manager to install it on your computer. Make sure
        it can be found on your system PATH.
        )r-   rZ   r.   textwrapdedent)r0   r2   s    r   r.   NoGitException.__init__y   s#    	.$(  	r   r   r4   r6   s   @r   rZ   rZ   v   s    1 r   rZ   c                 4    [         R                  " SXS.5      $ )zGet the URI for a repository, given its project and alias.

Args:
  project: str, The project name.
  alias: str, The repository alias.

Returns:
  str, The repository URI.
z:https://source.developers.google.com/p/{project}/r/{alias}projectalias)uritemplateexpandrh   s     r   _GetRepositoryURIrm      s      
		B*
, ,r   Fc                 x   [         R                  R                  5       [         R                  R                  :X  a  SnOSnSn[        R
                  " X!/S9nU(       d  [        S5      eU (       aH  [        R                  " SU5      (       d*  [        R                  " [        R                  " S5      5        U$ X!-   $ )zGet name of the gcloud script.

Args:
  full_path: boolean, True if the gcloud full path should be used if free
    of spaces.

Returns:
  str, command to use to execute gcloud

Raises:
  GcloudIsNotInPath: if gcloud is not found in the path
z.cmd gcloud)pathextzbCould not verify that gcloud is in the PATH. Please make sure the Cloud SDK bin folder is in PATH.z[-a-zA-Z0-9_/]+$z          You specified the option to use the full gcloud path in the git
          credential.helper, but the path contains non alphanumberic characters
          so the credential helper may not work correctly.)r   OperatingSystemCurrentWINDOWSr
   FindExecutableOnPathr;   rQ   r\   r   warningrd   re   )	full_path
gcloud_extgcloud_namerp   s       r   _GetGcloudScriptrz      s     '')''(JJ+%%k<H&	
	@A A 88'00	kk
// > ?@
 M##r   c           
        ^  S/n[         R                  R                  R                  R	                  5       nU(       a   UR                  UR                  S5      5        [        U 4S jU 5       5      (       aV   [        U5        S	R                  [#        U5      [         R                  R                  R$                  R	                  S
S95      $ g! [         ad  nSR                  S U 5       5      n[        R                  " [        R                  " SR                  UR                   US95      5         SnAgSnAff = f)a]  Returns the gcloud credential helper command for a remote repository.

The command will be of the form '!gcloud auth git-helper --account=EMAIL
--ignore-unknown $@`. See https://git-scm.com/docs/git-config. If the
installed version of git or the remote repository does not support
the gcloud credential helper, then returns None.

Args:
  uri: str, The uri of the remote repository.
  full_path: bool, If true, use the full path to gcloud.
  min_version: minimum git version; if found git is earlier than this, warn
      and return None

Returns:
  str, The credential helper command if it is available.
zsource.developers.google.com,c              3   R   >#    U  H  nTR                  S U-   S-   5      v   M     g7f)zhttps:///N)rP   )rH   hosturis     r   rJ   (_GetCredHelperCommand.<locals>.<genexpr>   s,      	N:L$cnnZ$&,--:Ls   $'rA   c              3   N   #    U  H  n[         R                  " U5      v   M     g 7frC   rD   rG   s     r   rJ   r      s     F+Qa 0 0+rL   a            You are using a Google-hosted repository with a
          {current} which is older than {min_version}. If you upgrade
          to {min_version} or later, gcloud can handle authentication to
          this repository. Otherwise, to authenticate, use your Google
          account and the password found by running the following command.
           $ gcloud auth print-access-token)currentr,   Nz6!{0} auth git-helper --account={1} --ignore-unknown $@T)required)r   VALUEScore credentialed_hosted_repo_domainsGetextendsplitanyr`   r'   rV   r   rv   rd   re   r/   r+   rz   account)r   rw   r,   credentialed_hostsextrar_   helper_min_strs   `      r   _GetCredHelperCommandr      s   " 77



 
 
A
A
E
E
G%
ekk#./ 	N:L	N N Nk"  DJJ#&&**D*9; ; 
%  xxF+FFn	kk
// / 06v}}. 06 0BCD s   9C 
D=AD88D=c                   8    \ rS rSrSrS	S jrS rS
S jrS rSr	g)Git   zRepresents project git repo.Nc                     Xl         X l        U=(       d    [        X5      U l        U R                  (       d
  [	        5       eg)at  Constructor.

Args:
  project_id: str, The name of the project that has a repository associated
      with it.
  repo_name: str, The name of the repository to clone.
  uri: str, The URI of the repository, or None if it will be inferred from
      the name.

Raises:
  UnknownRepositoryAliasException: If the repo name is not known to be
      associated with the project.
N)_project_id
_repo_namerm   _urir   )r0   
project_id	repo_namer   s       r   r.   Git.__init__   s6     "O?(?DI99+-- r   c                     U R                   $ rC   )r   )r0   s    r   GetNameGit.GetName   s    ??r   c                 X   [         R                  R                  U5      n[         R                  R                  U5      (       a0  [	        5         [         R
                  " U5      (       a  [        S5      e SSU R                  U/n[        n[        R                  R                  5       [        R                  R                  :X  a  [        n[        U R                  X6S9nU(       a  USSSSU-   /-  nU R                  XR5        U$ ! [         R"                   a  n[%        U5      eSnAff = f)a  Clone a git repository into a gcloud workspace.

If the resulting clone does not have a .gcloud directory, create one. Also,
sets the credential.helper to use the gcloud credential helper.

Args:
  destination_path: str, The relative path for the repository clone.
  dry_run: bool, If true do not run but print commands instead.
  full_path: bool, If true use the full path to gcloud.

Returns:
  str, The absolute path of cloned repository.

Raises:
  CannotInitRepositoryException: If there is already a file or directory in
      the way of creating this repository.
  CannotFetchRepositoryException: If there is a problem fetching the
      repository from the remote host, or if the repository is otherwise
      misconfigured.
z0Directory path specified exists and is not emptyr>   clone)rw   r,   z--configz8credential.https://source.developers.google.com/.helper=N)ospathabspathexistsr`   listdirr    r   _HELPER_MINr   rr   rs   rt   _WINDOWS_HELPER_MINr   _RunCommandrN   CalledProcessErrorr$   )	r0   destination_pathdry_runrw   abs_repository_pathcmdmin_gitcred_helper_commandr_   s	            r   Clone	Git.Clone  s   * ''//*:;	ww~~)**	'	(	(+>@ 	@.GTYY(;<cg

#
#
+
+
-

#
#
+
+,%1
))y?	FF 	
 	
 s$  (( .*1--.s   5BD D)D$$D)c                     [         R                  " SU5        U(       a/  [         R                  R                  SR	                  U5      5        g [
        R                  " U5        g )NzExecuting %s )r   debugoutPrintrV   rN   
check_call)r0   r   r   s      r   r   Git._RunCommand6  s9    IInc"	ggmmCHHSM"C r   )r   r   r   rC   )FF)
r   r   r   r   r   r.   r   r   r   r   r   r   r   r   r      s    $.(2h!r   r   rC   )F)$r   
__future__r   r   r   rX   r   rQ   rN   rd   googlecloudsdk.corer   r   r   googlecloudsdk.core.utilr	   r
   r   rE   rk   r   r   r   r   r    r$   r'   r8   r;   r`   rZ   rm   rz   r   objectr   r   r   r   <module>r      s     0 &  '  	 	   * # * - * . 
    $J $Qe QDE DDU DI% I=% =7 7'TU ,$$N */K +\S!& S!r   