@@ -200,24 +200,31 @@ def to_unix_path(p):
200
200
EMSDK_SET_ENV = os .path .join (EMSDK_PATH , 'emsdk_set_env.bat' )
201
201
202
202
203
- # Parses https://github.com/emscripten-core/emscripten/tree/d6aced8 to a pair (https://github.com/emscripten-core/emscripten, d6aced8)
203
+ # Parses https://github.com/emscripten-core/emscripten/tree/d6aced8 to a triplet
204
+ # (https://github.com/emscripten-core/emscripten, d6aced8, emscripten-core)
205
+ # or https://github.com/emscripten-core/emscripten/commit/00b76f81f6474113fcf540db69297cfeb180347e
206
+ # to (https://github.com/emscripten-core/emscripten, 00b76f81f6474113fcf540db69297cfeb180347e, emscripten-core)
204
207
def parse_github_url_and_refspec (url ):
205
208
if not url :
206
- return ('' , '' )
209
+ return ('' , '' , None )
207
210
208
211
if url .endswith (('/tree/' , '/tree' , '/commit/' , '/commit' )):
209
212
raise Exception ('Malformed git URL and refspec ' + url + '!' )
210
213
211
214
if '/tree/' in url :
212
215
if url .endswith ('/' ):
213
216
raise Exception ('Malformed git URL and refspec ' + url + '!' )
214
- return url .split ('/tree/' )
217
+ url , refspec = url .split ('/tree/' )
218
+ remote_name = url .split ('/' )[- 2 ]
219
+ return (url , refspec , remote_name )
215
220
elif '/commit/' in url :
216
221
if url .endswith ('/' ):
217
222
raise Exception ('Malformed git URL and refspec ' + url + '!' )
218
- return url .split ('/commit/' )
223
+ url , refspec = url .split ('/commit/' )
224
+ remote_name = url .split ('/' )[- 2 ]
225
+ return (url , refspec , remote_name )
219
226
else :
220
- return (url , 'main' ) # Assume the default branch is main in the absence of a refspec
227
+ return (url , 'main' , None ) # Assume the default branch is main in the absence of a refspec
221
228
222
229
223
230
ARCHIVE_SUFFIXES = ('zip' , '.tar' , '.gz' , '.xz' , '.tbz2' , '.bz2' )
@@ -812,40 +819,58 @@ def git_recent_commits(repo_path, n=20):
812
819
return []
813
820
814
821
815
- def git_clone (url , dstpath , branch ):
822
+ def get_git_remotes (repo_path ):
823
+ remotes = []
824
+ output = subprocess .check_output ([GIT (), 'remote' , '-v' ], stderr = subprocess .STDOUT , text = True , cwd = repo_path )
825
+ for line in output .splitlines ():
826
+ remotes += [line .split ()[0 ]]
827
+ return remotes
828
+
829
+
830
+ def git_clone (url , dstpath , branch , remote_name = 'origin' ):
816
831
debug_print ('git_clone(url=' + url + ', dstpath=' + dstpath + ')' )
817
832
if os .path .isdir (os .path .join (dstpath , '.git' )):
818
- debug_print ("Repository '" + url + "' already cloned to directory '" + dstpath + "', skipping." )
819
- return True
833
+ remotes = get_git_remotes (dstpath )
834
+ if remote_name in remotes :
835
+ debug_print ('Repository ' + url + ' with remote "' + remote_name + '" already cloned to directory ' + dstpath + ', skipping.' )
836
+ return True
837
+ else :
838
+ debug_print ('Repository ' + url + ' with remote "' + remote_name + '" already cloned to directory ' + dstpath + ', but remote has not yet been added. Creating.' )
839
+ return run ([GIT (), 'remote' , 'add' , remote_name , url ], cwd = dstpath ) == 0
840
+
820
841
mkdir_p (dstpath )
821
842
git_clone_args = ['--recurse-submodules' , '--branch' , branch ] # Do not check out a branch (installer will issue a checkout command right after)
822
843
if GIT_CLONE_SHALLOW :
823
844
git_clone_args += ['--depth' , '1' ]
824
845
print ('Cloning from ' + url + '...' )
825
- return run ([GIT (), 'clone' ] + git_clone_args + [url , dstpath ]) == 0
846
+ return run ([GIT (), 'clone' , '-o' , remote_name ] + git_clone_args + [url , dstpath ]) == 0
826
847
827
848
828
- def git_pull (repo_path , branch_or_tag ):
829
- debug_print ('git_pull(repo_path=' + repo_path + ', branch/tag=' + branch_or_tag + ')' )
830
- ret = run ([GIT (), 'fetch' , '--quiet' , 'origin' ], repo_path )
849
+ def git_pull (repo_path , branch_or_tag , remote_name = 'origin' ):
850
+ debug_print ('git_pull(repo_path=' + repo_path + ', branch/tag=' + branch_or_tag + ', remote_name=' + remote_name + ' )' )
851
+ ret = run ([GIT (), 'fetch' , '--quiet' , remote_name ], repo_path )
831
852
if ret != 0 :
832
853
return False
833
854
try :
834
855
print ("Fetching latest changes to the branch/tag '" + branch_or_tag + "' for '" + repo_path + "'..." )
835
- ret = run ([GIT (), 'fetch' , '--quiet' , 'origin' ], repo_path )
836
- if ret != 0 :
837
- return False
838
- # this line assumes that the user has not gone and manually messed with the
839
- # repo and added new remotes to ambiguate the checkout.
840
- ret = run ([GIT (), 'checkout' , '--recurse-submodules' , '--quiet' , branch_or_tag ], repo_path )
856
+ ret = run ([GIT (), 'fetch' , '--quiet' , remote_name ], repo_path )
841
857
if ret != 0 :
842
858
return False
843
859
# Test if branch_or_tag is a branch, or if it is a tag that needs to be updated
844
860
target_is_tag = run ([GIT (), 'symbolic-ref' , '-q' , 'HEAD' ], repo_path , quiet = True )
861
+
862
+ if target_is_tag :
863
+ ret = run ([GIT (), 'checkout' , '--recurse-submodules' , '--quiet' , branch_or_tag ], repo_path )
864
+ else :
865
+ local_branch_prefix = (remote_name + '_' ) if remote_name != 'origin' else ''
866
+ ret = run ([GIT (), 'checkout' , '--recurse-submodules' , '--quiet' , '-B' , local_branch_prefix + branch_or_tag ,
867
+ '--track' , remote_name + '/' + branch_or_tag ], repo_path )
868
+ if ret != 0 :
869
+ return False
845
870
if not target_is_tag :
846
871
# update branch to latest (not needed for tags)
847
872
# this line assumes that the user has not gone and made local changes to the repo
848
- ret = run ([GIT (), 'merge' , '--ff-only' , 'origin /' + branch_or_tag ], repo_path )
873
+ ret = run ([GIT (), 'merge' , '--ff-only' , remote_name + ' /' + branch_or_tag ], repo_path )
849
874
if ret != 0 :
850
875
return False
851
876
run ([GIT (), 'submodule' , 'update' , '--init' ], repo_path , quiet = True )
@@ -857,14 +882,16 @@ def git_pull(repo_path, branch_or_tag):
857
882
return True
858
883
859
884
860
- def git_clone_checkout_and_pull (url , dstpath , branch ):
861
- debug_print ('git_clone_checkout_and_pull(url=' + url + ', dstpath=' + dstpath + ', branch=' + branch + ')' )
885
+ def git_clone_checkout_and_pull (url , dstpath , branch , override_remote_name = 'origin' ):
886
+ debug_print ('git_clone_checkout_and_pull(url=' + url + ', dstpath=' + dstpath + ', branch=' + branch + ', override_remote_name=' + override_remote_name + ' )' )
862
887
863
- # If the repository has already been cloned before, issue a pull operation. Otherwise do a new clone.
864
- if os .path .isdir (os .path .join (dstpath , '.git' )):
865
- return git_pull (dstpath , branch )
866
- else :
867
- return git_clone (url , dstpath , branch )
888
+ # Make sure the repository is cloned first
889
+ success = git_clone (url , dstpath , branch , override_remote_name )
890
+ if not success :
891
+ return False
892
+
893
+ # And/or issue a pull/checkout to get to latest code.
894
+ return git_pull (dstpath , branch , override_remote_name )
868
895
869
896
870
897
# Each tool can have its own build type, or it can be overridden on the command
@@ -1856,7 +1883,7 @@ def install_tool(self):
1856
1883
elif hasattr (self , 'custom_install_script' ) and self .custom_install_script == 'build_ccache' :
1857
1884
success = build_ccache (self )
1858
1885
elif hasattr (self , 'git_branch' ):
1859
- success = git_clone_checkout_and_pull (url , self .installation_path (), self .git_branch )
1886
+ success = git_clone_checkout_and_pull (url , self .installation_path (), self .git_branch , getattr ( self , 'remote_name' , 'origin' ) )
1860
1887
elif url .endswith (ARCHIVE_SUFFIXES ):
1861
1888
success = download_and_extract (url , self .installation_path (),
1862
1889
filename_prefix = getattr (self , 'download_prefix' , '' ))
@@ -2826,7 +2853,7 @@ def extract_string_arg(name):
2826
2853
errlog ('Failed to find tool ' + tool_name + '!' )
2827
2854
return False
2828
2855
else :
2829
- t .url , t .git_branch = parse_github_url_and_refspec (url_and_refspec )
2856
+ t .url , t .git_branch , t . remote_name = parse_github_url_and_refspec (url_and_refspec )
2830
2857
debug_print ('Reading git repository URL "' + t .url + '" and git branch "' + t .git_branch + '" for Tool "' + tool_name + '".' )
2831
2858
2832
2859
forked_url = extract_string_arg ('--override-repository' )
0 commit comments