| 1 |
# show.py - Extension implementing `hg show` |
| 2 |
# |
| 3 |
# Copyright 2017 Gregory Szorc <[email protected]> |
| 4 |
# |
| 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. |
| 7 |
|
| 8 |
"""unified command to show various repository information (EXPERIMENTAL) |
| 9 |
|
| 10 |
This extension provides the :hg:`show` command, which provides a central |
| 11 |
command for displaying commonly-accessed repository data and views of that |
| 12 |
data. |
| 13 |
|
| 14 |
The following config options can influence operation. |
| 15 |
|
| 16 |
``commands`` |
| 17 |
------------ |
| 18 |
|
| 19 |
``show.aliasprefix`` |
| 20 |
List of strings that will register aliases for views. e.g. ``s`` will |
| 21 |
effectively set config options ``alias.s<view> = show <view>`` for all |
| 22 |
views. i.e. `hg swork` would execute `hg show work`. |
| 23 |
|
| 24 |
Aliases that would conflict with existing registrations will not be |
| 25 |
performed. |
| 26 |
""" |
| 27 |
|
| 28 |
from __future__ import annotations |
| 29 |
|
| 30 |
from mercurial.i18n import _ |
| 31 |
from mercurial.node import nullrev |
| 32 |
from mercurial import ( |
| 33 |
commands, |
| 34 |
destutil, |
| 35 |
error, |
| 36 |
formatter, |
| 37 |
graphmod, |
| 38 |
logcmdutil, |
| 39 |
phases, |
| 40 |
pycompat, |
| 41 |
registrar, |
| 42 |
revset, |
| 43 |
revsetlang, |
| 44 |
scmutil, |
| 45 |
) |
| 46 |
from mercurial.main_script import ( |
| 47 |
cmd_finder, |
| 48 |
) |
| 49 |
|
| 50 |
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
| 51 |
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
| 52 |
# be specifying the version(s) of Mercurial they are tested with, or |
| 53 |
# leave the attribute unspecified. |
| 54 |
testedwith = b'ships-with-hg-core' |
| 55 |
|
| 56 |
cmdtable = {} |
| 57 |
command = registrar.command(cmdtable) |
| 58 |
|
| 59 |
revsetpredicate = registrar.revsetpredicate() |
| 60 |
|
| 61 |
|
| 62 |
class showcmdfunc(registrar._funcregistrarbase): |
| 63 |
"""Register a function to be invoked for an `hg show <thing>`.""" |
| 64 |
|
| 65 |
# Used by _formatdoc(). |
| 66 |
_docformat = b'%s -- %s' |
| 67 |
|
| 68 |
def _extrasetup(self, name, func, fmtopic=None, csettopic=None): |
| 69 |
"""Called with decorator arguments to register a show view. |
| 70 |
|
| 71 |
``name`` is the sub-command name. |
| 72 |
|
| 73 |
``func`` is the function being decorated. |
| 74 |
|
| 75 |
``fmtopic`` is the topic in the style that will be rendered for |
| 76 |
this view. |
| 77 |
|
| 78 |
``csettopic`` is the topic in the style to be used for a changeset |
| 79 |
printer. |
| 80 |
|
| 81 |
If ``fmtopic`` is specified, the view function will receive a |
| 82 |
formatter instance. If ``csettopic`` is specified, the view |
| 83 |
function will receive a changeset printer. |
| 84 |
""" |
| 85 |
func._fmtopic = fmtopic |
| 86 |
func._csettopic = csettopic |
| 87 |
|
| 88 |
|
| 89 |
showview = showcmdfunc() |
| 90 |
|
| 91 |
|
| 92 |
@command( |
| 93 |
b'show', |
| 94 |
[ |
| 95 |
# TODO: Switch this template flag to use cmdutil.formatteropts if |
| 96 |
# 'hg show' becomes stable before --template/-T is stable. For now, |
| 97 |
# we are putting it here without the '(EXPERIMENTAL)' flag because it |
| 98 |
# is an important part of the 'hg show' user experience and the entire |
| 99 |
# 'hg show' experience is experimental. |
| 100 |
(b'T', b'template', b'', b'display with template', _(b'TEMPLATE')), |
| 101 |
], |
| 102 |
_(b'VIEW'), |
| 103 |
helpcategory=command.CATEGORY_CHANGE_NAVIGATION, |
| 104 |
) |
| 105 |
def show(ui, repo, view=None, template=None): |
| 106 |
"""show various repository information |
| 107 |
|
| 108 |
A requested view of repository data is displayed. |
| 109 |
|
| 110 |
If no view is requested, the list of available views is shown and the |
| 111 |
command aborts. |
| 112 |
|
| 113 |
.. note:: |
| 114 |
|
| 115 |
There are no backwards compatibility guarantees for the output of this |
| 116 |
command. Output may change in any future Mercurial release. |
| 117 |
|
| 118 |
Consumers wanting stable command output should specify a template via |
| 119 |
``-T/--template``. |
| 120 |
|
| 121 |
List of available views: |
| 122 |
""" |
| 123 |
if ui.plain() and not template: |
| 124 |
hint = _(b'invoke with -T/--template to control output format') |
| 125 |
raise error.Abort( |
| 126 |
_(b'must specify a template in plain mode'), hint=hint |
| 127 |
) |
| 128 |
|
| 129 |
views = showview._table |
| 130 |
|
| 131 |
if not view: |
| 132 |
ui.pager(b'show') |
| 133 |
# TODO consider using formatter here so available views can be |
| 134 |
# rendered to custom format. |
| 135 |
ui.write(_(b'available views:\n')) |
| 136 |
ui.write(b'\n') |
| 137 |
|
| 138 |
for name, func in sorted(views.items()): |
| 139 |
ui.write(b'%s\n' % pycompat.sysbytes(func.__doc__)) |
| 140 |
|
| 141 |
ui.write(b'\n') |
| 142 |
raise error.Abort( |
| 143 |
_(b'no view requested'), |
| 144 |
hint=_(b'use "hg show VIEW" to choose a view'), |
| 145 |
) |
| 146 |
|
| 147 |
# TODO use same logic as dispatch to perform prefix matching. |
| 148 |
if view not in views: |
| 149 |
raise error.Abort( |
| 150 |
_(b'unknown view: %s') % view, |
| 151 |
hint=_(b'run "hg show" to see available views'), |
| 152 |
) |
| 153 |
|
| 154 |
template = template or b'show' |
| 155 |
|
| 156 |
fn = views[view] |
| 157 |
ui.pager(b'show') |
| 158 |
|
| 159 |
if fn._fmtopic: |
| 160 |
fmtopic = b'show%s' % fn._fmtopic |
| 161 |
with ui.formatter(fmtopic, {b'template': template}) as fm: |
| 162 |
return fn(ui, repo, fm) |
| 163 |
elif fn._csettopic: |
| 164 |
ref = b'show%s' % fn._csettopic |
| 165 |
spec = formatter.lookuptemplate(ui, ref, template) |
| 166 |
displayer = logcmdutil.changesettemplater(ui, repo, spec, buffered=True) |
| 167 |
return fn(ui, repo, displayer) |
| 168 |
else: |
| 169 |
return fn(ui, repo) |
| 170 |
|
| 171 |
|
| 172 |
@showview(b'bookmarks', fmtopic=b'bookmarks') |
| 173 |
def showbookmarks(ui, repo, fm): |
| 174 |
"""bookmarks and their associated changeset""" |
| 175 |
marks = repo._bookmarks |
| 176 |
if not len(marks): |
| 177 |
# This is a bit hacky. Ideally, templates would have a way to |
| 178 |
# specify an empty output, but we shouldn't corrupt JSON while |
| 179 |
# waiting for this functionality. |
| 180 |
if not isinstance(fm, formatter.jsonformatter): |
| 181 |
ui.write(_(b'(no bookmarks set)\n')) |
| 182 |
return |
| 183 |
|
| 184 |
revs = [repo[node].rev() for node in marks.values()] |
| 185 |
active = repo._activebookmark |
| 186 |
longestname = max(len(b) for b in marks) |
| 187 |
nodelen = longestshortest(repo, revs) |
| 188 |
|
| 189 |
for bm, node in sorted(marks.items()): |
| 190 |
fm.startitem() |
| 191 |
fm.context(ctx=repo[node]) |
| 192 |
fm.write(b'bookmark', b'%s', bm) |
| 193 |
fm.write(b'node', fm.hexfunc(node), fm.hexfunc(node)) |
| 194 |
fm.data( |
| 195 |
active=bm == active, longestbookmarklen=longestname, nodelen=nodelen |
| 196 |
) |
| 197 |
|
| 198 |
|
| 199 |
@showview(b'stack', csettopic=b'stack') |
| 200 |
def showstack(ui, repo, displayer): |
| 201 |
"""current line of work""" |
| 202 |
wdirctx = repo[b'.'] |
| 203 |
if wdirctx.rev() == nullrev: |
| 204 |
raise error.Abort( |
| 205 |
_( |
| 206 |
b'stack view only available when there is a ' |
| 207 |
b'working directory' |
| 208 |
) |
| 209 |
) |
| 210 |
|
| 211 |
if wdirctx.phase() == phases.public: |
| 212 |
ui.write( |
| 213 |
_( |
| 214 |
b'(empty stack; working directory parent is a published ' |
| 215 |
b'changeset)\n' |
| 216 |
) |
| 217 |
) |
| 218 |
return |
| 219 |
|
| 220 |
# TODO extract "find stack" into a function to facilitate |
| 221 |
# customization and reuse. |
| 222 |
|
| 223 |
baserev = destutil.stackbase(ui, repo) |
| 224 |
basectx = None |
| 225 |
|
| 226 |
if baserev is None: |
| 227 |
baserev = wdirctx.rev() |
| 228 |
stackrevs = {wdirctx.rev()} |
| 229 |
else: |
| 230 |
stackrevs = set(repo.revs(b'%d::.', baserev)) |
| 231 |
|
| 232 |
ctx = repo[baserev] |
| 233 |
if ctx.p1().rev() != nullrev: |
| 234 |
basectx = ctx.p1() |
| 235 |
|
| 236 |
# And relevant descendants. |
| 237 |
branchpointattip = False |
| 238 |
cl = repo.changelog |
| 239 |
|
| 240 |
for rev in cl.descendants([wdirctx.rev()]): |
| 241 |
ctx = repo[rev] |
| 242 |
|
| 243 |
# Will only happen if . is public. |
| 244 |
if ctx.phase() == phases.public: |
| 245 |
break |
| 246 |
|
| 247 |
stackrevs.add(ctx.rev()) |
| 248 |
|
| 249 |
# ctx.children() within a function iterating on descandants |
| 250 |
# potentially has severe performance concerns because revlog.children() |
| 251 |
# iterates over all revisions after ctx's node. However, the number of |
| 252 |
# draft changesets should be a reasonably small number. So even if |
| 253 |
# this is quadratic, the perf impact should be minimal. |
| 254 |
if len(ctx.children()) > 1: |
| 255 |
branchpointattip = True |
| 256 |
break |
| 257 |
|
| 258 |
stackrevs = list(sorted(stackrevs, reverse=True)) |
| 259 |
|
| 260 |
# Find likely target heads for the current stack. These are likely |
| 261 |
# merge or rebase targets. |
| 262 |
if basectx: |
| 263 |
# TODO make this customizable? |
| 264 |
newheads = set( |
| 265 |
repo.revs( |
| 266 |
b'heads(%d::) - %ld - not public()', basectx.rev(), stackrevs |
| 267 |
) |
| 268 |
) |
| 269 |
else: |
| 270 |
newheads = set() |
| 271 |
|
| 272 |
allrevs = set(stackrevs) | newheads | {baserev} |
| 273 |
nodelen = longestshortest(repo, allrevs) |
| 274 |
|
| 275 |
try: |
| 276 |
cmd_finder.find_cmd(b'rebase', commands.table) |
| 277 |
haverebase = True |
| 278 |
except (error.AmbiguousCommand, error.UnknownCommand): |
| 279 |
haverebase = False |
| 280 |
|
| 281 |
# TODO use templating. |
| 282 |
# TODO consider using graphmod. But it may not be necessary given |
| 283 |
# our simplicity and the customizations required. |
| 284 |
# TODO use proper graph symbols from graphmod |
| 285 |
|
| 286 |
tres = formatter.templateresources(ui, repo) |
| 287 |
shortesttmpl = formatter.maketemplater( |
| 288 |
ui, b'{shortest(node, %d)}' % nodelen, resources=tres |
| 289 |
) |
| 290 |
|
| 291 |
def shortest(ctx): |
| 292 |
return shortesttmpl.renderdefault({b'ctx': ctx, b'node': ctx.hex()}) |
| 293 |
|
| 294 |
# We write out new heads to aid in DAG awareness and to help with decision |
| 295 |
# making on how the stack should be reconciled with commits made since the |
| 296 |
# branch point. |
| 297 |
if newheads: |
| 298 |
# Calculate distance from base so we can render the count and so we can |
| 299 |
# sort display order by commit distance. |
| 300 |
revdistance = {} |
| 301 |
for head in newheads: |
| 302 |
# There is some redundancy in DAG traversal here and therefore |
| 303 |
# room to optimize. |
| 304 |
ancestors = cl.ancestors([head], stoprev=basectx.rev()) |
| 305 |
revdistance[head] = len(list(ancestors)) |
| 306 |
|
| 307 |
sourcectx = repo[stackrevs[-1]] |
| 308 |
|
| 309 |
sortedheads = sorted( |
| 310 |
newheads, key=lambda x: revdistance[x], reverse=True |
| 311 |
) |
| 312 |
|
| 313 |
for i, rev in enumerate(sortedheads): |
| 314 |
ctx = repo[rev] |
| 315 |
|
| 316 |
if i: |
| 317 |
ui.write(b': ') |
| 318 |
else: |
| 319 |
ui.write(b' ') |
| 320 |
|
| 321 |
ui.writenoi18n(b'o ') |
| 322 |
displayer.show(ctx, nodelen=nodelen) |
| 323 |
displayer.flush(ctx) |
| 324 |
ui.write(b'\n') |
| 325 |
|
| 326 |
if i: |
| 327 |
ui.write(b':/') |
| 328 |
else: |
| 329 |
ui.write(b' /') |
| 330 |
|
| 331 |
ui.write(b' (') |
| 332 |
ui.write( |
| 333 |
_(b'%d commits ahead') % revdistance[rev], |
| 334 |
label=b'stack.commitdistance', |
| 335 |
) |
| 336 |
|
| 337 |
if haverebase: |
| 338 |
# TODO may be able to omit --source in some scenarios |
| 339 |
ui.write(b'; ') |
| 340 |
ui.write( |
| 341 |
( |
| 342 |
b'hg rebase --source %s --dest %s' |
| 343 |
% (shortest(sourcectx), shortest(ctx)) |
| 344 |
), |
| 345 |
label=b'stack.rebasehint', |
| 346 |
) |
| 347 |
|
| 348 |
ui.write(b')\n') |
| 349 |
|
| 350 |
ui.write(b':\n: ') |
| 351 |
ui.write(_(b'(stack head)\n'), label=b'stack.label') |
| 352 |
|
| 353 |
if branchpointattip: |
| 354 |
ui.write(b' \\ / ') |
| 355 |
ui.write(_(b'(multiple children)\n'), label=b'stack.label') |
| 356 |
ui.write(b' |\n') |
| 357 |
|
| 358 |
for rev in stackrevs: |
| 359 |
ctx = repo[rev] |
| 360 |
symbol = b'@' if rev == wdirctx.rev() else b'o' |
| 361 |
|
| 362 |
if newheads: |
| 363 |
ui.write(b': ') |
| 364 |
else: |
| 365 |
ui.write(b' ') |
| 366 |
|
| 367 |
ui.write(symbol, b' ') |
| 368 |
displayer.show(ctx, nodelen=nodelen) |
| 369 |
displayer.flush(ctx) |
| 370 |
ui.write(b'\n') |
| 371 |
|
| 372 |
# TODO display histedit hint? |
| 373 |
|
| 374 |
if basectx: |
| 375 |
# Vertically and horizontally separate stack base from parent |
| 376 |
# to reinforce stack boundary. |
| 377 |
if newheads: |
| 378 |
ui.write(b':/ ') |
| 379 |
else: |
| 380 |
ui.write(b' / ') |
| 381 |
|
| 382 |
ui.write(_(b'(stack base)'), b'\n', label=b'stack.label') |
| 383 |
ui.writenoi18n(b'o ') |
| 384 |
|
| 385 |
displayer.show(basectx, nodelen=nodelen) |
| 386 |
displayer.flush(basectx) |
| 387 |
ui.write(b'\n') |
| 388 |
|
| 389 |
|
| 390 |
@revsetpredicate(b'_underway([commitage[, headage]])') |
| 391 |
def underwayrevset(repo, subset, x): |
| 392 |
args = revset.getargsdict(x, b'underway', b'commitage headage') |
| 393 |
if b'commitage' not in args: |
| 394 |
args[b'commitage'] = None |
| 395 |
if b'headage' not in args: |
| 396 |
args[b'headage'] = None |
| 397 |
|
| 398 |
# We assume callers of this revset add a topographical sort on the |
| 399 |
# result. This means there is no benefit to making the revset lazy |
| 400 |
# since the topographical sort needs to consume all revs. |
| 401 |
# |
| 402 |
# With this in mind, we build up the set manually instead of constructing |
| 403 |
# a complex revset. This enables faster execution. |
| 404 |
|
| 405 |
# Mutable changesets (non-public) are the most important changesets |
| 406 |
# to return. ``not public()`` will also pull in obsolete changesets if |
| 407 |
# there is a non-obsolete changeset with obsolete ancestors. This is |
| 408 |
# why we exclude obsolete changesets from this query. |
| 409 |
rs = b'not public() and not obsolete()' |
| 410 |
rsargs = [] |
| 411 |
if args[b'commitage']: |
| 412 |
rs += b' and date(%s)' |
| 413 |
rsargs.append( |
| 414 |
revsetlang.getstring( |
| 415 |
args[b'commitage'], _(b'commitage requires a string') |
| 416 |
) |
| 417 |
) |
| 418 |
|
| 419 |
mutable = repo.revs(rs, *rsargs) |
| 420 |
relevant = revset.baseset(mutable) |
| 421 |
|
| 422 |
# Add parents of mutable changesets to provide context. |
| 423 |
relevant += repo.revs(b'parents(%ld)', mutable) |
| 424 |
|
| 425 |
# We also pull in (public) heads if they a) aren't closing a branch |
| 426 |
# b) are recent. |
| 427 |
rs = b'head() and not closed()' |
| 428 |
rsargs = [] |
| 429 |
if args[b'headage']: |
| 430 |
rs += b' and date(%s)' |
| 431 |
rsargs.append( |
| 432 |
revsetlang.getstring( |
| 433 |
args[b'headage'], _(b'headage requires a string') |
| 434 |
) |
| 435 |
) |
| 436 |
|
| 437 |
relevant += repo.revs(rs, *rsargs) |
| 438 |
|
| 439 |
# Add working directory parent. |
| 440 |
wdirrev = repo[b'.'].rev() |
| 441 |
if wdirrev != nullrev: |
| 442 |
relevant += revset.baseset({wdirrev}) |
| 443 |
|
| 444 |
return subset & relevant |
| 445 |
|
| 446 |
|
| 447 |
@showview(b'work', csettopic=b'work') |
| 448 |
def showwork(ui, repo, displayer): |
| 449 |
"""changesets that aren't finished""" |
| 450 |
# TODO support date-based limiting when calling revset. |
| 451 |
revs = repo.revs(b'sort(_underway(), topo)') |
| 452 |
nodelen = longestshortest(repo, revs) |
| 453 |
|
| 454 |
revdag = graphmod.dagwalker(repo, revs) |
| 455 |
|
| 456 |
ui.setconfig(b'experimental', b'graphshorten', True) |
| 457 |
logcmdutil.displaygraph( |
| 458 |
ui, |
| 459 |
repo, |
| 460 |
revdag, |
| 461 |
displayer, |
| 462 |
graphmod.asciiedges, |
| 463 |
props={b'nodelen': nodelen}, |
| 464 |
) |
| 465 |
|
| 466 |
|
| 467 |
def extsetup(ui): |
| 468 |
# Alias `hg <prefix><view>` to `hg show <view>`. |
| 469 |
for prefix in ui.configlist(b'commands', b'show.aliasprefix'): |
| 470 |
for view in showview._table: |
| 471 |
name = b'%s%s' % (prefix, view) |
| 472 |
|
| 473 |
choice, allcommands = cmd_finder.find_possible( |
| 474 |
name, commands.table, strict=True |
| 475 |
) |
| 476 |
|
| 477 |
# This alias is already a command name. Don't set it. |
| 478 |
if name in choice: |
| 479 |
continue |
| 480 |
|
| 481 |
# Same for aliases. |
| 482 |
if ui.config(b'alias', name, None): |
| 483 |
continue |
| 484 |
|
| 485 |
ui.setconfig(b'alias', name, b'show %s' % view, source=b'show') |
| 486 |
|
| 487 |
|
| 488 |
def longestshortest(repo, revs, minlen=4): |
| 489 |
"""Return the length of the longest shortest node to identify revisions. |
| 490 |
|
| 491 |
The result of this function can be used with the ``shortest()`` template |
| 492 |
function to ensure that a value is unique and unambiguous for a given |
| 493 |
set of nodes. |
| 494 |
|
| 495 |
The number of revisions in the repo is taken into account to prevent |
| 496 |
a numeric node prefix from conflicting with an integer revision number. |
| 497 |
If we fail to do this, a value of e.g. ``10023`` could mean either |
| 498 |
revision 10023 or node ``10023abc...``. |
| 499 |
""" |
| 500 |
if not revs: |
| 501 |
return minlen |
| 502 |
cl = repo.changelog |
| 503 |
return max( |
| 504 |
len(scmutil.shortesthexnodeidprefix(repo, cl.node(r), minlen)) |
| 505 |
for r in revs |
| 506 |
) |
| 507 |
|
| 508 |
|
| 509 |
# Adjust the docstring of the show command so it shows all registered views. |
| 510 |
# This is a bit hacky because it runs at the end of module load. When moved |
| 511 |
# into core or when another extension wants to provide a view, we'll need |
| 512 |
# to do this more robustly. |
| 513 |
# TODO make this more robust. |
| 514 |
def _updatedocstring(): |
| 515 |
longest = max(map(len, showview._table.keys())) |
| 516 |
entries = [] |
| 517 |
for key in sorted(showview._table.keys()): |
| 518 |
entries.append( |
| 519 |
r'%s %s' |
| 520 |
% ( |
| 521 |
pycompat.sysstr(key.ljust(longest)), |
| 522 |
showview._table[key]._origdoc, |
| 523 |
) |
| 524 |
) |
| 525 |
|
| 526 |
cmdtable[b'show'][0].__doc__ = pycompat.sysstr(b'%s\n\n%s\n ') % ( |
| 527 |
pycompat.cleandoc(cmdtable[b'show'][0].__doc__), |
| 528 |
pycompat.sysstr(b'\n\n').join(entries), |
| 529 |
) |
| 530 |
|
| 531 |
|
| 532 |
_updatedocstring() |