3838import pkgsdb
3939from package import PackageOperation , Bridge_SConscript
4040from vars import Import , Export
41- from .cmd_package_utils import get_url_from_mirror_server , execute_command , git_pull_repo , user_input , find_bool_macro_in_config
41+ from .cmd_package_utils import (
42+ get_url_from_mirror_server ,
43+ execute_command ,
44+ git_pull_repo ,
45+ user_input ,
46+ find_bool_macro_in_config ,
47+ )
48+
49+
50+ def _get_git_restore_url (package_obj , ver ):
51+ """Return a suitable git URL to restore remote.origin.url.
52+
53+ Prefer the per-version URL if it ends with '.git'. Otherwise, fall back to
54+ the 'repository' field (appending '.git' when necessary). Return None when
55+ no suitable git URL can be determined.
56+ """
57+ try :
58+ url = package_obj .get_url (ver )
59+ except Exception :
60+ url = None
61+
62+ if url and isinstance (url , str ) and url .endswith ('.git' ):
63+ return url
64+
65+ repo = None
66+ try :
67+ repo = package_obj .pkg .get ('repository' ) if package_obj and package_obj .pkg else None
68+ except Exception :
69+ repo = None
70+
71+ if repo and isinstance (repo , str ):
72+ return repo if repo .endswith ('.git' ) else repo + '.git'
73+
74+ return None
4275
4376
4477def determine_support_chinese (env_root ):
@@ -167,7 +200,12 @@ def need_using_mirror_download():
167200 elif os .path .isfile (config_file ) and find_bool_macro_in_config (config_file , 'SYS_DOWNLOAD_SERVER_GITEE' ):
168201 is_China_ip = True # Gitee which means China IP
169202 server_decision = "manually decision"
203+ elif not os .path .isfile (config_file ):
204+ # env cmds/.config not found: default to GitHub without noisy prompts
205+ is_China_ip = False
206+ server_decision = "default GitHub (no env .config)"
170207 else :
208+ # env .config exists but no explicit server set: keep legacy auto decision
171209 try :
172210 ip = requests .get ('https://ifconfig.me/ip' ).content .decode ()
173211 url = 'http://www.ip-api.com/json/' + ip
@@ -214,7 +252,15 @@ def update_submodule(repo_path, use_esp_mirror):
214252 execute_command (cmd , cwd = repo_path )
215253
216254
217- def install_git_package (bsp_package_path , package_name , package_info , package_url , ver_sha , upstream_changed , url_origin ):
255+ def install_git_package (
256+ bsp_package_path ,
257+ package_name ,
258+ package_info ,
259+ package_url ,
260+ ver_sha ,
261+ upstream_changed ,
262+ url_origin ,
263+ ):
218264 try :
219265 repo_path = os .path .join (bsp_package_path , package_name )
220266 repo_path = repo_path + '-' + package_info ['ver' ]
@@ -236,8 +282,9 @@ def install_git_package(bsp_package_path, package_name, package_info, package_ur
236282 print ("\n Failed to download software package with git. Please check the network connection." )
237283 return False
238284
239- # change upstream to origin url
240- if upstream_changed :
285+ # change upstream back to origin url when applicable
286+ # only restore when a valid git URL is available
287+ if upstream_changed and url_origin and str (url_origin ).endswith ('.git' ):
241288 cmd = 'git remote set-url origin ' + url_origin
242289 execute_command (cmd , cwd = repo_path )
243290
@@ -340,9 +387,18 @@ def install_package(env_root, pkgs_root, bsp_root, package_info, force_update):
340387 logging .warning ("Failed to connect to the mirror server, package will be downloaded from non-mirror server.\n " )
341388
342389 logging .info ("Package url: %s" % package_url )
390+ # compute a safe origin url to restore (git only)
391+ restore_origin_url = _get_git_restore_url (package , package_info ['ver' ])
392+
343393 if is_git_url (package_url ):
344394 if not install_git_package (
345- bsp_package_path , pkgs_name_in_json , package_info , package_url , ver_sha , upstream_changed , url_from_json
395+ bsp_package_path ,
396+ pkgs_name_in_json ,
397+ package_info ,
398+ package_url ,
399+ ver_sha ,
400+ upstream_changed ,
401+ restore_origin_url ,
346402 ):
347403 result = False
348404 else :
@@ -463,12 +519,13 @@ def update_latest_packages(sys_value):
463519 # If the package has submodules, update the submodules.
464520 update_submodule (repo_path , pkgs_name_in_json == u"ESP-IDF" )
465521
466- # recover origin url to the path which get from packages.json file
467- if package .get_url (pkg ['ver' ]):
468- cmd = 'git remote set-url origin ' + package .get_url (pkg ['ver' ])
522+ # recover origin url to a proper git URL from package info
523+ restore_url = _get_git_restore_url (package , pkg ['ver' ])
524+ if restore_url :
525+ cmd = 'git remote set-url origin ' + restore_url
469526 git_cmd_exec (cmd , repo_path )
470527 else :
471- print ("Can't find the package : %s's url in file : %s" % ( payload_pkgs_name_in_json , pkg_path ) )
528+ print ("Can't restore origin: no git URL found for package in %s" % pkg_path )
472529
473530 print ("==============================> %s update done\n " % pkgs_name_in_json )
474531
0 commit comments