Mercurial > hg-stable
annotate mercurial/repoview.py @ 18102:3c7b67b76190
clfilter: introduce a "unserver" filtering mode
This mode is for repository used as a server. It filter secret and hidden
changeset out.
It is put to use in later changeset.
author | Pierre-Yves David <pierre-yves.david@logilab.fr> |
---|---|
date | Mon, 17 Dec 2012 17:12:02 +0100 |
parents | a464deecc9dd |
children | c0c943ef4e55 |
rev | line source |
---|---|
18100
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
1 # repoview.py - Filtered view of a localrepo object |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
2 # |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
3 # Copyright 2012 Pierre-Yves David <pierre-yves.david@ens-lyon.org> |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
4 # Logilab SA <contact@logilab.fr> |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
5 # |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
6 # This software may be used and distributed according to the terms of the |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
7 # GNU General Public License version 2 or any later version. |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
8 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
9 import copy |
18102
3c7b67b76190
clfilter: introduce a "unserver" filtering mode
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
18101
diff
changeset
|
10 import phases |
3c7b67b76190
clfilter: introduce a "unserver" filtering mode
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
18101
diff
changeset
|
11 |
3c7b67b76190
clfilter: introduce a "unserver" filtering mode
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
18101
diff
changeset
|
12 def computeunserved(repo): |
3c7b67b76190
clfilter: introduce a "unserver" filtering mode
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
18101
diff
changeset
|
13 """compute the set of revision that should be filtered when used a server |
3c7b67b76190
clfilter: introduce a "unserver" filtering mode
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
18101
diff
changeset
|
14 |
3c7b67b76190
clfilter: introduce a "unserver" filtering mode
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
18101
diff
changeset
|
15 Secret and hidden changeset should not pretend to be here.""" |
3c7b67b76190
clfilter: introduce a "unserver" filtering mode
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
18101
diff
changeset
|
16 assert not repo.changelog.filteredrevs |
3c7b67b76190
clfilter: introduce a "unserver" filtering mode
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
18101
diff
changeset
|
17 # fast path in simple case to avoid impact of non optimised code |
3c7b67b76190
clfilter: introduce a "unserver" filtering mode
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
18101
diff
changeset
|
18 if phases.hassecret(repo) or repo.obsstore: |
3c7b67b76190
clfilter: introduce a "unserver" filtering mode
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
18101
diff
changeset
|
19 return frozenset(repo.revs('hidden() + secret()')) |
3c7b67b76190
clfilter: introduce a "unserver" filtering mode
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
18101
diff
changeset
|
20 return () |
18100
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
21 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
22 # function to compute filtered set |
18102
3c7b67b76190
clfilter: introduce a "unserver" filtering mode
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
18101
diff
changeset
|
23 filtertable = {'unserved': computeunserved} |
18100
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
24 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
25 def filteredrevs(repo, filtername): |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
26 """returns set of filtered revision for this filter name""" |
18101
a464deecc9dd
clfilter: add a cache on repo for set of revision to filter for a given set.
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18100
diff
changeset
|
27 if filtername not in repo.filteredrevcache: |
a464deecc9dd
clfilter: add a cache on repo for set of revision to filter for a given set.
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18100
diff
changeset
|
28 func = filtertable[filtername] |
a464deecc9dd
clfilter: add a cache on repo for set of revision to filter for a given set.
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18100
diff
changeset
|
29 repo.filteredrevcache[filtername] = func(repo.unfiltered()) |
a464deecc9dd
clfilter: add a cache on repo for set of revision to filter for a given set.
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18100
diff
changeset
|
30 return repo.filteredrevcache[filtername] |
18100
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
31 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
32 class repoview(object): |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
33 """Provide a read/write view of a repo through a filtered changelog |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
34 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
35 This object is used to access a filtered version of a repository without |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
36 altering the original repository object itself. We can not alter the |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
37 original object for two main reasons: |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
38 - It prevents the use of a repo with multiple filters at the same time. In |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
39 particular when multiple threads are involved. |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
40 - It makes scope of the filtering harder to control. |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
41 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
42 This object behaves very closely to the original repository. All attribute |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
43 operations are done on the original repository: |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
44 - An access to `repoview.someattr` actually returns `repo.someattr`, |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
45 - A write to `repoview.someattr` actually sets value of `repo.someattr`, |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
46 - A deletion of `repoview.someattr` actually drops `someattr` |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
47 from `repo.__dict__`. |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
48 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
49 The only exception is the `changelog` property. It is overridden to return |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
50 a (surface) copy of `repo.changelog` with some revisions filtered. The |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
51 `filtername` attribute of the view control the revisions that need to be |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
52 filtered. (the fact the changelog is copied is an implementation detail). |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
53 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
54 Unlike attributes, this object intercepts all method calls. This means that |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
55 all methods are run on the `repoview` object with the filtered `changelog` |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
56 property. For this purpose the simple `repoview` class must be mixed with |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
57 the actual class of the repository. This ensures that the resulting |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
58 `repoview` object have the very same methods than the repo object. This |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
59 leads to the property below. |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
60 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
61 repoview.method() --> repo.__class__.method(repoview) |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
62 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
63 The inheritance has to be done dynamically because `repo` can be of any |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
64 subclasses of `localrepo`. Eg: `bundlerepo` or `httprepo`. |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
65 """ |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
66 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
67 def __init__(self, repo, filtername): |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
68 object.__setattr__(self, '_unfilteredrepo', repo) |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
69 object.__setattr__(self, 'filtername', filtername) |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
70 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
71 # not a cacheproperty on purpose we shall implement a proper cache later |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
72 @property |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
73 def changelog(self): |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
74 """return a filtered version of the changeset |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
75 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
76 this changelog must not be used for writing""" |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
77 # some cache may be implemented later |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
78 cl = copy.copy(self._unfilteredrepo.changelog) |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
79 cl.filteredrevs = filteredrevs(self._unfilteredrepo, self.filtername) |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
80 return cl |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
81 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
82 def unfiltered(self): |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
83 """Return an unfiltered version of a repo""" |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
84 return self._unfilteredrepo |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
85 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
86 def filtered(self, name): |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
87 """Return a filtered version of a repository""" |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
88 if name == self.filtername: |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
89 return self |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
90 return self.unfiltered().filtered(name) |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
91 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
92 # everything access are forwarded to the proxied repo |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
93 def __getattr__(self, attr): |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
94 return getattr(self._unfilteredrepo, attr) |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
95 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
96 def __setattr__(self, attr, value): |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
97 return setattr(self._unfilteredrepo, attr, value) |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
98 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
99 def __delattr__(self, attr): |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
100 return delattr(self._unfilteredrepo, attr) |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
101 |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
102 # The `requirement` attribut is initialiazed during __init__. But |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
103 # __getattr__ won't be called as it also exists on the class. We need |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
104 # explicit forwarding to main repo here |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
105 @property |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
106 def requirements(self): |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
107 return self._unfilteredrepo.requirements |
3a6ddacb7198
clfilter: add actual repo filtering mechanism
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff
changeset
|
108 |