comparison mercurial/destutil.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents fbd168455b26
children 687b865b95ad
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
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 __future__ import absolute_import 8 from __future__ import absolute_import
9 9
10 from .i18n import _ 10 from .i18n import _
11 from . import ( 11 from . import bookmarks, error, obsutil, scmutil, stack
12 bookmarks, 12
13 error,
14 obsutil,
15 scmutil,
16 stack
17 )
18 13
19 def orphanpossibledestination(repo, rev): 14 def orphanpossibledestination(repo, rev):
20 """Return all changesets that may be a new parent for orphan `rev`. 15 """Return all changesets that may be a new parent for orphan `rev`.
21 16
22 This function works fine on non-orphan revisions, it's just silly 17 This function works fine on non-orphan revisions, it's just silly
47 dr = torev(n) 42 dr = torev(n)
48 if dr != -1: 43 if dr != -1:
49 dest.add(dr) 44 dest.add(dr)
50 return dest 45 return dest
51 46
47
52 def _destupdateobs(repo, clean): 48 def _destupdateobs(repo, clean):
53 """decide of an update destination from obsolescence markers""" 49 """decide of an update destination from obsolescence markers"""
54 node = None 50 node = None
55 wc = repo[None] 51 wc = repo[None]
56 p1 = wc.p1() 52 p1 = wc.p1()
82 # i.e. the 'tip' of a set 78 # i.e. the 'tip' of a set
83 node = repo.revs('max(%ln)', successors).first() 79 node = repo.revs('max(%ln)', successors).first()
84 if bookmarks.isactivewdirparent(repo): 80 if bookmarks.isactivewdirparent(repo):
85 movemark = repo['.'].node() 81 movemark = repo['.'].node()
86 return node, movemark, None 82 return node, movemark, None
83
87 84
88 def _destupdatebook(repo, clean): 85 def _destupdatebook(repo, clean):
89 """decide on an update destination from active bookmark""" 86 """decide on an update destination from active bookmark"""
90 # we also move the active bookmark, if any 87 # we also move the active bookmark, if any
91 node = None 88 node = None
92 activemark, movemark = bookmarks.calculateupdate(repo.ui, repo) 89 activemark, movemark = bookmarks.calculateupdate(repo.ui, repo)
93 if activemark is not None: 90 if activemark is not None:
94 node = repo._bookmarks[activemark] 91 node = repo._bookmarks[activemark]
95 return node, movemark, activemark 92 return node, movemark, activemark
93
96 94
97 def _destupdatebranch(repo, clean): 95 def _destupdatebranch(repo, clean):
98 """decide on an update destination from current branch 96 """decide on an update destination from current branch
99 97
100 This ignores closed branch heads. 98 This ignores closed branch heads.
118 node = repo.revs('max(head() and not closed())').first() 116 node = repo.revs('max(head() and not closed())').first()
119 else: 117 else:
120 node = repo['.'].node() 118 node = repo['.'].node()
121 return node, movemark, None 119 return node, movemark, None
122 120
121
123 def _destupdatebranchfallback(repo, clean): 122 def _destupdatebranchfallback(repo, clean):
124 """decide on an update destination from closed heads in current branch""" 123 """decide on an update destination from closed heads in current branch"""
125 wc = repo[None] 124 wc = repo[None]
126 currentbranch = wc.branch() 125 currentbranch = wc.branch()
127 movemark = None 126 movemark = None
128 if currentbranch in repo.branchmap(): 127 if currentbranch in repo.branchmap():
129 # here, all descendant branch heads are closed 128 # here, all descendant branch heads are closed
130 heads = repo.branchheads(currentbranch, closed=True) 129 heads = repo.branchheads(currentbranch, closed=True)
131 assert heads, "any branch has at least one head" 130 assert heads, "any branch has at least one head"
132 node = repo.revs('max(.::(%ln))', heads).first() 131 node = repo.revs('max(.::(%ln))', heads).first()
133 assert node is not None, ("any revision has at least " 132 assert node is not None, (
134 "one descendant branch head") 133 "any revision has at least " "one descendant branch head"
134 )
135 if bookmarks.isactivewdirparent(repo): 135 if bookmarks.isactivewdirparent(repo):
136 movemark = repo['.'].node() 136 movemark = repo['.'].node()
137 else: 137 else:
138 # here, no "default" branch, and all branches are closed 138 # here, no "default" branch, and all branches are closed
139 node = repo.lookup('tip') 139 node = repo.lookup('tip')
140 assert node is not None, "'tip' exists even in empty repository" 140 assert node is not None, "'tip' exists even in empty repository"
141 return node, movemark, None 141 return node, movemark, None
142 142
143
143 # order in which each step should be evaluated 144 # order in which each step should be evaluated
144 # steps are run until one finds a destination 145 # steps are run until one finds a destination
145 destupdatesteps = ['evolution', 'bookmark', 'branch', 'branchfallback'] 146 destupdatesteps = ['evolution', 'bookmark', 'branch', 'branchfallback']
146 # mapping to ease extension overriding steps. 147 # mapping to ease extension overriding steps.
147 destupdatestepmap = {'evolution': _destupdateobs, 148 destupdatestepmap = {
148 'bookmark': _destupdatebook, 149 'evolution': _destupdateobs,
149 'branch': _destupdatebranch, 150 'bookmark': _destupdatebook,
150 'branchfallback': _destupdatebranchfallback, 151 'branch': _destupdatebranch,
151 } 152 'branchfallback': _destupdatebranchfallback,
153 }
154
152 155
153 def destupdate(repo, clean=False): 156 def destupdate(repo, clean=False):
154 """destination for bare update operation 157 """destination for bare update operation
155 158
156 return (rev, movemark, activemark) 159 return (rev, movemark, activemark)
168 break 171 break
169 rev = repo[node].rev() 172 rev = repo[node].rev()
170 173
171 return rev, movemark, activemark 174 return rev, movemark, activemark
172 175
176
173 msgdestmerge = { 177 msgdestmerge = {
174 # too many matching divergent bookmark 178 # too many matching divergent bookmark
175 'toomanybookmarks': 179 'toomanybookmarks': {
176 {'merge': 180 'merge': (
177 (_("multiple matching bookmarks to merge -" 181 _(
178 " please merge with an explicit rev or bookmark"), 182 "multiple matching bookmarks to merge -"
179 _("run 'hg heads' to see all heads")), 183 " please merge with an explicit rev or bookmark"
180 'rebase': 184 ),
181 (_("multiple matching bookmarks to rebase -" 185 _("run 'hg heads' to see all heads"),
182 " please rebase to an explicit rev or bookmark"), 186 ),
183 _("run 'hg heads' to see all heads")), 187 'rebase': (
184 }, 188 _(
189 "multiple matching bookmarks to rebase -"
190 " please rebase to an explicit rev or bookmark"
191 ),
192 _("run 'hg heads' to see all heads"),
193 ),
194 },
185 # no other matching divergent bookmark 195 # no other matching divergent bookmark
186 'nootherbookmarks': 196 'nootherbookmarks': {
187 {'merge': 197 'merge': (
188 (_("no matching bookmark to merge - " 198 _(
189 "please merge with an explicit rev or bookmark"), 199 "no matching bookmark to merge - "
190 _("run 'hg heads' to see all heads")), 200 "please merge with an explicit rev or bookmark"
191 'rebase': 201 ),
192 (_("no matching bookmark to rebase - " 202 _("run 'hg heads' to see all heads"),
193 "please rebase to an explicit rev or bookmark"), 203 ),
194 _("run 'hg heads' to see all heads")), 204 'rebase': (
195 }, 205 _(
206 "no matching bookmark to rebase - "
207 "please rebase to an explicit rev or bookmark"
208 ),
209 _("run 'hg heads' to see all heads"),
210 ),
211 },
196 # branch have too many unbookmarked heads, no obvious destination 212 # branch have too many unbookmarked heads, no obvious destination
197 'toomanyheads': 213 'toomanyheads': {
198 {'merge': 214 'merge': (
199 (_("branch '%s' has %d heads - please merge with an explicit rev"), 215 _("branch '%s' has %d heads - please merge with an explicit rev"),
200 _("run 'hg heads .' to see heads")), 216 _("run 'hg heads .' to see heads"),
201 'rebase': 217 ),
202 (_("branch '%s' has %d heads - please rebase to an explicit rev"), 218 'rebase': (
203 _("run 'hg heads .' to see heads")), 219 _("branch '%s' has %d heads - please rebase to an explicit rev"),
204 }, 220 _("run 'hg heads .' to see heads"),
221 ),
222 },
205 # branch have no other unbookmarked heads 223 # branch have no other unbookmarked heads
206 'bookmarkedheads': 224 'bookmarkedheads': {
207 {'merge': 225 'merge': (
208 (_("heads are bookmarked - please merge with an explicit rev"), 226 _("heads are bookmarked - please merge with an explicit rev"),
209 _("run 'hg heads' to see all heads")), 227 _("run 'hg heads' to see all heads"),
210 'rebase': 228 ),
211 (_("heads are bookmarked - please rebase to an explicit rev"), 229 'rebase': (
212 _("run 'hg heads' to see all heads")), 230 _("heads are bookmarked - please rebase to an explicit rev"),
213 }, 231 _("run 'hg heads' to see all heads"),
232 ),
233 },
214 # branch have just a single heads, but there is other branches 234 # branch have just a single heads, but there is other branches
215 'nootherbranchheads': 235 'nootherbranchheads': {
216 {'merge': 236 'merge': (
217 (_("branch '%s' has one head - please merge with an explicit rev"), 237 _("branch '%s' has one head - please merge with an explicit rev"),
218 _("run 'hg heads' to see all heads")), 238 _("run 'hg heads' to see all heads"),
219 'rebase': 239 ),
220 (_("branch '%s' has one head - please rebase to an explicit rev"), 240 'rebase': (
221 _("run 'hg heads' to see all heads")), 241 _("branch '%s' has one head - please rebase to an explicit rev"),
222 }, 242 _("run 'hg heads' to see all heads"),
243 ),
244 },
223 # repository have a single head 245 # repository have a single head
224 'nootherheads': 246 'nootherheads': {
225 {'merge': 247 'merge': (_('nothing to merge'), None),
226 (_('nothing to merge'), 248 'rebase': (_('nothing to rebase'), None),
227 None), 249 },
228 'rebase':
229 (_('nothing to rebase'),
230 None),
231 },
232 # repository have a single head and we are not on it 250 # repository have a single head and we are not on it
233 'nootherheadsbehind': 251 'nootherheadsbehind': {
234 {'merge': 252 'merge': (_('nothing to merge'), _("use 'hg update' instead")),
235 (_('nothing to merge'), 253 'rebase': (_('nothing to rebase'), _("use 'hg update' instead")),
236 _("use 'hg update' instead")), 254 },
237 'rebase':
238 (_('nothing to rebase'),
239 _("use 'hg update' instead")),
240 },
241 # We are not on a head 255 # We are not on a head
242 'notatheads': 256 'notatheads': {
243 {'merge': 257 'merge': (
244 (_('working directory not at a head revision'), 258 _('working directory not at a head revision'),
245 _("use 'hg update' or merge with an explicit revision")), 259 _("use 'hg update' or merge with an explicit revision"),
246 'rebase': 260 ),
247 (_('working directory not at a head revision'), 261 'rebase': (
248 _("use 'hg update' or rebase to an explicit revision")) 262 _('working directory not at a head revision'),
249 }, 263 _("use 'hg update' or rebase to an explicit revision"),
250 'emptysourceset': 264 ),
251 {'merge': 265 },
252 (_('source set is empty'), 266 'emptysourceset': {
253 None), 267 'merge': (_('source set is empty'), None),
254 'rebase': 268 'rebase': (_('source set is empty'), None),
255 (_('source set is empty'), 269 },
256 None), 270 'multiplebranchessourceset': {
257 }, 271 'merge': (_('source set is rooted in multiple branches'), None),
258 'multiplebranchessourceset': 272 'rebase': (
259 {'merge': 273 _('rebaseset is rooted in multiple named branches'),
260 (_('source set is rooted in multiple branches'), 274 _('specify an explicit destination with --dest'),
261 None), 275 ),
262 'rebase': 276 },
263 (_('rebaseset is rooted in multiple named branches'), 277 }
264 _('specify an explicit destination with --dest')), 278
265 },
266 }
267 279
268 def _destmergebook(repo, action='merge', sourceset=None, destspace=None): 280 def _destmergebook(repo, action='merge', sourceset=None, destspace=None):
269 """find merge destination in the active bookmark case""" 281 """find merge destination in the active bookmark case"""
270 node = None 282 node = None
271 bmheads = bookmarks.headsforactive(repo) 283 bmheads = bookmarks.headsforactive(repo)
282 msg, hint = msgdestmerge['nootherbookmarks'][action] 294 msg, hint = msgdestmerge['nootherbookmarks'][action]
283 raise error.NoMergeDestAbort(msg, hint=hint) 295 raise error.NoMergeDestAbort(msg, hint=hint)
284 assert node is not None 296 assert node is not None
285 return node 297 return node
286 298
287 def _destmergebranch(repo, action='merge', sourceset=None, onheadcheck=True, 299
288 destspace=None): 300 def _destmergebranch(
301 repo, action='merge', sourceset=None, onheadcheck=True, destspace=None
302 ):
289 """find merge destination based on branch heads""" 303 """find merge destination based on branch heads"""
290 node = None 304 node = None
291 305
292 if sourceset is None: 306 if sourceset is None:
293 sourceset = [repo[repo.dirstate.p1()].rev()] 307 sourceset = [repo[repo.dirstate.p1()].rev()]
353 else: 367 else:
354 node = nbhs[0] 368 node = nbhs[0]
355 assert node is not None 369 assert node is not None
356 return node 370 return node
357 371
358 def destmerge(repo, action='merge', sourceset=None, onheadcheck=True, 372
359 destspace=None): 373 def destmerge(
374 repo, action='merge', sourceset=None, onheadcheck=True, destspace=None
375 ):
360 """return the default destination for a merge 376 """return the default destination for a merge
361 377
362 (or raise exception about why it can't pick one) 378 (or raise exception about why it can't pick one)
363 379
364 :action: the action being performed, controls emitted error message 380 :action: the action being performed, controls emitted error message
365 """ 381 """
366 # destspace is here to work around issues with `hg pull --rebase` see 382 # destspace is here to work around issues with `hg pull --rebase` see
367 # issue5214 for details 383 # issue5214 for details
368 if repo._activebookmark: 384 if repo._activebookmark:
369 node = _destmergebook(repo, action=action, sourceset=sourceset, 385 node = _destmergebook(
370 destspace=destspace) 386 repo, action=action, sourceset=sourceset, destspace=destspace
371 else: 387 )
372 node = _destmergebranch(repo, action=action, sourceset=sourceset, 388 else:
373 onheadcheck=onheadcheck, destspace=destspace) 389 node = _destmergebranch(
390 repo,
391 action=action,
392 sourceset=sourceset,
393 onheadcheck=onheadcheck,
394 destspace=destspace,
395 )
374 return repo[node].rev() 396 return repo[node].rev()
397
375 398
376 def desthistedit(ui, repo): 399 def desthistedit(ui, repo):
377 """Default base revision to edit for `hg histedit`.""" 400 """Default base revision to edit for `hg histedit`."""
378 default = ui.config('histedit', 'defaultrev') 401 default = ui.config('histedit', 'defaultrev')
379 402
388 # Take the first revision of the revset as the root 411 # Take the first revision of the revset as the root
389 return revs.min() 412 return revs.min()
390 413
391 return None 414 return None
392 415
416
393 def stackbase(ui, repo): 417 def stackbase(ui, repo):
394 revs = stack.getstack(repo) 418 revs = stack.getstack(repo)
395 return revs.first() if revs else None 419 return revs.first() if revs else None
420
396 421
397 def _statusotherbook(ui, repo): 422 def _statusotherbook(ui, repo):
398 bmheads = bookmarks.headsforactive(repo) 423 bmheads = bookmarks.headsforactive(repo)
399 curhead = repo._bookmarks[repo._activebookmark] 424 curhead = repo._bookmarks[repo._activebookmark]
400 if repo.revs('%n and parents()', curhead): 425 if repo.revs('%n and parents()', curhead):
402 bmheads = [b for b in bmheads if curhead != b] 427 bmheads = [b for b in bmheads if curhead != b]
403 if bmheads: 428 if bmheads:
404 msg = _('%i other divergent bookmarks for "%s"\n') 429 msg = _('%i other divergent bookmarks for "%s"\n')
405 ui.status(msg % (len(bmheads), repo._activebookmark)) 430 ui.status(msg % (len(bmheads), repo._activebookmark))
406 431
432
407 def _statusotherbranchheads(ui, repo): 433 def _statusotherbranchheads(ui, repo):
408 currentbranch = repo.dirstate.branch() 434 currentbranch = repo.dirstate.branch()
409 allheads = repo.branchheads(currentbranch, closed=True) 435 allheads = repo.branchheads(currentbranch, closed=True)
410 heads = repo.branchheads(currentbranch) 436 heads = repo.branchheads(currentbranch)
411 if repo.revs('%ln and parents()', allheads): 437 if repo.revs('%ln and parents()', allheads):
418 # x 0 there is only one non-closed branch head 444 # x 0 there is only one non-closed branch head
419 # N there are some non-closed branch heads 445 # N there are some non-closed branch heads
420 # ========= ========== 446 # ========= ==========
421 otherheads = repo.revs('%ln - parents()', heads) 447 otherheads = repo.revs('%ln - parents()', heads)
422 if repo['.'].closesbranch(): 448 if repo['.'].closesbranch():
423 ui.warn(_('no open descendant heads on branch "%s", ' 449 ui.warn(
424 'updating to a closed head\n') % 450 _(
425 (currentbranch)) 451 'no open descendant heads on branch "%s", '
452 'updating to a closed head\n'
453 )
454 % currentbranch
455 )
426 if otherheads: 456 if otherheads:
427 ui.warn(_("(committing will reopen the head, " 457 ui.warn(
428 "use 'hg heads .' to see %i other heads)\n") % 458 _(
429 (len(otherheads))) 459 "(committing will reopen the head, "
460 "use 'hg heads .' to see %i other heads)\n"
461 )
462 % (len(otherheads))
463 )
430 else: 464 else:
431 ui.warn(_('(committing will reopen branch "%s")\n') % 465 ui.warn(
432 (currentbranch)) 466 _('(committing will reopen branch "%s")\n') % currentbranch
467 )
433 elif otherheads: 468 elif otherheads:
434 curhead = repo['.'] 469 curhead = repo['.']
435 ui.status(_('updated to "%s: %s"\n') % (curhead, 470 ui.status(
436 curhead.description().split('\n')[0])) 471 _('updated to "%s: %s"\n')
437 ui.status(_('%i other heads for branch "%s"\n') % 472 % (curhead, curhead.description().split('\n')[0])
438 (len(otherheads), currentbranch)) 473 )
474 ui.status(
475 _('%i other heads for branch "%s"\n')
476 % (len(otherheads), currentbranch)
477 )
478
439 479
440 def statusotherdests(ui, repo): 480 def statusotherdests(ui, repo):
441 """Print message about other head""" 481 """Print message about other head"""
442 # XXX we should probably include a hint: 482 # XXX we should probably include a hint:
443 # - about what to do 483 # - about what to do