1111
1212from  sphinxcontrib .versioning .lib  import  TempDir 
1313
14- RE_FETCH_PUSH  =  re .compile (r'origin \t([A-Za-z0-9@:/._-]+) \((fetch|push)\)\n' )
14+ RE_ALL_REMOTES  =  re .compile (r'([\w./-]+) \t([A-Za-z0-9@:/._-]+) \((fetch|push)\)\n' )
1515RE_REMOTE  =  re .compile (r'^(?P<sha>[0-9a-f]{5,40})\trefs/(?P<kind>heads|tags)/(?P<name>[\w./-]+(?:\^\{})?)$' ,
1616                       re .MULTILINE )
1717RE_UNIX_TIME  =  re .compile (r'^\d{10}$' , re .MULTILINE )
@@ -313,14 +313,18 @@ def clone(local_root, new_root, branch, rel_dest, exclude):
313313    """ 
314314    log  =  logging .getLogger (__name__ )
315315    output  =  run_command (local_root , ['git' , 'remote' , '-v' ])
316-     remote_urls  =  dict ((m [1 ], m [0 ]) for  m  in  RE_FETCH_PUSH .findall (output ))
317-     if  not  remote_urls :
316+     matches  =  RE_ALL_REMOTES .findall (output )
317+     if  not  matches :
318+         raise  GitError ('Git repo has no remotes.' , output )
319+     remotes  =  {m [0 ]: [m [1 ], '' ] for  m  in  matches  if  m [2 ] ==  'fetch' }
320+     for  match  in  (m  for  m  in  matches  if  m [2 ] ==  'push' ):
321+         remotes [match [0 ]][1 ] =  match [1 ]
322+     if  'origin'  not  in   remotes :
318323        raise  GitError ('Git repo missing remote "origin".' , output )
319-     remote_push_url , remote_fetch_url  =  remote_urls ['push' ], remote_urls ['fetch' ]
320324
321325    # Clone. 
322326    try :
323-         run_command (new_root , ['git' , 'clone' , remote_fetch_url , '--depth=1' , '--branch' , branch , '.' ])
327+         run_command (new_root , ['git' , 'clone' , remotes [ 'origin' ][ 0 ] , '--depth=1' , '--branch' , branch , '.' ])
324328    except  CalledProcessError  as  exc :
325329        raise  GitError ('Failed to clone from remote repo URL.' , exc .output )
326330
@@ -330,9 +334,11 @@ def clone(local_root, new_root, branch, rel_dest, exclude):
330334    except  CalledProcessError  as  exc :
331335        raise  GitError ('Specified branch is not a real branch.' , exc .output )
332336
333-     # Set push URL if different. 
334-     if  remote_fetch_url  !=  remote_push_url :
335-         run_command (new_root , ['git' , 'remote' , 'set-url' , '--push' , 'origin' , remote_push_url ])
337+     # Copy all remotes from original repo. 
338+     for  name , (fetch , push ) in  remotes .items ():
339+         if  name  !=  'origin' :
340+             run_command (new_root , ['git' , 'remote' , 'add' , name , fetch ])
341+         run_command (new_root , ['git' , 'remote' , 'set-url' , '--push' , name , push ])
336342
337343    # Done if no exclude. 
338344    if  not  exclude :
@@ -355,13 +361,14 @@ def clone(local_root, new_root, branch, rel_dest, exclude):
355361    run_command (new_root , ['git' , 'checkout' , '--' ] +  exclude_joined )
356362
357363
358- def  commit_and_push (local_root , versions ):
359-     """Commit changed, new, and deleted files in the repo and attempt to push the branch to origin . 
364+ def  commit_and_push (local_root , remote ,  versions ):
365+     """Commit changed, new, and deleted files in the repo and attempt to push the branch to the remote repository . 
360366
361367    :raise CalledProcessError: Unhandled git command failure. 
362368    :raise GitError: Conflicting changes made in remote by other client and bad git config for commits. 
363369
364370    :param str local_root: Local path to git root directory. 
371+     :param str remote: The git remote to push to. 
365372    :param sphinxcontrib.versioning.versions.Versions versions: Versions class instance. 
366373
367374    :return: If push succeeded. 
@@ -410,7 +417,7 @@ def commit_and_push(local_root, versions):
410417
411418    # Push. 
412419    try :
413-         run_command (local_root , ['git' , 'push' , 'origin' , current_branch ])
420+         run_command (local_root , ['git' , 'push' , remote , current_branch ])
414421    except  CalledProcessError  as  exc :
415422        if  '[rejected]'  in  exc .output  and  '(fetch first)'  in  exc .output :
416423            log .debug ('Remote has changed since cloning the repo. Must retry.' )
0 commit comments