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