##// END OF EJS Templates
wrapfunction: use sysstr instead of bytes as argument in "beautifygraph"...
wrapfunction: use sysstr instead of bytes as argument in "beautifygraph" This is as valid and simpler, it will help us to eventually get ride of `safehasattr`.

File last commit:

r49730:6000f5b2 default
r51667:caa0a25f default
Show More
commands.py
357 lines | 11.9 KiB | text/x-python | PythonLexer
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 # Copyright 2016-present Facebook. All Rights Reserved.
#
# commands: fastannotate commands
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
import os
from mercurial.i18n import _
from mercurial import (
commands,
Matt Harbison
py3: rename pycompat.getcwd() to encoding.getcwd() (API)...
r39843 encoding,
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 error,
extensions,
Martin von Zweigbergk
errors: raise InputError from revsingle() iff revset provided by the user...
r48930 logcmdutil,
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 patch,
pycompat,
registrar,
scmutil,
util,
)
from . import (
context as facontext,
error as faerror,
formatter as faformatter,
)
cmdtable = {}
command = registrar.command(cmdtable)
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 def _matchpaths(repo, rev, pats, opts, aopts=facontext.defaultopts):
"""generate paths matching given patterns"""
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 perfhack = repo.ui.configbool(b'fastannotate', b'perfhack')
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243
# disable perfhack if:
# a) any walkopt is used
# b) if we treat pats as plain file names, some of them do not have
# corresponding linelog files
if perfhack:
# cwd related to reporoot
reporoot = os.path.dirname(repo.path)
Matt Harbison
py3: rename pycompat.getcwd() to encoding.getcwd() (API)...
r39843 reldir = os.path.relpath(encoding.getcwd(), reporoot)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if reldir == b'.':
reldir = b''
Augie Fackler
formatting: blacken the codebase...
r43346 if any(opts.get(o[1]) for o in commands.walkopts): # a)
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 perfhack = False
Augie Fackler
formatting: blacken the codebase...
r43346 else: # b)
relpats = [
os.path.relpath(p, reporoot) if os.path.isabs(p) else p
for p in pats
]
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 # disable perfhack on '..' since it allows escaping from the repo
Augie Fackler
formatting: blacken the codebase...
r43346 if any(
(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'..' in f
Augie Fackler
formatting: blacken the codebase...
r43346 or not os.path.isfile(
facontext.pathhelper(repo, f, aopts).linelogpath
)
)
for f in relpats
):
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 perfhack = False
# perfhack: emit paths directory without checking with manifest
# this can be incorrect if the rev dos not have file.
if perfhack:
for p in relpats:
yield os.path.join(reldir, p)
else:
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 def bad(x, y):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.Abort(b"%s: %s" % (x, y))
Augie Fackler
formatting: blacken the codebase...
r43346
Martin von Zweigbergk
errors: raise InputError from revsingle() iff revset provided by the user...
r48930 ctx = logcmdutil.revsingle(repo, rev)
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 m = scmutil.match(ctx, pats, opts, badfn=bad)
for p in ctx.walk(m):
yield p
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 fastannotatecommandargs = {
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 'options': [
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 (b'r', b'rev', b'.', _(b'annotate the specified revision'), _(b'REV')),
(b'u', b'user', None, _(b'list the author (long with -v)')),
(b'f', b'file', None, _(b'list the filename')),
(b'd', b'date', None, _(b'list the date (short with -q)')),
(b'n', b'number', None, _(b'list the revision number (default)')),
(b'c', b'changeset', None, _(b'list the changeset')),
(
b'l',
b'line-number',
None,
Martin von Zweigbergk
cleanup: join string literals that are already on one line...
r43387 _(b'show line number at the first appearance'),
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 ),
Augie Fackler
formatting: blacken the codebase...
r43346 (
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'e',
b'deleted',
Augie Fackler
formatting: blacken the codebase...
r43346 None,
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'show deleted lines (slow) (EXPERIMENTAL)'),
Augie Fackler
formatting: blacken the codebase...
r43346 ),
(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'',
b'no-content',
None,
_(b'do not show file content (EXPERIMENTAL)'),
),
(b'', b'no-follow', None, _(b"don't follow copies and renames")),
(
b'',
b'linear',
Augie Fackler
formatting: blacken the codebase...
r43346 None,
_(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'enforce linear history, ignore second parent '
b'of merges (EXPERIMENTAL)'
Augie Fackler
formatting: blacken the codebase...
r43346 ),
),
(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'',
b'long-hash',
Augie Fackler
formatting: blacken the codebase...
r43346 None,
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'show long changeset hash (EXPERIMENTAL)'),
),
(
b'',
b'rebuild',
None,
Martin von Zweigbergk
cleanup: join string literals that are already on one line...
r43387 _(b'rebuild cache even if it exists (EXPERIMENTAL)'),
Augie Fackler
formatting: blacken the codebase...
r43346 ),
]
+ commands.diffwsopts
+ commands.walkopts
+ commands.formatteropts,
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 'synopsis': _(b'[-r REV] [-f] [-a] [-u] [-d] [-n] [-c] [-l] FILE...'),
'inferrepo': True,
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 }
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 def fastannotate(ui, repo, *pats, **opts):
"""show changeset information by line for each file
List changes in files, showing the revision id responsible for each line.
This command is useful for discovering when a change was made and by whom.
By default this command prints revision numbers. If you include --file,
--user, or --date, the revision number is suppressed unless you also
include --number. The default format can also be customized by setting
fastannotate.defaultformat.
Returns 0 on success.
.. container:: verbose
This command uses an implementation different from the vanilla annotate
command, which may produce slightly different (while still reasonable)
outputs for some cases.
Unlike the vanilla anootate, fastannotate follows rename regardless of
the existence of --file.
For the best performance when running on a full repo, use -c, -l,
avoid -u, -d, -n. Use --linear and --no-content to make it even faster.
For the best performance when running on a shallow (remotefilelog)
repo, avoid --linear, --no-follow, or any diff options. As the server
won't be able to populate annotate cache when non-default options
affecting results are used.
"""
if not pats:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.Abort(_(b'at least one filename or pattern is required'))
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243
# performance hack: filtered repo can be slow. unfilter by default.
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if ui.configbool(b'fastannotate', b'unfilteredrepo'):
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 repo = repo.unfiltered()
Pulkit Goyal
py3: fix kwargs handling in hgext/fastannotate.py...
r39702 opts = pycompat.byteskwargs(opts)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 rev = opts.get(b'rev', b'.')
rebuild = opts.get(b'rebuild', False)
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243
Augie Fackler
formatting: blacken the codebase...
r43346 diffopts = patch.difffeatureopts(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 ui, opts, section=b'annotate', whitespace=True
Augie Fackler
formatting: blacken the codebase...
r43346 )
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 aopts = facontext.annotateopts(
diffopts=diffopts,
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 followmerge=not opts.get(b'linear', False),
followrename=not opts.get(b'no_follow', False),
Augie Fackler
formatting: blacken the codebase...
r43346 )
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243
Augie Fackler
formatting: blacken the codebase...
r43346 if not any(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 opts.get(s)
for s in [b'user', b'date', b'file', b'number', b'changeset']
Augie Fackler
formatting: blacken the codebase...
r43346 ):
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 # default 'number' for compatibility. but fastannotate is more
# efficient with "changeset", "line-number" and "no-content".
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 for name in ui.configlist(
b'fastannotate', b'defaultformat', [b'number']
):
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 opts[name] = True
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 ui.pager(b'fastannotate')
template = opts.get(b'template')
if template == b'json':
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 formatter = faformatter.jsonformatter(ui, repo, opts)
else:
formatter = faformatter.defaultformatter(ui, repo, opts)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 showdeleted = opts.get(b'deleted', False)
showlines = not bool(opts.get(b'no_content'))
showpath = opts.get(b'file', False)
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243
# find the head of the main (master) branch
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 master = ui.config(b'fastannotate', b'mainbranch') or rev
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243
# paths will be used for prefetching and the real annotating
paths = list(_matchpaths(repo, rev, pats, opts, aopts))
# for client, prefetch from the server
Martin von Zweigbergk
py3: delete b'' prefix from safehasattr arguments...
r43385 if util.safehasattr(repo, 'prefetchfastannotate'):
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 repo.prefetchfastannotate(paths)
for path in paths:
result = lines = existinglines = None
while True:
try:
with facontext.annotatecontext(repo, path, aopts, rebuild) as a:
Augie Fackler
formatting: blacken the codebase...
r43346 result = a.annotate(
rev,
master=master,
showpath=showpath,
showlines=(showlines and not showdeleted),
)
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 if showdeleted:
Augie Fackler
cleanup: run pyupgrade on our source tree to clean up varying things...
r44937 existinglines = {(l[0], l[1]) for l in result}
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 result = a.annotatealllines(
Augie Fackler
formatting: blacken the codebase...
r43346 rev, showpath=showpath, showlines=showlines
)
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 break
except (faerror.CannotReuseError, faerror.CorruptedFileError):
# happens if master moves backwards, or the file was deleted
# and readded, or renamed to an existing name, or corrupted.
Augie Fackler
formatting: blacken the codebase...
r43346 if rebuild: # give up since we have tried rebuild already
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 raise
Augie Fackler
formatting: blacken the codebase...
r43346 else: # try a second time rebuilding the cache (slow)
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 rebuild = True
continue
if showlines:
result, lines = result
formatter.write(result, lines, existinglines=existinglines)
formatter.end()
Augie Fackler
formatting: blacken the codebase...
r43346
Martin von Zweigbergk
cleanup: use set literals where possible...
r42224 _newopts = set()
Augie Fackler
formatting: blacken the codebase...
r43346 _knownopts = {
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 opt[1].replace(b'-', b'_')
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 for opt in (fastannotatecommandargs['options'] + commands.globalopts)
Augie Fackler
formatting: blacken the codebase...
r43346 }
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243
def _annotatewrapper(orig, ui, repo, *pats, **opts):
"""used by wrapdefault"""
# we need this hack until the obsstore has 0.0 seconds perf impact
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if ui.configbool(b'fastannotate', b'unfilteredrepo'):
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 repo = repo.unfiltered()
# treat the file as text (skip the isbinary check)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if ui.configbool(b'fastannotate', b'forcetext'):
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 opts['text'] = True
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243
# check if we need to do prefetch (client-side)
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 rev = opts.get('rev')
Martin von Zweigbergk
py3: delete b'' prefix from safehasattr arguments...
r43385 if util.safehasattr(repo, 'prefetchfastannotate') and rev is not None:
Pulkit Goyal
py3: fix kwargs handling in hgext/fastannotate.py...
r39702 paths = list(_matchpaths(repo, rev, pats, pycompat.byteskwargs(opts)))
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 repo.prefetchfastannotate(paths)
return orig(ui, repo, *pats, **opts)
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 def registercommand():
"""register the fastannotate command"""
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 name = b'fastannotate|fastblame|fa'
Rodrigo Damazio
help: adding a proper declaration for shortlist/basic commands (API)...
r40331 command(name, helpbasic=True, **fastannotatecommandargs)(fastannotate)
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 def wrapdefault():
"""wrap the default annotate command, to be aware of the protocol"""
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 extensions.wrapcommand(commands.table, b'annotate', _annotatewrapper)
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243
Augie Fackler
formatting: blacken the codebase...
r43346
@command(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'debugbuildannotatecache',
[(b'r', b'rev', b'', _(b'build up to the specific revision'), _(b'REV'))]
Augie Fackler
formatting: blacken the codebase...
r43346 + commands.walkopts,
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'[-r REV] FILE...'),
Augie Fackler
formatting: blacken the codebase...
r43346 )
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 def debugbuildannotatecache(ui, repo, *pats, **opts):
"""incrementally build fastannotate cache up to REV for specified files
If REV is not specified, use the config 'fastannotate.mainbranch'.
If fastannotate.client is True, download the annotate cache from the
server. Otherwise, build the annotate cache locally.
The annotate cache will be built using the default diff and follow
options and lives in '.hg/fastannotate/default'.
"""
Pulkit Goyal
py3: fix kwargs handling in hgext/fastannotate.py...
r39702 opts = pycompat.byteskwargs(opts)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 rev = opts.get(b'REV') or ui.config(b'fastannotate', b'mainbranch')
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 if not rev:
Augie Fackler
formatting: blacken the codebase...
r43346 raise error.Abort(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'you need to provide a revision'),
hint=_(b'set fastannotate.mainbranch or use --rev'),
Augie Fackler
formatting: blacken the codebase...
r43346 )
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if ui.configbool(b'fastannotate', b'unfilteredrepo'):
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 repo = repo.unfiltered()
Martin von Zweigbergk
errors: raise InputError from revsingle() iff revset provided by the user...
r48930 ctx = logcmdutil.revsingle(repo, rev)
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 m = scmutil.match(ctx, pats, opts)
paths = list(ctx.walk(m))
Martin von Zweigbergk
py3: delete b'' prefix from safehasattr arguments...
r43385 if util.safehasattr(repo, 'prefetchfastannotate'):
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 # client
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if opts.get(b'REV'):
raise error.Abort(_(b'--rev cannot be used for client'))
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 repo.prefetchfastannotate(paths)
else:
# server, or full repo
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 progress = ui.makeprogress(_(b'building'), total=len(paths))
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 for i, path in enumerate(paths):
Martin von Zweigbergk
debugbuildannotatecache: use progress helper...
r40873 progress.update(i)
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 with facontext.annotatecontext(repo, path) as actx:
try:
if actx.isuptodate(rev):
continue
actx.annotate(rev, rev)
except (faerror.CannotReuseError, faerror.CorruptedFileError):
# the cache is broken (could happen with renaming so the
# file history gets invalidated). rebuild and try again.
Augie Fackler
formatting: blacken the codebase...
r43346 ui.debug(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'fastannotate: %s: rebuilding broken cache\n' % path
Augie Fackler
formatting: blacken the codebase...
r43346 )
Augie Fackler
fastannotate: initial import from Facebook's hg-experimental...
r39243 actx.rebuild()
try:
actx.annotate(rev, rev)
except Exception as ex:
# possibly a bug, but should not stop us from building
# cache for other files.
Augie Fackler
formatting: blacken the codebase...
r43346 ui.warn(
_(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'fastannotate: %s: failed to '
b'build cache: %r\n'
Augie Fackler
formatting: blacken the codebase...
r43346 )
% (path, ex)
)
Martin von Zweigbergk
debugbuildannotatecache: use progress helper...
r40873 progress.complete()