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