77import re
88import sys
99import tarfile
10+ import time
1011from datetime import datetime
1112from subprocess import CalledProcessError , PIPE , Popen , STDOUT
1213
@@ -111,7 +112,7 @@ def chunk(iterator, max_size):
111112 yield chunked
112113
113114
114- def run_command (local_root , command , env_var = True , pipeto = None ):
115+ def run_command (local_root , command , env_var = True , pipeto = None , retry = 0 ):
115116 """Run a command and return the output.
116117
117118 :raise CalledProcessError: Command exits non-zero.
@@ -120,6 +121,7 @@ def run_command(local_root, command, env_var=True, pipeto=None):
120121 :param iter command: Command to run.
121122 :param bool env_var: Define GIT_DIR environment variable (on non-Windows).
122123 :param function pipeto: Pipe `command`'s stdout to this function (only parameter given).
124+ :param int retry: Retry this many times on CalledProcessError after 0.1 seconds.
123125
124126 :return: Command output.
125127 :rtype: str
@@ -145,7 +147,10 @@ def run_command(local_root, command, env_var=True, pipeto=None):
145147
146148 # Verify success.
147149 if main .poll () != 0 :
148- raise CalledProcessError (main .poll (), command , output = main_output )
150+ if retry < 1 :
151+ raise CalledProcessError (main .poll (), command , output = main_output )
152+ time .sleep (0.1 )
153+ return run_command (local_root , command , env_var , pipeto , retry - 1 )
149154
150155 return main_output
151156
@@ -345,8 +350,11 @@ def clone(local_root, new_root, remote, branch, rel_dest, exclude):
345350
346351 # Copy all remotes from original repo.
347352 for name , (fetch , push ) in remotes .items ():
348- run_command (new_root , ['git' , 'remote' , 'set-url' if name == 'origin' else 'add' , name , fetch ])
349- run_command (new_root , ['git' , 'remote' , 'set-url' , '--push' , name , push ])
353+ try :
354+ run_command (new_root , ['git' , 'remote' , 'set-url' if name == 'origin' else 'add' , name , fetch ], retry = 3 )
355+ run_command (new_root , ['git' , 'remote' , 'set-url' , '--push' , name , push ], retry = 3 )
356+ except CalledProcessError as exc :
357+ raise GitError ('Failed to set git remote URL.' , exc .output )
350358
351359 # Done if no exclude.
352360 if not exclude :
0 commit comments