Skip to content

Commit fc3e14b

Browse files
committed
update_checkout: re-read config for cross-repo testing
When updating versions of some dependencies, it's important to be able to re-read the checkout config to get the new versions to actually check them out.
1 parent a478034 commit fc3e14b

File tree

1 file changed

+69
-21
lines changed

1 file changed

+69
-21
lines changed

utils/update_checkout/update_checkout/update_checkout.py

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@ def find_rev_by_timestamp(timestamp, repo_name, refspec):
9797

9898
def get_branch_for_repo(config, repo_name, scheme_name, scheme_map,
9999
cross_repos_pr):
100+
"""Infer, fetch, and return a branch corresponding to a given PR, otherwise
101+
return a branch found in the config for this repository name.
102+
103+
Args:
104+
config (Dict[str, Any]): deserialized `update-checkout-config.json`
105+
repo_name (str): name of the repository for checking out the branch
106+
scheme_name (str): name of the scheme to look up in the config
107+
scheme_map (Dict[str, str]): map of repo names to branches to check out
108+
cross_repos_pr (Dict[str, str]): map of repo ids to PRs to check out
109+
110+
Returns:
111+
Tuple[str, bool]: a pair of a checked out branch and a boolean
112+
indicating whether this repo matched any `cross_repos_pr`.
113+
"""
114+
100115
cross_repo = False
101116
repo_branch = scheme_name
102117
if scheme_map:
@@ -249,17 +264,30 @@ def get_timestamp_to_match(args):
249264
echo=False).strip()
250265

251266

252-
def update_all_repositories(args, config, scheme_name, cross_repos_pr):
253-
scheme_map = None
267+
def get_scheme_map(config, scheme_name):
268+
"""Find a mapping from repository IDs to branches in the config.
269+
270+
Args:
271+
config (Dict[str, Any]): deserialized `update-checkout-config.json`
272+
scheme_name (str): name of the scheme to look up in `config`
273+
274+
Returns:
275+
Dict[str, str]: a mapping from repos to branches for the given scheme.
276+
"""
277+
254278
if scheme_name:
255279
# This loop is only correct, since we know that each alias set has
256280
# unique contents. This is checked by validate_config. Thus the first
257281
# branch scheme data that has scheme_name as one of its aliases is
258282
# the only possible correct answer.
259283
for v in config['branch-schemes'].values():
260284
if scheme_name in v['aliases']:
261-
scheme_map = v['repos']
262-
break
285+
return v['repos']
286+
287+
return None
288+
289+
290+
def update_all_repositories(args, config, scheme_name, scheme_map, cross_repos_pr):
263291
pool_args = []
264292
timestamp = get_timestamp_to_match(args)
265293
for repo_name in config['repos'].keys():
@@ -598,22 +626,14 @@ def main():
598626
clone_with_ssh = args.clone_with_ssh
599627
skip_history = args.skip_history
600628
skip_tags = args.skip_tags
601-
scheme = args.scheme
629+
scheme_name = args.scheme
602630
github_comment = args.github_comment
603631
all_repos = args.all_repositories
604632

605633
with open(args.config) as f:
606634
config = json.load(f)
607635
validate_config(config)
608636

609-
if args.dump_hashes:
610-
dump_repo_hashes(args, config)
611-
return (None, None)
612-
613-
if args.dump_hashes_config:
614-
dump_repo_hashes(args, config, args.dump_hashes_config)
615-
return (None, None)
616-
617637
cross_repos_pr = {}
618638
if github_comment:
619639
regex_pr = r'(apple/[-a-zA-Z0-9_]+/pull/\d+|apple/[-a-zA-Z0-9_]+#\d+)'
@@ -622,18 +642,46 @@ def main():
622642
repos_with_pr = [pr.replace('/pull/', '#') for pr in repos_with_pr]
623643
cross_repos_pr = dict(pr.split('#') for pr in repos_with_pr)
624644

645+
# If branch is None, default to using the default branch alias
646+
# specified by our configuration file.
647+
if scheme_name is None:
648+
scheme_name = config['default-branch-scheme']
649+
650+
scheme_map = get_scheme_map(config, scheme_name)
651+
652+
swift_repo_path = os.path.join(args.source_root, 'swift')
653+
if os.path.exists(swift_repo_path):
654+
with shell.pushd(swift_repo_path, dry_run=False, echo=True):
655+
# Check if `swift` repo itself needs to switch to a cross-repo branch.
656+
branch_name, cross_repo = get_branch_for_repo(config, 'swift',
657+
scheme_name,
658+
scheme_map,
659+
cross_repos_pr)
660+
661+
if cross_repo:
662+
shell.run(['git', 'checkout', branch_name], echo=True,
663+
prefix="[swift] ")
664+
665+
# Re-read the config after checkout.
666+
with open(args.config) as f:
667+
config = json.load(f)
668+
validate_config(config)
669+
670+
if args.dump_hashes:
671+
dump_repo_hashes(args, config)
672+
return (None, None)
673+
674+
if args.dump_hashes_config:
675+
dump_repo_hashes(args, config, args.dump_hashes_config)
676+
return (None, None)
677+
625678
clone_results = None
626679
if clone or clone_with_ssh:
627-
# If branch is None, default to using the default branch alias
628-
# specified by our configuration file.
629-
if scheme is None:
630-
scheme = config['default-branch-scheme']
631-
632680
skip_repo_list = skip_list_for_platform(config, all_repos)
633681
skip_repo_list.extend(args.skip_repository_list)
634682
clone_results = obtain_all_additional_swift_sources(args, config,
635683
clone_with_ssh,
636-
scheme,
684+
scheme_name,
637685
skip_history,
638686
skip_tags,
639687
skip_repo_list)
@@ -646,8 +694,8 @@ def main():
646694
print("You don't have all swift sources. "
647695
"Call this script with --clone to get them.")
648696

649-
update_results = update_all_repositories(args, config, scheme,
650-
cross_repos_pr)
697+
update_results = update_all_repositories(args, config, scheme_name,
698+
scheme_map, cross_repos_pr)
651699
fail_count = 0
652700
fail_count += check_parallel_results(clone_results, "CLONE")
653701
fail_count += check_parallel_results(update_results, "UPDATE")

0 commit comments

Comments
 (0)