1515import sys
1616import traceback
1717from multiprocessing import freeze_support
18- from typing import Any , Dict , Optional , Set , List , Union
18+ from typing import Any , Dict , Hashable , Optional , Set , List , Union
1919
2020from .cli_arguments import CliArguments
21- from .git_command import Git
21+ from .git_command import Git , GitException
2222from .runner_arguments import AdditionalSwiftSourcesArguments , UpdateArguments
2323from .parallel_runner import ParallelRunner
2424
@@ -74,7 +74,7 @@ def get_branch_for_repo(
7474 config : Dict [str , Any ],
7575 repo_name : str ,
7676 scheme_name : str ,
77- scheme_map : Dict [str , str ],
77+ scheme_map : Optional [ Dict [str , str ] ],
7878 cross_repos_pr : Dict [str , str ],
7979):
8080 """Infer, fetch, and return a branch corresponding to a given PR, otherwise
@@ -85,7 +85,7 @@ def get_branch_for_repo(
8585 config (Dict[str, Any]): deserialized `update-checkout-config.json`
8686 repo_name (str): name of the repository for checking out the branch
8787 scheme_name (str): name of the scheme to look up in the config
88- scheme_map (Dict[str, str]): map of repo names to branches to check out
88+ scheme_map (Dict[str, str] | None ): map of repo names to branches to check out
8989 cross_repos_pr (Dict[str, str]): map of repo ids to PRs to check out
9090
9191 Returns:
@@ -239,8 +239,8 @@ def run_for_repo_and_each_submodule_rec(args: List[str]):
239239 # Otherwise there was some other error, and we need to handle
240240 # it like other command errors.
241241 Git .run (repo_path , ["symbolic-ref" , "-q" , "HEAD" ])
242- except Exception as e :
243- if e .ret == 1 :
242+ except GitException as e :
243+ if e .returncode == 1 :
244244 detached_head = True
245245 else :
246246 raise # Pass this error up the chain.
@@ -268,19 +268,17 @@ def run_for_repo_and_each_submodule_rec(args: List[str]):
268268 prefix = prefix ,
269269 )
270270 except Exception :
271- (type , value , tb ) = sys .exc_info ()
272271 if verbose :
273272 print ('Error on repo "%s": %s' % (repo_path , traceback .format_exc ()))
274- return value
273+ raise
275274
276275
277- def get_timestamp_to_match (match_timestamp , source_root ):
278- # type: (str | None, str) -> str | None
276+ def get_timestamp_to_match (match_timestamp : bool , source_root : str ):
279277 """Computes a timestamp of the last commit on the current branch in
280278 the `swift` repository.
281279
282280 Args:
283- match_timestamp (str | None ): value of `--match-timestamp` to check.
281+ match_timestamp (bool ): value of `--match-timestamp` to check.
284282 source_root (str): directory that contains sources of the Swift project.
285283
286284 Returns:
@@ -295,7 +293,7 @@ def get_timestamp_to_match(match_timestamp, source_root):
295293 return output
296294
297295
298- def get_scheme_map (config : Dict [str , Any ], scheme_name : str ):
296+ def get_scheme_map (config : Dict [str , Any ], scheme_name : str ) -> Optional [ Dict [ str , str ]] :
299297 """Find a mapping from repository IDs to branches in the config.
300298
301299 Args:
@@ -342,7 +340,7 @@ def _is_any_repository_locked(pool_args: List[UpdateArguments]) -> Set[str]:
342340 locked_repositories .add (repo_name )
343341 return locked_repositories
344342
345- def _move_llvm_project_to_first_index (pool_args : List [ Union [UpdateArguments , AdditionalSwiftSourcesArguments ]]):
343+ def _move_llvm_project_to_first_index (pool_args : Union [List [ UpdateArguments ], List [ AdditionalSwiftSourcesArguments ]]):
346344 llvm_project_idx = None
347345 for i in range (len (pool_args )):
348346 if pool_args [i ].repo_name == "llvm-project" :
@@ -351,7 +349,13 @@ def _move_llvm_project_to_first_index(pool_args: List[Union[UpdateArguments, Add
351349 if llvm_project_idx is not None :
352350 pool_args .insert (0 , pool_args .pop (llvm_project_idx ))
353351
354- def update_all_repositories (args : CliArguments , config , scheme_name , scheme_map , cross_repos_pr ):
352+ def update_all_repositories (
353+ args : CliArguments ,
354+ config : Dict [str , Any ],
355+ scheme_name : str ,
356+ scheme_map : Optional [Dict [str , Any ]],
357+ cross_repos_pr : Dict [str , str ],
358+ ):
355359 pool_args : List [UpdateArguments ] = []
356360 timestamp = get_timestamp_to_match (args .match_timestamp , args .source_root )
357361 for repo_name in config ['repos' ].keys ():
@@ -392,7 +396,7 @@ def update_all_repositories(args: CliArguments, config, scheme_name, scheme_map,
392396 locked_repositories : set [str ] = _is_any_repository_locked (pool_args )
393397 if len (locked_repositories ) > 0 :
394398 return [
395- f"'{ repo_name } ' is locked by git. Cannot update it."
399+ Exception ( f"'{ repo_name } ' is locked by git. Cannot update it." )
396400 for repo_name in locked_repositories
397401 ]
398402 _move_llvm_project_to_first_index (pool_args )
@@ -484,7 +488,7 @@ def obtain_all_additional_swift_sources(
484488 else :
485489 remote = config ['https-clone-pattern' ] % remote_repo_id
486490
487- repo_branch = None
491+ repo_branch : Optional [ str ] = None
488492 repo_not_in_scheme = False
489493 if scheme_name :
490494 for v in config ['branch-schemes' ].values ():
@@ -500,6 +504,9 @@ def obtain_all_additional_swift_sources(
500504 repo_branch = scheme_name
501505 if repo_not_in_scheme :
502506 continue
507+
508+ if repo_branch is None :
509+ raise RuntimeError ("repo_branch is None" )
503510
504511 new_args = AdditionalSwiftSourcesArguments (
505512 args = args ,
@@ -570,7 +577,7 @@ def print_repo_hashes(args: CliArguments, config: Dict[str, Any]):
570577 print ("{:<35}: {:<35}" .format (repo_name , repo_hash ))
571578
572579
573- def merge_no_duplicates (a : dict , b : dict ) -> dict :
580+ def merge_no_duplicates (a : Dict [ Hashable , Any ], b : Dict [ Hashable , Any ] ) -> Dict [ Hashable , Any ] :
574581 result = {** a }
575582 for key , value in b .items ():
576583 if key in a :
@@ -580,7 +587,7 @@ def merge_no_duplicates(a: dict, b: dict) -> dict:
580587 return result
581588
582589
583- def merge_config (config : dict , new_config : dict ) -> dict :
590+ def merge_config (config : Dict [ str , Any ], new_config : Dict [ str , Any ] ) -> Dict [ str , Any ] :
584591 """
585592 Merge two configs, with a 'last-wins' strategy.
586593
@@ -619,7 +626,7 @@ def validate_config(config: Dict[str, Any]):
619626 'too.' .format (scheme_name ))
620627
621628 # Then make sure the alias names used by our branches are unique.
622- seen = dict ()
629+ seen : Dict [ str , Any ] = dict ()
623630 for (scheme_name , scheme ) in config ['branch-schemes' ].items ():
624631 aliases = scheme ['aliases' ]
625632 for alias in aliases :
@@ -631,7 +638,7 @@ def validate_config(config: Dict[str, Any]):
631638 seen [alias ] = scheme_name
632639
633640
634- def full_target_name (repo_path , repository , target ) :
641+ def full_target_name (repo_path : str , repository : str , target : str ) -> str :
635642 tag , _ , _ = Git .run (repo_path , ["tag" , "-l" , target ], fatal = True )
636643 if tag == target :
637644 return tag
@@ -645,13 +652,13 @@ def full_target_name(repo_path, repository, target):
645652 raise RuntimeError ('Cannot determine if %s is a branch or a tag' % target )
646653
647654
648- def skip_list_for_platform (config : Dict [str , Any ], all_repos : List [ str ] ) -> List [str ]:
655+ def skip_list_for_platform (config : Dict [str , Any ], all_repos : bool ) -> List [str ]:
649656 """Computes a list of repositories to skip when updating or cloning, if not
650657 overridden by `--all-repositories` CLI argument.
651658
652659 Args:
653660 config (Dict[str, Any]): deserialized `update-checkout-config.json`
654- all_repos (List[str] ): repositories not required for current platform .
661+ all_repos (bool ): include all repositories .
655662
656663 Returns:
657664 List[str]: a resulting list of repositories to skip or empty list if
@@ -677,7 +684,7 @@ def skip_list_for_platform(config: Dict[str, Any], all_repos: List[str]) -> List
677684 return skip_list
678685
679686
680- def main ():
687+ def main () -> int :
681688 freeze_support ()
682689 args = CliArguments .parse_args ()
683690
@@ -704,7 +711,7 @@ def main():
704711 config = merge_config (config , json .load (f ))
705712 validate_config (config )
706713
707- cross_repos_pr = {}
714+ cross_repos_pr : Dict [ str , str ] = {}
708715 if args .github_comment :
709716 regex_pr = r'(apple/[-a-zA-Z0-9_]+/pull/\d+' \
710717 r'|apple/[-a-zA-Z0-9_]+#\d+' \
@@ -755,11 +762,11 @@ def main():
755762
756763 if args .dump_hashes :
757764 dump_repo_hashes (args , config )
758- return ( None , None )
765+ return 0
759766
760767 if args .dump_hashes_config :
761768 dump_repo_hashes (args , config , args .dump_hashes_config )
762- return ( None , None )
769+ return 0
763770
764771 # Quick check whether somebody is calling update in an empty directory
765772 directory_contents = os .listdir (args .source_root )
0 commit comments