##// END OF EJS Templates
flagutil: introduce a flagprocessorsmixin class...
marmoute -
r43140:5bb68fb7 default
parent child Browse files
Show More
@@ -261,7 +261,7 b' class revlogio(object):'
261 p = versionformat_pack(version) + p[4:]
261 p = versionformat_pack(version) + p[4:]
262 return p
262 return p
263
263
264 class revlog(object):
264 class revlog(flagutil.flagprocessorsmixin):
265 """
265 """
266 the underlying revision storage object
266 the underlying revision storage object
267
267
@@ -1715,69 +1715,6 b' class revlog(object):'
1715 """
1715 """
1716 return storageutil.hashrevisionsha1(text, p1, p2)
1716 return storageutil.hashrevisionsha1(text, p1, p2)
1717
1717
1718 def _processflags(self, text, flags, operation, raw=False):
1719 """Inspect revision data flags and applies transforms defined by
1720 registered flag processors.
1721
1722 ``text`` - the revision data to process
1723 ``flags`` - the revision flags
1724 ``operation`` - the operation being performed (read or write)
1725 ``raw`` - an optional argument describing if the raw transform should be
1726 applied.
1727
1728 This method processes the flags in the order (or reverse order if
1729 ``operation`` is 'write') defined by REVIDX_FLAGS_ORDER, applying the
1730 flag processors registered for present flags. The order of flags defined
1731 in REVIDX_FLAGS_ORDER needs to be stable to allow non-commutativity.
1732
1733 Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the
1734 processed text and ``validatehash`` is a bool indicating whether the
1735 returned text should be checked for hash integrity.
1736
1737 Note: If the ``raw`` argument is set, it has precedence over the
1738 operation and will only update the value of ``validatehash``.
1739 """
1740 # fast path: no flag processors will run
1741 if flags == 0:
1742 return text, True
1743 if not operation in ('read', 'write'):
1744 raise error.ProgrammingError(_("invalid '%s' operation") %
1745 operation)
1746 # Check all flags are known.
1747 if flags & ~flagutil.REVIDX_KNOWN_FLAGS:
1748 raise error.RevlogError(_("incompatible revision flag '%#x'") %
1749 (flags & ~flagutil.REVIDX_KNOWN_FLAGS))
1750 validatehash = True
1751 # Depending on the operation (read or write), the order might be
1752 # reversed due to non-commutative transforms.
1753 orderedflags = REVIDX_FLAGS_ORDER
1754 if operation == 'write':
1755 orderedflags = reversed(orderedflags)
1756
1757 for flag in orderedflags:
1758 # If a flagprocessor has been registered for a known flag, apply the
1759 # related operation transform and update result tuple.
1760 if flag & flags:
1761 vhash = True
1762
1763 if flag not in self._flagprocessors:
1764 message = _("missing processor for flag '%#x'") % (flag)
1765 raise error.RevlogError(message)
1766
1767 processor = self._flagprocessors[flag]
1768 if processor is not None:
1769 readtransform, writetransform, rawtransform = processor
1770
1771 if raw:
1772 vhash = rawtransform(self, text)
1773 elif operation == 'read':
1774 text, vhash = readtransform(self, text)
1775 else: # write operation
1776 text, vhash = writetransform(self, text)
1777 validatehash = validatehash and vhash
1778
1779 return text, validatehash
1780
1781 def checkhash(self, text, node, p1=None, p2=None, rev=None):
1718 def checkhash(self, text, node, p1=None, p2=None, rev=None):
1782 """Check node hash integrity.
1719 """Check node hash integrity.
1783
1720
@@ -78,3 +78,74 b' def insertflagprocessor(flag, processor,'
78 msg = _("cannot register multiple processors on flag '%#x'.") % (flag)
78 msg = _("cannot register multiple processors on flag '%#x'.") % (flag)
79 raise error.Abort(msg)
79 raise error.Abort(msg)
80 flagprocessors[flag] = processor
80 flagprocessors[flag] = processor
81
82 class flagprocessorsmixin(object):
83 """basic mixin to support revlog flag processing
84
85 Make sure the `_flagprocessors` attribute is set at ``__init__`` time.
86
87 See the documentation of the ``_processflags`` method for details.
88 """
89
90 def _processflags(self, text, flags, operation, raw=False):
91 """Inspect revision data flags and applies transforms defined by
92 registered flag processors.
93
94 ``text`` - the revision data to process
95 ``flags`` - the revision flags
96 ``operation`` - the operation being performed (read or write)
97 ``raw`` - an optional argument describing if the raw transform should be
98 applied.
99
100 This method processes the flags in the order (or reverse order if
101 ``operation`` is 'write') defined by REVIDX_FLAGS_ORDER, applying the
102 flag processors registered for present flags. The order of flags defined
103 in REVIDX_FLAGS_ORDER needs to be stable to allow non-commutativity.
104
105 Returns a 2-tuple of ``(text, validatehash)`` where ``text`` is the
106 processed text and ``validatehash`` is a bool indicating whether the
107 returned text should be checked for hash integrity.
108
109 Note: If the ``raw`` argument is set, it has precedence over the
110 operation and will only update the value of ``validatehash``.
111 """
112 # fast path: no flag processors will run
113 if flags == 0:
114 return text, True
115 if not operation in ('read', 'write'):
116 raise error.ProgrammingError(_("invalid '%s' operation") %
117 operation)
118 # Check all flags are known.
119 if flags & ~REVIDX_KNOWN_FLAGS:
120 raise error.RevlogError(_("incompatible revision flag '%#x'") %
121 (flags & ~REVIDX_KNOWN_FLAGS))
122 validatehash = True
123 # Depending on the operation (read or write), the order might be
124 # reversed due to non-commutative transforms.
125 orderedflags = REVIDX_FLAGS_ORDER
126 if operation == 'write':
127 orderedflags = reversed(orderedflags)
128
129 for flag in orderedflags:
130 # If a flagprocessor has been registered for a known flag, apply the
131 # related operation transform and update result tuple.
132 if flag & flags:
133 vhash = True
134
135 if flag not in self._flagprocessors:
136 message = _("missing processor for flag '%#x'") % (flag)
137 raise error.RevlogError(message)
138
139 processor = self._flagprocessors[flag]
140 if processor is not None:
141 readtransform, writetransform, rawtransform = processor
142
143 if raw:
144 vhash = rawtransform(self, text)
145 elif operation == 'read':
146 text, vhash = readtransform(self, text)
147 else: # write operation
148 text, vhash = writetransform(self, text)
149 validatehash = validatehash and vhash
150
151 return text, validatehash
General Comments 0
You need to be logged in to leave comments. Login now