| 1 |
# Copyright 2016-present Facebook. All Rights Reserved. |
| 2 |
# |
| 3 |
# support: fastannotate support for hgweb, and filectx |
| 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 |
from mercurial import ( |
| 11 |
context as hgcontext, |
| 12 |
extensions, |
| 13 |
hgweb, |
| 14 |
patch, |
| 15 |
util, |
| 16 |
) |
| 17 |
from mercurial.utils import ( |
| 18 |
dag_util, |
| 19 |
) |
| 20 |
|
| 21 |
from . import ( |
| 22 |
context, |
| 23 |
revmap, |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
class _lazyfctx: |
| 28 |
"""delegates to fctx but do not construct fctx when unnecessary""" |
| 29 |
|
| 30 |
def __init__(self, repo, node, path): |
| 31 |
self._node = node |
| 32 |
self._path = path |
| 33 |
self._repo = repo |
| 34 |
|
| 35 |
def node(self): |
| 36 |
return self._node |
| 37 |
|
| 38 |
def path(self): |
| 39 |
return self._path |
| 40 |
|
| 41 |
@util.propertycache |
| 42 |
def _fctx(self): |
| 43 |
return context.resolvefctx(self._repo, self._node, self._path) |
| 44 |
|
| 45 |
def __getattr__(self, name): |
| 46 |
return getattr(self._fctx, name) |
| 47 |
|
| 48 |
|
| 49 |
def _convertoutputs(repo, annotated, contents): |
| 50 |
"""convert fastannotate outputs to vanilla annotate format""" |
| 51 |
# fastannotate returns: [(nodeid, linenum, path)], [linecontent] |
| 52 |
# convert to what fctx.annotate returns: [annotateline] |
| 53 |
results = [] |
| 54 |
fctxmap = {} |
| 55 |
annotateline = dag_util.annotateline |
| 56 |
for i, (hsh, linenum, path) in enumerate(annotated): |
| 57 |
if (hsh, path) not in fctxmap: |
| 58 |
fctxmap[(hsh, path)] = _lazyfctx(repo, hsh, path) |
| 59 |
# linenum: the user wants 1-based, we have 0-based. |
| 60 |
lineno = linenum + 1 |
| 61 |
fctx = fctxmap[(hsh, path)] |
| 62 |
line = contents[i] |
| 63 |
results.append(annotateline(fctx=fctx, lineno=lineno, text=line)) |
| 64 |
return results |
| 65 |
|
| 66 |
|
| 67 |
def _getmaster(fctx): |
| 68 |
"""(fctx) -> str""" |
| 69 |
return fctx._repo.ui.config(b'fastannotate', b'mainbranch') or b'default' |
| 70 |
|
| 71 |
|
| 72 |
def _doannotate(fctx, follow=True, diffopts=None): |
| 73 |
"""like the vanilla fctx.annotate, but do it via fastannotate, and make |
| 74 |
the output format compatible with the vanilla fctx.annotate. |
| 75 |
may raise Exception, and always return line numbers. |
| 76 |
""" |
| 77 |
master = _getmaster(fctx) |
| 78 |
|
| 79 |
with context.fctxannotatecontext(fctx, follow, diffopts) as ac: |
| 80 |
try: |
| 81 |
annotated, contents = ac.annotate( |
| 82 |
fctx.rev(), master=master, showpath=True, showlines=True |
| 83 |
) |
| 84 |
except Exception: |
| 85 |
ac.rebuild() # try rebuild once |
| 86 |
fctx._repo.ui.debug( |
| 87 |
b'fastannotate: %s: rebuilding broken cache\n' % fctx._path |
| 88 |
) |
| 89 |
try: |
| 90 |
annotated, contents = ac.annotate( |
| 91 |
fctx.rev(), master=master, showpath=True, showlines=True |
| 92 |
) |
| 93 |
except Exception: |
| 94 |
raise |
| 95 |
|
| 96 |
assert annotated and contents |
| 97 |
return _convertoutputs(fctx._repo, annotated, contents) |
| 98 |
|
| 99 |
|
| 100 |
def _hgwebannotate(orig, fctx, ui): |
| 101 |
diffopts = patch.difffeatureopts( |
| 102 |
ui, untrusted=True, section=b'annotate', whitespace=True |
| 103 |
) |
| 104 |
return _doannotate(fctx, diffopts=diffopts) |
| 105 |
|
| 106 |
|
| 107 |
def _fctxannotate( |
| 108 |
orig, self, follow=False, linenumber=False, skiprevs=None, diffopts=None |
| 109 |
): |
| 110 |
if skiprevs: |
| 111 |
# skiprevs is not supported yet |
| 112 |
return orig( |
| 113 |
self, follow, linenumber, skiprevs=skiprevs, diffopts=diffopts |
| 114 |
) |
| 115 |
try: |
| 116 |
return _doannotate(self, follow, diffopts) |
| 117 |
except Exception as ex: |
| 118 |
self._repo.ui.debug( |
| 119 |
b'fastannotate: falling back to the vanilla annotate: %r\n' % ex |
| 120 |
) |
| 121 |
return orig(self, follow=follow, skiprevs=skiprevs, diffopts=diffopts) |
| 122 |
|
| 123 |
|
| 124 |
def _remotefctxannotate(orig, self, follow=False, skiprevs=None, diffopts=None): |
| 125 |
# skipset: a set-like used to test if a fctx needs to be downloaded |
| 126 |
with context.fctxannotatecontext(self, follow, diffopts) as ac: |
| 127 |
skipset = revmap.revmap(ac.revmappath) |
| 128 |
return orig( |
| 129 |
self, follow, skiprevs=skiprevs, diffopts=diffopts, prefetchskip=skipset |
| 130 |
) |
| 131 |
|
| 132 |
|
| 133 |
def replacehgwebannotate(): |
| 134 |
extensions.wrapfunction(hgweb.webutil, 'annotate', _hgwebannotate) |
| 135 |
|
| 136 |
|
| 137 |
def replacefctxannotate(): |
| 138 |
extensions.wrapfunction(hgcontext.basefilectx, 'annotate', _fctxannotate) |