diff options
author | Mike Frysinger <vapier@google.com> | 2021-05-05 20:03:26 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2021-05-06 19:46:09 +0000 |
commit | b44294395f44352d6d9592e896587dd46eea5b6d (patch) | |
tree | 939ed400b03eb1792f01c2a06de2dd553a549f8e /subcmds/sync.py | |
parent | 5291eafa412117b80ebbf645fc51559dd0b2caaf (diff) | |
download | git-repo-b44294395f44352d6d9592e896587dd46eea5b6d.tar.gz |
sync: refactor main fetch loop
This is a large chunk of code that is largely isolated. Move it into
a class method to make it easier to manage & reason about, and in a
follow up CL, easier to scope.
Bug: https://crbug.com/gerrit/12389
Change-Id: I0c69d95a9e03478d347b761580b2343bffa012d5
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/305484
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: Chris Mcdonald <cjmcdonald@google.com>
Diffstat (limited to 'subcmds/sync.py')
-rw-r--r-- | subcmds/sync.py | 101 |
1 files changed, 59 insertions, 42 deletions
diff --git a/subcmds/sync.py b/subcmds/sync.py index d3c326ac..381e9e77 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py | |||
@@ -439,6 +439,63 @@ later is required to fix a server side protocol bug. | |||
439 | 439 | ||
440 | return (ret, fetched) | 440 | return (ret, fetched) |
441 | 441 | ||
442 | def _FetchMain(self, opt, args, all_projects, err_event, manifest_name, | ||
443 | load_local_manifests): | ||
444 | """The main network fetch loop. | ||
445 | |||
446 | Args: | ||
447 | opt: Program options returned from optparse. See _Options(). | ||
448 | args: Command line args used to filter out projects. | ||
449 | all_projects: List of all projects that should be checked out. | ||
450 | err_event: Whether an error was hit while processing. | ||
451 | manifest_name: Manifest file to be reloaded. | ||
452 | load_local_manifests: Whether to load local manifests. | ||
453 | """ | ||
454 | rp = self.manifest.repoProject | ||
455 | |||
456 | to_fetch = [] | ||
457 | now = time.time() | ||
458 | if _ONE_DAY_S <= (now - rp.LastFetch): | ||
459 | to_fetch.append(rp) | ||
460 | to_fetch.extend(all_projects) | ||
461 | to_fetch.sort(key=self._fetch_times.Get, reverse=True) | ||
462 | |||
463 | success, fetched = self._Fetch(to_fetch, opt, err_event) | ||
464 | if not success: | ||
465 | err_event.set() | ||
466 | |||
467 | _PostRepoFetch(rp, opt.repo_verify) | ||
468 | if opt.network_only: | ||
469 | # bail out now; the rest touches the working tree | ||
470 | if err_event.is_set(): | ||
471 | print('\nerror: Exited sync due to fetch errors.\n', file=sys.stderr) | ||
472 | sys.exit(1) | ||
473 | return | ||
474 | |||
475 | # Iteratively fetch missing and/or nested unregistered submodules | ||
476 | previously_missing_set = set() | ||
477 | while True: | ||
478 | self._ReloadManifest(manifest_name, load_local_manifests) | ||
479 | all_projects = self.GetProjects(args, | ||
480 | missing_ok=True, | ||
481 | submodules_ok=opt.fetch_submodules) | ||
482 | missing = [] | ||
483 | for project in all_projects: | ||
484 | if project.gitdir not in fetched: | ||
485 | missing.append(project) | ||
486 | if not missing: | ||
487 | break | ||
488 | # Stop us from non-stopped fetching actually-missing repos: If set of | ||
489 | # missing repos has not been changed from last fetch, we break. | ||
490 | missing_set = set(p.name for p in missing) | ||
491 | if previously_missing_set == missing_set: | ||
492 | break | ||
493 | previously_missing_set = missing_set | ||
494 | success, new_fetched = self._Fetch(missing, opt, err_event) | ||
495 | if not success: | ||
496 | err_event.set() | ||
497 | fetched.update(new_fetched) | ||
498 | |||
442 | def _CheckoutOne(self, detach_head, force_sync, project): | 499 | def _CheckoutOne(self, detach_head, force_sync, project): |
443 | """Checkout work tree for one project | 500 | """Checkout work tree for one project |
444 | 501 | ||
@@ -921,48 +978,8 @@ later is required to fix a server side protocol bug. | |||
921 | 978 | ||
922 | self._fetch_times = _FetchTimes(self.manifest) | 979 | self._fetch_times = _FetchTimes(self.manifest) |
923 | if not opt.local_only: | 980 | if not opt.local_only: |
924 | to_fetch = [] | 981 | self._FetchMain(opt, args, all_projects, err_event, manifest_name, |
925 | now = time.time() | 982 | load_local_manifests) |
926 | if _ONE_DAY_S <= (now - rp.LastFetch): | ||
927 | to_fetch.append(rp) | ||
928 | to_fetch.extend(all_projects) | ||
929 | to_fetch.sort(key=self._fetch_times.Get, reverse=True) | ||
930 | |||
931 | success, fetched = self._Fetch(to_fetch, opt, err_event) | ||
932 | if not success: | ||
933 | err_event.set() | ||
934 | |||
935 | _PostRepoFetch(rp, opt.repo_verify) | ||
936 | if opt.network_only: | ||
937 | # bail out now; the rest touches the working tree | ||
938 | if err_event.is_set(): | ||
939 | print('\nerror: Exited sync due to fetch errors.\n', file=sys.stderr) | ||
940 | sys.exit(1) | ||
941 | return | ||
942 | |||
943 | # Iteratively fetch missing and/or nested unregistered submodules | ||
944 | previously_missing_set = set() | ||
945 | while True: | ||
946 | self._ReloadManifest(manifest_name, load_local_manifests) | ||
947 | all_projects = self.GetProjects(args, | ||
948 | missing_ok=True, | ||
949 | submodules_ok=opt.fetch_submodules) | ||
950 | missing = [] | ||
951 | for project in all_projects: | ||
952 | if project.gitdir not in fetched: | ||
953 | missing.append(project) | ||
954 | if not missing: | ||
955 | break | ||
956 | # Stop us from non-stopped fetching actually-missing repos: If set of | ||
957 | # missing repos has not been changed from last fetch, we break. | ||
958 | missing_set = set(p.name for p in missing) | ||
959 | if previously_missing_set == missing_set: | ||
960 | break | ||
961 | previously_missing_set = missing_set | ||
962 | success, new_fetched = self._Fetch(missing, opt, err_event) | ||
963 | if not success: | ||
964 | err_event.set() | ||
965 | fetched.update(new_fetched) | ||
966 | 983 | ||
967 | # If we saw an error, exit with code 1 so that other scripts can check. | 984 | # If we saw an error, exit with code 1 so that other scripts can check. |
968 | if err_event.is_set(): | 985 | if err_event.is_set(): |