# HG changeset patch # User Pierre-Yves David # Date 2024-02-26 14:23:45 # Node ID 87b830e4de35cea6e64bfad87572e354816da771 # Parent de1bc7db9f61dbb5760322a0fca9a012876b836a branchcache: move the header loading in a `_load_header` class method This will help changing header parsing in format variants. diff --git a/mercurial/branchmap.py b/mercurial/branchmap.py --- a/mercurial/branchmap.py +++ b/mercurial/branchmap.py @@ -15,6 +15,7 @@ from .node import ( ) from typing import ( + Any, Callable, Dict, Iterable, @@ -473,18 +474,11 @@ class branchcache(_BaseBranchCache): try: f = repo.cachevfs(cls._filename(repo)) lineiter = iter(f) - cachekey = next(lineiter).rstrip(b'\n').split(b" ", 2) - last, lrev = cachekey[:2] - last, lrev = bin(last), int(lrev) - filteredhash = None - if len(cachekey) > 2: - filteredhash = bin(cachekey[2]) + init_kwargs = cls._load_header(repo, lineiter) bcache = cls( repo, - tipnode=last, - tiprev=lrev, - filteredhash=filteredhash, verify_node=True, + **init_kwargs, ) if not bcache.validfor(repo): # invalidate the cache @@ -509,6 +503,24 @@ class branchcache(_BaseBranchCache): return bcache + @classmethod + def _load_header(cls, repo, lineiter) -> "dict[str, Any]": + """parse the head of a branchmap file + + return parameters to pass to a newly created class instance. + """ + cachekey = next(lineiter).rstrip(b'\n').split(b" ", 2) + last, lrev = cachekey[:2] + last, lrev = bin(last), int(lrev) + filteredhash = None + if len(cachekey) > 2: + filteredhash = bin(cachekey[2]) + return { + "tipnode": last, + "tiprev": lrev, + "filteredhash": filteredhash, + } + def _load_heads(self, repo, lineiter): """fully loads the branchcache by reading from the file using the line iterator passed"""