Mercurial > public > mercurial-scm > hg
comparison mercurial/branchmap.py @ 51460:87b830e4de35
branchcache: move the header loading in a `_load_header` class method
This will help changing header parsing in format variants.
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Mon, 26 Feb 2024 15:23:45 +0100 |
parents | de1bc7db9f61 |
children | 09782c097035 |
comparison
equal
deleted
inserted
replaced
51459:de1bc7db9f61 | 51460:87b830e4de35 |
---|---|
13 hex, | 13 hex, |
14 nullrev, | 14 nullrev, |
15 ) | 15 ) |
16 | 16 |
17 from typing import ( | 17 from typing import ( |
18 Any, | |
18 Callable, | 19 Callable, |
19 Dict, | 20 Dict, |
20 Iterable, | 21 Iterable, |
21 List, | 22 List, |
22 Optional, | 23 Optional, |
471 def fromfile(cls, repo): | 472 def fromfile(cls, repo): |
472 f = None | 473 f = None |
473 try: | 474 try: |
474 f = repo.cachevfs(cls._filename(repo)) | 475 f = repo.cachevfs(cls._filename(repo)) |
475 lineiter = iter(f) | 476 lineiter = iter(f) |
476 cachekey = next(lineiter).rstrip(b'\n').split(b" ", 2) | 477 init_kwargs = cls._load_header(repo, lineiter) |
477 last, lrev = cachekey[:2] | |
478 last, lrev = bin(last), int(lrev) | |
479 filteredhash = None | |
480 if len(cachekey) > 2: | |
481 filteredhash = bin(cachekey[2]) | |
482 bcache = cls( | 478 bcache = cls( |
483 repo, | 479 repo, |
484 tipnode=last, | |
485 tiprev=lrev, | |
486 filteredhash=filteredhash, | |
487 verify_node=True, | 480 verify_node=True, |
481 **init_kwargs, | |
488 ) | 482 ) |
489 if not bcache.validfor(repo): | 483 if not bcache.validfor(repo): |
490 # invalidate the cache | 484 # invalidate the cache |
491 raise ValueError('tip differs') | 485 raise ValueError('tip differs') |
492 bcache._load_heads(repo, lineiter) | 486 bcache._load_heads(repo, lineiter) |
506 finally: | 500 finally: |
507 if f: | 501 if f: |
508 f.close() | 502 f.close() |
509 | 503 |
510 return bcache | 504 return bcache |
505 | |
506 @classmethod | |
507 def _load_header(cls, repo, lineiter) -> "dict[str, Any]": | |
508 """parse the head of a branchmap file | |
509 | |
510 return parameters to pass to a newly created class instance. | |
511 """ | |
512 cachekey = next(lineiter).rstrip(b'\n').split(b" ", 2) | |
513 last, lrev = cachekey[:2] | |
514 last, lrev = bin(last), int(lrev) | |
515 filteredhash = None | |
516 if len(cachekey) > 2: | |
517 filteredhash = bin(cachekey[2]) | |
518 return { | |
519 "tipnode": last, | |
520 "tiprev": lrev, | |
521 "filteredhash": filteredhash, | |
522 } | |
511 | 523 |
512 def _load_heads(self, repo, lineiter): | 524 def _load_heads(self, repo, lineiter): |
513 """fully loads the branchcache by reading from the file using the line | 525 """fully loads the branchcache by reading from the file using the line |
514 iterator passed""" | 526 iterator passed""" |
515 for line in lineiter: | 527 for line in lineiter: |