Show More
@@ -1,1146 +1,1145 b'' | |||||
1 | # obsolete.py - obsolete markers handling |
|
1 | # obsolete.py - obsolete markers handling | |
2 | # |
|
2 | # | |
3 | # Copyright 2012 Pierre-Yves David <pierre-yves.david@ens-lyon.org> |
|
3 | # Copyright 2012 Pierre-Yves David <pierre-yves.david@ens-lyon.org> | |
4 | # Logilab SA <contact@logilab.fr> |
|
4 | # Logilab SA <contact@logilab.fr> | |
5 | # |
|
5 | # | |
6 | # This software may be used and distributed according to the terms of the |
|
6 | # This software may be used and distributed according to the terms of the | |
7 | # GNU General Public License version 2 or any later version. |
|
7 | # GNU General Public License version 2 or any later version. | |
8 |
|
8 | |||
9 | """Obsolete marker handling |
|
9 | """Obsolete marker handling | |
10 |
|
10 | |||
11 | An obsolete marker maps an old changeset to a list of new |
|
11 | An obsolete marker maps an old changeset to a list of new | |
12 | changesets. If the list of new changesets is empty, the old changeset |
|
12 | changesets. If the list of new changesets is empty, the old changeset | |
13 | is said to be "killed". Otherwise, the old changeset is being |
|
13 | is said to be "killed". Otherwise, the old changeset is being | |
14 | "replaced" by the new changesets. |
|
14 | "replaced" by the new changesets. | |
15 |
|
15 | |||
16 | Obsolete markers can be used to record and distribute changeset graph |
|
16 | Obsolete markers can be used to record and distribute changeset graph | |
17 | transformations performed by history rewrite operations, and help |
|
17 | transformations performed by history rewrite operations, and help | |
18 | building new tools to reconcile conflicting rewrite actions. To |
|
18 | building new tools to reconcile conflicting rewrite actions. To | |
19 | facilitate conflict resolution, markers include various annotations |
|
19 | facilitate conflict resolution, markers include various annotations | |
20 | besides old and news changeset identifiers, such as creation date or |
|
20 | besides old and news changeset identifiers, such as creation date or | |
21 | author name. |
|
21 | author name. | |
22 |
|
22 | |||
23 | The old obsoleted changeset is called a "predecessor" and possible |
|
23 | The old obsoleted changeset is called a "predecessor" and possible | |
24 | replacements are called "successors". Markers that used changeset X as |
|
24 | replacements are called "successors". Markers that used changeset X as | |
25 | a predecessor are called "successor markers of X" because they hold |
|
25 | a predecessor are called "successor markers of X" because they hold | |
26 | information about the successors of X. Markers that use changeset Y as |
|
26 | information about the successors of X. Markers that use changeset Y as | |
27 | a successors are call "predecessor markers of Y" because they hold |
|
27 | a successors are call "predecessor markers of Y" because they hold | |
28 | information about the predecessors of Y. |
|
28 | information about the predecessors of Y. | |
29 |
|
29 | |||
30 | Examples: |
|
30 | Examples: | |
31 |
|
31 | |||
32 | - When changeset A is replaced by changeset A', one marker is stored: |
|
32 | - When changeset A is replaced by changeset A', one marker is stored: | |
33 |
|
33 | |||
34 | (A, (A',)) |
|
34 | (A, (A',)) | |
35 |
|
35 | |||
36 | - When changesets A and B are folded into a new changeset C, two markers are |
|
36 | - When changesets A and B are folded into a new changeset C, two markers are | |
37 | stored: |
|
37 | stored: | |
38 |
|
38 | |||
39 | (A, (C,)) and (B, (C,)) |
|
39 | (A, (C,)) and (B, (C,)) | |
40 |
|
40 | |||
41 | - When changeset A is simply "pruned" from the graph, a marker is created: |
|
41 | - When changeset A is simply "pruned" from the graph, a marker is created: | |
42 |
|
42 | |||
43 | (A, ()) |
|
43 | (A, ()) | |
44 |
|
44 | |||
45 | - When changeset A is split into B and C, a single marker is used: |
|
45 | - When changeset A is split into B and C, a single marker is used: | |
46 |
|
46 | |||
47 | (A, (B, C)) |
|
47 | (A, (B, C)) | |
48 |
|
48 | |||
49 | We use a single marker to distinguish the "split" case from the "divergence" |
|
49 | We use a single marker to distinguish the "split" case from the "divergence" | |
50 | case. If two independent operations rewrite the same changeset A in to A' and |
|
50 | case. If two independent operations rewrite the same changeset A in to A' and | |
51 | A'', we have an error case: divergent rewriting. We can detect it because |
|
51 | A'', we have an error case: divergent rewriting. We can detect it because | |
52 | two markers will be created independently: |
|
52 | two markers will be created independently: | |
53 |
|
53 | |||
54 | (A, (B,)) and (A, (C,)) |
|
54 | (A, (B,)) and (A, (C,)) | |
55 |
|
55 | |||
56 | Format |
|
56 | Format | |
57 | ------ |
|
57 | ------ | |
58 |
|
58 | |||
59 | Markers are stored in an append-only file stored in |
|
59 | Markers are stored in an append-only file stored in | |
60 | '.hg/store/obsstore'. |
|
60 | '.hg/store/obsstore'. | |
61 |
|
61 | |||
62 | The file starts with a version header: |
|
62 | The file starts with a version header: | |
63 |
|
63 | |||
64 | - 1 unsigned byte: version number, starting at zero. |
|
64 | - 1 unsigned byte: version number, starting at zero. | |
65 |
|
65 | |||
66 | The header is followed by the markers. Marker format depend of the version. See |
|
66 | The header is followed by the markers. Marker format depend of the version. See | |
67 | comment associated with each format for details. |
|
67 | comment associated with each format for details. | |
68 |
|
68 | |||
69 | """ |
|
69 | """ | |
70 | from __future__ import absolute_import |
|
70 | from __future__ import absolute_import | |
71 |
|
71 | |||
72 | import errno |
|
72 | import errno | |
73 | import struct |
|
73 | import struct | |
74 |
|
74 | |||
75 | from .i18n import _ |
|
75 | from .i18n import _ | |
76 | from .pycompat import getattr |
|
76 | from .pycompat import getattr | |
77 | from .node import ( |
|
77 | from .node import ( | |
78 | bin, |
|
78 | bin, | |
79 | hex, |
|
79 | hex, | |
80 | ) |
|
80 | ) | |
81 | from . import ( |
|
81 | from . import ( | |
82 | encoding, |
|
82 | encoding, | |
83 | error, |
|
83 | error, | |
84 | obsutil, |
|
84 | obsutil, | |
85 | phases, |
|
85 | phases, | |
86 | policy, |
|
86 | policy, | |
87 | pycompat, |
|
87 | pycompat, | |
88 | util, |
|
88 | util, | |
89 | ) |
|
89 | ) | |
90 | from .utils import ( |
|
90 | from .utils import ( | |
91 | dateutil, |
|
91 | dateutil, | |
92 | hashutil, |
|
92 | hashutil, | |
93 | ) |
|
93 | ) | |
94 |
|
94 | |||
95 | parsers = policy.importmod('parsers') |
|
95 | parsers = policy.importmod('parsers') | |
96 |
|
96 | |||
97 | _pack = struct.pack |
|
97 | _pack = struct.pack | |
98 | _unpack = struct.unpack |
|
98 | _unpack = struct.unpack | |
99 | _calcsize = struct.calcsize |
|
99 | _calcsize = struct.calcsize | |
100 | propertycache = util.propertycache |
|
100 | propertycache = util.propertycache | |
101 |
|
101 | |||
102 | # Options for obsolescence |
|
102 | # Options for obsolescence | |
103 | createmarkersopt = b'createmarkers' |
|
103 | createmarkersopt = b'createmarkers' | |
104 | allowunstableopt = b'allowunstable' |
|
104 | allowunstableopt = b'allowunstable' | |
105 | allowdivergenceopt = b'allowdivergence' |
|
105 | allowdivergenceopt = b'allowdivergence' | |
106 | exchangeopt = b'exchange' |
|
106 | exchangeopt = b'exchange' | |
107 |
|
107 | |||
108 |
|
108 | |||
109 | def _getoptionvalue(repo, option): |
|
109 | def _getoptionvalue(repo, option): | |
110 | """Returns True if the given repository has the given obsolete option |
|
110 | """Returns True if the given repository has the given obsolete option | |
111 | enabled. |
|
111 | enabled. | |
112 | """ |
|
112 | """ | |
113 | configkey = b'evolution.%s' % option |
|
113 | configkey = b'evolution.%s' % option | |
114 | newconfig = repo.ui.configbool(b'experimental', configkey) |
|
114 | newconfig = repo.ui.configbool(b'experimental', configkey) | |
115 |
|
115 | |||
116 | # Return the value only if defined |
|
116 | # Return the value only if defined | |
117 | if newconfig is not None: |
|
117 | if newconfig is not None: | |
118 | return newconfig |
|
118 | return newconfig | |
119 |
|
119 | |||
120 | # Fallback on generic option |
|
120 | # Fallback on generic option | |
121 | try: |
|
121 | try: | |
122 | return repo.ui.configbool(b'experimental', b'evolution') |
|
122 | return repo.ui.configbool(b'experimental', b'evolution') | |
123 | except (error.ConfigError, AttributeError): |
|
123 | except (error.ConfigError, AttributeError): | |
124 | # Fallback on old-fashion config |
|
124 | # Fallback on old-fashion config | |
125 | # inconsistent config: experimental.evolution |
|
125 | # inconsistent config: experimental.evolution | |
126 | result = set(repo.ui.configlist(b'experimental', b'evolution')) |
|
126 | result = set(repo.ui.configlist(b'experimental', b'evolution')) | |
127 |
|
127 | |||
128 | if b'all' in result: |
|
128 | if b'all' in result: | |
129 | return True |
|
129 | return True | |
130 |
|
130 | |||
131 | # Temporary hack for next check |
|
131 | # Temporary hack for next check | |
132 | newconfig = repo.ui.config(b'experimental', b'evolution.createmarkers') |
|
132 | newconfig = repo.ui.config(b'experimental', b'evolution.createmarkers') | |
133 | if newconfig: |
|
133 | if newconfig: | |
134 | result.add(b'createmarkers') |
|
134 | result.add(b'createmarkers') | |
135 |
|
135 | |||
136 | return option in result |
|
136 | return option in result | |
137 |
|
137 | |||
138 |
|
138 | |||
139 | def getoptions(repo): |
|
139 | def getoptions(repo): | |
140 | """Returns dicts showing state of obsolescence features.""" |
|
140 | """Returns dicts showing state of obsolescence features.""" | |
141 |
|
141 | |||
142 | createmarkersvalue = _getoptionvalue(repo, createmarkersopt) |
|
142 | createmarkersvalue = _getoptionvalue(repo, createmarkersopt) | |
143 | if createmarkersvalue: |
|
143 | if createmarkersvalue: | |
144 | unstablevalue = _getoptionvalue(repo, allowunstableopt) |
|
144 | unstablevalue = _getoptionvalue(repo, allowunstableopt) | |
145 | divergencevalue = _getoptionvalue(repo, allowdivergenceopt) |
|
145 | divergencevalue = _getoptionvalue(repo, allowdivergenceopt) | |
146 | exchangevalue = _getoptionvalue(repo, exchangeopt) |
|
146 | exchangevalue = _getoptionvalue(repo, exchangeopt) | |
147 | else: |
|
147 | else: | |
148 | # if we cannot create obsolescence markers, we shouldn't exchange them |
|
148 | # if we cannot create obsolescence markers, we shouldn't exchange them | |
149 | # or perform operations that lead to instability or divergence |
|
149 | # or perform operations that lead to instability or divergence | |
150 | unstablevalue = False |
|
150 | unstablevalue = False | |
151 | divergencevalue = False |
|
151 | divergencevalue = False | |
152 | exchangevalue = False |
|
152 | exchangevalue = False | |
153 |
|
153 | |||
154 | return { |
|
154 | return { | |
155 | createmarkersopt: createmarkersvalue, |
|
155 | createmarkersopt: createmarkersvalue, | |
156 | allowunstableopt: unstablevalue, |
|
156 | allowunstableopt: unstablevalue, | |
157 | allowdivergenceopt: divergencevalue, |
|
157 | allowdivergenceopt: divergencevalue, | |
158 | exchangeopt: exchangevalue, |
|
158 | exchangeopt: exchangevalue, | |
159 | } |
|
159 | } | |
160 |
|
160 | |||
161 |
|
161 | |||
162 | def isenabled(repo, option): |
|
162 | def isenabled(repo, option): | |
163 | """Returns True if the given repository has the given obsolete option |
|
163 | """Returns True if the given repository has the given obsolete option | |
164 | enabled. |
|
164 | enabled. | |
165 | """ |
|
165 | """ | |
166 | return getoptions(repo)[option] |
|
166 | return getoptions(repo)[option] | |
167 |
|
167 | |||
168 |
|
168 | |||
169 | # Creating aliases for marker flags because evolve extension looks for |
|
169 | # Creating aliases for marker flags because evolve extension looks for | |
170 | # bumpedfix in obsolete.py |
|
170 | # bumpedfix in obsolete.py | |
171 | bumpedfix = obsutil.bumpedfix |
|
171 | bumpedfix = obsutil.bumpedfix | |
172 | usingsha256 = obsutil.usingsha256 |
|
172 | usingsha256 = obsutil.usingsha256 | |
173 |
|
173 | |||
174 | ## Parsing and writing of version "0" |
|
174 | ## Parsing and writing of version "0" | |
175 | # |
|
175 | # | |
176 | # The header is followed by the markers. Each marker is made of: |
|
176 | # The header is followed by the markers. Each marker is made of: | |
177 | # |
|
177 | # | |
178 | # - 1 uint8 : number of new changesets "N", can be zero. |
|
178 | # - 1 uint8 : number of new changesets "N", can be zero. | |
179 | # |
|
179 | # | |
180 | # - 1 uint32: metadata size "M" in bytes. |
|
180 | # - 1 uint32: metadata size "M" in bytes. | |
181 | # |
|
181 | # | |
182 | # - 1 byte: a bit field. It is reserved for flags used in common |
|
182 | # - 1 byte: a bit field. It is reserved for flags used in common | |
183 | # obsolete marker operations, to avoid repeated decoding of metadata |
|
183 | # obsolete marker operations, to avoid repeated decoding of metadata | |
184 | # entries. |
|
184 | # entries. | |
185 | # |
|
185 | # | |
186 | # - 20 bytes: obsoleted changeset identifier. |
|
186 | # - 20 bytes: obsoleted changeset identifier. | |
187 | # |
|
187 | # | |
188 | # - N*20 bytes: new changesets identifiers. |
|
188 | # - N*20 bytes: new changesets identifiers. | |
189 | # |
|
189 | # | |
190 | # - M bytes: metadata as a sequence of nul-terminated strings. Each |
|
190 | # - M bytes: metadata as a sequence of nul-terminated strings. Each | |
191 | # string contains a key and a value, separated by a colon ':', without |
|
191 | # string contains a key and a value, separated by a colon ':', without | |
192 | # additional encoding. Keys cannot contain '\0' or ':' and values |
|
192 | # additional encoding. Keys cannot contain '\0' or ':' and values | |
193 | # cannot contain '\0'. |
|
193 | # cannot contain '\0'. | |
194 | _fm0version = 0 |
|
194 | _fm0version = 0 | |
195 | _fm0fixed = b'>BIB20s' |
|
195 | _fm0fixed = b'>BIB20s' | |
196 | _fm0node = b'20s' |
|
196 | _fm0node = b'20s' | |
197 | _fm0fsize = _calcsize(_fm0fixed) |
|
197 | _fm0fsize = _calcsize(_fm0fixed) | |
198 | _fm0fnodesize = _calcsize(_fm0node) |
|
198 | _fm0fnodesize = _calcsize(_fm0node) | |
199 |
|
199 | |||
200 |
|
200 | |||
201 | def _fm0readmarkers(data, off, stop): |
|
201 | def _fm0readmarkers(data, off, stop): | |
202 | # Loop on markers |
|
202 | # Loop on markers | |
203 | while off < stop: |
|
203 | while off < stop: | |
204 | # read fixed part |
|
204 | # read fixed part | |
205 | cur = data[off : off + _fm0fsize] |
|
205 | cur = data[off : off + _fm0fsize] | |
206 | off += _fm0fsize |
|
206 | off += _fm0fsize | |
207 | numsuc, mdsize, flags, pre = _unpack(_fm0fixed, cur) |
|
207 | numsuc, mdsize, flags, pre = _unpack(_fm0fixed, cur) | |
208 | # read replacement |
|
208 | # read replacement | |
209 | sucs = () |
|
209 | sucs = () | |
210 | if numsuc: |
|
210 | if numsuc: | |
211 | s = _fm0fnodesize * numsuc |
|
211 | s = _fm0fnodesize * numsuc | |
212 | cur = data[off : off + s] |
|
212 | cur = data[off : off + s] | |
213 | sucs = _unpack(_fm0node * numsuc, cur) |
|
213 | sucs = _unpack(_fm0node * numsuc, cur) | |
214 | off += s |
|
214 | off += s | |
215 | # read metadata |
|
215 | # read metadata | |
216 | # (metadata will be decoded on demand) |
|
216 | # (metadata will be decoded on demand) | |
217 | metadata = data[off : off + mdsize] |
|
217 | metadata = data[off : off + mdsize] | |
218 | if len(metadata) != mdsize: |
|
218 | if len(metadata) != mdsize: | |
219 | raise error.Abort( |
|
219 | raise error.Abort( | |
220 | _( |
|
220 | _( | |
221 | b'parsing obsolete marker: metadata is too ' |
|
221 | b'parsing obsolete marker: metadata is too ' | |
222 | b'short, %d bytes expected, got %d' |
|
222 | b'short, %d bytes expected, got %d' | |
223 | ) |
|
223 | ) | |
224 | % (mdsize, len(metadata)) |
|
224 | % (mdsize, len(metadata)) | |
225 | ) |
|
225 | ) | |
226 | off += mdsize |
|
226 | off += mdsize | |
227 | metadata = _fm0decodemeta(metadata) |
|
227 | metadata = _fm0decodemeta(metadata) | |
228 | try: |
|
228 | try: | |
229 | when, offset = metadata.pop(b'date', b'0 0').split(b' ') |
|
229 | when, offset = metadata.pop(b'date', b'0 0').split(b' ') | |
230 | date = float(when), int(offset) |
|
230 | date = float(when), int(offset) | |
231 | except ValueError: |
|
231 | except ValueError: | |
232 | date = (0.0, 0) |
|
232 | date = (0.0, 0) | |
233 | parents = None |
|
233 | parents = None | |
234 | if b'p2' in metadata: |
|
234 | if b'p2' in metadata: | |
235 | parents = (metadata.pop(b'p1', None), metadata.pop(b'p2', None)) |
|
235 | parents = (metadata.pop(b'p1', None), metadata.pop(b'p2', None)) | |
236 | elif b'p1' in metadata: |
|
236 | elif b'p1' in metadata: | |
237 | parents = (metadata.pop(b'p1', None),) |
|
237 | parents = (metadata.pop(b'p1', None),) | |
238 | elif b'p0' in metadata: |
|
238 | elif b'p0' in metadata: | |
239 | parents = () |
|
239 | parents = () | |
240 | if parents is not None: |
|
240 | if parents is not None: | |
241 | try: |
|
241 | try: | |
242 | parents = tuple(bin(p) for p in parents) |
|
242 | parents = tuple(bin(p) for p in parents) | |
243 | # if parent content is not a nodeid, drop the data |
|
243 | # if parent content is not a nodeid, drop the data | |
244 | for p in parents: |
|
244 | for p in parents: | |
245 | if len(p) != 20: |
|
245 | if len(p) != 20: | |
246 | parents = None |
|
246 | parents = None | |
247 | break |
|
247 | break | |
248 | except TypeError: |
|
248 | except TypeError: | |
249 | # if content cannot be translated to nodeid drop the data. |
|
249 | # if content cannot be translated to nodeid drop the data. | |
250 | parents = None |
|
250 | parents = None | |
251 |
|
251 | |||
252 | metadata = tuple(sorted(pycompat.iteritems(metadata))) |
|
252 | metadata = tuple(sorted(pycompat.iteritems(metadata))) | |
253 |
|
253 | |||
254 | yield (pre, sucs, flags, metadata, date, parents) |
|
254 | yield (pre, sucs, flags, metadata, date, parents) | |
255 |
|
255 | |||
256 |
|
256 | |||
257 | def _fm0encodeonemarker(marker): |
|
257 | def _fm0encodeonemarker(marker): | |
258 | pre, sucs, flags, metadata, date, parents = marker |
|
258 | pre, sucs, flags, metadata, date, parents = marker | |
259 | if flags & usingsha256: |
|
259 | if flags & usingsha256: | |
260 | raise error.Abort(_(b'cannot handle sha256 with old obsstore format')) |
|
260 | raise error.Abort(_(b'cannot handle sha256 with old obsstore format')) | |
261 | metadata = dict(metadata) |
|
261 | metadata = dict(metadata) | |
262 | time, tz = date |
|
262 | time, tz = date | |
263 | metadata[b'date'] = b'%r %i' % (time, tz) |
|
263 | metadata[b'date'] = b'%r %i' % (time, tz) | |
264 | if parents is not None: |
|
264 | if parents is not None: | |
265 | if not parents: |
|
265 | if not parents: | |
266 | # mark that we explicitly recorded no parents |
|
266 | # mark that we explicitly recorded no parents | |
267 | metadata[b'p0'] = b'' |
|
267 | metadata[b'p0'] = b'' | |
268 | for i, p in enumerate(parents, 1): |
|
268 | for i, p in enumerate(parents, 1): | |
269 | metadata[b'p%i' % i] = hex(p) |
|
269 | metadata[b'p%i' % i] = hex(p) | |
270 | metadata = _fm0encodemeta(metadata) |
|
270 | metadata = _fm0encodemeta(metadata) | |
271 | numsuc = len(sucs) |
|
271 | numsuc = len(sucs) | |
272 | format = _fm0fixed + (_fm0node * numsuc) |
|
272 | format = _fm0fixed + (_fm0node * numsuc) | |
273 | data = [numsuc, len(metadata), flags, pre] |
|
273 | data = [numsuc, len(metadata), flags, pre] | |
274 | data.extend(sucs) |
|
274 | data.extend(sucs) | |
275 | return _pack(format, *data) + metadata |
|
275 | return _pack(format, *data) + metadata | |
276 |
|
276 | |||
277 |
|
277 | |||
278 | def _fm0encodemeta(meta): |
|
278 | def _fm0encodemeta(meta): | |
279 | """Return encoded metadata string to string mapping. |
|
279 | """Return encoded metadata string to string mapping. | |
280 |
|
280 | |||
281 | Assume no ':' in key and no '\0' in both key and value.""" |
|
281 | Assume no ':' in key and no '\0' in both key and value.""" | |
282 | for key, value in pycompat.iteritems(meta): |
|
282 | for key, value in pycompat.iteritems(meta): | |
283 | if b':' in key or b'\0' in key: |
|
283 | if b':' in key or b'\0' in key: | |
284 | raise ValueError(b"':' and '\0' are forbidden in metadata key'") |
|
284 | raise ValueError(b"':' and '\0' are forbidden in metadata key'") | |
285 | if b'\0' in value: |
|
285 | if b'\0' in value: | |
286 | raise ValueError(b"':' is forbidden in metadata value'") |
|
286 | raise ValueError(b"':' is forbidden in metadata value'") | |
287 | return b'\0'.join([b'%s:%s' % (k, meta[k]) for k in sorted(meta)]) |
|
287 | return b'\0'.join([b'%s:%s' % (k, meta[k]) for k in sorted(meta)]) | |
288 |
|
288 | |||
289 |
|
289 | |||
290 | def _fm0decodemeta(data): |
|
290 | def _fm0decodemeta(data): | |
291 | """Return string to string dictionary from encoded version.""" |
|
291 | """Return string to string dictionary from encoded version.""" | |
292 | d = {} |
|
292 | d = {} | |
293 | for l in data.split(b'\0'): |
|
293 | for l in data.split(b'\0'): | |
294 | if l: |
|
294 | if l: | |
295 | key, value = l.split(b':', 1) |
|
295 | key, value = l.split(b':', 1) | |
296 | d[key] = value |
|
296 | d[key] = value | |
297 | return d |
|
297 | return d | |
298 |
|
298 | |||
299 |
|
299 | |||
300 | ## Parsing and writing of version "1" |
|
300 | ## Parsing and writing of version "1" | |
301 | # |
|
301 | # | |
302 | # The header is followed by the markers. Each marker is made of: |
|
302 | # The header is followed by the markers. Each marker is made of: | |
303 | # |
|
303 | # | |
304 | # - uint32: total size of the marker (including this field) |
|
304 | # - uint32: total size of the marker (including this field) | |
305 | # |
|
305 | # | |
306 | # - float64: date in seconds since epoch |
|
306 | # - float64: date in seconds since epoch | |
307 | # |
|
307 | # | |
308 | # - int16: timezone offset in minutes |
|
308 | # - int16: timezone offset in minutes | |
309 | # |
|
309 | # | |
310 | # - uint16: a bit field. It is reserved for flags used in common |
|
310 | # - uint16: a bit field. It is reserved for flags used in common | |
311 | # obsolete marker operations, to avoid repeated decoding of metadata |
|
311 | # obsolete marker operations, to avoid repeated decoding of metadata | |
312 | # entries. |
|
312 | # entries. | |
313 | # |
|
313 | # | |
314 | # - uint8: number of successors "N", can be zero. |
|
314 | # - uint8: number of successors "N", can be zero. | |
315 | # |
|
315 | # | |
316 | # - uint8: number of parents "P", can be zero. |
|
316 | # - uint8: number of parents "P", can be zero. | |
317 | # |
|
317 | # | |
318 | # 0: parents data stored but no parent, |
|
318 | # 0: parents data stored but no parent, | |
319 | # 1: one parent stored, |
|
319 | # 1: one parent stored, | |
320 | # 2: two parents stored, |
|
320 | # 2: two parents stored, | |
321 | # 3: no parent data stored |
|
321 | # 3: no parent data stored | |
322 | # |
|
322 | # | |
323 | # - uint8: number of metadata entries M |
|
323 | # - uint8: number of metadata entries M | |
324 | # |
|
324 | # | |
325 | # - 20 or 32 bytes: predecessor changeset identifier. |
|
325 | # - 20 or 32 bytes: predecessor changeset identifier. | |
326 | # |
|
326 | # | |
327 | # - N*(20 or 32) bytes: successors changesets identifiers. |
|
327 | # - N*(20 or 32) bytes: successors changesets identifiers. | |
328 | # |
|
328 | # | |
329 | # - P*(20 or 32) bytes: parents of the predecessors changesets. |
|
329 | # - P*(20 or 32) bytes: parents of the predecessors changesets. | |
330 | # |
|
330 | # | |
331 | # - M*(uint8, uint8): size of all metadata entries (key and value) |
|
331 | # - M*(uint8, uint8): size of all metadata entries (key and value) | |
332 | # |
|
332 | # | |
333 | # - remaining bytes: the metadata, each (key, value) pair after the other. |
|
333 | # - remaining bytes: the metadata, each (key, value) pair after the other. | |
334 | _fm1version = 1 |
|
334 | _fm1version = 1 | |
335 | _fm1fixed = b'>IdhHBBB' |
|
335 | _fm1fixed = b'>IdhHBBB' | |
336 | _fm1nodesha1 = b'20s' |
|
336 | _fm1nodesha1 = b'20s' | |
337 | _fm1nodesha256 = b'32s' |
|
337 | _fm1nodesha256 = b'32s' | |
338 | _fm1nodesha1size = _calcsize(_fm1nodesha1) |
|
338 | _fm1nodesha1size = _calcsize(_fm1nodesha1) | |
339 | _fm1nodesha256size = _calcsize(_fm1nodesha256) |
|
339 | _fm1nodesha256size = _calcsize(_fm1nodesha256) | |
340 | _fm1fsize = _calcsize(_fm1fixed) |
|
340 | _fm1fsize = _calcsize(_fm1fixed) | |
341 | _fm1parentnone = 3 |
|
341 | _fm1parentnone = 3 | |
342 | _fm1parentshift = 14 |
|
342 | _fm1parentshift = 14 | |
343 | _fm1parentmask = _fm1parentnone << _fm1parentshift |
|
343 | _fm1parentmask = _fm1parentnone << _fm1parentshift | |
344 | _fm1metapair = b'BB' |
|
344 | _fm1metapair = b'BB' | |
345 | _fm1metapairsize = _calcsize(_fm1metapair) |
|
345 | _fm1metapairsize = _calcsize(_fm1metapair) | |
346 |
|
346 | |||
347 |
|
347 | |||
348 | def _fm1purereadmarkers(data, off, stop): |
|
348 | def _fm1purereadmarkers(data, off, stop): | |
349 | # make some global constants local for performance |
|
349 | # make some global constants local for performance | |
350 | noneflag = _fm1parentnone |
|
350 | noneflag = _fm1parentnone | |
351 | sha2flag = usingsha256 |
|
351 | sha2flag = usingsha256 | |
352 | sha1size = _fm1nodesha1size |
|
352 | sha1size = _fm1nodesha1size | |
353 | sha2size = _fm1nodesha256size |
|
353 | sha2size = _fm1nodesha256size | |
354 | sha1fmt = _fm1nodesha1 |
|
354 | sha1fmt = _fm1nodesha1 | |
355 | sha2fmt = _fm1nodesha256 |
|
355 | sha2fmt = _fm1nodesha256 | |
356 | metasize = _fm1metapairsize |
|
356 | metasize = _fm1metapairsize | |
357 | metafmt = _fm1metapair |
|
357 | metafmt = _fm1metapair | |
358 | fsize = _fm1fsize |
|
358 | fsize = _fm1fsize | |
359 | unpack = _unpack |
|
359 | unpack = _unpack | |
360 |
|
360 | |||
361 | # Loop on markers |
|
361 | # Loop on markers | |
362 | ufixed = struct.Struct(_fm1fixed).unpack |
|
362 | ufixed = struct.Struct(_fm1fixed).unpack | |
363 |
|
363 | |||
364 | while off < stop: |
|
364 | while off < stop: | |
365 | # read fixed part |
|
365 | # read fixed part | |
366 | o1 = off + fsize |
|
366 | o1 = off + fsize | |
367 | t, secs, tz, flags, numsuc, numpar, nummeta = ufixed(data[off:o1]) |
|
367 | t, secs, tz, flags, numsuc, numpar, nummeta = ufixed(data[off:o1]) | |
368 |
|
368 | |||
369 | if flags & sha2flag: |
|
369 | if flags & sha2flag: | |
370 | nodefmt = sha2fmt |
|
370 | nodefmt = sha2fmt | |
371 | nodesize = sha2size |
|
371 | nodesize = sha2size | |
372 | else: |
|
372 | else: | |
373 | nodefmt = sha1fmt |
|
373 | nodefmt = sha1fmt | |
374 | nodesize = sha1size |
|
374 | nodesize = sha1size | |
375 |
|
375 | |||
376 | (prec,) = unpack(nodefmt, data[o1 : o1 + nodesize]) |
|
376 | (prec,) = unpack(nodefmt, data[o1 : o1 + nodesize]) | |
377 | o1 += nodesize |
|
377 | o1 += nodesize | |
378 |
|
378 | |||
379 | # read 0 or more successors |
|
379 | # read 0 or more successors | |
380 | if numsuc == 1: |
|
380 | if numsuc == 1: | |
381 | o2 = o1 + nodesize |
|
381 | o2 = o1 + nodesize | |
382 | sucs = (data[o1:o2],) |
|
382 | sucs = (data[o1:o2],) | |
383 | else: |
|
383 | else: | |
384 | o2 = o1 + nodesize * numsuc |
|
384 | o2 = o1 + nodesize * numsuc | |
385 | sucs = unpack(nodefmt * numsuc, data[o1:o2]) |
|
385 | sucs = unpack(nodefmt * numsuc, data[o1:o2]) | |
386 |
|
386 | |||
387 | # read parents |
|
387 | # read parents | |
388 | if numpar == noneflag: |
|
388 | if numpar == noneflag: | |
389 | o3 = o2 |
|
389 | o3 = o2 | |
390 | parents = None |
|
390 | parents = None | |
391 | elif numpar == 1: |
|
391 | elif numpar == 1: | |
392 | o3 = o2 + nodesize |
|
392 | o3 = o2 + nodesize | |
393 | parents = (data[o2:o3],) |
|
393 | parents = (data[o2:o3],) | |
394 | else: |
|
394 | else: | |
395 | o3 = o2 + nodesize * numpar |
|
395 | o3 = o2 + nodesize * numpar | |
396 | parents = unpack(nodefmt * numpar, data[o2:o3]) |
|
396 | parents = unpack(nodefmt * numpar, data[o2:o3]) | |
397 |
|
397 | |||
398 | # read metadata |
|
398 | # read metadata | |
399 | off = o3 + metasize * nummeta |
|
399 | off = o3 + metasize * nummeta | |
400 | metapairsize = unpack(b'>' + (metafmt * nummeta), data[o3:off]) |
|
400 | metapairsize = unpack(b'>' + (metafmt * nummeta), data[o3:off]) | |
401 | metadata = [] |
|
401 | metadata = [] | |
402 | for idx in pycompat.xrange(0, len(metapairsize), 2): |
|
402 | for idx in pycompat.xrange(0, len(metapairsize), 2): | |
403 | o1 = off + metapairsize[idx] |
|
403 | o1 = off + metapairsize[idx] | |
404 | o2 = o1 + metapairsize[idx + 1] |
|
404 | o2 = o1 + metapairsize[idx + 1] | |
405 | metadata.append((data[off:o1], data[o1:o2])) |
|
405 | metadata.append((data[off:o1], data[o1:o2])) | |
406 | off = o2 |
|
406 | off = o2 | |
407 |
|
407 | |||
408 | yield (prec, sucs, flags, tuple(metadata), (secs, tz * 60), parents) |
|
408 | yield (prec, sucs, flags, tuple(metadata), (secs, tz * 60), parents) | |
409 |
|
409 | |||
410 |
|
410 | |||
411 | def _fm1encodeonemarker(marker): |
|
411 | def _fm1encodeonemarker(marker): | |
412 | pre, sucs, flags, metadata, date, parents = marker |
|
412 | pre, sucs, flags, metadata, date, parents = marker | |
413 | # determine node size |
|
413 | # determine node size | |
414 | _fm1node = _fm1nodesha1 |
|
414 | _fm1node = _fm1nodesha1 | |
415 | if flags & usingsha256: |
|
415 | if flags & usingsha256: | |
416 | _fm1node = _fm1nodesha256 |
|
416 | _fm1node = _fm1nodesha256 | |
417 | numsuc = len(sucs) |
|
417 | numsuc = len(sucs) | |
418 | numextranodes = 1 + numsuc |
|
418 | numextranodes = 1 + numsuc | |
419 | if parents is None: |
|
419 | if parents is None: | |
420 | numpar = _fm1parentnone |
|
420 | numpar = _fm1parentnone | |
421 | else: |
|
421 | else: | |
422 | numpar = len(parents) |
|
422 | numpar = len(parents) | |
423 | numextranodes += numpar |
|
423 | numextranodes += numpar | |
424 | formatnodes = _fm1node * numextranodes |
|
424 | formatnodes = _fm1node * numextranodes | |
425 | formatmeta = _fm1metapair * len(metadata) |
|
425 | formatmeta = _fm1metapair * len(metadata) | |
426 | format = _fm1fixed + formatnodes + formatmeta |
|
426 | format = _fm1fixed + formatnodes + formatmeta | |
427 | # tz is stored in minutes so we divide by 60 |
|
427 | # tz is stored in minutes so we divide by 60 | |
428 | tz = date[1] // 60 |
|
428 | tz = date[1] // 60 | |
429 | data = [None, date[0], tz, flags, numsuc, numpar, len(metadata), pre] |
|
429 | data = [None, date[0], tz, flags, numsuc, numpar, len(metadata), pre] | |
430 | data.extend(sucs) |
|
430 | data.extend(sucs) | |
431 | if parents is not None: |
|
431 | if parents is not None: | |
432 | data.extend(parents) |
|
432 | data.extend(parents) | |
433 | totalsize = _calcsize(format) |
|
433 | totalsize = _calcsize(format) | |
434 | for key, value in metadata: |
|
434 | for key, value in metadata: | |
435 | lk = len(key) |
|
435 | lk = len(key) | |
436 | lv = len(value) |
|
436 | lv = len(value) | |
437 | if lk > 255: |
|
437 | if lk > 255: | |
438 | msg = ( |
|
438 | msg = ( | |
439 | b'obsstore metadata key cannot be longer than 255 bytes' |
|
439 | b'obsstore metadata key cannot be longer than 255 bytes' | |
440 | b' (key "%s" is %u bytes)' |
|
440 | b' (key "%s" is %u bytes)' | |
441 | ) % (key, lk) |
|
441 | ) % (key, lk) | |
442 | raise error.ProgrammingError(msg) |
|
442 | raise error.ProgrammingError(msg) | |
443 | if lv > 255: |
|
443 | if lv > 255: | |
444 | msg = ( |
|
444 | msg = ( | |
445 | b'obsstore metadata value cannot be longer than 255 bytes' |
|
445 | b'obsstore metadata value cannot be longer than 255 bytes' | |
446 | b' (value "%s" for key "%s" is %u bytes)' |
|
446 | b' (value "%s" for key "%s" is %u bytes)' | |
447 | ) % (value, key, lv) |
|
447 | ) % (value, key, lv) | |
448 | raise error.ProgrammingError(msg) |
|
448 | raise error.ProgrammingError(msg) | |
449 | data.append(lk) |
|
449 | data.append(lk) | |
450 | data.append(lv) |
|
450 | data.append(lv) | |
451 | totalsize += lk + lv |
|
451 | totalsize += lk + lv | |
452 | data[0] = totalsize |
|
452 | data[0] = totalsize | |
453 | data = [_pack(format, *data)] |
|
453 | data = [_pack(format, *data)] | |
454 | for key, value in metadata: |
|
454 | for key, value in metadata: | |
455 | data.append(key) |
|
455 | data.append(key) | |
456 | data.append(value) |
|
456 | data.append(value) | |
457 | return b''.join(data) |
|
457 | return b''.join(data) | |
458 |
|
458 | |||
459 |
|
459 | |||
460 | def _fm1readmarkers(data, off, stop): |
|
460 | def _fm1readmarkers(data, off, stop): | |
461 | native = getattr(parsers, 'fm1readmarkers', None) |
|
461 | native = getattr(parsers, 'fm1readmarkers', None) | |
462 | if not native: |
|
462 | if not native: | |
463 | return _fm1purereadmarkers(data, off, stop) |
|
463 | return _fm1purereadmarkers(data, off, stop) | |
464 | return native(data, off, stop) |
|
464 | return native(data, off, stop) | |
465 |
|
465 | |||
466 |
|
466 | |||
467 | # mapping to read/write various marker formats |
|
467 | # mapping to read/write various marker formats | |
468 | # <version> -> (decoder, encoder) |
|
468 | # <version> -> (decoder, encoder) | |
469 | formats = { |
|
469 | formats = { | |
470 | _fm0version: (_fm0readmarkers, _fm0encodeonemarker), |
|
470 | _fm0version: (_fm0readmarkers, _fm0encodeonemarker), | |
471 | _fm1version: (_fm1readmarkers, _fm1encodeonemarker), |
|
471 | _fm1version: (_fm1readmarkers, _fm1encodeonemarker), | |
472 | } |
|
472 | } | |
473 |
|
473 | |||
474 |
|
474 | |||
475 | def _readmarkerversion(data): |
|
475 | def _readmarkerversion(data): | |
476 | return _unpack(b'>B', data[0:1])[0] |
|
476 | return _unpack(b'>B', data[0:1])[0] | |
477 |
|
477 | |||
478 |
|
478 | |||
479 | @util.nogc |
|
479 | @util.nogc | |
480 | def _readmarkers(data, off=None, stop=None): |
|
480 | def _readmarkers(data, off=None, stop=None): | |
481 | """Read and enumerate markers from raw data""" |
|
481 | """Read and enumerate markers from raw data""" | |
482 | diskversion = _readmarkerversion(data) |
|
482 | diskversion = _readmarkerversion(data) | |
483 | if not off: |
|
483 | if not off: | |
484 | off = 1 # skip 1 byte version number |
|
484 | off = 1 # skip 1 byte version number | |
485 | if stop is None: |
|
485 | if stop is None: | |
486 | stop = len(data) |
|
486 | stop = len(data) | |
487 | if diskversion not in formats: |
|
487 | if diskversion not in formats: | |
488 | msg = _(b'parsing obsolete marker: unknown version %r') % diskversion |
|
488 | msg = _(b'parsing obsolete marker: unknown version %r') % diskversion | |
489 | raise error.UnknownVersion(msg, version=diskversion) |
|
489 | raise error.UnknownVersion(msg, version=diskversion) | |
490 | return diskversion, formats[diskversion][0](data, off, stop) |
|
490 | return diskversion, formats[diskversion][0](data, off, stop) | |
491 |
|
491 | |||
492 |
|
492 | |||
493 | def encodeheader(version=_fm0version): |
|
493 | def encodeheader(version=_fm0version): | |
494 | return _pack(b'>B', version) |
|
494 | return _pack(b'>B', version) | |
495 |
|
495 | |||
496 |
|
496 | |||
497 | def encodemarkers(markers, addheader=False, version=_fm0version): |
|
497 | def encodemarkers(markers, addheader=False, version=_fm0version): | |
498 | # Kept separate from flushmarkers(), it will be reused for |
|
498 | # Kept separate from flushmarkers(), it will be reused for | |
499 | # markers exchange. |
|
499 | # markers exchange. | |
500 | encodeone = formats[version][1] |
|
500 | encodeone = formats[version][1] | |
501 | if addheader: |
|
501 | if addheader: | |
502 | yield encodeheader(version) |
|
502 | yield encodeheader(version) | |
503 | for marker in markers: |
|
503 | for marker in markers: | |
504 | yield encodeone(marker) |
|
504 | yield encodeone(marker) | |
505 |
|
505 | |||
506 |
|
506 | |||
507 | @util.nogc |
|
507 | @util.nogc | |
508 | def _addsuccessors(successors, markers): |
|
508 | def _addsuccessors(successors, markers): | |
509 | for mark in markers: |
|
509 | for mark in markers: | |
510 | successors.setdefault(mark[0], set()).add(mark) |
|
510 | successors.setdefault(mark[0], set()).add(mark) | |
511 |
|
511 | |||
512 |
|
512 | |||
513 | @util.nogc |
|
513 | @util.nogc | |
514 | def _addpredecessors(predecessors, markers): |
|
514 | def _addpredecessors(predecessors, markers): | |
515 | for mark in markers: |
|
515 | for mark in markers: | |
516 | for suc in mark[1]: |
|
516 | for suc in mark[1]: | |
517 | predecessors.setdefault(suc, set()).add(mark) |
|
517 | predecessors.setdefault(suc, set()).add(mark) | |
518 |
|
518 | |||
519 |
|
519 | |||
520 | @util.nogc |
|
520 | @util.nogc | |
521 | def _addchildren(children, markers): |
|
521 | def _addchildren(children, markers): | |
522 | for mark in markers: |
|
522 | for mark in markers: | |
523 | parents = mark[5] |
|
523 | parents = mark[5] | |
524 | if parents is not None: |
|
524 | if parents is not None: | |
525 | for p in parents: |
|
525 | for p in parents: | |
526 | children.setdefault(p, set()).add(mark) |
|
526 | children.setdefault(p, set()).add(mark) | |
527 |
|
527 | |||
528 |
|
528 | |||
529 | def _checkinvalidmarkers(repo, markers): |
|
529 | def _checkinvalidmarkers(repo, markers): | |
530 | """search for marker with invalid data and raise error if needed |
|
530 | """search for marker with invalid data and raise error if needed | |
531 |
|
531 | |||
532 | Exist as a separated function to allow the evolve extension for a more |
|
532 | Exist as a separated function to allow the evolve extension for a more | |
533 | subtle handling. |
|
533 | subtle handling. | |
534 | """ |
|
534 | """ | |
535 | for mark in markers: |
|
535 | for mark in markers: | |
536 | if repo.nullid in mark[1]: |
|
536 | if repo.nullid in mark[1]: | |
537 | raise error.Abort( |
|
537 | raise error.Abort( | |
538 | _( |
|
538 | _( | |
539 | b'bad obsolescence marker detected: ' |
|
539 | b'bad obsolescence marker detected: ' | |
540 | b'invalid successors nullid' |
|
540 | b'invalid successors nullid' | |
541 | ) |
|
541 | ) | |
542 | ) |
|
542 | ) | |
543 |
|
543 | |||
544 |
|
544 | |||
545 | class obsstore(object): |
|
545 | class obsstore(object): | |
546 | """Store obsolete markers |
|
546 | """Store obsolete markers | |
547 |
|
547 | |||
548 | Markers can be accessed with two mappings: |
|
548 | Markers can be accessed with two mappings: | |
549 | - predecessors[x] -> set(markers on predecessors edges of x) |
|
549 | - predecessors[x] -> set(markers on predecessors edges of x) | |
550 | - successors[x] -> set(markers on successors edges of x) |
|
550 | - successors[x] -> set(markers on successors edges of x) | |
551 | - children[x] -> set(markers on predecessors edges of children(x) |
|
551 | - children[x] -> set(markers on predecessors edges of children(x) | |
552 | """ |
|
552 | """ | |
553 |
|
553 | |||
554 | fields = (b'prec', b'succs', b'flag', b'meta', b'date', b'parents') |
|
554 | fields = (b'prec', b'succs', b'flag', b'meta', b'date', b'parents') | |
555 | # prec: nodeid, predecessors changesets |
|
555 | # prec: nodeid, predecessors changesets | |
556 | # succs: tuple of nodeid, successor changesets (0-N length) |
|
556 | # succs: tuple of nodeid, successor changesets (0-N length) | |
557 | # flag: integer, flag field carrying modifier for the markers (see doc) |
|
557 | # flag: integer, flag field carrying modifier for the markers (see doc) | |
558 | # meta: binary blob in UTF-8, encoded metadata dictionary |
|
558 | # meta: binary blob in UTF-8, encoded metadata dictionary | |
559 | # date: (float, int) tuple, date of marker creation |
|
559 | # date: (float, int) tuple, date of marker creation | |
560 | # parents: (tuple of nodeid) or None, parents of predecessors |
|
560 | # parents: (tuple of nodeid) or None, parents of predecessors | |
561 | # None is used when no data has been recorded |
|
561 | # None is used when no data has been recorded | |
562 |
|
562 | |||
563 | def __init__(self, repo, svfs, defaultformat=_fm1version, readonly=False): |
|
563 | def __init__(self, repo, svfs, defaultformat=_fm1version, readonly=False): | |
564 | # caches for various obsolescence related cache |
|
564 | # caches for various obsolescence related cache | |
565 | self.caches = {} |
|
565 | self.caches = {} | |
566 | self.svfs = svfs |
|
566 | self.svfs = svfs | |
567 | self.repo = repo |
|
567 | self.repo = repo | |
568 | self._defaultformat = defaultformat |
|
568 | self._defaultformat = defaultformat | |
569 | self._readonly = readonly |
|
569 | self._readonly = readonly | |
570 |
|
570 | |||
571 | def __iter__(self): |
|
571 | def __iter__(self): | |
572 | return iter(self._all) |
|
572 | return iter(self._all) | |
573 |
|
573 | |||
574 | def __len__(self): |
|
574 | def __len__(self): | |
575 | return len(self._all) |
|
575 | return len(self._all) | |
576 |
|
576 | |||
577 | def __nonzero__(self): |
|
577 | def __nonzero__(self): | |
578 | if not self._cached('_all'): |
|
578 | if not self._cached('_all'): | |
579 | try: |
|
579 | try: | |
580 | return self.svfs.stat(b'obsstore').st_size > 1 |
|
580 | return self.svfs.stat(b'obsstore').st_size > 1 | |
581 | except OSError as inst: |
|
581 | except OSError as inst: | |
582 | if inst.errno not in (errno.ENOENT, errno.EINVAL): |
|
582 | if inst.errno not in (errno.ENOENT, errno.EINVAL): | |
583 | raise |
|
583 | raise | |
584 | # just build an empty _all list if no obsstore exists, which |
|
584 | # just build an empty _all list if no obsstore exists, which | |
585 | # avoids further stat() syscalls |
|
585 | # avoids further stat() syscalls | |
586 | return bool(self._all) |
|
586 | return bool(self._all) | |
587 |
|
587 | |||
588 | __bool__ = __nonzero__ |
|
588 | __bool__ = __nonzero__ | |
589 |
|
589 | |||
590 | @property |
|
590 | @property | |
591 | def readonly(self): |
|
591 | def readonly(self): | |
592 | """True if marker creation is disabled |
|
592 | """True if marker creation is disabled | |
593 |
|
593 | |||
594 | Remove me in the future when obsolete marker is always on.""" |
|
594 | Remove me in the future when obsolete marker is always on.""" | |
595 | return self._readonly |
|
595 | return self._readonly | |
596 |
|
596 | |||
597 | def create( |
|
597 | def create( | |
598 | self, |
|
598 | self, | |
599 | transaction, |
|
599 | transaction, | |
600 | prec, |
|
600 | prec, | |
601 | succs=(), |
|
601 | succs=(), | |
602 | flag=0, |
|
602 | flag=0, | |
603 | parents=None, |
|
603 | parents=None, | |
604 | date=None, |
|
604 | date=None, | |
605 | metadata=None, |
|
605 | metadata=None, | |
606 | ui=None, |
|
606 | ui=None, | |
607 | ): |
|
607 | ): | |
608 | """obsolete: add a new obsolete marker |
|
608 | """obsolete: add a new obsolete marker | |
609 |
|
609 | |||
610 | * ensuring it is hashable |
|
610 | * ensuring it is hashable | |
611 | * check mandatory metadata |
|
611 | * check mandatory metadata | |
612 | * encode metadata |
|
612 | * encode metadata | |
613 |
|
613 | |||
614 | If you are a human writing code creating marker you want to use the |
|
614 | If you are a human writing code creating marker you want to use the | |
615 | `createmarkers` function in this module instead. |
|
615 | `createmarkers` function in this module instead. | |
616 |
|
616 | |||
617 | return True if a new marker have been added, False if the markers |
|
617 | return True if a new marker have been added, False if the markers | |
618 | already existed (no op). |
|
618 | already existed (no op). | |
619 | """ |
|
619 | """ | |
620 | flag = int(flag) |
|
620 | flag = int(flag) | |
621 | if metadata is None: |
|
621 | if metadata is None: | |
622 | metadata = {} |
|
622 | metadata = {} | |
623 | if date is None: |
|
623 | if date is None: | |
624 | if b'date' in metadata: |
|
624 | if b'date' in metadata: | |
625 | # as a courtesy for out-of-tree extensions |
|
625 | # as a courtesy for out-of-tree extensions | |
626 | date = dateutil.parsedate(metadata.pop(b'date')) |
|
626 | date = dateutil.parsedate(metadata.pop(b'date')) | |
627 | elif ui is not None: |
|
627 | elif ui is not None: | |
628 | date = ui.configdate(b'devel', b'default-date') |
|
628 | date = ui.configdate(b'devel', b'default-date') | |
629 | if date is None: |
|
629 | if date is None: | |
630 | date = dateutil.makedate() |
|
630 | date = dateutil.makedate() | |
631 | else: |
|
631 | else: | |
632 | date = dateutil.makedate() |
|
632 | date = dateutil.makedate() | |
633 | if flag & usingsha256: |
|
633 | if flag & usingsha256: | |
634 | if len(prec) != 32: |
|
634 | if len(prec) != 32: | |
635 | raise ValueError(prec) |
|
635 | raise ValueError(prec) | |
636 | for succ in succs: |
|
636 | for succ in succs: | |
637 | if len(succ) != 32: |
|
637 | if len(succ) != 32: | |
638 | raise ValueError(succ) |
|
638 | raise ValueError(succ) | |
639 | else: |
|
639 | else: | |
640 | if len(prec) != 20: |
|
640 | if len(prec) != 20: | |
641 | raise ValueError(prec) |
|
641 | raise ValueError(prec) | |
642 | for succ in succs: |
|
642 | for succ in succs: | |
643 | if len(succ) != 20: |
|
643 | if len(succ) != 20: | |
644 | raise ValueError(succ) |
|
644 | raise ValueError(succ) | |
645 | if prec in succs: |
|
645 | if prec in succs: | |
646 | raise ValueError( |
|
646 | raise ValueError( | |
647 | 'in-marker cycle with %s' % pycompat.sysstr(hex(prec)) |
|
647 | 'in-marker cycle with %s' % pycompat.sysstr(hex(prec)) | |
648 | ) |
|
648 | ) | |
649 |
|
649 | |||
650 | metadata = tuple(sorted(pycompat.iteritems(metadata))) |
|
650 | metadata = tuple(sorted(pycompat.iteritems(metadata))) | |
651 | for k, v in metadata: |
|
651 | for k, v in metadata: | |
652 | try: |
|
652 | try: | |
653 | # might be better to reject non-ASCII keys |
|
653 | # might be better to reject non-ASCII keys | |
654 | k.decode('utf-8') |
|
654 | k.decode('utf-8') | |
655 | v.decode('utf-8') |
|
655 | v.decode('utf-8') | |
656 | except UnicodeDecodeError: |
|
656 | except UnicodeDecodeError: | |
657 | raise error.ProgrammingError( |
|
657 | raise error.ProgrammingError( | |
658 | b'obsstore metadata must be valid UTF-8 sequence ' |
|
658 | b'obsstore metadata must be valid UTF-8 sequence ' | |
659 | b'(key = %r, value = %r)' |
|
659 | b'(key = %r, value = %r)' | |
660 | % (pycompat.bytestr(k), pycompat.bytestr(v)) |
|
660 | % (pycompat.bytestr(k), pycompat.bytestr(v)) | |
661 | ) |
|
661 | ) | |
662 |
|
662 | |||
663 | marker = (bytes(prec), tuple(succs), flag, metadata, date, parents) |
|
663 | marker = (bytes(prec), tuple(succs), flag, metadata, date, parents) | |
664 | return bool(self.add(transaction, [marker])) |
|
664 | return bool(self.add(transaction, [marker])) | |
665 |
|
665 | |||
666 | def add(self, transaction, markers): |
|
666 | def add(self, transaction, markers): | |
667 | """Add new markers to the store |
|
667 | """Add new markers to the store | |
668 |
|
668 | |||
669 | Take care of filtering duplicate. |
|
669 | Take care of filtering duplicate. | |
670 | Return the number of new marker.""" |
|
670 | Return the number of new marker.""" | |
671 | if self._readonly: |
|
671 | if self._readonly: | |
672 | raise error.Abort( |
|
672 | raise error.Abort( | |
673 | _(b'creating obsolete markers is not enabled on this repo') |
|
673 | _(b'creating obsolete markers is not enabled on this repo') | |
674 | ) |
|
674 | ) | |
675 | known = set() |
|
675 | known = set() | |
676 | getsuccessors = self.successors.get |
|
676 | getsuccessors = self.successors.get | |
677 | new = [] |
|
677 | new = [] | |
678 | for m in markers: |
|
678 | for m in markers: | |
679 | if m not in getsuccessors(m[0], ()) and m not in known: |
|
679 | if m not in getsuccessors(m[0], ()) and m not in known: | |
680 | known.add(m) |
|
680 | known.add(m) | |
681 | new.append(m) |
|
681 | new.append(m) | |
682 | if new: |
|
682 | if new: | |
683 | f = self.svfs(b'obsstore', b'ab') |
|
683 | f = self.svfs(b'obsstore', b'ab') | |
684 | try: |
|
684 | try: | |
685 | offset = f.tell() |
|
685 | offset = f.tell() | |
686 | transaction.add(b'obsstore', offset) |
|
686 | transaction.add(b'obsstore', offset) | |
687 | # offset == 0: new file - add the version header |
|
687 | # offset == 0: new file - add the version header | |
688 | data = b''.join(encodemarkers(new, offset == 0, self._version)) |
|
688 | data = b''.join(encodemarkers(new, offset == 0, self._version)) | |
689 | f.write(data) |
|
689 | f.write(data) | |
690 | finally: |
|
690 | finally: | |
691 | # XXX: f.close() == filecache invalidation == obsstore rebuilt. |
|
691 | # XXX: f.close() == filecache invalidation == obsstore rebuilt. | |
692 | # call 'filecacheentry.refresh()' here |
|
692 | # call 'filecacheentry.refresh()' here | |
693 | f.close() |
|
693 | f.close() | |
694 | addedmarkers = transaction.changes.get(b'obsmarkers') |
|
694 | addedmarkers = transaction.changes.get(b'obsmarkers') | |
695 | if addedmarkers is not None: |
|
695 | if addedmarkers is not None: | |
696 | addedmarkers.update(new) |
|
696 | addedmarkers.update(new) | |
697 | self._addmarkers(new, data) |
|
697 | self._addmarkers(new, data) | |
698 | # new marker *may* have changed several set. invalidate the cache. |
|
698 | # new marker *may* have changed several set. invalidate the cache. | |
699 | self.caches.clear() |
|
699 | self.caches.clear() | |
700 | # records the number of new markers for the transaction hooks |
|
700 | # records the number of new markers for the transaction hooks | |
701 | previous = int(transaction.hookargs.get(b'new_obsmarkers', b'0')) |
|
701 | previous = int(transaction.hookargs.get(b'new_obsmarkers', b'0')) | |
702 | transaction.hookargs[b'new_obsmarkers'] = b'%d' % (previous + len(new)) |
|
702 | transaction.hookargs[b'new_obsmarkers'] = b'%d' % (previous + len(new)) | |
703 | return len(new) |
|
703 | return len(new) | |
704 |
|
704 | |||
705 | def mergemarkers(self, transaction, data): |
|
705 | def mergemarkers(self, transaction, data): | |
706 | """merge a binary stream of markers inside the obsstore |
|
706 | """merge a binary stream of markers inside the obsstore | |
707 |
|
707 | |||
708 | Returns the number of new markers added.""" |
|
708 | Returns the number of new markers added.""" | |
709 | version, markers = _readmarkers(data) |
|
709 | version, markers = _readmarkers(data) | |
710 | return self.add(transaction, markers) |
|
710 | return self.add(transaction, markers) | |
711 |
|
711 | |||
712 | @propertycache |
|
712 | @propertycache | |
713 | def _data(self): |
|
713 | def _data(self): | |
714 | return self.svfs.tryread(b'obsstore') |
|
714 | return self.svfs.tryread(b'obsstore') | |
715 |
|
715 | |||
716 | @propertycache |
|
716 | @propertycache | |
717 | def _version(self): |
|
717 | def _version(self): | |
718 | if len(self._data) >= 1: |
|
718 | if len(self._data) >= 1: | |
719 | return _readmarkerversion(self._data) |
|
719 | return _readmarkerversion(self._data) | |
720 | else: |
|
720 | else: | |
721 | return self._defaultformat |
|
721 | return self._defaultformat | |
722 |
|
722 | |||
723 | @propertycache |
|
723 | @propertycache | |
724 | def _all(self): |
|
724 | def _all(self): | |
725 | data = self._data |
|
725 | data = self._data | |
726 | if not data: |
|
726 | if not data: | |
727 | return [] |
|
727 | return [] | |
728 | self._version, markers = _readmarkers(data) |
|
728 | self._version, markers = _readmarkers(data) | |
729 | markers = list(markers) |
|
729 | markers = list(markers) | |
730 | _checkinvalidmarkers(self.repo, markers) |
|
730 | _checkinvalidmarkers(self.repo, markers) | |
731 | return markers |
|
731 | return markers | |
732 |
|
732 | |||
733 | @propertycache |
|
733 | @propertycache | |
734 | def successors(self): |
|
734 | def successors(self): | |
735 | successors = {} |
|
735 | successors = {} | |
736 | _addsuccessors(successors, self._all) |
|
736 | _addsuccessors(successors, self._all) | |
737 | return successors |
|
737 | return successors | |
738 |
|
738 | |||
739 | @propertycache |
|
739 | @propertycache | |
740 | def predecessors(self): |
|
740 | def predecessors(self): | |
741 | predecessors = {} |
|
741 | predecessors = {} | |
742 | _addpredecessors(predecessors, self._all) |
|
742 | _addpredecessors(predecessors, self._all) | |
743 | return predecessors |
|
743 | return predecessors | |
744 |
|
744 | |||
745 | @propertycache |
|
745 | @propertycache | |
746 | def children(self): |
|
746 | def children(self): | |
747 | children = {} |
|
747 | children = {} | |
748 | _addchildren(children, self._all) |
|
748 | _addchildren(children, self._all) | |
749 | return children |
|
749 | return children | |
750 |
|
750 | |||
751 | def _cached(self, attr): |
|
751 | def _cached(self, attr): | |
752 | return attr in self.__dict__ |
|
752 | return attr in self.__dict__ | |
753 |
|
753 | |||
754 | def _addmarkers(self, markers, rawdata): |
|
754 | def _addmarkers(self, markers, rawdata): | |
755 | markers = list(markers) # to allow repeated iteration |
|
755 | markers = list(markers) # to allow repeated iteration | |
756 | self._data = self._data + rawdata |
|
756 | self._data = self._data + rawdata | |
757 | self._all.extend(markers) |
|
757 | self._all.extend(markers) | |
758 | if self._cached('successors'): |
|
758 | if self._cached('successors'): | |
759 | _addsuccessors(self.successors, markers) |
|
759 | _addsuccessors(self.successors, markers) | |
760 | if self._cached('predecessors'): |
|
760 | if self._cached('predecessors'): | |
761 | _addpredecessors(self.predecessors, markers) |
|
761 | _addpredecessors(self.predecessors, markers) | |
762 | if self._cached('children'): |
|
762 | if self._cached('children'): | |
763 | _addchildren(self.children, markers) |
|
763 | _addchildren(self.children, markers) | |
764 | _checkinvalidmarkers(self.repo, markers) |
|
764 | _checkinvalidmarkers(self.repo, markers) | |
765 |
|
765 | |||
766 | def relevantmarkers(self, nodes): |
|
766 | def relevantmarkers(self, nodes): | |
767 | """return a set of all obsolescence markers relevant to a set of nodes. |
|
767 | """return a set of all obsolescence markers relevant to a set of nodes. | |
768 |
|
768 | |||
769 | "relevant" to a set of nodes mean: |
|
769 | "relevant" to a set of nodes mean: | |
770 |
|
770 | |||
771 | - marker that use this changeset as successor |
|
771 | - marker that use this changeset as successor | |
772 | - prune marker of direct children on this changeset |
|
772 | - prune marker of direct children on this changeset | |
773 | - recursive application of the two rules on predecessors of these |
|
773 | - recursive application of the two rules on predecessors of these | |
774 | markers |
|
774 | markers | |
775 |
|
775 | |||
776 | It is a set so you cannot rely on order.""" |
|
776 | It is a set so you cannot rely on order.""" | |
777 |
|
777 | |||
778 | pendingnodes = set(nodes) |
|
778 | pendingnodes = set(nodes) | |
779 | seenmarkers = set() |
|
779 | seenmarkers = set() | |
780 | seennodes = set(pendingnodes) |
|
780 | seennodes = set(pendingnodes) | |
781 | precursorsmarkers = self.predecessors |
|
781 | precursorsmarkers = self.predecessors | |
782 | succsmarkers = self.successors |
|
782 | succsmarkers = self.successors | |
783 | children = self.children |
|
783 | children = self.children | |
784 | while pendingnodes: |
|
784 | while pendingnodes: | |
785 | direct = set() |
|
785 | direct = set() | |
786 | for current in pendingnodes: |
|
786 | for current in pendingnodes: | |
787 | direct.update(precursorsmarkers.get(current, ())) |
|
787 | direct.update(precursorsmarkers.get(current, ())) | |
788 | pruned = [m for m in children.get(current, ()) if not m[1]] |
|
788 | pruned = [m for m in children.get(current, ()) if not m[1]] | |
789 | direct.update(pruned) |
|
789 | direct.update(pruned) | |
790 | pruned = [m for m in succsmarkers.get(current, ()) if not m[1]] |
|
790 | pruned = [m for m in succsmarkers.get(current, ()) if not m[1]] | |
791 | direct.update(pruned) |
|
791 | direct.update(pruned) | |
792 | direct -= seenmarkers |
|
792 | direct -= seenmarkers | |
793 | pendingnodes = {m[0] for m in direct} |
|
793 | pendingnodes = {m[0] for m in direct} | |
794 | seenmarkers |= direct |
|
794 | seenmarkers |= direct | |
795 | pendingnodes -= seennodes |
|
795 | pendingnodes -= seennodes | |
796 | seennodes |= pendingnodes |
|
796 | seennodes |= pendingnodes | |
797 | return seenmarkers |
|
797 | return seenmarkers | |
798 |
|
798 | |||
799 |
|
799 | |||
800 | def makestore(ui, repo): |
|
800 | def makestore(ui, repo): | |
801 | """Create an obsstore instance from a repo.""" |
|
801 | """Create an obsstore instance from a repo.""" | |
802 | # read default format for new obsstore. |
|
802 | # read default format for new obsstore. | |
803 | # developer config: format.obsstore-version |
|
803 | # developer config: format.obsstore-version | |
804 | defaultformat = ui.configint(b'format', b'obsstore-version') |
|
804 | defaultformat = ui.configint(b'format', b'obsstore-version') | |
805 | # rely on obsstore class default when possible. |
|
805 | # rely on obsstore class default when possible. | |
806 | kwargs = {} |
|
806 | kwargs = {} | |
807 | if defaultformat is not None: |
|
807 | if defaultformat is not None: | |
808 | kwargs['defaultformat'] = defaultformat |
|
808 | kwargs['defaultformat'] = defaultformat | |
809 | readonly = not isenabled(repo, createmarkersopt) |
|
809 | readonly = not isenabled(repo, createmarkersopt) | |
810 | store = obsstore(repo, repo.svfs, readonly=readonly, **kwargs) |
|
810 | store = obsstore(repo, repo.svfs, readonly=readonly, **kwargs) | |
811 | if store and readonly: |
|
811 | if store and readonly: | |
812 | ui.warn( |
|
812 | ui.warn( | |
813 | _(b'obsolete feature not enabled but %i markers found!\n') |
|
813 | _(b'obsolete feature not enabled but %i markers found!\n') | |
814 | % len(list(store)) |
|
814 | % len(list(store)) | |
815 | ) |
|
815 | ) | |
816 | return store |
|
816 | return store | |
817 |
|
817 | |||
818 |
|
818 | |||
819 | def commonversion(versions): |
|
819 | def commonversion(versions): | |
820 | """Return the newest version listed in both versions and our local formats. |
|
820 | """Return the newest version listed in both versions and our local formats. | |
821 |
|
821 | |||
822 | Returns None if no common version exists. |
|
822 | Returns None if no common version exists. | |
823 | """ |
|
823 | """ | |
824 | versions.sort(reverse=True) |
|
824 | versions.sort(reverse=True) | |
825 | # search for highest version known on both side |
|
825 | # search for highest version known on both side | |
826 | for v in versions: |
|
826 | for v in versions: | |
827 | if v in formats: |
|
827 | if v in formats: | |
828 | return v |
|
828 | return v | |
829 | return None |
|
829 | return None | |
830 |
|
830 | |||
831 |
|
831 | |||
832 | # arbitrary picked to fit into 8K limit from HTTP server |
|
832 | # arbitrary picked to fit into 8K limit from HTTP server | |
833 | # you have to take in account: |
|
833 | # you have to take in account: | |
834 | # - the version header |
|
834 | # - the version header | |
835 | # - the base85 encoding |
|
835 | # - the base85 encoding | |
836 | _maxpayload = 5300 |
|
836 | _maxpayload = 5300 | |
837 |
|
837 | |||
838 |
|
838 | |||
839 | def _pushkeyescape(markers): |
|
839 | def _pushkeyescape(markers): | |
840 | """encode markers into a dict suitable for pushkey exchange |
|
840 | """encode markers into a dict suitable for pushkey exchange | |
841 |
|
841 | |||
842 | - binary data is base85 encoded |
|
842 | - binary data is base85 encoded | |
843 | - split in chunks smaller than 5300 bytes""" |
|
843 | - split in chunks smaller than 5300 bytes""" | |
844 | keys = {} |
|
844 | keys = {} | |
845 | parts = [] |
|
845 | parts = [] | |
846 | currentlen = _maxpayload * 2 # ensure we create a new part |
|
846 | currentlen = _maxpayload * 2 # ensure we create a new part | |
847 | for marker in markers: |
|
847 | for marker in markers: | |
848 | nextdata = _fm0encodeonemarker(marker) |
|
848 | nextdata = _fm0encodeonemarker(marker) | |
849 | if len(nextdata) + currentlen > _maxpayload: |
|
849 | if len(nextdata) + currentlen > _maxpayload: | |
850 | currentpart = [] |
|
850 | currentpart = [] | |
851 | currentlen = 0 |
|
851 | currentlen = 0 | |
852 | parts.append(currentpart) |
|
852 | parts.append(currentpart) | |
853 | currentpart.append(nextdata) |
|
853 | currentpart.append(nextdata) | |
854 | currentlen += len(nextdata) |
|
854 | currentlen += len(nextdata) | |
855 | for idx, part in enumerate(reversed(parts)): |
|
855 | for idx, part in enumerate(reversed(parts)): | |
856 | data = b''.join([_pack(b'>B', _fm0version)] + part) |
|
856 | data = b''.join([_pack(b'>B', _fm0version)] + part) | |
857 | keys[b'dump%i' % idx] = util.b85encode(data) |
|
857 | keys[b'dump%i' % idx] = util.b85encode(data) | |
858 | return keys |
|
858 | return keys | |
859 |
|
859 | |||
860 |
|
860 | |||
861 | def listmarkers(repo): |
|
861 | def listmarkers(repo): | |
862 | """List markers over pushkey""" |
|
862 | """List markers over pushkey""" | |
863 | if not repo.obsstore: |
|
863 | if not repo.obsstore: | |
864 | return {} |
|
864 | return {} | |
865 | return _pushkeyescape(sorted(repo.obsstore)) |
|
865 | return _pushkeyescape(sorted(repo.obsstore)) | |
866 |
|
866 | |||
867 |
|
867 | |||
868 | def pushmarker(repo, key, old, new): |
|
868 | def pushmarker(repo, key, old, new): | |
869 | """Push markers over pushkey""" |
|
869 | """Push markers over pushkey""" | |
870 | if not key.startswith(b'dump'): |
|
870 | if not key.startswith(b'dump'): | |
871 | repo.ui.warn(_(b'unknown key: %r') % key) |
|
871 | repo.ui.warn(_(b'unknown key: %r') % key) | |
872 | return False |
|
872 | return False | |
873 | if old: |
|
873 | if old: | |
874 | repo.ui.warn(_(b'unexpected old value for %r') % key) |
|
874 | repo.ui.warn(_(b'unexpected old value for %r') % key) | |
875 | return False |
|
875 | return False | |
876 | data = util.b85decode(new) |
|
876 | data = util.b85decode(new) | |
877 | with repo.lock(), repo.transaction(b'pushkey: obsolete markers') as tr: |
|
877 | with repo.lock(), repo.transaction(b'pushkey: obsolete markers') as tr: | |
878 | repo.obsstore.mergemarkers(tr, data) |
|
878 | repo.obsstore.mergemarkers(tr, data) | |
879 | repo.invalidatevolatilesets() |
|
879 | repo.invalidatevolatilesets() | |
880 | return True |
|
880 | return True | |
881 |
|
881 | |||
882 |
|
882 | |||
883 | # mapping of 'set-name' -> <function to compute this set> |
|
883 | # mapping of 'set-name' -> <function to compute this set> | |
884 | cachefuncs = {} |
|
884 | cachefuncs = {} | |
885 |
|
885 | |||
886 |
|
886 | |||
887 | def cachefor(name): |
|
887 | def cachefor(name): | |
888 | """Decorator to register a function as computing the cache for a set""" |
|
888 | """Decorator to register a function as computing the cache for a set""" | |
889 |
|
889 | |||
890 | def decorator(func): |
|
890 | def decorator(func): | |
891 | if name in cachefuncs: |
|
891 | if name in cachefuncs: | |
892 | msg = b"duplicated registration for volatileset '%s' (existing: %r)" |
|
892 | msg = b"duplicated registration for volatileset '%s' (existing: %r)" | |
893 | raise error.ProgrammingError(msg % (name, cachefuncs[name])) |
|
893 | raise error.ProgrammingError(msg % (name, cachefuncs[name])) | |
894 | cachefuncs[name] = func |
|
894 | cachefuncs[name] = func | |
895 | return func |
|
895 | return func | |
896 |
|
896 | |||
897 | return decorator |
|
897 | return decorator | |
898 |
|
898 | |||
899 |
|
899 | |||
900 | def getrevs(repo, name): |
|
900 | def getrevs(repo, name): | |
901 | """Return the set of revision that belong to the <name> set |
|
901 | """Return the set of revision that belong to the <name> set | |
902 |
|
902 | |||
903 | Such access may compute the set and cache it for future use""" |
|
903 | Such access may compute the set and cache it for future use""" | |
904 | repo = repo.unfiltered() |
|
904 | repo = repo.unfiltered() | |
905 | with util.timedcm('getrevs %s', name): |
|
905 | with util.timedcm('getrevs %s', name): | |
906 | if not repo.obsstore: |
|
906 | if not repo.obsstore: | |
907 | return frozenset() |
|
907 | return frozenset() | |
908 | if name not in repo.obsstore.caches: |
|
908 | if name not in repo.obsstore.caches: | |
909 | repo.obsstore.caches[name] = cachefuncs[name](repo) |
|
909 | repo.obsstore.caches[name] = cachefuncs[name](repo) | |
910 | return repo.obsstore.caches[name] |
|
910 | return repo.obsstore.caches[name] | |
911 |
|
911 | |||
912 |
|
912 | |||
913 | # To be simple we need to invalidate obsolescence cache when: |
|
913 | # To be simple we need to invalidate obsolescence cache when: | |
914 | # |
|
914 | # | |
915 | # - new changeset is added: |
|
915 | # - new changeset is added: | |
916 | # - public phase is changed |
|
916 | # - public phase is changed | |
917 | # - obsolescence marker are added |
|
917 | # - obsolescence marker are added | |
918 | # - strip is used a repo |
|
918 | # - strip is used a repo | |
919 | def clearobscaches(repo): |
|
919 | def clearobscaches(repo): | |
920 | """Remove all obsolescence related cache from a repo |
|
920 | """Remove all obsolescence related cache from a repo | |
921 |
|
921 | |||
922 | This remove all cache in obsstore is the obsstore already exist on the |
|
922 | This remove all cache in obsstore is the obsstore already exist on the | |
923 | repo. |
|
923 | repo. | |
924 |
|
924 | |||
925 | (We could be smarter here given the exact event that trigger the cache |
|
925 | (We could be smarter here given the exact event that trigger the cache | |
926 | clearing)""" |
|
926 | clearing)""" | |
927 | # only clear cache is there is obsstore data in this repo |
|
927 | # only clear cache is there is obsstore data in this repo | |
928 | if b'obsstore' in repo._filecache: |
|
928 | if b'obsstore' in repo._filecache: | |
929 | repo.obsstore.caches.clear() |
|
929 | repo.obsstore.caches.clear() | |
930 |
|
930 | |||
931 |
|
931 | |||
932 | def _mutablerevs(repo): |
|
932 | def _mutablerevs(repo): | |
933 | """the set of mutable revision in the repository""" |
|
933 | """the set of mutable revision in the repository""" | |
934 | return repo._phasecache.getrevset(repo, phases.mutablephases) |
|
934 | return repo._phasecache.getrevset(repo, phases.mutablephases) | |
935 |
|
935 | |||
936 |
|
936 | |||
937 | @cachefor(b'obsolete') |
|
937 | @cachefor(b'obsolete') | |
938 | def _computeobsoleteset(repo): |
|
938 | def _computeobsoleteset(repo): | |
939 | """the set of obsolete revisions""" |
|
939 | """the set of obsolete revisions""" | |
940 | getnode = repo.changelog.node |
|
940 | getnode = repo.changelog.node | |
941 | notpublic = _mutablerevs(repo) |
|
941 | notpublic = _mutablerevs(repo) | |
942 | isobs = repo.obsstore.successors.__contains__ |
|
942 | isobs = repo.obsstore.successors.__contains__ | |
943 |
|
|
943 | return frozenset(r for r in notpublic if isobs(getnode(r))) | |
944 | return obs |
|
|||
945 |
|
944 | |||
946 |
|
945 | |||
947 | @cachefor(b'orphan') |
|
946 | @cachefor(b'orphan') | |
948 | def _computeorphanset(repo): |
|
947 | def _computeorphanset(repo): | |
949 | """the set of non obsolete revisions with obsolete parents""" |
|
948 | """the set of non obsolete revisions with obsolete parents""" | |
950 | pfunc = repo.changelog.parentrevs |
|
949 | pfunc = repo.changelog.parentrevs | |
951 | mutable = _mutablerevs(repo) |
|
950 | mutable = _mutablerevs(repo) | |
952 | obsolete = getrevs(repo, b'obsolete') |
|
951 | obsolete = getrevs(repo, b'obsolete') | |
953 | others = mutable - obsolete |
|
952 | others = mutable - obsolete | |
954 | unstable = set() |
|
953 | unstable = set() | |
955 | for r in sorted(others): |
|
954 | for r in sorted(others): | |
956 | # A rev is unstable if one of its parent is obsolete or unstable |
|
955 | # A rev is unstable if one of its parent is obsolete or unstable | |
957 | # this works since we traverse following growing rev order |
|
956 | # this works since we traverse following growing rev order | |
958 | for p in pfunc(r): |
|
957 | for p in pfunc(r): | |
959 | if p in obsolete or p in unstable: |
|
958 | if p in obsolete or p in unstable: | |
960 | unstable.add(r) |
|
959 | unstable.add(r) | |
961 | break |
|
960 | break | |
962 | return unstable |
|
961 | return frozenset(unstable) | |
963 |
|
962 | |||
964 |
|
963 | |||
965 | @cachefor(b'suspended') |
|
964 | @cachefor(b'suspended') | |
966 | def _computesuspendedset(repo): |
|
965 | def _computesuspendedset(repo): | |
967 | """the set of obsolete parents with non obsolete descendants""" |
|
966 | """the set of obsolete parents with non obsolete descendants""" | |
968 | suspended = repo.changelog.ancestors(getrevs(repo, b'orphan')) |
|
967 | suspended = repo.changelog.ancestors(getrevs(repo, b'orphan')) | |
969 |
return |
|
968 | return frozenset(r for r in getrevs(repo, b'obsolete') if r in suspended) | |
970 |
|
969 | |||
971 |
|
970 | |||
972 | @cachefor(b'extinct') |
|
971 | @cachefor(b'extinct') | |
973 | def _computeextinctset(repo): |
|
972 | def _computeextinctset(repo): | |
974 | """the set of obsolete parents without non obsolete descendants""" |
|
973 | """the set of obsolete parents without non obsolete descendants""" | |
975 | return getrevs(repo, b'obsolete') - getrevs(repo, b'suspended') |
|
974 | return getrevs(repo, b'obsolete') - getrevs(repo, b'suspended') | |
976 |
|
975 | |||
977 |
|
976 | |||
978 | @cachefor(b'phasedivergent') |
|
977 | @cachefor(b'phasedivergent') | |
979 | def _computephasedivergentset(repo): |
|
978 | def _computephasedivergentset(repo): | |
980 | """the set of revs trying to obsolete public revisions""" |
|
979 | """the set of revs trying to obsolete public revisions""" | |
981 | bumped = set() |
|
980 | bumped = set() | |
982 | # util function (avoid attribute lookup in the loop) |
|
981 | # util function (avoid attribute lookup in the loop) | |
983 | phase = repo._phasecache.phase # would be faster to grab the full list |
|
982 | phase = repo._phasecache.phase # would be faster to grab the full list | |
984 | public = phases.public |
|
983 | public = phases.public | |
985 | cl = repo.changelog |
|
984 | cl = repo.changelog | |
986 | torev = cl.index.get_rev |
|
985 | torev = cl.index.get_rev | |
987 | tonode = cl.node |
|
986 | tonode = cl.node | |
988 | obsstore = repo.obsstore |
|
987 | obsstore = repo.obsstore | |
989 | for rev in repo.revs(b'(not public()) and (not obsolete())'): |
|
988 | for rev in repo.revs(b'(not public()) and (not obsolete())'): | |
990 | # We only evaluate mutable, non-obsolete revision |
|
989 | # We only evaluate mutable, non-obsolete revision | |
991 | node = tonode(rev) |
|
990 | node = tonode(rev) | |
992 | # (future) A cache of predecessors may worth if split is very common |
|
991 | # (future) A cache of predecessors may worth if split is very common | |
993 | for pnode in obsutil.allpredecessors( |
|
992 | for pnode in obsutil.allpredecessors( | |
994 | obsstore, [node], ignoreflags=bumpedfix |
|
993 | obsstore, [node], ignoreflags=bumpedfix | |
995 | ): |
|
994 | ): | |
996 | prev = torev(pnode) # unfiltered! but so is phasecache |
|
995 | prev = torev(pnode) # unfiltered! but so is phasecache | |
997 | if (prev is not None) and (phase(repo, prev) <= public): |
|
996 | if (prev is not None) and (phase(repo, prev) <= public): | |
998 | # we have a public predecessor |
|
997 | # we have a public predecessor | |
999 | bumped.add(rev) |
|
998 | bumped.add(rev) | |
1000 | break # Next draft! |
|
999 | break # Next draft! | |
1001 | return bumped |
|
1000 | return frozenset(bumped) | |
1002 |
|
1001 | |||
1003 |
|
1002 | |||
1004 | @cachefor(b'contentdivergent') |
|
1003 | @cachefor(b'contentdivergent') | |
1005 | def _computecontentdivergentset(repo): |
|
1004 | def _computecontentdivergentset(repo): | |
1006 | """the set of rev that compete to be the final successors of some revision.""" |
|
1005 | """the set of rev that compete to be the final successors of some revision.""" | |
1007 | divergent = set() |
|
1006 | divergent = set() | |
1008 | obsstore = repo.obsstore |
|
1007 | obsstore = repo.obsstore | |
1009 | newermap = {} |
|
1008 | newermap = {} | |
1010 | tonode = repo.changelog.node |
|
1009 | tonode = repo.changelog.node | |
1011 | for rev in repo.revs(b'(not public()) - obsolete()'): |
|
1010 | for rev in repo.revs(b'(not public()) - obsolete()'): | |
1012 | node = tonode(rev) |
|
1011 | node = tonode(rev) | |
1013 | mark = obsstore.predecessors.get(node, ()) |
|
1012 | mark = obsstore.predecessors.get(node, ()) | |
1014 | toprocess = set(mark) |
|
1013 | toprocess = set(mark) | |
1015 | seen = set() |
|
1014 | seen = set() | |
1016 | while toprocess: |
|
1015 | while toprocess: | |
1017 | prec = toprocess.pop()[0] |
|
1016 | prec = toprocess.pop()[0] | |
1018 | if prec in seen: |
|
1017 | if prec in seen: | |
1019 | continue # emergency cycle hanging prevention |
|
1018 | continue # emergency cycle hanging prevention | |
1020 | seen.add(prec) |
|
1019 | seen.add(prec) | |
1021 | if prec not in newermap: |
|
1020 | if prec not in newermap: | |
1022 | obsutil.successorssets(repo, prec, cache=newermap) |
|
1021 | obsutil.successorssets(repo, prec, cache=newermap) | |
1023 | newer = [n for n in newermap[prec] if n] |
|
1022 | newer = [n for n in newermap[prec] if n] | |
1024 | if len(newer) > 1: |
|
1023 | if len(newer) > 1: | |
1025 | divergent.add(rev) |
|
1024 | divergent.add(rev) | |
1026 | break |
|
1025 | break | |
1027 | toprocess.update(obsstore.predecessors.get(prec, ())) |
|
1026 | toprocess.update(obsstore.predecessors.get(prec, ())) | |
1028 | return divergent |
|
1027 | return frozenset(divergent) | |
1029 |
|
1028 | |||
1030 |
|
1029 | |||
1031 | def makefoldid(relation, user): |
|
1030 | def makefoldid(relation, user): | |
1032 |
|
1031 | |||
1033 | folddigest = hashutil.sha1(user) |
|
1032 | folddigest = hashutil.sha1(user) | |
1034 | for p in relation[0] + relation[1]: |
|
1033 | for p in relation[0] + relation[1]: | |
1035 | folddigest.update(b'%d' % p.rev()) |
|
1034 | folddigest.update(b'%d' % p.rev()) | |
1036 | folddigest.update(p.node()) |
|
1035 | folddigest.update(p.node()) | |
1037 | # Since fold only has to compete against fold for the same successors, it |
|
1036 | # Since fold only has to compete against fold for the same successors, it | |
1038 | # seems fine to use a small ID. Smaller ID save space. |
|
1037 | # seems fine to use a small ID. Smaller ID save space. | |
1039 | return hex(folddigest.digest())[:8] |
|
1038 | return hex(folddigest.digest())[:8] | |
1040 |
|
1039 | |||
1041 |
|
1040 | |||
1042 | def createmarkers( |
|
1041 | def createmarkers( | |
1043 | repo, relations, flag=0, date=None, metadata=None, operation=None |
|
1042 | repo, relations, flag=0, date=None, metadata=None, operation=None | |
1044 | ): |
|
1043 | ): | |
1045 | """Add obsolete markers between changesets in a repo |
|
1044 | """Add obsolete markers between changesets in a repo | |
1046 |
|
1045 | |||
1047 | <relations> must be an iterable of ((<old>,...), (<new>, ...)[,{metadata}]) |
|
1046 | <relations> must be an iterable of ((<old>,...), (<new>, ...)[,{metadata}]) | |
1048 | tuple. `old` and `news` are changectx. metadata is an optional dictionary |
|
1047 | tuple. `old` and `news` are changectx. metadata is an optional dictionary | |
1049 | containing metadata for this marker only. It is merged with the global |
|
1048 | containing metadata for this marker only. It is merged with the global | |
1050 | metadata specified through the `metadata` argument of this function. |
|
1049 | metadata specified through the `metadata` argument of this function. | |
1051 | Any string values in metadata must be UTF-8 bytes. |
|
1050 | Any string values in metadata must be UTF-8 bytes. | |
1052 |
|
1051 | |||
1053 | Trying to obsolete a public changeset will raise an exception. |
|
1052 | Trying to obsolete a public changeset will raise an exception. | |
1054 |
|
1053 | |||
1055 | Current user and date are used except if specified otherwise in the |
|
1054 | Current user and date are used except if specified otherwise in the | |
1056 | metadata attribute. |
|
1055 | metadata attribute. | |
1057 |
|
1056 | |||
1058 | This function operates within a transaction of its own, but does |
|
1057 | This function operates within a transaction of its own, but does | |
1059 | not take any lock on the repo. |
|
1058 | not take any lock on the repo. | |
1060 | """ |
|
1059 | """ | |
1061 | # prepare metadata |
|
1060 | # prepare metadata | |
1062 | if metadata is None: |
|
1061 | if metadata is None: | |
1063 | metadata = {} |
|
1062 | metadata = {} | |
1064 | if b'user' not in metadata: |
|
1063 | if b'user' not in metadata: | |
1065 | luser = ( |
|
1064 | luser = ( | |
1066 | repo.ui.config(b'devel', b'user.obsmarker') or repo.ui.username() |
|
1065 | repo.ui.config(b'devel', b'user.obsmarker') or repo.ui.username() | |
1067 | ) |
|
1066 | ) | |
1068 | metadata[b'user'] = encoding.fromlocal(luser) |
|
1067 | metadata[b'user'] = encoding.fromlocal(luser) | |
1069 |
|
1068 | |||
1070 | # Operation metadata handling |
|
1069 | # Operation metadata handling | |
1071 | useoperation = repo.ui.configbool( |
|
1070 | useoperation = repo.ui.configbool( | |
1072 | b'experimental', b'evolution.track-operation' |
|
1071 | b'experimental', b'evolution.track-operation' | |
1073 | ) |
|
1072 | ) | |
1074 | if useoperation and operation: |
|
1073 | if useoperation and operation: | |
1075 | metadata[b'operation'] = operation |
|
1074 | metadata[b'operation'] = operation | |
1076 |
|
1075 | |||
1077 | # Effect flag metadata handling |
|
1076 | # Effect flag metadata handling | |
1078 | saveeffectflag = repo.ui.configbool( |
|
1077 | saveeffectflag = repo.ui.configbool( | |
1079 | b'experimental', b'evolution.effect-flags' |
|
1078 | b'experimental', b'evolution.effect-flags' | |
1080 | ) |
|
1079 | ) | |
1081 |
|
1080 | |||
1082 | with repo.transaction(b'add-obsolescence-marker') as tr: |
|
1081 | with repo.transaction(b'add-obsolescence-marker') as tr: | |
1083 | markerargs = [] |
|
1082 | markerargs = [] | |
1084 | for rel in relations: |
|
1083 | for rel in relations: | |
1085 | predecessors = rel[0] |
|
1084 | predecessors = rel[0] | |
1086 | if not isinstance(predecessors, tuple): |
|
1085 | if not isinstance(predecessors, tuple): | |
1087 | # preserve compat with old API until all caller are migrated |
|
1086 | # preserve compat with old API until all caller are migrated | |
1088 | predecessors = (predecessors,) |
|
1087 | predecessors = (predecessors,) | |
1089 | if len(predecessors) > 1 and len(rel[1]) != 1: |
|
1088 | if len(predecessors) > 1 and len(rel[1]) != 1: | |
1090 | msg = b'Fold markers can only have 1 successors, not %d' |
|
1089 | msg = b'Fold markers can only have 1 successors, not %d' | |
1091 | raise error.ProgrammingError(msg % len(rel[1])) |
|
1090 | raise error.ProgrammingError(msg % len(rel[1])) | |
1092 | foldid = None |
|
1091 | foldid = None | |
1093 | foldsize = len(predecessors) |
|
1092 | foldsize = len(predecessors) | |
1094 | if 1 < foldsize: |
|
1093 | if 1 < foldsize: | |
1095 | foldid = makefoldid(rel, metadata[b'user']) |
|
1094 | foldid = makefoldid(rel, metadata[b'user']) | |
1096 | for foldidx, prec in enumerate(predecessors, 1): |
|
1095 | for foldidx, prec in enumerate(predecessors, 1): | |
1097 | sucs = rel[1] |
|
1096 | sucs = rel[1] | |
1098 | localmetadata = metadata.copy() |
|
1097 | localmetadata = metadata.copy() | |
1099 | if len(rel) > 2: |
|
1098 | if len(rel) > 2: | |
1100 | localmetadata.update(rel[2]) |
|
1099 | localmetadata.update(rel[2]) | |
1101 | if foldid is not None: |
|
1100 | if foldid is not None: | |
1102 | localmetadata[b'fold-id'] = foldid |
|
1101 | localmetadata[b'fold-id'] = foldid | |
1103 | localmetadata[b'fold-idx'] = b'%d' % foldidx |
|
1102 | localmetadata[b'fold-idx'] = b'%d' % foldidx | |
1104 | localmetadata[b'fold-size'] = b'%d' % foldsize |
|
1103 | localmetadata[b'fold-size'] = b'%d' % foldsize | |
1105 |
|
1104 | |||
1106 | if not prec.mutable(): |
|
1105 | if not prec.mutable(): | |
1107 | raise error.Abort( |
|
1106 | raise error.Abort( | |
1108 | _(b"cannot obsolete public changeset: %s") % prec, |
|
1107 | _(b"cannot obsolete public changeset: %s") % prec, | |
1109 | hint=b"see 'hg help phases' for details", |
|
1108 | hint=b"see 'hg help phases' for details", | |
1110 | ) |
|
1109 | ) | |
1111 | nprec = prec.node() |
|
1110 | nprec = prec.node() | |
1112 | nsucs = tuple(s.node() for s in sucs) |
|
1111 | nsucs = tuple(s.node() for s in sucs) | |
1113 | npare = None |
|
1112 | npare = None | |
1114 | if not nsucs: |
|
1113 | if not nsucs: | |
1115 | npare = tuple(p.node() for p in prec.parents()) |
|
1114 | npare = tuple(p.node() for p in prec.parents()) | |
1116 | if nprec in nsucs: |
|
1115 | if nprec in nsucs: | |
1117 | raise error.Abort( |
|
1116 | raise error.Abort( | |
1118 | _(b"changeset %s cannot obsolete itself") % prec |
|
1117 | _(b"changeset %s cannot obsolete itself") % prec | |
1119 | ) |
|
1118 | ) | |
1120 |
|
1119 | |||
1121 | # Effect flag can be different by relation |
|
1120 | # Effect flag can be different by relation | |
1122 | if saveeffectflag: |
|
1121 | if saveeffectflag: | |
1123 | # The effect flag is saved in a versioned field name for |
|
1122 | # The effect flag is saved in a versioned field name for | |
1124 | # future evolution |
|
1123 | # future evolution | |
1125 | effectflag = obsutil.geteffectflag(prec, sucs) |
|
1124 | effectflag = obsutil.geteffectflag(prec, sucs) | |
1126 | localmetadata[obsutil.EFFECTFLAGFIELD] = b"%d" % effectflag |
|
1125 | localmetadata[obsutil.EFFECTFLAGFIELD] = b"%d" % effectflag | |
1127 |
|
1126 | |||
1128 | # Creating the marker causes the hidden cache to become |
|
1127 | # Creating the marker causes the hidden cache to become | |
1129 | # invalid, which causes recomputation when we ask for |
|
1128 | # invalid, which causes recomputation when we ask for | |
1130 | # prec.parents() above. Resulting in n^2 behavior. So let's |
|
1129 | # prec.parents() above. Resulting in n^2 behavior. So let's | |
1131 | # prepare all of the args first, then create the markers. |
|
1130 | # prepare all of the args first, then create the markers. | |
1132 | markerargs.append((nprec, nsucs, npare, localmetadata)) |
|
1131 | markerargs.append((nprec, nsucs, npare, localmetadata)) | |
1133 |
|
1132 | |||
1134 | for args in markerargs: |
|
1133 | for args in markerargs: | |
1135 | nprec, nsucs, npare, localmetadata = args |
|
1134 | nprec, nsucs, npare, localmetadata = args | |
1136 | repo.obsstore.create( |
|
1135 | repo.obsstore.create( | |
1137 | tr, |
|
1136 | tr, | |
1138 | nprec, |
|
1137 | nprec, | |
1139 | nsucs, |
|
1138 | nsucs, | |
1140 | flag, |
|
1139 | flag, | |
1141 | parents=npare, |
|
1140 | parents=npare, | |
1142 | date=date, |
|
1141 | date=date, | |
1143 | metadata=localmetadata, |
|
1142 | metadata=localmetadata, | |
1144 | ui=repo.ui, |
|
1143 | ui=repo.ui, | |
1145 | ) |
|
1144 | ) | |
1146 | repo.filteredrevcache.clear() |
|
1145 | repo.filteredrevcache.clear() |
General Comments 0
You need to be logged in to leave comments.
Login now