| 1 |
# Copyright 2016-present Facebook. All Rights Reserved. |
| 2 |
# |
| 3 |
# commands: fastannotate commands |
| 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 |
from __future__ import annotations |
| 9 |
|
| 10 |
import os |
| 11 |
import typing |
| 12 |
|
| 13 |
from mercurial.i18n import _ |
| 14 |
from mercurial import ( |
| 15 |
cmd_impls, |
| 16 |
commands, |
| 17 |
encoding, |
| 18 |
error, |
| 19 |
extensions, |
| 20 |
logcmdutil, |
| 21 |
patch, |
| 22 |
pycompat, |
| 23 |
registrar, |
| 24 |
scmutil, |
| 25 |
) |
| 26 |
|
| 27 |
from . import ( |
| 28 |
context as facontext, |
| 29 |
error as faerror, |
| 30 |
formatter as faformatter, |
| 31 |
) |
| 32 |
|
| 33 |
cmdtable = {} |
| 34 |
command = registrar.command(cmdtable) |
| 35 |
|
| 36 |
|
| 37 |
def _matchpaths(repo, rev, pats, opts, aopts=facontext.defaultopts): |
| 38 |
"""generate paths matching given patterns""" |
| 39 |
perfhack = repo.ui.configbool(b'fastannotate', b'perfhack') |
| 40 |
|
| 41 |
# disable perfhack if: |
| 42 |
# a) any walkopt is used |
| 43 |
# b) if we treat pats as plain file names, some of them do not have |
| 44 |
# corresponding linelog files |
| 45 |
if perfhack: |
| 46 |
# cwd related to reporoot |
| 47 |
reporoot = os.path.dirname(repo.path) |
| 48 |
reldir = os.path.relpath(encoding.getcwd(), reporoot) |
| 49 |
if reldir == b'.': |
| 50 |
reldir = b'' |
| 51 |
if any(opts.get(o[1]) for o in commands.walkopts): # a) |
| 52 |
perfhack = False |
| 53 |
else: # b) |
| 54 |
relpats = [ |
| 55 |
os.path.relpath(p, reporoot) if os.path.isabs(p) else p |
| 56 |
for p in pats |
| 57 |
] |
| 58 |
# disable perfhack on '..' since it allows escaping from the repo |
| 59 |
if any( |
| 60 |
( |
| 61 |
b'..' in f |
| 62 |
or not os.path.isfile( |
| 63 |
facontext.pathhelper(repo, f, aopts).linelogpath |
| 64 |
) |
| 65 |
) |
| 66 |
for f in relpats |
| 67 |
): |
| 68 |
perfhack = False |
| 69 |
|
| 70 |
# perfhack: emit paths directory without checking with manifest |
| 71 |
# this can be incorrect if the rev dos not have file. |
| 72 |
if perfhack: |
| 73 |
for p in relpats: |
| 74 |
yield os.path.join(reldir, p) |
| 75 |
else: |
| 76 |
|
| 77 |
def bad(x, y): |
| 78 |
raise error.Abort(b"%s: %s" % (x, y)) |
| 79 |
|
| 80 |
ctx = logcmdutil.revsingle(repo, rev) |
| 81 |
m = scmutil.match(ctx, pats, opts, badfn=bad) |
| 82 |
for p in ctx.walk(m): |
| 83 |
yield p |
| 84 |
|
| 85 |
|
| 86 |
fastannotatecommandargs = { |
| 87 |
'options': [ |
| 88 |
(b'r', b'rev', b'.', _(b'annotate the specified revision'), _(b'REV')), |
| 89 |
(b'u', b'user', None, _(b'list the author (long with -v)')), |
| 90 |
(b'f', b'file', None, _(b'list the filename')), |
| 91 |
(b'd', b'date', None, _(b'list the date (short with -q)')), |
| 92 |
(b'n', b'number', None, _(b'list the revision number (default)')), |
| 93 |
(b'c', b'changeset', None, _(b'list the changeset')), |
| 94 |
( |
| 95 |
b'l', |
| 96 |
b'line-number', |
| 97 |
None, |
| 98 |
_(b'show line number at the first appearance'), |
| 99 |
), |
| 100 |
( |
| 101 |
b'e', |
| 102 |
b'deleted', |
| 103 |
None, |
| 104 |
_(b'show deleted lines (slow) (EXPERIMENTAL)'), |
| 105 |
), |
| 106 |
( |
| 107 |
b'', |
| 108 |
b'no-content', |
| 109 |
None, |
| 110 |
_(b'do not show file content (EXPERIMENTAL)'), |
| 111 |
), |
| 112 |
(b'', b'no-follow', None, _(b"don't follow copies and renames")), |
| 113 |
( |
| 114 |
b'', |
| 115 |
b'linear', |
| 116 |
None, |
| 117 |
_( |
| 118 |
b'enforce linear history, ignore second parent ' |
| 119 |
b'of merges (EXPERIMENTAL)' |
| 120 |
), |
| 121 |
), |
| 122 |
( |
| 123 |
b'', |
| 124 |
b'long-hash', |
| 125 |
None, |
| 126 |
_(b'show long changeset hash (EXPERIMENTAL)'), |
| 127 |
), |
| 128 |
( |
| 129 |
b'', |
| 130 |
b'rebuild', |
| 131 |
None, |
| 132 |
_(b'rebuild cache even if it exists (EXPERIMENTAL)'), |
| 133 |
), |
| 134 |
] |
| 135 |
+ commands.diffwsopts |
| 136 |
+ commands.walkopts |
| 137 |
+ commands.formatteropts, |
| 138 |
'synopsis': _(b'[-r REV] [-f] [-a] [-u] [-d] [-n] [-c] [-l] FILE...'), |
| 139 |
'inferrepo': True, |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
def fastannotate(ui, repo, *pats, **opts): |
| 144 |
"""show changeset information by line for each file |
| 145 |
|
| 146 |
List changes in files, showing the revision id responsible for each line. |
| 147 |
|
| 148 |
This command is useful for discovering when a change was made and by whom. |
| 149 |
|
| 150 |
By default this command prints revision numbers. If you include --file, |
| 151 |
--user, or --date, the revision number is suppressed unless you also |
| 152 |
include --number. The default format can also be customized by setting |
| 153 |
fastannotate.defaultformat. |
| 154 |
|
| 155 |
Returns 0 on success. |
| 156 |
|
| 157 |
.. container:: verbose |
| 158 |
|
| 159 |
This command uses an implementation different from the vanilla annotate |
| 160 |
command, which may produce slightly different (while still reasonable) |
| 161 |
outputs for some cases. |
| 162 |
|
| 163 |
Unlike the vanilla anootate, fastannotate follows rename regardless of |
| 164 |
the existence of --file. |
| 165 |
|
| 166 |
For the best performance when running on a full repo, use -c, -l, |
| 167 |
avoid -u, -d, -n. Use --linear and --no-content to make it even faster. |
| 168 |
|
| 169 |
For the best performance when running on a shallow (remotefilelog) |
| 170 |
repo, avoid --linear, --no-follow, or any diff options. As the server |
| 171 |
won't be able to populate annotate cache when non-default options |
| 172 |
affecting results are used. |
| 173 |
""" |
| 174 |
if not pats: |
| 175 |
raise error.Abort(_(b'at least one filename or pattern is required')) |
| 176 |
|
| 177 |
# performance hack: filtered repo can be slow. unfilter by default. |
| 178 |
if ui.configbool(b'fastannotate', b'unfilteredrepo'): |
| 179 |
repo = repo.unfiltered() |
| 180 |
|
| 181 |
opts = pycompat.byteskwargs(opts) |
| 182 |
|
| 183 |
rev = opts.get(b'rev', b'.') |
| 184 |
rebuild = opts.get(b'rebuild', False) |
| 185 |
|
| 186 |
diffopts = patch.difffeatureopts( |
| 187 |
ui, opts, section=b'annotate', whitespace=True |
| 188 |
) |
| 189 |
aopts = facontext.annotateopts( |
| 190 |
diffopts=diffopts, |
| 191 |
followmerge=not opts.get(b'linear', False), |
| 192 |
followrename=not opts.get(b'no_follow', False), |
| 193 |
) |
| 194 |
|
| 195 |
if not any( |
| 196 |
opts.get(s) |
| 197 |
for s in [b'user', b'date', b'file', b'number', b'changeset'] |
| 198 |
): |
| 199 |
# default 'number' for compatibility. but fastannotate is more |
| 200 |
# efficient with "changeset", "line-number" and "no-content". |
| 201 |
for name in ui.configlist( |
| 202 |
b'fastannotate', b'defaultformat', [b'number'] |
| 203 |
): |
| 204 |
opts[name] = True |
| 205 |
|
| 206 |
ui.pager(b'fastannotate') |
| 207 |
template = opts.get(b'template') |
| 208 |
if template == b'json': |
| 209 |
formatter = faformatter.jsonformatter(ui, repo, opts) |
| 210 |
else: |
| 211 |
formatter = faformatter.defaultformatter(ui, repo, opts) |
| 212 |
showdeleted = opts.get(b'deleted', False) |
| 213 |
showlines = not bool(opts.get(b'no_content')) |
| 214 |
showpath = opts.get(b'file', False) |
| 215 |
|
| 216 |
# find the head of the main (master) branch |
| 217 |
master = ui.config(b'fastannotate', b'mainbranch') or rev |
| 218 |
|
| 219 |
# paths will be used for prefetching and the real annotating |
| 220 |
paths = list(_matchpaths(repo, rev, pats, opts, aopts)) |
| 221 |
|
| 222 |
# for client, prefetch from the server |
| 223 |
if hasattr(repo, 'prefetchfastannotate'): |
| 224 |
repo.prefetchfastannotate(paths) |
| 225 |
|
| 226 |
for path in paths: |
| 227 |
result = lines = existinglines = None |
| 228 |
while True: |
| 229 |
try: |
| 230 |
with facontext.annotatecontext(repo, path, aopts, rebuild) as a: |
| 231 |
result = a.annotate( |
| 232 |
rev, |
| 233 |
master=master, |
| 234 |
showpath=showpath, |
| 235 |
showlines=(showlines and not showdeleted), |
| 236 |
) |
| 237 |
if showdeleted: |
| 238 |
existinglines = {(l[0], l[1]) for l in result} |
| 239 |
result = a.annotatealllines( |
| 240 |
rev, showpath=showpath, showlines=showlines |
| 241 |
) |
| 242 |
break |
| 243 |
except (faerror.CannotReuseError, faerror.CorruptedFileError): |
| 244 |
# happens if master moves backwards, or the file was deleted |
| 245 |
# and readded, or renamed to an existing name, or corrupted. |
| 246 |
if rebuild: # give up since we have tried rebuild already |
| 247 |
raise |
| 248 |
else: # try a second time rebuilding the cache (slow) |
| 249 |
rebuild = True |
| 250 |
continue |
| 251 |
|
| 252 |
if showlines: |
| 253 |
result, lines = result |
| 254 |
|
| 255 |
formatter.write(result, lines, existinglines=existinglines) |
| 256 |
formatter.end() |
| 257 |
|
| 258 |
|
| 259 |
_newopts = set() |
| 260 |
_knownopts: set[bytes] = { |
| 261 |
# TODO: drop the cast when pytype stops munging the tuple elements into a |
| 262 |
# single Union. |
| 263 |
typing.cast(bytes, opt[1]).replace(b'-', b'_') |
| 264 |
for opt in (fastannotatecommandargs['options'] + cmd_impls.global_opts) |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
def _annotatewrapper(orig, ui, repo, *pats, **opts): |
| 269 |
"""used by wrapdefault""" |
| 270 |
# we need this hack until the obsstore has 0.0 seconds perf impact |
| 271 |
if ui.configbool(b'fastannotate', b'unfilteredrepo'): |
| 272 |
repo = repo.unfiltered() |
| 273 |
|
| 274 |
# treat the file as text (skip the isbinary check) |
| 275 |
if ui.configbool(b'fastannotate', b'forcetext'): |
| 276 |
opts['text'] = True |
| 277 |
|
| 278 |
# check if we need to do prefetch (client-side) |
| 279 |
rev = opts.get('rev') |
| 280 |
if hasattr(repo, 'prefetchfastannotate') and rev is not None: |
| 281 |
paths = list(_matchpaths(repo, rev, pats, pycompat.byteskwargs(opts))) |
| 282 |
repo.prefetchfastannotate(paths) |
| 283 |
|
| 284 |
return orig(ui, repo, *pats, **opts) |
| 285 |
|
| 286 |
|
| 287 |
def registercommand(): |
| 288 |
"""register the fastannotate command""" |
| 289 |
name = b'fastannotate|fastblame|fa' |
| 290 |
command(name, helpbasic=True, **fastannotatecommandargs)(fastannotate) |
| 291 |
|
| 292 |
|
| 293 |
def wrapdefault(): |
| 294 |
"""wrap the default annotate command, to be aware of the protocol""" |
| 295 |
extensions.wrapcommand(commands.table, b'annotate', _annotatewrapper) |
| 296 |
|
| 297 |
|
| 298 |
@command( |
| 299 |
b'debugbuildannotatecache', |
| 300 |
[(b'r', b'rev', b'', _(b'build up to the specific revision'), _(b'REV'))] |
| 301 |
+ commands.walkopts, |
| 302 |
_(b'[-r REV] FILE...'), |
| 303 |
) |
| 304 |
def debugbuildannotatecache(ui, repo, *pats, **opts): |
| 305 |
"""incrementally build fastannotate cache up to REV for specified files |
| 306 |
|
| 307 |
If REV is not specified, use the config 'fastannotate.mainbranch'. |
| 308 |
|
| 309 |
If fastannotate.client is True, download the annotate cache from the |
| 310 |
server. Otherwise, build the annotate cache locally. |
| 311 |
|
| 312 |
The annotate cache will be built using the default diff and follow |
| 313 |
options and lives in '.hg/fastannotate/default'. |
| 314 |
""" |
| 315 |
opts = pycompat.byteskwargs(opts) |
| 316 |
rev = opts.get(b'REV') or ui.config(b'fastannotate', b'mainbranch') |
| 317 |
if not rev: |
| 318 |
raise error.Abort( |
| 319 |
_(b'you need to provide a revision'), |
| 320 |
hint=_(b'set fastannotate.mainbranch or use --rev'), |
| 321 |
) |
| 322 |
if ui.configbool(b'fastannotate', b'unfilteredrepo'): |
| 323 |
repo = repo.unfiltered() |
| 324 |
ctx = logcmdutil.revsingle(repo, rev) |
| 325 |
m = scmutil.match(ctx, pats, opts) |
| 326 |
paths = list(ctx.walk(m)) |
| 327 |
if hasattr(repo, 'prefetchfastannotate'): |
| 328 |
# client |
| 329 |
if opts.get(b'REV'): |
| 330 |
raise error.Abort(_(b'--rev cannot be used for client')) |
| 331 |
repo.prefetchfastannotate(paths) |
| 332 |
else: |
| 333 |
# server, or full repo |
| 334 |
progress = ui.makeprogress(_(b'building'), total=len(paths)) |
| 335 |
for i, path in enumerate(paths): |
| 336 |
progress.update(i) |
| 337 |
with facontext.annotatecontext(repo, path) as actx: |
| 338 |
try: |
| 339 |
if actx.isuptodate(rev): |
| 340 |
continue |
| 341 |
actx.annotate(rev, rev) |
| 342 |
except (faerror.CannotReuseError, faerror.CorruptedFileError): |
| 343 |
# the cache is broken (could happen with renaming so the |
| 344 |
# file history gets invalidated). rebuild and try again. |
| 345 |
ui.debug( |
| 346 |
b'fastannotate: %s: rebuilding broken cache\n' % path |
| 347 |
) |
| 348 |
actx.rebuild() |
| 349 |
try: |
| 350 |
actx.annotate(rev, rev) |
| 351 |
except Exception as ex: |
| 352 |
# possibly a bug, but should not stop us from building |
| 353 |
# cache for other files. |
| 354 |
ui.warn( |
| 355 |
_( |
| 356 |
b'fastannotate: %s: failed to ' |
| 357 |
b'build cache: %r\n' |
| 358 |
) |
| 359 |
% (path, ex) |
| 360 |
) |
| 361 |
progress.complete() |