Mercurial > public > mercurial-scm > hg
comparison mercurial/filelog.py @ 37442:0596d27457c6
revlog: move parsemeta() and packmeta() from filelog (API)
filelog.parsemeta() and filelog.packmeta() are used to decode
and encode metadata for file copies and censor.
An upcoming commit will move the core logic for censoring revlogs
into revlog.py. This would create a cycle between revlog.py and
filelog.py. So we move these metadata functions to revlog.py.
.. api::
filelog.parsemeta() and filelog.packmeta() have been moved to
the revlog module.
Differential Revision: https://phab.mercurial-scm.org/D3150
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Thu, 05 Apr 2018 18:22:35 -0700 |
parents | a3202fa83aff |
children | 65250a66b55c |
comparison
equal
deleted
inserted
replaced
37441:a3202fa83aff | 37442:0596d27457c6 |
---|---|
5 # This software may be used and distributed according to the terms of the | 5 # This software may be used and distributed according to the terms of the |
6 # GNU General Public License version 2 or any later version. | 6 # GNU General Public License version 2 or any later version. |
7 | 7 |
8 from __future__ import absolute_import | 8 from __future__ import absolute_import |
9 | 9 |
10 import re | |
11 import struct | 10 import struct |
12 | 11 |
13 from .thirdparty.zope import ( | 12 from .thirdparty.zope import ( |
14 interface as zi, | 13 interface as zi, |
15 ) | 14 ) |
18 mdiff, | 17 mdiff, |
19 repository, | 18 repository, |
20 revlog, | 19 revlog, |
21 ) | 20 ) |
22 | 21 |
23 _mdre = re.compile('\1\n') | |
24 def parsemeta(text): | |
25 """return (metadatadict, metadatasize)""" | |
26 # text can be buffer, so we can't use .startswith or .index | |
27 if text[:2] != '\1\n': | |
28 return None, None | |
29 s = _mdre.search(text, 2).start() | |
30 mtext = text[2:s] | |
31 meta = {} | |
32 for l in mtext.splitlines(): | |
33 k, v = l.split(": ", 1) | |
34 meta[k] = v | |
35 return meta, (s + 2) | |
36 | |
37 def packmeta(meta, text): | |
38 keys = sorted(meta) | |
39 metatext = "".join("%s: %s\n" % (k, meta[k]) for k in keys) | |
40 return "\1\n%s\1\n%s" % (metatext, text) | |
41 | |
42 def _censoredtext(text): | 22 def _censoredtext(text): |
43 m, offs = parsemeta(text) | 23 m, offs = revlog.parsemeta(text) |
44 return m and "censored" in m | 24 return m and "censored" in m |
45 | 25 |
46 @zi.implementer(repository.ifilestorage) | 26 @zi.implementer(repository.ifilestorage) |
47 class filelog(revlog.revlog): | 27 class filelog(revlog.revlog): |
48 def __init__(self, opener, path): | 28 def __init__(self, opener, path): |
58 s = t.index('\1\n', 2) | 38 s = t.index('\1\n', 2) |
59 return t[s + 2:] | 39 return t[s + 2:] |
60 | 40 |
61 def add(self, text, meta, transaction, link, p1=None, p2=None): | 41 def add(self, text, meta, transaction, link, p1=None, p2=None): |
62 if meta or text.startswith('\1\n'): | 42 if meta or text.startswith('\1\n'): |
63 text = packmeta(meta, text) | 43 text = revlog.packmeta(meta, text) |
64 return self.addrevision(text, transaction, link, p1, p2) | 44 return self.addrevision(text, transaction, link, p1, p2) |
65 | 45 |
66 def renamed(self, node): | 46 def renamed(self, node): |
67 if self.parents(node)[0] != revlog.nullid: | 47 if self.parents(node)[0] != revlog.nullid: |
68 return False | 48 return False |
69 t = self.revision(node) | 49 t = self.revision(node) |
70 m = parsemeta(t)[0] | 50 m = revlog.parsemeta(t)[0] |
71 if m and "copy" in m: | 51 if m and "copy" in m: |
72 return (m["copy"], revlog.bin(m["copyrev"])) | 52 return (m["copy"], revlog.bin(m["copyrev"])) |
73 return False | 53 return False |
74 | 54 |
75 def size(self, rev): | 55 def size(self, rev): |