##// END OF EJS Templates
discovery: factor out calculation of heads to not warn about...
Ryan McElroy -
r26862:894f54d8 default
parent child Browse files
Show More
@@ -1,397 +1,403 b''
1 1 # discovery.py - protocol changeset discovery functions
2 2 #
3 3 # Copyright 2010 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 __future__ import absolute_import
9 9
10 10 from .i18n import _
11 11 from .node import (
12 12 nullid,
13 13 short,
14 14 )
15 15
16 16 from . import (
17 17 bookmarks,
18 18 branchmap,
19 19 error,
20 20 obsolete,
21 21 phases,
22 22 setdiscovery,
23 23 treediscovery,
24 24 util,
25 25 )
26 26
27 27 def findcommonincoming(repo, remote, heads=None, force=False):
28 28 """Return a tuple (common, anyincoming, heads) used to identify the common
29 29 subset of nodes between repo and remote.
30 30
31 31 "common" is a list of (at least) the heads of the common subset.
32 32 "anyincoming" is testable as a boolean indicating if any nodes are missing
33 33 locally. If remote does not support getbundle, this actually is a list of
34 34 roots of the nodes that would be incoming, to be supplied to
35 35 changegroupsubset. No code except for pull should be relying on this fact
36 36 any longer.
37 37 "heads" is either the supplied heads, or else the remote's heads.
38 38
39 39 If you pass heads and they are all known locally, the response lists just
40 40 these heads in "common" and in "heads".
41 41
42 42 Please use findcommonoutgoing to compute the set of outgoing nodes to give
43 43 extensions a good hook into outgoing.
44 44 """
45 45
46 46 if not remote.capable('getbundle'):
47 47 return treediscovery.findcommonincoming(repo, remote, heads, force)
48 48
49 49 if heads:
50 50 allknown = True
51 51 knownnode = repo.changelog.hasnode # no nodemap until it is filtered
52 52 for h in heads:
53 53 if not knownnode(h):
54 54 allknown = False
55 55 break
56 56 if allknown:
57 57 return (heads, False, heads)
58 58
59 59 res = setdiscovery.findcommonheads(repo.ui, repo, remote,
60 60 abortwhenunrelated=not force)
61 61 common, anyinc, srvheads = res
62 62 return (list(common), anyinc, heads or list(srvheads))
63 63
64 64 class outgoing(object):
65 65 '''Represents the set of nodes present in a local repo but not in a
66 66 (possibly) remote one.
67 67
68 68 Members:
69 69
70 70 missing is a list of all nodes present in local but not in remote.
71 71 common is a list of all nodes shared between the two repos.
72 72 excluded is the list of missing changeset that shouldn't be sent remotely.
73 73 missingheads is the list of heads of missing.
74 74 commonheads is the list of heads of common.
75 75
76 76 The sets are computed on demand from the heads, unless provided upfront
77 77 by discovery.'''
78 78
79 79 def __init__(self, revlog, commonheads, missingheads):
80 80 self.commonheads = commonheads
81 81 self.missingheads = missingheads
82 82 self._revlog = revlog
83 83 self._common = None
84 84 self._missing = None
85 85 self.excluded = []
86 86
87 87 def _computecommonmissing(self):
88 88 sets = self._revlog.findcommonmissing(self.commonheads,
89 89 self.missingheads)
90 90 self._common, self._missing = sets
91 91
92 92 @util.propertycache
93 93 def common(self):
94 94 if self._common is None:
95 95 self._computecommonmissing()
96 96 return self._common
97 97
98 98 @util.propertycache
99 99 def missing(self):
100 100 if self._missing is None:
101 101 self._computecommonmissing()
102 102 return self._missing
103 103
104 104 def findcommonoutgoing(repo, other, onlyheads=None, force=False,
105 105 commoninc=None, portable=False):
106 106 '''Return an outgoing instance to identify the nodes present in repo but
107 107 not in other.
108 108
109 109 If onlyheads is given, only nodes ancestral to nodes in onlyheads
110 110 (inclusive) are included. If you already know the local repo's heads,
111 111 passing them in onlyheads is faster than letting them be recomputed here.
112 112
113 113 If commoninc is given, it must be the result of a prior call to
114 114 findcommonincoming(repo, other, force) to avoid recomputing it here.
115 115
116 116 If portable is given, compute more conservative common and missingheads,
117 117 to make bundles created from the instance more portable.'''
118 118 # declare an empty outgoing object to be filled later
119 119 og = outgoing(repo.changelog, None, None)
120 120
121 121 # get common set if not provided
122 122 if commoninc is None:
123 123 commoninc = findcommonincoming(repo, other, force=force)
124 124 og.commonheads, _any, _hds = commoninc
125 125
126 126 # compute outgoing
127 127 mayexclude = (repo._phasecache.phaseroots[phases.secret] or repo.obsstore)
128 128 if not mayexclude:
129 129 og.missingheads = onlyheads or repo.heads()
130 130 elif onlyheads is None:
131 131 # use visible heads as it should be cached
132 132 og.missingheads = repo.filtered("served").heads()
133 133 og.excluded = [ctx.node() for ctx in repo.set('secret() or extinct()')]
134 134 else:
135 135 # compute common, missing and exclude secret stuff
136 136 sets = repo.changelog.findcommonmissing(og.commonheads, onlyheads)
137 137 og._common, allmissing = sets
138 138 og._missing = missing = []
139 139 og.excluded = excluded = []
140 140 for node in allmissing:
141 141 ctx = repo[node]
142 142 if ctx.phase() >= phases.secret or ctx.extinct():
143 143 excluded.append(node)
144 144 else:
145 145 missing.append(node)
146 146 if len(missing) == len(allmissing):
147 147 missingheads = onlyheads
148 148 else: # update missing heads
149 149 missingheads = phases.newheads(repo, onlyheads, excluded)
150 150 og.missingheads = missingheads
151 151 if portable:
152 152 # recompute common and missingheads as if -r<rev> had been given for
153 153 # each head of missing, and --base <rev> for each head of the proper
154 154 # ancestors of missing
155 155 og._computecommonmissing()
156 156 cl = repo.changelog
157 157 missingrevs = set(cl.rev(n) for n in og._missing)
158 158 og._common = set(cl.ancestors(missingrevs)) - missingrevs
159 159 commonheads = set(og.commonheads)
160 160 og.missingheads = [h for h in og.missingheads if h not in commonheads]
161 161
162 162 return og
163 163
164 164 def _headssummary(repo, remote, outgoing):
165 165 """compute a summary of branch and heads status before and after push
166 166
167 167 return {'branch': ([remoteheads], [newheads], [unsyncedheads])} mapping
168 168
169 169 - branch: the branch name
170 170 - remoteheads: the list of remote heads known locally
171 171 None if the branch is new
172 172 - newheads: the new remote heads (known locally) with outgoing pushed
173 173 - unsyncedheads: the list of remote heads unknown locally.
174 174 """
175 175 cl = repo.changelog
176 176 headssum = {}
177 177 # A. Create set of branches involved in the push.
178 178 branches = set(repo[n].branch() for n in outgoing.missing)
179 179 remotemap = remote.branchmap()
180 180 newbranches = branches - set(remotemap)
181 181 branches.difference_update(newbranches)
182 182
183 183 # A. register remote heads
184 184 remotebranches = set()
185 185 for branch, heads in remote.branchmap().iteritems():
186 186 remotebranches.add(branch)
187 187 known = []
188 188 unsynced = []
189 189 knownnode = cl.hasnode # do not use nodemap until it is filtered
190 190 for h in heads:
191 191 if knownnode(h):
192 192 known.append(h)
193 193 else:
194 194 unsynced.append(h)
195 195 headssum[branch] = (known, list(known), unsynced)
196 196 # B. add new branch data
197 197 missingctx = list(repo[n] for n in outgoing.missing)
198 198 touchedbranches = set()
199 199 for ctx in missingctx:
200 200 branch = ctx.branch()
201 201 touchedbranches.add(branch)
202 202 if branch not in headssum:
203 203 headssum[branch] = (None, [], [])
204 204
205 205 # C drop data about untouched branches:
206 206 for branch in remotebranches - touchedbranches:
207 207 del headssum[branch]
208 208
209 209 # D. Update newmap with outgoing changes.
210 210 # This will possibly add new heads and remove existing ones.
211 211 newmap = branchmap.branchcache((branch, heads[1])
212 212 for branch, heads in headssum.iteritems()
213 213 if heads[0] is not None)
214 214 newmap.update(repo, (ctx.rev() for ctx in missingctx))
215 215 for branch, newheads in newmap.iteritems():
216 216 headssum[branch][1][:] = newheads
217 217 return headssum
218 218
219 219 def _oldheadssummary(repo, remoteheads, outgoing, inc=False):
220 220 """Compute branchmapsummary for repo without branchmap support"""
221 221
222 222 # 1-4b. old servers: Check for new topological heads.
223 223 # Construct {old,new}map with branch = None (topological branch).
224 224 # (code based on update)
225 225 knownnode = repo.changelog.hasnode # no nodemap until it is filtered
226 226 oldheads = set(h for h in remoteheads if knownnode(h))
227 227 # all nodes in outgoing.missing are children of either:
228 228 # - an element of oldheads
229 229 # - another element of outgoing.missing
230 230 # - nullrev
231 231 # This explains why the new head are very simple to compute.
232 232 r = repo.set('heads(%ln + %ln)', oldheads, outgoing.missing)
233 233 newheads = list(c.node() for c in r)
234 234 # set some unsynced head to issue the "unsynced changes" warning
235 235 if inc:
236 236 unsynced = set([None])
237 237 else:
238 238 unsynced = set()
239 239 return {None: (oldheads, newheads, unsynced)}
240 240
241 def _nowarnheads(repo, remote, newbookmarks):
242 # Compute newly pushed bookmarks. We don't warn about bookmarked heads.
243 localbookmarks = repo._bookmarks
244 remotebookmarks = remote.listkeys('bookmarks')
245 bookmarkedheads = set()
246 for bm in localbookmarks:
247 rnode = remotebookmarks.get(bm)
248 if rnode and rnode in repo:
249 lctx, rctx = repo[bm], repo[rnode]
250 if bookmarks.validdest(repo, rctx, lctx):
251 bookmarkedheads.add(lctx.node())
252 else:
253 if bm in newbookmarks and bm not in remotebookmarks:
254 bookmarkedheads.add(repo[bm].node())
255
256 return bookmarkedheads
257
241 258 def checkheads(repo, remote, outgoing, remoteheads, newbranch=False, inc=False,
242 259 newbookmarks=[]):
243 260 """Check that a push won't add any outgoing head
244 261
245 262 raise Abort error and display ui message as needed.
246 263 """
247 264 # Check for each named branch if we're creating new remote heads.
248 265 # To be a remote head after push, node must be either:
249 266 # - unknown locally
250 267 # - a local outgoing head descended from update
251 268 # - a remote head that's known locally and not
252 269 # ancestral to an outgoing head
253 270 if remoteheads == [nullid]:
254 271 # remote is empty, nothing to check.
255 272 return
256 273
257 274 if remote.capable('branchmap'):
258 275 headssum = _headssummary(repo, remote, outgoing)
259 276 else:
260 277 headssum = _oldheadssummary(repo, remoteheads, outgoing, inc)
261 278 newbranches = [branch for branch, heads in headssum.iteritems()
262 279 if heads[0] is None]
263 280 # 1. Check for new branches on the remote.
264 281 if newbranches and not newbranch: # new branch requires --new-branch
265 282 branchnames = ', '.join(sorted(newbranches))
266 283 raise error.Abort(_("push creates new remote branches: %s!")
267 284 % branchnames,
268 285 hint=_("use 'hg push --new-branch' to create"
269 286 " new remote branches"))
270 287
271 # 2. Compute newly pushed bookmarks. We don't warn about bookmarked heads.
272 localbookmarks = repo._bookmarks
273 remotebookmarks = remote.listkeys('bookmarks')
274 bookmarkedheads = set()
275 for bm in localbookmarks:
276 rnode = remotebookmarks.get(bm)
277 if rnode and rnode in repo:
278 lctx, rctx = repo[bm], repo[rnode]
279 if bookmarks.validdest(repo, rctx, lctx):
280 bookmarkedheads.add(lctx.node())
281 else:
282 if bm in newbookmarks and bm not in remotebookmarks:
283 bookmarkedheads.add(repo[bm].node())
288 # 2. Find heads that we need not warn about
289 nowarnheads = _nowarnheads(repo, remote, newbookmarks)
284 290
285 291 # 3. Check for new heads.
286 292 # If there are more heads after the push than before, a suitable
287 293 # error message, depending on unsynced status, is displayed.
288 294 errormsg = None
289 295 # If there is no obsstore, allfuturecommon won't be used, so no
290 296 # need to compute it.
291 297 if repo.obsstore:
292 298 allmissing = set(outgoing.missing)
293 299 cctx = repo.set('%ld', outgoing.common)
294 300 allfuturecommon = set(c.node() for c in cctx)
295 301 allfuturecommon.update(allmissing)
296 302 for branch, heads in sorted(headssum.iteritems()):
297 303 remoteheads, newheads, unsyncedheads = heads
298 304 candidate_newhs = set(newheads)
299 305 # add unsynced data
300 306 if remoteheads is None:
301 307 oldhs = set()
302 308 else:
303 309 oldhs = set(remoteheads)
304 310 oldhs.update(unsyncedheads)
305 311 candidate_newhs.update(unsyncedheads)
306 312 dhs = None # delta heads, the new heads on branch
307 313 discardedheads = set()
308 314 if not repo.obsstore:
309 315 newhs = candidate_newhs
310 316 else:
311 317 # remove future heads which are actually obsoleted by another
312 318 # pushed element:
313 319 #
314 320 # XXX as above, There are several cases this code does not handle
315 321 # XXX properly
316 322 #
317 323 # (1) if <nh> is public, it won't be affected by obsolete marker
318 324 # and a new is created
319 325 #
320 326 # (2) if the new heads have ancestors which are not obsolete and
321 327 # not ancestors of any other heads we will have a new head too.
322 328 #
323 329 # These two cases will be easy to handle for known changeset but
324 330 # much more tricky for unsynced changes.
325 331 #
326 332 # In addition, this code is confused by prune as it only looks for
327 333 # successors of the heads (none if pruned) leading to issue4354
328 334 newhs = set()
329 335 for nh in candidate_newhs:
330 336 if nh in repo and repo[nh].phase() <= phases.public:
331 337 newhs.add(nh)
332 338 else:
333 339 for suc in obsolete.allsuccessors(repo.obsstore, [nh]):
334 340 if suc != nh and suc in allfuturecommon:
335 341 discardedheads.add(nh)
336 342 break
337 343 else:
338 344 newhs.add(nh)
339 345 unsynced = sorted(h for h in unsyncedheads if h not in discardedheads)
340 346 if unsynced:
341 347 if None in unsynced:
342 348 # old remote, no heads data
343 349 heads = None
344 350 elif len(unsynced) <= 4 or repo.ui.verbose:
345 351 heads = ' '.join(short(h) for h in unsynced)
346 352 else:
347 353 heads = (' '.join(short(h) for h in unsynced[:4]) +
348 354 ' ' + _("and %s others") % (len(unsynced) - 4))
349 355 if heads is None:
350 356 repo.ui.status(_("remote has heads that are "
351 357 "not known locally\n"))
352 358 elif branch is None:
353 359 repo.ui.status(_("remote has heads that are "
354 360 "not known locally: %s\n") % heads)
355 361 else:
356 362 repo.ui.status(_("remote has heads on branch '%s' that are "
357 363 "not known locally: %s\n") % (branch, heads))
358 364 if remoteheads is None:
359 365 if len(newhs) > 1:
360 366 dhs = list(newhs)
361 367 if errormsg is None:
362 368 errormsg = (_("push creates new branch '%s' "
363 369 "with multiple heads") % (branch))
364 370 hint = _("merge or"
365 371 " see \"hg help push\" for details about"
366 372 " pushing new heads")
367 373 elif len(newhs) > len(oldhs):
368 374 # remove bookmarked or existing remote heads from the new heads list
369 dhs = sorted(newhs - bookmarkedheads - oldhs)
375 dhs = sorted(newhs - nowarnheads - oldhs)
370 376 if dhs:
371 377 if errormsg is None:
372 378 if branch not in ('default', None):
373 379 errormsg = _("push creates new remote head %s "
374 380 "on branch '%s'!") % (short(dhs[0]), branch)
375 381 elif repo[dhs[0]].bookmarks():
376 382 errormsg = _("push creates new remote head %s "
377 383 "with bookmark '%s'!") % (
378 384 short(dhs[0]), repo[dhs[0]].bookmarks()[0])
379 385 else:
380 386 errormsg = _("push creates new remote head %s!"
381 387 ) % short(dhs[0])
382 388 if unsyncedheads:
383 389 hint = _("pull and merge or"
384 390 " see \"hg help push\" for details about"
385 391 " pushing new heads")
386 392 else:
387 393 hint = _("merge or"
388 394 " see \"hg help push\" for details about"
389 395 " pushing new heads")
390 396 if branch is None:
391 397 repo.ui.note(_("new remote heads:\n"))
392 398 else:
393 399 repo.ui.note(_("new remote heads on branch '%s':\n") % branch)
394 400 for h in dhs:
395 401 repo.ui.note((" %s\n") % short(h))
396 402 if errormsg:
397 403 raise error.Abort(errormsg, hint=hint)
General Comments 0
You need to be logged in to leave comments. Login now