diff options
author | LaMont Jones <lamontjones@google.com> | 2022-09-01 15:15:04 +0000 |
---|---|---|
committer | LaMont Jones <lamontjones@google.com> | 2022-09-19 22:03:18 +0000 |
commit | 1eddca847625c50d985d9310e2bee2901c909925 (patch) | |
tree | 9c4383b0c729bf7cb9573264c9d0973eea52331d /project.py | |
parent | aefa4d3a29f35b00ce7e94870435b0a63c1e1612 (diff) | |
download | git-repo-1eddca847625c50d985d9310e2bee2901c909925.tar.gz |
sync: use namedtuples for internal return values
Replace tuple returns with namedtuples, to simplify adding new fields.
Extend the Sync_NetworkHalf return value to:
- success: True if successful (the former return value)
- remote_fetched: True if we called `git fetch`
Change-Id: If63c24c2f849523f77fa19c05bbf23a5e9a20ba9
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/344534
Reviewed-by: Mike Frysinger <vapier@google.com>
Tested-by: LaMont Jones <lamontjones@google.com>
Diffstat (limited to 'project.py')
-rw-r--r-- | project.py | 27 |
1 files changed, 19 insertions, 8 deletions
@@ -26,6 +26,7 @@ import sys | |||
26 | import tarfile | 26 | import tarfile |
27 | import tempfile | 27 | import tempfile |
28 | import time | 28 | import time |
29 | from typing import NamedTuple | ||
29 | import urllib.parse | 30 | import urllib.parse |
30 | 31 | ||
31 | from color import Coloring | 32 | from color import Coloring |
@@ -45,6 +46,14 @@ from repo_trace import IsTrace, Trace | |||
45 | from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M, R_WORKTREE_M | 46 | from git_refs import GitRefs, HEAD, R_HEADS, R_TAGS, R_PUB, R_M, R_WORKTREE_M |
46 | 47 | ||
47 | 48 | ||
49 | class SyncNetworkHalfResult(NamedTuple): | ||
50 | """Sync_NetworkHalf return value.""" | ||
51 | # True if successful. | ||
52 | success: bool | ||
53 | # Did we query the remote? False when optimized_fetch is True and we have the | ||
54 | # commit already present. | ||
55 | remote_fetched: bool | ||
56 | |||
48 | # Maximum sleep time allowed during retries. | 57 | # Maximum sleep time allowed during retries. |
49 | MAXIMUM_RETRY_SLEEP_SEC = 3600.0 | 58 | MAXIMUM_RETRY_SLEEP_SEC = 3600.0 |
50 | # +-10% random jitter is added to each Fetches retry sleep duration. | 59 | # +-10% random jitter is added to each Fetches retry sleep duration. |
@@ -1133,7 +1142,7 @@ class Project(object): | |||
1133 | if archive and not isinstance(self, MetaProject): | 1142 | if archive and not isinstance(self, MetaProject): |
1134 | if self.remote.url.startswith(('http://', 'https://')): | 1143 | if self.remote.url.startswith(('http://', 'https://')): |
1135 | _error("%s: Cannot fetch archives from http/https remotes.", self.name) | 1144 | _error("%s: Cannot fetch archives from http/https remotes.", self.name) |
1136 | return False | 1145 | return SyncNetworkHalfResult(False, False) |
1137 | 1146 | ||
1138 | name = self.relpath.replace('\\', '/') | 1147 | name = self.relpath.replace('\\', '/') |
1139 | name = name.replace('/', '_') | 1148 | name = name.replace('/', '_') |
@@ -1144,19 +1153,19 @@ class Project(object): | |||
1144 | self._FetchArchive(tarpath, cwd=topdir) | 1153 | self._FetchArchive(tarpath, cwd=topdir) |
1145 | except GitError as e: | 1154 | except GitError as e: |
1146 | _error('%s', e) | 1155 | _error('%s', e) |
1147 | return False | 1156 | return SyncNetworkHalfResult(False, False) |
1148 | 1157 | ||
1149 | # From now on, we only need absolute tarpath | 1158 | # From now on, we only need absolute tarpath |
1150 | tarpath = os.path.join(topdir, tarpath) | 1159 | tarpath = os.path.join(topdir, tarpath) |
1151 | 1160 | ||
1152 | if not self._ExtractArchive(tarpath, path=topdir): | 1161 | if not self._ExtractArchive(tarpath, path=topdir): |
1153 | return False | 1162 | return SyncNetworkHalfResult(False, True) |
1154 | try: | 1163 | try: |
1155 | platform_utils.remove(tarpath) | 1164 | platform_utils.remove(tarpath) |
1156 | except OSError as e: | 1165 | except OSError as e: |
1157 | _warn("Cannot remove archive %s: %s", tarpath, str(e)) | 1166 | _warn("Cannot remove archive %s: %s", tarpath, str(e)) |
1158 | self._CopyAndLinkFiles() | 1167 | self._CopyAndLinkFiles() |
1159 | return True | 1168 | return SyncNetworkHalfResult(True, True) |
1160 | 1169 | ||
1161 | # If the shared object dir already exists, don't try to rebootstrap with a | 1170 | # If the shared object dir already exists, don't try to rebootstrap with a |
1162 | # clone bundle download. We should have the majority of objects already. | 1171 | # clone bundle download. We should have the majority of objects already. |
@@ -1220,9 +1229,11 @@ class Project(object): | |||
1220 | depth = self.manifest.manifestProject.depth | 1229 | depth = self.manifest.manifestProject.depth |
1221 | 1230 | ||
1222 | # See if we can skip the network fetch entirely. | 1231 | # See if we can skip the network fetch entirely. |
1232 | remote_fetched = False | ||
1223 | if not (optimized_fetch and | 1233 | if not (optimized_fetch and |
1224 | (ID_RE.match(self.revisionExpr) and | 1234 | (ID_RE.match(self.revisionExpr) and |
1225 | self._CheckForImmutableRevision())): | 1235 | self._CheckForImmutableRevision())): |
1236 | remote_fetched = True | ||
1226 | if not self._RemoteFetch( | 1237 | if not self._RemoteFetch( |
1227 | initial=is_new, | 1238 | initial=is_new, |
1228 | quiet=quiet, verbose=verbose, output_redir=output_redir, | 1239 | quiet=quiet, verbose=verbose, output_redir=output_redir, |
@@ -1231,7 +1242,7 @@ class Project(object): | |||
1231 | submodules=submodules, force_sync=force_sync, | 1242 | submodules=submodules, force_sync=force_sync, |
1232 | ssh_proxy=ssh_proxy, | 1243 | ssh_proxy=ssh_proxy, |
1233 | clone_filter=clone_filter, retry_fetches=retry_fetches): | 1244 | clone_filter=clone_filter, retry_fetches=retry_fetches): |
1234 | return False | 1245 | return SyncNetworkHalfResult(False, remote_fetched) |
1235 | 1246 | ||
1236 | mp = self.manifest.manifestProject | 1247 | mp = self.manifest.manifestProject |
1237 | dissociate = mp.dissociate | 1248 | dissociate = mp.dissociate |
@@ -1244,7 +1255,7 @@ class Project(object): | |||
1244 | if p.stdout and output_redir: | 1255 | if p.stdout and output_redir: |
1245 | output_redir.write(p.stdout) | 1256 | output_redir.write(p.stdout) |
1246 | if p.Wait() != 0: | 1257 | if p.Wait() != 0: |
1247 | return False | 1258 | return SyncNetworkHalfResult(False, remote_fetched) |
1248 | platform_utils.remove(alternates_file) | 1259 | platform_utils.remove(alternates_file) |
1249 | 1260 | ||
1250 | if self.worktree: | 1261 | if self.worktree: |
@@ -1253,7 +1264,7 @@ class Project(object): | |||
1253 | self._InitMirrorHead() | 1264 | self._InitMirrorHead() |
1254 | platform_utils.remove(os.path.join(self.gitdir, 'FETCH_HEAD'), | 1265 | platform_utils.remove(os.path.join(self.gitdir, 'FETCH_HEAD'), |
1255 | missing_ok=True) | 1266 | missing_ok=True) |
1256 | return True | 1267 | return SyncNetworkHalfResult(True, remote_fetched) |
1257 | 1268 | ||
1258 | def PostRepoUpgrade(self): | 1269 | def PostRepoUpgrade(self): |
1259 | self._InitHooks() | 1270 | self._InitHooks() |
@@ -3836,7 +3847,7 @@ class ManifestProject(MetaProject): | |||
3836 | is_new=is_new, quiet=not verbose, verbose=verbose, | 3847 | is_new=is_new, quiet=not verbose, verbose=verbose, |
3837 | clone_bundle=clone_bundle, current_branch_only=current_branch_only, | 3848 | clone_bundle=clone_bundle, current_branch_only=current_branch_only, |
3838 | tags=tags, submodules=submodules, clone_filter=clone_filter, | 3849 | tags=tags, submodules=submodules, clone_filter=clone_filter, |
3839 | partial_clone_exclude=self.manifest.PartialCloneExclude): | 3850 | partial_clone_exclude=self.manifest.PartialCloneExclude).success: |
3840 | r = self.GetRemote() | 3851 | r = self.GetRemote() |
3841 | print('fatal: cannot obtain manifest %s' % r.url, file=sys.stderr) | 3852 | print('fatal: cannot obtain manifest %s' % r.url, file=sys.stderr) |
3842 | 3853 | ||