##// END OF EJS Templates
branchmap: introduce branchheads() method
Brodie Rao -
r20188:3a372782 default
parent child Browse files
Show More
@@ -1,275 +1,281 b''
1 # branchmap.py - logic to computes, maintain and stores branchmap for local repo
1 # branchmap.py - logic to computes, maintain and stores branchmap for local repo
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from node import bin, hex, nullid, nullrev
8 from node import bin, hex, nullid, nullrev
9 import encoding
9 import encoding
10 import util
10 import util
11
11
12 def _filename(repo):
12 def _filename(repo):
13 """name of a branchcache file for a given repo or repoview"""
13 """name of a branchcache file for a given repo or repoview"""
14 filename = "cache/branch2"
14 filename = "cache/branch2"
15 if repo.filtername:
15 if repo.filtername:
16 filename = '%s-%s' % (filename, repo.filtername)
16 filename = '%s-%s' % (filename, repo.filtername)
17 return filename
17 return filename
18
18
19 def read(repo):
19 def read(repo):
20 try:
20 try:
21 f = repo.opener(_filename(repo))
21 f = repo.opener(_filename(repo))
22 lines = f.read().split('\n')
22 lines = f.read().split('\n')
23 f.close()
23 f.close()
24 except (IOError, OSError):
24 except (IOError, OSError):
25 return None
25 return None
26
26
27 try:
27 try:
28 cachekey = lines.pop(0).split(" ", 2)
28 cachekey = lines.pop(0).split(" ", 2)
29 last, lrev = cachekey[:2]
29 last, lrev = cachekey[:2]
30 last, lrev = bin(last), int(lrev)
30 last, lrev = bin(last), int(lrev)
31 filteredhash = None
31 filteredhash = None
32 if len(cachekey) > 2:
32 if len(cachekey) > 2:
33 filteredhash = bin(cachekey[2])
33 filteredhash = bin(cachekey[2])
34 partial = branchcache(tipnode=last, tiprev=lrev,
34 partial = branchcache(tipnode=last, tiprev=lrev,
35 filteredhash=filteredhash)
35 filteredhash=filteredhash)
36 if not partial.validfor(repo):
36 if not partial.validfor(repo):
37 # invalidate the cache
37 # invalidate the cache
38 raise ValueError('tip differs')
38 raise ValueError('tip differs')
39 for l in lines:
39 for l in lines:
40 if not l:
40 if not l:
41 continue
41 continue
42 node, state, label = l.split(" ", 2)
42 node, state, label = l.split(" ", 2)
43 if state not in 'oc':
43 if state not in 'oc':
44 raise ValueError('invalid branch state')
44 raise ValueError('invalid branch state')
45 label = encoding.tolocal(label.strip())
45 label = encoding.tolocal(label.strip())
46 if not node in repo:
46 if not node in repo:
47 raise ValueError('node %s does not exist' % node)
47 raise ValueError('node %s does not exist' % node)
48 node = bin(node)
48 node = bin(node)
49 partial.setdefault(label, []).append(node)
49 partial.setdefault(label, []).append(node)
50 if state == 'c':
50 if state == 'c':
51 partial._closednodes.add(node)
51 partial._closednodes.add(node)
52 except KeyboardInterrupt:
52 except KeyboardInterrupt:
53 raise
53 raise
54 except Exception, inst:
54 except Exception, inst:
55 if repo.ui.debugflag:
55 if repo.ui.debugflag:
56 msg = 'invalid branchheads cache'
56 msg = 'invalid branchheads cache'
57 if repo.filtername is not None:
57 if repo.filtername is not None:
58 msg += ' (%s)' % repo.filtername
58 msg += ' (%s)' % repo.filtername
59 msg += ': %s\n'
59 msg += ': %s\n'
60 repo.ui.warn(msg % inst)
60 repo.ui.warn(msg % inst)
61 partial = None
61 partial = None
62 return partial
62 return partial
63
63
64
64
65
65
66 ### Nearest subset relation
66 ### Nearest subset relation
67 # Nearest subset of filter X is a filter Y so that:
67 # Nearest subset of filter X is a filter Y so that:
68 # * Y is included in X,
68 # * Y is included in X,
69 # * X - Y is as small as possible.
69 # * X - Y is as small as possible.
70 # This create and ordering used for branchmap purpose.
70 # This create and ordering used for branchmap purpose.
71 # the ordering may be partial
71 # the ordering may be partial
72 subsettable = {None: 'visible',
72 subsettable = {None: 'visible',
73 'visible': 'served',
73 'visible': 'served',
74 'served': 'immutable',
74 'served': 'immutable',
75 'immutable': 'base'}
75 'immutable': 'base'}
76
76
77 def updatecache(repo):
77 def updatecache(repo):
78 cl = repo.changelog
78 cl = repo.changelog
79 filtername = repo.filtername
79 filtername = repo.filtername
80 partial = repo._branchcaches.get(filtername)
80 partial = repo._branchcaches.get(filtername)
81
81
82 revs = []
82 revs = []
83 if partial is None or not partial.validfor(repo):
83 if partial is None or not partial.validfor(repo):
84 partial = read(repo)
84 partial = read(repo)
85 if partial is None:
85 if partial is None:
86 subsetname = subsettable.get(filtername)
86 subsetname = subsettable.get(filtername)
87 if subsetname is None:
87 if subsetname is None:
88 partial = branchcache()
88 partial = branchcache()
89 else:
89 else:
90 subset = repo.filtered(subsetname)
90 subset = repo.filtered(subsetname)
91 partial = subset.branchmap().copy()
91 partial = subset.branchmap().copy()
92 extrarevs = subset.changelog.filteredrevs - cl.filteredrevs
92 extrarevs = subset.changelog.filteredrevs - cl.filteredrevs
93 revs.extend(r for r in extrarevs if r <= partial.tiprev)
93 revs.extend(r for r in extrarevs if r <= partial.tiprev)
94 revs.extend(cl.revs(start=partial.tiprev + 1))
94 revs.extend(cl.revs(start=partial.tiprev + 1))
95 if revs:
95 if revs:
96 partial.update(repo, revs)
96 partial.update(repo, revs)
97 partial.write(repo)
97 partial.write(repo)
98 assert partial.validfor(repo), filtername
98 assert partial.validfor(repo), filtername
99 repo._branchcaches[repo.filtername] = partial
99 repo._branchcaches[repo.filtername] = partial
100
100
101 class branchcache(dict):
101 class branchcache(dict):
102 """A dict like object that hold branches heads cache.
102 """A dict like object that hold branches heads cache.
103
103
104 This cache is used to avoid costly computations to determine all the
104 This cache is used to avoid costly computations to determine all the
105 branch heads of a repo.
105 branch heads of a repo.
106
106
107 The cache is serialized on disk in the following format:
107 The cache is serialized on disk in the following format:
108
108
109 <tip hex node> <tip rev number> [optional filtered repo hex hash]
109 <tip hex node> <tip rev number> [optional filtered repo hex hash]
110 <branch head hex node> <open/closed state> <branch name>
110 <branch head hex node> <open/closed state> <branch name>
111 <branch head hex node> <open/closed state> <branch name>
111 <branch head hex node> <open/closed state> <branch name>
112 ...
112 ...
113
113
114 The first line is used to check if the cache is still valid. If the
114 The first line is used to check if the cache is still valid. If the
115 branch cache is for a filtered repo view, an optional third hash is
115 branch cache is for a filtered repo view, an optional third hash is
116 included that hashes the hashes of all filtered revisions.
116 included that hashes the hashes of all filtered revisions.
117
117
118 The open/closed state is represented by a single letter 'o' or 'c'.
118 The open/closed state is represented by a single letter 'o' or 'c'.
119 This field can be used to avoid changelog reads when determining if a
119 This field can be used to avoid changelog reads when determining if a
120 branch head closes a branch or not.
120 branch head closes a branch or not.
121 """
121 """
122
122
123 def __init__(self, entries=(), tipnode=nullid, tiprev=nullrev,
123 def __init__(self, entries=(), tipnode=nullid, tiprev=nullrev,
124 filteredhash=None, closednodes=None):
124 filteredhash=None, closednodes=None):
125 super(branchcache, self).__init__(entries)
125 super(branchcache, self).__init__(entries)
126 self.tipnode = tipnode
126 self.tipnode = tipnode
127 self.tiprev = tiprev
127 self.tiprev = tiprev
128 self.filteredhash = filteredhash
128 self.filteredhash = filteredhash
129 # closednodes is a set of nodes that close their branch. If the branch
129 # closednodes is a set of nodes that close their branch. If the branch
130 # cache has been updated, it may contain nodes that are no longer
130 # cache has been updated, it may contain nodes that are no longer
131 # heads.
131 # heads.
132 if closednodes is None:
132 if closednodes is None:
133 self._closednodes = set()
133 self._closednodes = set()
134 else:
134 else:
135 self._closednodes = closednodes
135 self._closednodes = closednodes
136
136
137 def _hashfiltered(self, repo):
137 def _hashfiltered(self, repo):
138 """build hash of revision filtered in the current cache
138 """build hash of revision filtered in the current cache
139
139
140 Tracking tipnode and tiprev is not enough to ensure validity of the
140 Tracking tipnode and tiprev is not enough to ensure validity of the
141 cache as they do not help to distinct cache that ignored various
141 cache as they do not help to distinct cache that ignored various
142 revision bellow tiprev.
142 revision bellow tiprev.
143
143
144 To detect such difference, we build a cache of all ignored revisions.
144 To detect such difference, we build a cache of all ignored revisions.
145 """
145 """
146 cl = repo.changelog
146 cl = repo.changelog
147 if not cl.filteredrevs:
147 if not cl.filteredrevs:
148 return None
148 return None
149 key = None
149 key = None
150 revs = sorted(r for r in cl.filteredrevs if r <= self.tiprev)
150 revs = sorted(r for r in cl.filteredrevs if r <= self.tiprev)
151 if revs:
151 if revs:
152 s = util.sha1()
152 s = util.sha1()
153 for rev in revs:
153 for rev in revs:
154 s.update('%s;' % rev)
154 s.update('%s;' % rev)
155 key = s.digest()
155 key = s.digest()
156 return key
156 return key
157
157
158 def validfor(self, repo):
158 def validfor(self, repo):
159 """Is the cache content valid regarding a repo
159 """Is the cache content valid regarding a repo
160
160
161 - False when cached tipnode is unknown or if we detect a strip.
161 - False when cached tipnode is unknown or if we detect a strip.
162 - True when cache is up to date or a subset of current repo."""
162 - True when cache is up to date or a subset of current repo."""
163 try:
163 try:
164 return ((self.tipnode == repo.changelog.node(self.tiprev))
164 return ((self.tipnode == repo.changelog.node(self.tiprev))
165 and (self.filteredhash == self._hashfiltered(repo)))
165 and (self.filteredhash == self._hashfiltered(repo)))
166 except IndexError:
166 except IndexError:
167 return False
167 return False
168
168
169 def _branchtip(self, heads):
169 def _branchtip(self, heads):
170 tip = heads[-1]
170 tip = heads[-1]
171 closed = True
171 closed = True
172 for h in reversed(heads):
172 for h in reversed(heads):
173 if h not in self._closednodes:
173 if h not in self._closednodes:
174 tip = h
174 tip = h
175 closed = False
175 closed = False
176 break
176 break
177 return tip, closed
177 return tip, closed
178
178
179 def branchtip(self, branch):
179 def branchtip(self, branch):
180 return self._branchtip(self[branch])[0]
180 return self._branchtip(self[branch])[0]
181
181
182 def branchheads(self, branch, closed=False):
183 heads = self[branch]
184 if not closed:
185 heads = [h for h in heads if h not in self._closednodes]
186 return heads
187
182 def copy(self):
188 def copy(self):
183 """return an deep copy of the branchcache object"""
189 """return an deep copy of the branchcache object"""
184 return branchcache(self, self.tipnode, self.tiprev, self.filteredhash,
190 return branchcache(self, self.tipnode, self.tiprev, self.filteredhash,
185 self._closednodes)
191 self._closednodes)
186
192
187 def write(self, repo):
193 def write(self, repo):
188 try:
194 try:
189 f = repo.opener(_filename(repo), "w", atomictemp=True)
195 f = repo.opener(_filename(repo), "w", atomictemp=True)
190 cachekey = [hex(self.tipnode), str(self.tiprev)]
196 cachekey = [hex(self.tipnode), str(self.tiprev)]
191 if self.filteredhash is not None:
197 if self.filteredhash is not None:
192 cachekey.append(hex(self.filteredhash))
198 cachekey.append(hex(self.filteredhash))
193 f.write(" ".join(cachekey) + '\n')
199 f.write(" ".join(cachekey) + '\n')
194 for label, nodes in sorted(self.iteritems()):
200 for label, nodes in sorted(self.iteritems()):
195 for node in nodes:
201 for node in nodes:
196 if node in self._closednodes:
202 if node in self._closednodes:
197 state = 'c'
203 state = 'c'
198 else:
204 else:
199 state = 'o'
205 state = 'o'
200 f.write("%s %s %s\n" % (hex(node), state,
206 f.write("%s %s %s\n" % (hex(node), state,
201 encoding.fromlocal(label)))
207 encoding.fromlocal(label)))
202 f.close()
208 f.close()
203 except (IOError, OSError, util.Abort):
209 except (IOError, OSError, util.Abort):
204 # Abort may be raise by read only opener
210 # Abort may be raise by read only opener
205 pass
211 pass
206
212
207 def update(self, repo, revgen):
213 def update(self, repo, revgen):
208 """Given a branchhead cache, self, that may have extra nodes or be
214 """Given a branchhead cache, self, that may have extra nodes or be
209 missing heads, and a generator of nodes that are at least a superset of
215 missing heads, and a generator of nodes that are at least a superset of
210 heads missing, this function updates self to be correct.
216 heads missing, this function updates self to be correct.
211 """
217 """
212 cl = repo.changelog
218 cl = repo.changelog
213 # collect new branch entries
219 # collect new branch entries
214 newbranches = {}
220 newbranches = {}
215 getbranchinfo = cl.branchinfo
221 getbranchinfo = cl.branchinfo
216 for r in revgen:
222 for r in revgen:
217 branch, closesbranch = getbranchinfo(r)
223 branch, closesbranch = getbranchinfo(r)
218 node = cl.node(r)
224 node = cl.node(r)
219 newbranches.setdefault(branch, []).append(node)
225 newbranches.setdefault(branch, []).append(node)
220 if closesbranch:
226 if closesbranch:
221 self._closednodes.add(node)
227 self._closednodes.add(node)
222 # if older branchheads are reachable from new ones, they aren't
228 # if older branchheads are reachable from new ones, they aren't
223 # really branchheads. Note checking parents is insufficient:
229 # really branchheads. Note checking parents is insufficient:
224 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
230 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
225 for branch, newnodes in newbranches.iteritems():
231 for branch, newnodes in newbranches.iteritems():
226 bheads = self.setdefault(branch, [])
232 bheads = self.setdefault(branch, [])
227 # Remove candidate heads that no longer are in the repo (e.g., as
233 # Remove candidate heads that no longer are in the repo (e.g., as
228 # the result of a strip that just happened). Avoid using 'node in
234 # the result of a strip that just happened). Avoid using 'node in
229 # self' here because that dives down into branchcache code somewhat
235 # self' here because that dives down into branchcache code somewhat
230 # recursively.
236 # recursively.
231 bheadrevs = [cl.rev(node) for node in bheads
237 bheadrevs = [cl.rev(node) for node in bheads
232 if cl.hasnode(node)]
238 if cl.hasnode(node)]
233 newheadrevs = [cl.rev(node) for node in newnodes
239 newheadrevs = [cl.rev(node) for node in newnodes
234 if cl.hasnode(node)]
240 if cl.hasnode(node)]
235 ctxisnew = bheadrevs and min(newheadrevs) > max(bheadrevs)
241 ctxisnew = bheadrevs and min(newheadrevs) > max(bheadrevs)
236 # Remove duplicates - nodes that are in newheadrevs and are already
242 # Remove duplicates - nodes that are in newheadrevs and are already
237 # in bheadrevs. This can happen if you strip a node whose parent
243 # in bheadrevs. This can happen if you strip a node whose parent
238 # was already a head (because they're on different branches).
244 # was already a head (because they're on different branches).
239 bheadrevs = sorted(set(bheadrevs).union(newheadrevs))
245 bheadrevs = sorted(set(bheadrevs).union(newheadrevs))
240
246
241 # Starting from tip means fewer passes over reachable. If we know
247 # Starting from tip means fewer passes over reachable. If we know
242 # the new candidates are not ancestors of existing heads, we don't
248 # the new candidates are not ancestors of existing heads, we don't
243 # have to examine ancestors of existing heads
249 # have to examine ancestors of existing heads
244 if ctxisnew:
250 if ctxisnew:
245 iterrevs = sorted(newheadrevs)
251 iterrevs = sorted(newheadrevs)
246 else:
252 else:
247 iterrevs = list(bheadrevs)
253 iterrevs = list(bheadrevs)
248
254
249 # This loop prunes out two kinds of heads - heads that are
255 # This loop prunes out two kinds of heads - heads that are
250 # superseded by a head in newheadrevs, and newheadrevs that are not
256 # superseded by a head in newheadrevs, and newheadrevs that are not
251 # heads because an existing head is their descendant.
257 # heads because an existing head is their descendant.
252 while iterrevs:
258 while iterrevs:
253 latest = iterrevs.pop()
259 latest = iterrevs.pop()
254 if latest not in bheadrevs:
260 if latest not in bheadrevs:
255 continue
261 continue
256 ancestors = set(cl.ancestors([latest],
262 ancestors = set(cl.ancestors([latest],
257 bheadrevs[0]))
263 bheadrevs[0]))
258 if ancestors:
264 if ancestors:
259 bheadrevs = [b for b in bheadrevs if b not in ancestors]
265 bheadrevs = [b for b in bheadrevs if b not in ancestors]
260 self[branch] = [cl.node(rev) for rev in bheadrevs]
266 self[branch] = [cl.node(rev) for rev in bheadrevs]
261 tiprev = max(bheadrevs)
267 tiprev = max(bheadrevs)
262 if tiprev > self.tiprev:
268 if tiprev > self.tiprev:
263 self.tipnode = cl.node(tiprev)
269 self.tipnode = cl.node(tiprev)
264 self.tiprev = tiprev
270 self.tiprev = tiprev
265
271
266 if not self.validfor(repo):
272 if not self.validfor(repo):
267 # cache key are not valid anymore
273 # cache key are not valid anymore
268 self.tipnode = nullid
274 self.tipnode = nullid
269 self.tiprev = nullrev
275 self.tiprev = nullrev
270 for heads in self.values():
276 for heads in self.values():
271 tiprev = max(cl.rev(node) for node in heads)
277 tiprev = max(cl.rev(node) for node in heads)
272 if tiprev > self.tiprev:
278 if tiprev > self.tiprev:
273 self.tipnode = cl.node(tiprev)
279 self.tipnode = cl.node(tiprev)
274 self.tiprev = tiprev
280 self.tiprev = tiprev
275 self.filteredhash = self._hashfiltered(repo)
281 self.filteredhash = self._hashfiltered(repo)
General Comments 0
You need to be logged in to leave comments. Login now