##// END OF EJS Templates
repoview: make the conversion from node to rev explicit while computing hidden...
Pierre-Yves David -
r21059:d7e233df default
parent child Browse files
Show More
@@ -1,229 +1,230
1 # repoview.py - Filtered view of a localrepo object
1 # repoview.py - Filtered view of a localrepo object
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 import copy
9 import copy
10 import phases
10 import phases
11 import util
11 import util
12 import obsolete
12 import obsolete
13 import tags as tagsmod
13 import tags as tagsmod
14
14
15
15
16 def hideablerevs(repo):
16 def hideablerevs(repo):
17 """Revisions candidates to be hidden
17 """Revisions candidates to be hidden
18
18
19 This is a standalone function to help extensions to wrap it."""
19 This is a standalone function to help extensions to wrap it."""
20 return obsolete.getrevs(repo, 'obsolete')
20 return obsolete.getrevs(repo, 'obsolete')
21
21
22 def _gethiddenblockers(repo):
22 def _gethiddenblockers(repo):
23 """Get revisions that will block hidden changesets from being filtered
23 """Get revisions that will block hidden changesets from being filtered
24
24
25 This is a standalone function to help extensions to wrap it."""
25 This is a standalone function to help extensions to wrap it."""
26 assert not repo.changelog.filteredrevs
26 assert not repo.changelog.filteredrevs
27 hideable = hideablerevs(repo)
27 hideable = hideablerevs(repo)
28 blockers = []
28 blockers = []
29 if hideable:
29 if hideable:
30 # We use cl to avoid recursive lookup from repo[xxx]
30 cl = repo.changelog
31 cl = repo.changelog
31 firsthideable = min(hideable)
32 firsthideable = min(hideable)
32 revs = cl.revs(start=firsthideable)
33 revs = cl.revs(start=firsthideable)
33 tofilter = repo.revs(
34 tofilter = repo.revs(
34 '(%ld) and children(%ld)', list(revs), list(hideable))
35 '(%ld) and children(%ld)', list(revs), list(hideable))
35 blockers = [r for r in tofilter if r not in hideable]
36 blockers = [r for r in tofilter if r not in hideable]
36 for par in repo[None].parents():
37 for par in repo[None].parents():
37 blockers.append(par.rev())
38 blockers.append(par.rev())
38 for bm in repo._bookmarks.values():
39 for bm in repo._bookmarks.values():
39 blockers.append(repo[bm].rev())
40 blockers.append(cl.rev(bm))
40 tags = {}
41 tags = {}
41 tagsmod.readlocaltags(repo.ui, repo, tags, {})
42 tagsmod.readlocaltags(repo.ui, repo, tags, {})
42 if tags:
43 if tags:
43 blockers.extend(repo[t[0]].rev() for t in tags.values())
44 blockers.extend(cl.rev(t[0]) for t in tags.values())
44 return blockers
45 return blockers
45
46
46 def computehidden(repo):
47 def computehidden(repo):
47 """compute the set of hidden revision to filter
48 """compute the set of hidden revision to filter
48
49
49 During most operation hidden should be filtered."""
50 During most operation hidden should be filtered."""
50 assert not repo.changelog.filteredrevs
51 assert not repo.changelog.filteredrevs
51 hideable = hideablerevs(repo)
52 hideable = hideablerevs(repo)
52 if hideable:
53 if hideable:
53 cl = repo.changelog
54 cl = repo.changelog
54 blocked = cl.ancestors(_gethiddenblockers(repo), inclusive=True)
55 blocked = cl.ancestors(_gethiddenblockers(repo), inclusive=True)
55 return frozenset(r for r in hideable if r not in blocked)
56 return frozenset(r for r in hideable if r not in blocked)
56 return frozenset()
57 return frozenset()
57
58
58 def computeunserved(repo):
59 def computeunserved(repo):
59 """compute the set of revision that should be filtered when used a server
60 """compute the set of revision that should be filtered when used a server
60
61
61 Secret and hidden changeset should not pretend to be here."""
62 Secret and hidden changeset should not pretend to be here."""
62 assert not repo.changelog.filteredrevs
63 assert not repo.changelog.filteredrevs
63 # fast path in simple case to avoid impact of non optimised code
64 # fast path in simple case to avoid impact of non optimised code
64 hiddens = filterrevs(repo, 'visible')
65 hiddens = filterrevs(repo, 'visible')
65 if phases.hassecret(repo):
66 if phases.hassecret(repo):
66 cl = repo.changelog
67 cl = repo.changelog
67 secret = phases.secret
68 secret = phases.secret
68 getphase = repo._phasecache.phase
69 getphase = repo._phasecache.phase
69 first = min(cl.rev(n) for n in repo._phasecache.phaseroots[secret])
70 first = min(cl.rev(n) for n in repo._phasecache.phaseroots[secret])
70 revs = cl.revs(start=first)
71 revs = cl.revs(start=first)
71 secrets = set(r for r in revs if getphase(repo, r) >= secret)
72 secrets = set(r for r in revs if getphase(repo, r) >= secret)
72 return frozenset(hiddens | secrets)
73 return frozenset(hiddens | secrets)
73 else:
74 else:
74 return hiddens
75 return hiddens
75
76
76 def computemutable(repo):
77 def computemutable(repo):
77 """compute the set of revision that should be filtered when used a server
78 """compute the set of revision that should be filtered when used a server
78
79
79 Secret and hidden changeset should not pretend to be here."""
80 Secret and hidden changeset should not pretend to be here."""
80 assert not repo.changelog.filteredrevs
81 assert not repo.changelog.filteredrevs
81 # fast check to avoid revset call on huge repo
82 # fast check to avoid revset call on huge repo
82 if util.any(repo._phasecache.phaseroots[1:]):
83 if util.any(repo._phasecache.phaseroots[1:]):
83 getphase = repo._phasecache.phase
84 getphase = repo._phasecache.phase
84 maymutable = filterrevs(repo, 'base')
85 maymutable = filterrevs(repo, 'base')
85 return frozenset(r for r in maymutable if getphase(repo, r))
86 return frozenset(r for r in maymutable if getphase(repo, r))
86 return frozenset()
87 return frozenset()
87
88
88 def computeimpactable(repo):
89 def computeimpactable(repo):
89 """Everything impactable by mutable revision
90 """Everything impactable by mutable revision
90
91
91 The immutable filter still have some chance to get invalidated. This will
92 The immutable filter still have some chance to get invalidated. This will
92 happen when:
93 happen when:
93
94
94 - you garbage collect hidden changeset,
95 - you garbage collect hidden changeset,
95 - public phase is moved backward,
96 - public phase is moved backward,
96 - something is changed in the filtering (this could be fixed)
97 - something is changed in the filtering (this could be fixed)
97
98
98 This filter out any mutable changeset and any public changeset that may be
99 This filter out any mutable changeset and any public changeset that may be
99 impacted by something happening to a mutable revision.
100 impacted by something happening to a mutable revision.
100
101
101 This is achieved by filtered everything with a revision number egal or
102 This is achieved by filtered everything with a revision number egal or
102 higher than the first mutable changeset is filtered."""
103 higher than the first mutable changeset is filtered."""
103 assert not repo.changelog.filteredrevs
104 assert not repo.changelog.filteredrevs
104 cl = repo.changelog
105 cl = repo.changelog
105 firstmutable = len(cl)
106 firstmutable = len(cl)
106 for roots in repo._phasecache.phaseroots[1:]:
107 for roots in repo._phasecache.phaseroots[1:]:
107 if roots:
108 if roots:
108 firstmutable = min(firstmutable, min(cl.rev(r) for r in roots))
109 firstmutable = min(firstmutable, min(cl.rev(r) for r in roots))
109 # protect from nullrev root
110 # protect from nullrev root
110 firstmutable = max(0, firstmutable)
111 firstmutable = max(0, firstmutable)
111 return frozenset(xrange(firstmutable, len(cl)))
112 return frozenset(xrange(firstmutable, len(cl)))
112
113
113 # function to compute filtered set
114 # function to compute filtered set
114 #
115 #
115 # When adding a new filter you MUST update the table at:
116 # When adding a new filter you MUST update the table at:
116 # mercurial.branchmap.subsettable
117 # mercurial.branchmap.subsettable
117 # Otherwise your filter will have to recompute all its branches cache
118 # Otherwise your filter will have to recompute all its branches cache
118 # from scratch (very slow).
119 # from scratch (very slow).
119 filtertable = {'visible': computehidden,
120 filtertable = {'visible': computehidden,
120 'served': computeunserved,
121 'served': computeunserved,
121 'immutable': computemutable,
122 'immutable': computemutable,
122 'base': computeimpactable}
123 'base': computeimpactable}
123
124
124 def filterrevs(repo, filtername):
125 def filterrevs(repo, filtername):
125 """returns set of filtered revision for this filter name"""
126 """returns set of filtered revision for this filter name"""
126 if filtername not in repo.filteredrevcache:
127 if filtername not in repo.filteredrevcache:
127 func = filtertable[filtername]
128 func = filtertable[filtername]
128 repo.filteredrevcache[filtername] = func(repo.unfiltered())
129 repo.filteredrevcache[filtername] = func(repo.unfiltered())
129 return repo.filteredrevcache[filtername]
130 return repo.filteredrevcache[filtername]
130
131
131 class repoview(object):
132 class repoview(object):
132 """Provide a read/write view of a repo through a filtered changelog
133 """Provide a read/write view of a repo through a filtered changelog
133
134
134 This object is used to access a filtered version of a repository without
135 This object is used to access a filtered version of a repository without
135 altering the original repository object itself. We can not alter the
136 altering the original repository object itself. We can not alter the
136 original object for two main reasons:
137 original object for two main reasons:
137 - It prevents the use of a repo with multiple filters at the same time. In
138 - It prevents the use of a repo with multiple filters at the same time. In
138 particular when multiple threads are involved.
139 particular when multiple threads are involved.
139 - It makes scope of the filtering harder to control.
140 - It makes scope of the filtering harder to control.
140
141
141 This object behaves very closely to the original repository. All attribute
142 This object behaves very closely to the original repository. All attribute
142 operations are done on the original repository:
143 operations are done on the original repository:
143 - An access to `repoview.someattr` actually returns `repo.someattr`,
144 - An access to `repoview.someattr` actually returns `repo.someattr`,
144 - A write to `repoview.someattr` actually sets value of `repo.someattr`,
145 - A write to `repoview.someattr` actually sets value of `repo.someattr`,
145 - A deletion of `repoview.someattr` actually drops `someattr`
146 - A deletion of `repoview.someattr` actually drops `someattr`
146 from `repo.__dict__`.
147 from `repo.__dict__`.
147
148
148 The only exception is the `changelog` property. It is overridden to return
149 The only exception is the `changelog` property. It is overridden to return
149 a (surface) copy of `repo.changelog` with some revisions filtered. The
150 a (surface) copy of `repo.changelog` with some revisions filtered. The
150 `filtername` attribute of the view control the revisions that need to be
151 `filtername` attribute of the view control the revisions that need to be
151 filtered. (the fact the changelog is copied is an implementation detail).
152 filtered. (the fact the changelog is copied is an implementation detail).
152
153
153 Unlike attributes, this object intercepts all method calls. This means that
154 Unlike attributes, this object intercepts all method calls. This means that
154 all methods are run on the `repoview` object with the filtered `changelog`
155 all methods are run on the `repoview` object with the filtered `changelog`
155 property. For this purpose the simple `repoview` class must be mixed with
156 property. For this purpose the simple `repoview` class must be mixed with
156 the actual class of the repository. This ensures that the resulting
157 the actual class of the repository. This ensures that the resulting
157 `repoview` object have the very same methods than the repo object. This
158 `repoview` object have the very same methods than the repo object. This
158 leads to the property below.
159 leads to the property below.
159
160
160 repoview.method() --> repo.__class__.method(repoview)
161 repoview.method() --> repo.__class__.method(repoview)
161
162
162 The inheritance has to be done dynamically because `repo` can be of any
163 The inheritance has to be done dynamically because `repo` can be of any
163 subclasses of `localrepo`. Eg: `bundlerepo` or `statichttprepo`.
164 subclasses of `localrepo`. Eg: `bundlerepo` or `statichttprepo`.
164 """
165 """
165
166
166 def __init__(self, repo, filtername):
167 def __init__(self, repo, filtername):
167 object.__setattr__(self, '_unfilteredrepo', repo)
168 object.__setattr__(self, '_unfilteredrepo', repo)
168 object.__setattr__(self, 'filtername', filtername)
169 object.__setattr__(self, 'filtername', filtername)
169 object.__setattr__(self, '_clcachekey', None)
170 object.__setattr__(self, '_clcachekey', None)
170 object.__setattr__(self, '_clcache', None)
171 object.__setattr__(self, '_clcache', None)
171
172
172 # not a propertycache on purpose we shall implement a proper cache later
173 # not a propertycache on purpose we shall implement a proper cache later
173 @property
174 @property
174 def changelog(self):
175 def changelog(self):
175 """return a filtered version of the changeset
176 """return a filtered version of the changeset
176
177
177 this changelog must not be used for writing"""
178 this changelog must not be used for writing"""
178 # some cache may be implemented later
179 # some cache may be implemented later
179 unfi = self._unfilteredrepo
180 unfi = self._unfilteredrepo
180 unfichangelog = unfi.changelog
181 unfichangelog = unfi.changelog
181 revs = filterrevs(unfi, self.filtername)
182 revs = filterrevs(unfi, self.filtername)
182 cl = self._clcache
183 cl = self._clcache
183 newkey = (len(unfichangelog), unfichangelog.tip(), hash(revs))
184 newkey = (len(unfichangelog), unfichangelog.tip(), hash(revs))
184 if cl is not None:
185 if cl is not None:
185 # we need to check curkey too for some obscure reason.
186 # we need to check curkey too for some obscure reason.
186 # MQ test show a corruption of the underlying repo (in _clcache)
187 # MQ test show a corruption of the underlying repo (in _clcache)
187 # without change in the cachekey.
188 # without change in the cachekey.
188 oldfilter = cl.filteredrevs
189 oldfilter = cl.filteredrevs
189 try:
190 try:
190 cl.filterrevs = () # disable filtering for tip
191 cl.filterrevs = () # disable filtering for tip
191 curkey = (len(cl), cl.tip(), hash(oldfilter))
192 curkey = (len(cl), cl.tip(), hash(oldfilter))
192 finally:
193 finally:
193 cl.filteredrevs = oldfilter
194 cl.filteredrevs = oldfilter
194 if newkey != self._clcachekey or newkey != curkey:
195 if newkey != self._clcachekey or newkey != curkey:
195 cl = None
196 cl = None
196 # could have been made None by the previous if
197 # could have been made None by the previous if
197 if cl is None:
198 if cl is None:
198 cl = copy.copy(unfichangelog)
199 cl = copy.copy(unfichangelog)
199 cl.filteredrevs = revs
200 cl.filteredrevs = revs
200 object.__setattr__(self, '_clcache', cl)
201 object.__setattr__(self, '_clcache', cl)
201 object.__setattr__(self, '_clcachekey', newkey)
202 object.__setattr__(self, '_clcachekey', newkey)
202 return cl
203 return cl
203
204
204 def unfiltered(self):
205 def unfiltered(self):
205 """Return an unfiltered version of a repo"""
206 """Return an unfiltered version of a repo"""
206 return self._unfilteredrepo
207 return self._unfilteredrepo
207
208
208 def filtered(self, name):
209 def filtered(self, name):
209 """Return a filtered version of a repository"""
210 """Return a filtered version of a repository"""
210 if name == self.filtername:
211 if name == self.filtername:
211 return self
212 return self
212 return self.unfiltered().filtered(name)
213 return self.unfiltered().filtered(name)
213
214
214 # everything access are forwarded to the proxied repo
215 # everything access are forwarded to the proxied repo
215 def __getattr__(self, attr):
216 def __getattr__(self, attr):
216 return getattr(self._unfilteredrepo, attr)
217 return getattr(self._unfilteredrepo, attr)
217
218
218 def __setattr__(self, attr, value):
219 def __setattr__(self, attr, value):
219 return setattr(self._unfilteredrepo, attr, value)
220 return setattr(self._unfilteredrepo, attr, value)
220
221
221 def __delattr__(self, attr):
222 def __delattr__(self, attr):
222 return delattr(self._unfilteredrepo, attr)
223 return delattr(self._unfilteredrepo, attr)
223
224
224 # The `requirements` attribute is initialized during __init__. But
225 # The `requirements` attribute is initialized during __init__. But
225 # __getattr__ won't be called as it also exists on the class. We need
226 # __getattr__ won't be called as it also exists on the class. We need
226 # explicit forwarding to main repo here
227 # explicit forwarding to main repo here
227 @property
228 @property
228 def requirements(self):
229 def requirements(self):
229 return self._unfilteredrepo.requirements
230 return self._unfilteredrepo.requirements
General Comments 0
You need to be logged in to leave comments. Login now