logcmdutil.py
1085 lines
| 35.7 KiB
| text/x-python
|
PythonLexer
/ mercurial / logcmdutil.py
|
r35903 | # logcmdutil.py - utility for log-like commands | ||
# | ||||
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
from __future__ import absolute_import | ||||
import itertools | ||||
import os | ||||
|
r41795 | import posixpath | ||
|
r35903 | |||
from .i18n import _ | ||||
from .node import ( | ||||
nullid, | ||||
|
r39832 | wdirid, | ||
wdirrev, | ||||
|
r35903 | ) | ||
from . import ( | ||||
dagop, | ||||
error, | ||||
formatter, | ||||
graphmod, | ||||
match as matchmod, | ||||
mdiff, | ||||
patch, | ||||
pathutil, | ||||
pycompat, | ||||
revset, | ||||
revsetlang, | ||||
scmutil, | ||||
smartset, | ||||
templatekw, | ||||
templater, | ||||
util, | ||||
) | ||||
|
r37102 | from .utils import ( | ||
dateutil, | ||||
stringutil, | ||||
) | ||||
|
r35903 | |||
|
r43346 | |||
|
r44162 | if pycompat.TYPE_CHECKING: | ||
|
r44100 | from typing import ( | ||
Any, | ||||
|
r44163 | Optional, | ||
|
r44100 | Tuple, | ||
) | ||||
|
r44163 | for t in (Any, Optional, Tuple): | ||
|
r44100 | assert t | ||
|
r35905 | def getlimit(opts): | ||
|
r35903 | """get the log limit according to option -l/--limit""" | ||
|
r43347 | limit = opts.get(b'limit') | ||
|
r35903 | if limit: | ||
try: | ||||
limit = int(limit) | ||||
except ValueError: | ||||
|
r43347 | raise error.Abort(_(b'limit must be a positive integer')) | ||
|
r35903 | if limit <= 0: | ||
|
r43347 | raise error.Abort(_(b'limit must be positive')) | ||
|
r35903 | else: | ||
limit = None | ||||
return limit | ||||
|
r43346 | |||
def diffordiffstat( | ||||
ui, | ||||
repo, | ||||
diffopts, | ||||
node1, | ||||
node2, | ||||
match, | ||||
changes=None, | ||||
stat=False, | ||||
fp=None, | ||||
graphwidth=0, | ||||
|
r43347 | prefix=b'', | ||
root=b'', | ||||
|
r43346 | listsubrepos=False, | ||
hunksfilterfn=None, | ||||
): | ||||
|
r35903 | '''show diff or diffstat.''' | ||
|
r41761 | ctx1 = repo[node1] | ||
ctx2 = repo[node2] | ||||
|
r35903 | if root: | ||
relroot = pathutil.canonpath(repo.root, repo.getcwd(), root) | ||||
else: | ||||
|
r43347 | relroot = b'' | ||
|
r41769 | copysourcematch = None | ||
|
r43346 | |||
|
r41796 | def compose(f, g): | ||
return lambda x: f(g(x)) | ||||
|
r43346 | |||
|
r41795 | def pathfn(f): | ||
return posixpath.join(prefix, f) | ||||
|
r43346 | |||
|
r43347 | if relroot != b'': | ||
|
r35903 | # XXX relative roots currently don't work if the root is within a | ||
# subrepo | ||||
|
r41804 | uipathfn = scmutil.getuipathfn(repo, legacyrelativevalue=True) | ||
uirelroot = uipathfn(pathfn(relroot)) | ||||
|
r43347 | relroot += b'/' | ||
|
r35903 | for matchroot in match.files(): | ||
if not matchroot.startswith(relroot): | ||||
|
r43346 | ui.warn( | ||
|
r43347 | _(b'warning: %s not inside relative root %s\n') | ||
|
r43346 | % (uipathfn(pathfn(matchroot)), uirelroot) | ||
) | ||||
|
r35903 | |||
|
r43347 | relrootmatch = scmutil.match(ctx2, pats=[relroot], default=b'path') | ||
|
r41768 | match = matchmod.intersectmatchers(match, relrootmatch) | ||
|
r41769 | copysourcematch = relrootmatch | ||
|
r41768 | |||
|
r43346 | checkroot = repo.ui.configbool( | ||
|
r43347 | b'devel', b'all-warnings' | ||
) or repo.ui.configbool(b'devel', b'check-relroot') | ||||
|
r43346 | |||
|
r41796 | def relrootpathfn(f): | ||
|
r41795 | if checkroot and not f.startswith(relroot): | ||
raise AssertionError( | ||||
|
r43347 | b"file %s doesn't start with relroot %s" % (f, relroot) | ||
|
r43346 | ) | ||
return f[len(relroot) :] | ||||
|
r41796 | pathfn = compose(relrootpathfn, pathfn) | ||
|
r41795 | |||
|
r35903 | if stat: | ||
diffopts = diffopts.copy(context=0, noprefix=False) | ||||
width = 80 | ||||
if not ui.plain(): | ||||
|
r41129 | width = ui.termwidth() - graphwidth | ||
|
r41819 | # If an explicit --root was given, don't respect ui.relative-paths | ||
if not relroot: | ||||
pathfn = compose(scmutil.getuipathfn(repo), pathfn) | ||||
|
r35979 | |||
|
r43346 | chunks = ctx2.diff( | ||
ctx1, | ||||
match, | ||||
changes, | ||||
opts=diffopts, | ||||
pathfn=pathfn, | ||||
copysourcematch=copysourcematch, | ||||
hunksfilterfn=hunksfilterfn, | ||||
) | ||||
|
r35979 | |||
if fp is not None or ui.canwritewithoutlabels(): | ||||
|
r36025 | out = fp or ui | ||
|
r35979 | if stat: | ||
|
r36132 | chunks = [patch.diffstat(util.iterlines(chunks), width=width)] | ||
|
r35979 | for chunk in util.filechunkiter(util.chunkbuffer(chunks)): | ||
|
r36025 | out.write(chunk) | ||
|
r35903 | else: | ||
|
r35979 | if stat: | ||
chunks = patch.diffstatui(util.iterlines(chunks), width=width) | ||||
else: | ||||
|
r43346 | chunks = patch.difflabel( | ||
lambda chunks, **kwargs: chunks, chunks, opts=diffopts | ||||
) | ||||
|
r35979 | if ui.canbatchlabeledwrites(): | ||
|
r43346 | |||
|
r35979 | def gen(): | ||
for chunk, label in chunks: | ||||
yield ui.label(chunk, label=label) | ||||
|
r43346 | |||
|
r35979 | for chunk in util.filechunkiter(util.chunkbuffer(gen())): | ||
|
r36025 | ui.write(chunk) | ||
|
r35979 | else: | ||
for chunk, label in chunks: | ||||
|
r36025 | ui.write(chunk, label=label) | ||
|
r35903 | |||
|
r42177 | for subpath, sub in scmutil.itersubrepos(ctx1, ctx2): | ||
tempnode2 = node2 | ||||
try: | ||||
if node2 is not None: | ||||
tempnode2 = ctx2.substate[subpath][1] | ||||
except KeyError: | ||||
# A subrepo that existed in node1 was deleted between node1 and | ||||
# node2 (inclusive). Thus, ctx2's substate won't contain that | ||||
# subpath. The best we can do is to ignore it. | ||||
tempnode2 = None | ||||
submatch = matchmod.subdirmatcher(subpath, match) | ||||
subprefix = repo.wvfs.reljoin(prefix, subpath) | ||||
if listsubrepos or match.exact(subpath) or any(submatch.files()): | ||||
|
r43346 | sub.diff( | ||
ui, | ||||
diffopts, | ||||
tempnode2, | ||||
submatch, | ||||
changes=changes, | ||||
stat=stat, | ||||
fp=fp, | ||||
prefix=subprefix, | ||||
) | ||||
|
r35903 | |||
|
r36024 | class changesetdiffer(object): | ||
"""Generate diff of changeset with pre-configured filtering functions""" | ||||
def _makefilematcher(self, ctx): | ||||
return scmutil.matchall(ctx.repo()) | ||||
def _makehunksfilter(self, ctx): | ||||
return None | ||||
|
r41129 | def showdiff(self, ui, ctx, diffopts, graphwidth=0, stat=False): | ||
|
r36024 | repo = ctx.repo() | ||
node = ctx.node() | ||||
prev = ctx.p1().node() | ||||
|
r43346 | diffordiffstat( | ||
ui, | ||||
repo, | ||||
diffopts, | ||||
prev, | ||||
node, | ||||
match=self._makefilematcher(ctx), | ||||
stat=stat, | ||||
graphwidth=graphwidth, | ||||
hunksfilterfn=self._makehunksfilter(ctx), | ||||
) | ||||
|
r36024 | |||
|
r35904 | def changesetlabels(ctx): | ||
|
r43347 | labels = [b'log.changeset', b'changeset.%s' % ctx.phasestr()] | ||
|
r35903 | if ctx.obsolete(): | ||
|
r43347 | labels.append(b'changeset.obsolete') | ||
|
r35903 | if ctx.isunstable(): | ||
|
r43347 | labels.append(b'changeset.unstable') | ||
|
r35903 | for instability in ctx.instabilities(): | ||
|
r43347 | labels.append(b'instability.%s' % instability) | ||
return b' '.join(labels) | ||||
|
r35903 | |||
|
r43346 | |||
|
r35904 | class changesetprinter(object): | ||
|
r35903 | '''show changeset information when templating not requested.''' | ||
|
r36024 | def __init__(self, ui, repo, differ=None, diffopts=None, buffered=False): | ||
|
r35903 | self.ui = ui | ||
self.repo = repo | ||||
self.buffered = buffered | ||||
|
r36024 | self._differ = differ or changesetdiffer() | ||
|
r37858 | self._diffopts = patch.diffallopts(ui, diffopts) | ||
|
r43347 | self._includestat = diffopts and diffopts.get(b'stat') | ||
self._includediff = diffopts and diffopts.get(b'patch') | ||||
|
r35903 | self.header = {} | ||
self.hunk = {} | ||||
self.lastheader = None | ||||
self.footer = None | ||||
self._columns = templatekw.getlogcolumns() | ||||
def flush(self, ctx): | ||||
rev = ctx.rev() | ||||
if rev in self.header: | ||||
h = self.header[rev] | ||||
if h != self.lastheader: | ||||
self.lastheader = h | ||||
self.ui.write(h) | ||||
del self.header[rev] | ||||
if rev in self.hunk: | ||||
self.ui.write(self.hunk[rev]) | ||||
del self.hunk[rev] | ||||
def close(self): | ||||
if self.footer: | ||||
self.ui.write(self.footer) | ||||
|
r36020 | def show(self, ctx, copies=None, **props): | ||
|
r35903 | props = pycompat.byteskwargs(props) | ||
if self.buffered: | ||||
self.ui.pushbuffer(labeled=True) | ||||
|
r36020 | self._show(ctx, copies, props) | ||
|
r35903 | self.hunk[ctx.rev()] = self.ui.popbuffer() | ||
else: | ||||
|
r36020 | self._show(ctx, copies, props) | ||
|
r35903 | |||
|
r36020 | def _show(self, ctx, copies, props): | ||
|
r35903 | '''show a single changeset or file revision''' | ||
changenode = ctx.node() | ||||
|
r43347 | graphwidth = props.get(b'graphwidth', 0) | ||
|
r35903 | |||
if self.ui.quiet: | ||||
|
r43346 | self.ui.write( | ||
|
r43347 | b"%s\n" % scmutil.formatchangeid(ctx), label=b'log.node' | ||
|
r43346 | ) | ||
|
r35903 | return | ||
columns = self._columns | ||||
|
r43346 | self.ui.write( | ||
|
r43347 | columns[b'changeset'] % scmutil.formatchangeid(ctx), | ||
|
r43346 | label=changesetlabels(ctx), | ||
) | ||||
|
r35903 | |||
# branches are shown first before any other names due to backwards | ||||
# compatibility | ||||
branch = ctx.branch() | ||||
# don't show the default branch name | ||||
|
r43347 | if branch != b'default': | ||
self.ui.write(columns[b'branch'] % branch, label=b'log.branch') | ||||
|
r35903 | |||
|
r43376 | for nsname, ns in pycompat.iteritems(self.repo.names): | ||
|
r35903 | # branches has special logic already handled above, so here we just | ||
# skip it | ||||
|
r43347 | if nsname == b'branches': | ||
|
r35903 | continue | ||
# we will use the templatename as the color name since those two | ||||
# should be the same | ||||
for name in ns.names(self.repo, changenode): | ||||
|
r43347 | self.ui.write(ns.logfmt % name, label=b'log.%s' % ns.colorname) | ||
|
r35903 | if self.ui.debugflag: | ||
|
r43347 | self.ui.write( | ||
columns[b'phase'] % ctx.phasestr(), label=b'log.phase' | ||||
) | ||||
|
r35903 | for pctx in scmutil.meaningfulparents(self.repo, ctx): | ||
|
r43347 | label = b'log.parent changeset.%s' % pctx.phasestr() | ||
|
r43346 | self.ui.write( | ||
|
r43347 | columns[b'parent'] % scmutil.formatchangeid(pctx), label=label | ||
|
r43346 | ) | ||
|
r35903 | |||
|
r39832 | if self.ui.debugflag: | ||
|
r35903 | mnode = ctx.manifestnode() | ||
|
r39832 | if mnode is None: | ||
mnode = wdirid | ||||
mrev = wdirrev | ||||
else: | ||||
mrev = self.repo.manifestlog.rev(mnode) | ||||
|
r43346 | self.ui.write( | ||
|
r43347 | columns[b'manifest'] | ||
|
r43346 | % scmutil.formatrevnode(self.ui, mrev, mnode), | ||
|
r43347 | label=b'ui.debug log.manifest', | ||
|
r43346 | ) | ||
|
r43347 | self.ui.write(columns[b'user'] % ctx.user(), label=b'log.user') | ||
|
r43346 | self.ui.write( | ||
|
r43347 | columns[b'date'] % dateutil.datestr(ctx.date()), label=b'log.date' | ||
|
r43346 | ) | ||
|
r35903 | |||
if ctx.isunstable(): | ||||
instabilities = ctx.instabilities() | ||||
|
r43346 | self.ui.write( | ||
|
r43347 | columns[b'instability'] % b', '.join(instabilities), | ||
label=b'log.instability', | ||||
|
r43346 | ) | ||
|
r35903 | |||
elif ctx.obsolete(): | ||||
self._showobsfate(ctx) | ||||
self._exthook(ctx) | ||||
if self.ui.debugflag: | ||||
|
r44047 | files = ctx.p1().status(ctx) | ||
for key, value in zip( | ||||
[b'files', b'files+', b'files-'], | ||||
[files.modified, files.added, files.removed], | ||||
): | ||||
|
r35903 | if value: | ||
|
r43346 | self.ui.write( | ||
|
r43347 | columns[key] % b" ".join(value), | ||
label=b'ui.debug log.files', | ||||
|
r43346 | ) | ||
|
r35903 | elif ctx.files() and self.ui.verbose: | ||
|
r43346 | self.ui.write( | ||
|
r43347 | columns[b'files'] % b" ".join(ctx.files()), | ||
label=b'ui.note log.files', | ||||
|
r43346 | ) | ||
|
r35903 | if copies and self.ui.verbose: | ||
|
r43347 | copies = [b'%s (%s)' % c for c in copies] | ||
|
r43346 | self.ui.write( | ||
|
r43347 | columns[b'copies'] % b' '.join(copies), | ||
label=b'ui.note log.copies', | ||||
|
r43346 | ) | ||
|
r35903 | |||
extra = ctx.extra() | ||||
if extra and self.ui.debugflag: | ||||
for key, value in sorted(extra.items()): | ||||
|
r43346 | self.ui.write( | ||
|
r43347 | columns[b'extra'] % (key, stringutil.escapestr(value)), | ||
label=b'ui.debug log.extra', | ||||
|
r43346 | ) | ||
|
r35903 | |||
description = ctx.description().strip() | ||||
if description: | ||||
if self.ui.verbose: | ||||
|
r43346 | self.ui.write( | ||
|
r43347 | _(b"description:\n"), label=b'ui.note log.description' | ||
|
r43346 | ) | ||
|
r43347 | self.ui.write(description, label=b'ui.note log.description') | ||
self.ui.write(b"\n\n") | ||||
|
r35903 | else: | ||
|
r43346 | self.ui.write( | ||
|
r43347 | columns[b'summary'] % description.splitlines()[0], | ||
label=b'log.summary', | ||||
|
r43346 | ) | ||
|
r43347 | self.ui.write(b"\n") | ||
|
r35903 | |||
|
r41129 | self._showpatch(ctx, graphwidth) | ||
|
r35903 | |||
def _showobsfate(self, ctx): | ||||
|
r36534 | # TODO: do not depend on templater | ||
tres = formatter.templateresources(self.repo.ui, self.repo) | ||||
|
r43346 | t = formatter.maketemplater( | ||
self.repo.ui, | ||||
|
r43347 | b'{join(obsfate, "\n")}', | ||
|
r43346 | defaults=templatekw.keywords, | ||
resources=tres, | ||||
) | ||||
|
r43347 | obsfate = t.renderdefault({b'ctx': ctx}).splitlines() | ||
|
r35903 | |||
if obsfate: | ||||
for obsfateline in obsfate: | ||||
|
r43346 | self.ui.write( | ||
|
r43347 | self._columns[b'obsolete'] % obsfateline, | ||
label=b'log.obsfate', | ||||
|
r43346 | ) | ||
|
r35903 | |||
def _exthook(self, ctx): | ||||
'''empty method used by extension as a hook point | ||||
''' | ||||
|
r41129 | def _showpatch(self, ctx, graphwidth=0): | ||
|
r37859 | if self._includestat: | ||
|
r43346 | self._differ.showdiff( | ||
self.ui, ctx, self._diffopts, graphwidth, stat=True | ||||
) | ||||
|
r37859 | if self._includestat and self._includediff: | ||
|
r43347 | self.ui.write(b"\n") | ||
|
r37859 | if self._includediff: | ||
|
r43346 | self._differ.showdiff( | ||
self.ui, ctx, self._diffopts, graphwidth, stat=False | ||||
) | ||||
|
r37859 | if self._includestat or self._includediff: | ||
|
r43347 | self.ui.write(b"\n") | ||
|
r35903 | |||
|
r43346 | |||
|
r37791 | class changesetformatter(changesetprinter): | ||
"""Format changeset information by generic formatter""" | ||||
|
r35903 | |||
|
r43346 | def __init__( | ||
self, ui, repo, fm, differ=None, diffopts=None, buffered=False | ||||
): | ||||
|
r36024 | changesetprinter.__init__(self, ui, repo, differ, diffopts, buffered) | ||
|
r37858 | self._diffopts = patch.difffeatureopts(ui, diffopts, git=True) | ||
|
r37791 | self._fm = fm | ||
|
r35903 | |||
def close(self): | ||||
|
r37790 | self._fm.end() | ||
|
r35903 | |||
|
r36020 | def _show(self, ctx, copies, props): | ||
|
r35903 | '''show a single changeset or file revision''' | ||
|
r37790 | fm = self._fm | ||
fm.startitem() | ||||
|
r39741 | fm.context(ctx=ctx) | ||
|
r43346 | fm.data(rev=scmutil.intrev(ctx), node=fm.hexfunc(scmutil.binnode(ctx))) | ||
|
r35903 | |||
|
r43372 | datahint = fm.datahint() | ||
if self.ui.quiet and not datahint: | ||||
|
r35903 | return | ||
|
r43346 | fm.data( | ||
branch=ctx.branch(), | ||||
phase=ctx.phasestr(), | ||||
user=ctx.user(), | ||||
date=fm.formatdate(ctx.date()), | ||||
desc=ctx.description(), | ||||
|
r43347 | bookmarks=fm.formatlist(ctx.bookmarks(), name=b'bookmark'), | ||
tags=fm.formatlist(ctx.tags(), name=b'tag'), | ||||
|
r43346 | parents=fm.formatlist( | ||
|
r43347 | [fm.hexfunc(c.node()) for c in ctx.parents()], name=b'node' | ||
|
r43346 | ), | ||
) | ||||
|
r35903 | |||
|
r43372 | if self.ui.debugflag or b'manifest' in datahint: | ||
fm.data(manifest=fm.hexfunc(ctx.manifestnode() or wdirid)) | ||||
if self.ui.debugflag or b'extra' in datahint: | ||||
fm.data(extra=fm.formatdict(ctx.extra())) | ||||
|
r35903 | |||
|
r43372 | if ( | ||
self.ui.debugflag | ||||
or b'modified' in datahint | ||||
or b'added' in datahint | ||||
or b'removed' in datahint | ||||
): | ||||
|
r35903 | files = ctx.p1().status(ctx) | ||
|
r43346 | fm.data( | ||
|
r44047 | modified=fm.formatlist(files.modified, name=b'file'), | ||
added=fm.formatlist(files.added, name=b'file'), | ||||
removed=fm.formatlist(files.removed, name=b'file'), | ||||
|
r43346 | ) | ||
|
r35903 | |||
|
r43372 | verbose = not self.ui.debugflag and self.ui.verbose | ||
if verbose or b'files' in datahint: | ||||
|
r43347 | fm.data(files=fm.formatlist(ctx.files(), name=b'file')) | ||
|
r43372 | if verbose and copies or b'copies' in datahint: | ||
fm.data( | ||||
copies=fm.formatdict(copies or {}, key=b'name', value=b'source') | ||||
) | ||||
|
r35903 | |||
|
r43372 | if self._includestat or b'diffstat' in datahint: | ||
|
r36021 | self.ui.pushbuffer() | ||
|
r37858 | self._differ.showdiff(self.ui, ctx, self._diffopts, stat=True) | ||
|
r37790 | fm.data(diffstat=self.ui.popbuffer()) | ||
|
r43372 | if self._includediff or b'diff' in datahint: | ||
|
r36021 | self.ui.pushbuffer() | ||
|
r37858 | self._differ.showdiff(self.ui, ctx, self._diffopts, stat=False) | ||
|
r37790 | fm.data(diff=self.ui.popbuffer()) | ||
|
r35903 | |||
|
r43346 | |||
|
r35904 | class changesettemplater(changesetprinter): | ||
|
r35903 | '''format changeset information. | ||
Note: there are a variety of convenience functions to build a | ||||
|
r35904 | changesettemplater for common cases. See functions such as: | ||
|
r35905 | maketemplater, changesetdisplayer, buildcommittemplate, or other | ||
|
r35903 | functions that use changesest_templater. | ||
''' | ||||
# Arguments before "buffered" used to be positional. Consider not | ||||
# adding/removing arguments before "buffered" to not break callers. | ||||
|
r43346 | def __init__( | ||
self, ui, repo, tmplspec, differ=None, diffopts=None, buffered=False | ||||
): | ||||
|
r36024 | changesetprinter.__init__(self, ui, repo, differ, diffopts, buffered) | ||
|
r37090 | # tres is shared with _graphnodeformatter() | ||
self._tresources = tres = formatter.templateresources(ui, repo) | ||||
|
r43346 | self.t = formatter.loadtemplater( | ||
ui, | ||||
tmplspec, | ||||
defaults=templatekw.keywords, | ||||
resources=tres, | ||||
cache=templatekw.defaulttempl, | ||||
) | ||||
|
r35903 | self._counter = itertools.count() | ||
self._tref = tmplspec.ref | ||||
|
r43346 | self._parts = { | ||
|
r43347 | b'header': b'', | ||
b'footer': b'', | ||||
|
r43346 | tmplspec.ref: tmplspec.ref, | ||
|
r43347 | b'docheader': b'', | ||
b'docfooter': b'', | ||||
b'separator': b'', | ||||
|
r43346 | } | ||
|
r35903 | if tmplspec.mapfile: | ||
# find correct templates for current mode, for backward | ||||
# compatibility with 'log -v/-q/--debug' using a mapfile | ||||
tmplmodes = [ | ||||
|
r43347 | (True, b''), | ||
(self.ui.verbose, b'_verbose'), | ||||
(self.ui.quiet, b'_quiet'), | ||||
(self.ui.debugflag, b'_debug'), | ||||
|
r35903 | ] | ||
for mode, postfix in tmplmodes: | ||||
for t in self._parts: | ||||
cur = t + postfix | ||||
if mode and cur in self.t: | ||||
self._parts[t] = cur | ||||
else: | ||||
partnames = [p for p in self._parts.keys() if p != tmplspec.ref] | ||||
m = formatter.templatepartsmap(tmplspec, self.t, partnames) | ||||
self._parts.update(m) | ||||
|
r43347 | if self._parts[b'docheader']: | ||
self.ui.write(self.t.render(self._parts[b'docheader'], {})) | ||||
|
r35903 | |||
def close(self): | ||||
|
r43347 | if self._parts[b'docfooter']: | ||
|
r35903 | if not self.footer: | ||
|
r43347 | self.footer = b"" | ||
self.footer += self.t.render(self._parts[b'docfooter'], {}) | ||||
|
r35904 | return super(changesettemplater, self).close() | ||
|
r35903 | |||
|
r36020 | def _show(self, ctx, copies, props): | ||
|
r35903 | '''show a single changeset or file revision''' | ||
props = props.copy() | ||||
|
r43347 | props[b'ctx'] = ctx | ||
props[b'index'] = index = next(self._counter) | ||||
props[b'revcache'] = {b'copies': copies} | ||||
graphwidth = props.get(b'graphwidth', 0) | ||||
|
r35903 | |||
# write separator, which wouldn't work well with the header part below | ||||
# since there's inherently a conflict between header (across items) and | ||||
# separator (per item) | ||||
|
r43347 | if self._parts[b'separator'] and index > 0: | ||
self.ui.write(self.t.render(self._parts[b'separator'], {})) | ||||
|
r35903 | |||
# write header | ||||
|
r43347 | if self._parts[b'header']: | ||
h = self.t.render(self._parts[b'header'], props) | ||||
|
r35903 | if self.buffered: | ||
self.header[ctx.rev()] = h | ||||
else: | ||||
if self.lastheader != h: | ||||
self.lastheader = h | ||||
self.ui.write(h) | ||||
# write changeset metadata, then patch if requested | ||||
key = self._parts[self._tref] | ||||
|
r37004 | self.ui.write(self.t.render(key, props)) | ||
|
r41129 | self._showpatch(ctx, graphwidth) | ||
|
r35903 | |||
|
r43347 | if self._parts[b'footer']: | ||
|
r35903 | if not self.footer: | ||
|
r43347 | self.footer = self.t.render(self._parts[b'footer'], props) | ||
|
r35903 | |||
|
r43346 | |||
|
r35905 | def templatespec(tmpl, mapfile): | ||
|
r40321 | if pycompat.ispy3: | ||
|
r43347 | assert not isinstance(tmpl, str), b'tmpl must not be a str' | ||
|
r35903 | if mapfile: | ||
|
r43347 | return formatter.templatespec(b'changeset', tmpl, mapfile) | ||
|
r35903 | else: | ||
|
r43347 | return formatter.templatespec(b'', tmpl, None) | ||
|
r35903 | |||
|
r43346 | |||
|
r35905 | def _lookuptemplate(ui, tmpl, style): | ||
|
r35903 | """Find the template matching the given template spec or style | ||
See formatter.lookuptemplate() for details. | ||||
""" | ||||
# ui settings | ||||
|
r43346 | if not tmpl and not style: # template are stronger than style | ||
|
r43347 | tmpl = ui.config(b'ui', b'logtemplate') | ||
|
r35903 | if tmpl: | ||
|
r35905 | return templatespec(templater.unquotestring(tmpl), None) | ||
|
r35903 | else: | ||
|
r43347 | style = util.expandpath(ui.config(b'ui', b'style')) | ||
|
r35903 | |||
if not tmpl and style: | ||||
mapfile = style | ||||
if not os.path.split(mapfile)[0]: | ||||
|
r43346 | mapname = templater.templatepath( | ||
|
r43347 | b'map-cmdline.' + mapfile | ||
|
r43346 | ) or templater.templatepath(mapfile) | ||
|
r35903 | if mapname: | ||
mapfile = mapname | ||||
|
r35905 | return templatespec(None, mapfile) | ||
|
r35903 | |||
|
r43347 | return formatter.lookuptemplate(ui, b'changeset', tmpl) | ||
|
r35903 | |||
|
r43346 | |||
|
r35905 | def maketemplater(ui, repo, tmpl, buffered=False): | ||
|
r35904 | """Create a changesettemplater from a literal template 'tmpl' | ||
|
r35903 | byte-string.""" | ||
|
r35905 | spec = templatespec(tmpl, None) | ||
|
r35904 | return changesettemplater(ui, repo, spec, buffered=buffered) | ||
|
r35903 | |||
|
r43346 | |||
|
r36024 | def changesetdisplayer(ui, repo, opts, differ=None, buffered=False): | ||
|
r35903 | """show one changeset using template or regular display. | ||
Display format will be the first non-empty hit of: | ||||
1. option 'template' | ||||
2. option 'style' | ||||
3. [ui] setting 'logtemplate' | ||||
4. [ui] setting 'style' | ||||
If all of these values are either the unset or the empty string, | ||||
|
r35904 | regular display via changesetprinter() is done. | ||
|
r35903 | """ | ||
|
r36024 | postargs = (differ, opts, buffered) | ||
|
r43370 | spec = _lookuptemplate(ui, opts.get(b'template'), opts.get(b'style')) | ||
# machine-readable formats have slightly different keyword set than | ||||
# plain templates, which are handled by changesetformatter. | ||||
# note that {b'pickle', b'debug'} can also be added to the list if needed. | ||||
if spec.ref in {b'cbor', b'json'}: | ||||
|
r43347 | fm = ui.formatter(b'log', opts) | ||
|
r37791 | return changesetformatter(ui, repo, fm, *postargs) | ||
|
r35903 | |||
if not spec.ref and not spec.tmpl and not spec.mapfile: | ||||
|
r36020 | return changesetprinter(ui, repo, *postargs) | ||
|
r35903 | |||
|
r36020 | return changesettemplater(ui, repo, spec, *postargs) | ||
|
r35903 | |||
|
r43346 | |||
|
r35905 | def _makematcher(repo, revs, pats, opts): | ||
|
r35903 | """Build matcher and expanded patterns from log options | ||
If --follow, revs are the revisions to follow from. | ||||
Returns (match, pats, slowpath) where | ||||
- match: a matcher built from the given pats and -I/-X opts | ||||
- pats: patterns used (globs are expanded on Windows) | ||||
- slowpath: True if patterns aren't as simple as scanning filelogs | ||||
""" | ||||
# pats/include/exclude are passed to match.match() directly in | ||||
# _matchfiles() revset but walkchangerevs() builds its matcher with | ||||
# scmutil.match(). The difference is input pats are globbed on | ||||
# platforms without shell expansion (windows). | ||||
wctx = repo[None] | ||||
match, pats = scmutil.matchandpats(wctx, pats, opts) | ||||
|
r43347 | slowpath = match.anypats() or (not match.always() and opts.get(b'removed')) | ||
|
r35903 | if not slowpath: | ||
|
r43347 | follow = opts.get(b'follow') or opts.get(b'follow_first') | ||
|
r35903 | startctxs = [] | ||
|
r43347 | if follow and opts.get(b'rev'): | ||
|
r35903 | startctxs = [repo[r] for r in revs] | ||
for f in match.files(): | ||||
if follow and startctxs: | ||||
# No idea if the path was a directory at that revision, so | ||||
# take the slow path. | ||||
if any(f not in c for c in startctxs): | ||||
slowpath = True | ||||
continue | ||||
elif follow and f not in wctx: | ||||
# If the file exists, it may be a directory, so let it | ||||
# take the slow path. | ||||
if os.path.exists(repo.wjoin(f)): | ||||
slowpath = True | ||||
continue | ||||
else: | ||||
|
r43346 | raise error.Abort( | ||
|
r43347 | _( | ||
b'cannot follow file not in parent ' | ||||
b'revision: "%s"' | ||||
) | ||||
|
r43346 | % f | ||
) | ||||
|
r35903 | filelog = repo.file(f) | ||
if not filelog: | ||||
# A zero count may be a directory or deleted file, so | ||||
# try to find matching entries on the slow path. | ||||
if follow: | ||||
raise error.Abort( | ||||
|
r43347 | _(b'cannot follow nonexistent file: "%s"') % f | ||
|
r43346 | ) | ||
|
r35903 | slowpath = True | ||
# We decided to fall back to the slowpath because at least one | ||||
# of the paths was not a file. Check to see if at least one of them | ||||
# existed in history - in that case, we'll continue down the | ||||
# slowpath; otherwise, we can turn off the slowpath | ||||
if slowpath: | ||||
for path in match.files(): | ||||
|
r43347 | if path == b'.' or path in repo.store: | ||
|
r35903 | break | ||
else: | ||||
slowpath = False | ||||
return match, pats, slowpath | ||||
|
r43346 | |||
|
r35903 | def _fileancestors(repo, revs, match, followfirst): | ||
fctxs = [] | ||||
for r in revs: | ||||
ctx = repo[r] | ||||
fctxs.extend(ctx[f].introfilectx() for f in ctx.walk(match)) | ||||
# When displaying a revision with --patch --follow FILE, we have | ||||
# to know which file of the revision must be diffed. With | ||||
# --follow, we want the names of the ancestors of FILE in the | ||||
# revision, stored in "fcache". "fcache" is populated as a side effect | ||||
# of the graph traversal. | ||||
fcache = {} | ||||
|
r43346 | |||
|
r36019 | def filematcher(ctx): | ||
return scmutil.matchfiles(repo, fcache.get(ctx.rev(), [])) | ||||
|
r35903 | |||
def revgen(): | ||||
for rev, cs in dagop.filectxancestors(fctxs, followfirst=followfirst): | ||||
fcache[rev] = [c.path() for c in cs] | ||||
yield rev | ||||
|
r43346 | |||
|
r35903 | return smartset.generatorset(revgen(), iterasc=False), filematcher | ||
|
r43346 | |||
|
r35905 | def _makenofollowfilematcher(repo, pats, opts): | ||
|
r35903 | '''hook for extensions to override the filematcher for non-follow cases''' | ||
return None | ||||
|
r43346 | |||
|
r35903 | _opt2logrevset = { | ||
|
r43347 | b'no_merges': (b'not merge()', None), | ||
b'only_merges': (b'merge()', None), | ||||
b'_matchfiles': (None, b'_matchfiles(%ps)'), | ||||
b'date': (b'date(%s)', None), | ||||
b'branch': (b'branch(%s)', b'%lr'), | ||||
b'_patslog': (b'filelog(%s)', b'%lr'), | ||||
b'keyword': (b'keyword(%s)', b'%lr'), | ||||
b'prune': (b'ancestors(%s)', b'not %lr'), | ||||
b'user': (b'user(%s)', b'%lr'), | ||||
|
r35903 | } | ||
|
r43346 | |||
|
r35905 | def _makerevset(repo, match, pats, slowpath, opts): | ||
|
r35903 | """Return a revset string built from log options and file patterns""" | ||
opts = dict(opts) | ||||
# follow or not follow? | ||||
|
r43347 | follow = opts.get(b'follow') or opts.get(b'follow_first') | ||
|
r35903 | |||
# branch and only_branch are really aliases and must be handled at | ||||
# the same time | ||||
|
r43347 | opts[b'branch'] = opts.get(b'branch', []) + opts.get(b'only_branch', []) | ||
opts[b'branch'] = [repo.lookupbranch(b) for b in opts[b'branch']] | ||||
|
r35903 | |||
if slowpath: | ||||
# See walkchangerevs() slow path. | ||||
# | ||||
# pats/include/exclude cannot be represented as separate | ||||
# revset expressions as their filtering logic applies at file | ||||
# level. For instance "-I a -X b" matches a revision touching | ||||
# "a" and "b" while "file(a) and not file(b)" does | ||||
# not. Besides, filesets are evaluated against the working | ||||
# directory. | ||||
|
r43347 | matchargs = [b'r:', b'd:relpath'] | ||
|
r35903 | for p in pats: | ||
|
r43347 | matchargs.append(b'p:' + p) | ||
for p in opts.get(b'include', []): | ||||
matchargs.append(b'i:' + p) | ||||
for p in opts.get(b'exclude', []): | ||||
matchargs.append(b'x:' + p) | ||||
opts[b'_matchfiles'] = matchargs | ||||
|
r35903 | elif not follow: | ||
|
r43347 | opts[b'_patslog'] = list(pats) | ||
|
r35903 | |||
expr = [] | ||||
|
r43376 | for op, val in sorted(pycompat.iteritems(opts)): | ||
|
r35903 | if not val: | ||
continue | ||||
if op not in _opt2logrevset: | ||||
continue | ||||
revop, listop = _opt2logrevset[op] | ||||
|
r43347 | if revop and b'%' not in revop: | ||
|
r35903 | expr.append(revop) | ||
elif not listop: | ||||
expr.append(revsetlang.formatspec(revop, val)) | ||||
else: | ||||
if revop: | ||||
val = [revsetlang.formatspec(revop, v) for v in val] | ||||
expr.append(revsetlang.formatspec(listop, val)) | ||||
if expr: | ||||
|
r43347 | expr = b'(' + b' and '.join(expr) + b')' | ||
|
r35903 | else: | ||
expr = None | ||||
return expr | ||||
|
r43346 | |||
|
r35905 | def _initialrevs(repo, opts): | ||
|
r35903 | """Return the initial set of revisions to be filtered or followed""" | ||
|
r43347 | follow = opts.get(b'follow') or opts.get(b'follow_first') | ||
if opts.get(b'rev'): | ||||
revs = scmutil.revrange(repo, opts[b'rev']) | ||||
|
r35903 | elif follow and repo.dirstate.p1() == nullid: | ||
revs = smartset.baseset() | ||||
elif follow: | ||||
|
r43347 | revs = repo.revs(b'.') | ||
|
r35903 | else: | ||
revs = smartset.spanset(repo) | ||||
revs.reverse() | ||||
return revs | ||||
|
r43346 | |||
|
r35905 | def getrevs(repo, pats, opts): | ||
|
r44163 | # type: (Any, Any, Any) -> Tuple[smartset.abstractsmartset, Optional[changesetdiffer]] | ||
|
r36024 | """Return (revs, differ) where revs is a smartset | ||
|
r35903 | |||
|
r36024 | differ is a changesetdiffer with pre-configured file matcher. | ||
|
r35903 | """ | ||
|
r43347 | follow = opts.get(b'follow') or opts.get(b'follow_first') | ||
followfirst = opts.get(b'follow_first') | ||||
|
r35905 | limit = getlimit(opts) | ||
revs = _initialrevs(repo, opts) | ||||
|
r35903 | if not revs: | ||
return smartset.baseset(), None | ||||
|
r35905 | match, pats, slowpath = _makematcher(repo, revs, pats, opts) | ||
|
r35903 | filematcher = None | ||
if follow: | ||||
if slowpath or match.always(): | ||||
revs = dagop.revancestors(repo, revs, followfirst=followfirst) | ||||
else: | ||||
revs, filematcher = _fileancestors(repo, revs, match, followfirst) | ||||
revs.reverse() | ||||
if filematcher is None: | ||||
|
r35905 | filematcher = _makenofollowfilematcher(repo, pats, opts) | ||
|
r35903 | if filematcher is None: | ||
|
r43346 | |||
|
r36019 | def filematcher(ctx): | ||
|
r35903 | return match | ||
|
r35905 | expr = _makerevset(repo, match, pats, slowpath, opts) | ||
|
r43347 | if opts.get(b'graph'): | ||
|
r35903 | # User-specified revs might be unsorted, but don't sort before | ||
|
r35905 | # _makerevset because it might depend on the order of revs | ||
|
r43347 | if repo.ui.configbool(b'experimental', b'log.topo'): | ||
|
r42500 | if not revs.istopo(): | ||
revs = dagop.toposort(revs, repo.changelog.parentrevs) | ||||
# TODO: try to iterate the set lazily | ||||
|
r42510 | revs = revset.baseset(list(revs), istopo=True) | ||
|
r42500 | elif not (revs.isdescending() or revs.istopo()): | ||
|
r35903 | revs.sort(reverse=True) | ||
if expr: | ||||
matcher = revset.match(None, expr) | ||||
revs = matcher(repo, revs) | ||||
if limit is not None: | ||||
revs = revs.slice(0, limit) | ||||
|
r36024 | |||
differ = changesetdiffer() | ||||
differ._makefilematcher = filematcher | ||||
return revs, differ | ||||
|
r35903 | |||
|
r43346 | |||
|
r35905 | def _parselinerangeopt(repo, opts): | ||
|
r35903 | """Parse --line-range log option and return a list of tuples (filename, | ||
(fromline, toline)). | ||||
""" | ||||
linerangebyfname = [] | ||||
|
r43347 | for pat in opts.get(b'line_range', []): | ||
|
r35903 | try: | ||
|
r43347 | pat, linerange = pat.rsplit(b',', 1) | ||
|
r35903 | except ValueError: | ||
|
r43347 | raise error.Abort(_(b'malformatted line-range pattern %s') % pat) | ||
|
r35903 | try: | ||
|
r43347 | fromline, toline = map(int, linerange.split(b':')) | ||
|
r35903 | except ValueError: | ||
|
r43347 | raise error.Abort(_(b"invalid line range for %s") % pat) | ||
msg = _(b"line range pattern '%s' must match exactly one file") % pat | ||||
|
r35903 | fname = scmutil.parsefollowlinespattern(repo, None, pat, msg) | ||
linerangebyfname.append( | ||||
|
r43346 | (fname, util.processlinerange(fromline, toline)) | ||
) | ||||
|
r35903 | return linerangebyfname | ||
|
r43346 | |||
|
r35905 | def getlinerangerevs(repo, userrevs, opts): | ||
|
r36024 | """Return (revs, differ). | ||
|
r35903 | |||
"revs" are revisions obtained by processing "line-range" log options and | ||||
walking block ancestors of each specified file/line-range. | ||||
|
r36024 | "differ" is a changesetdiffer with pre-configured file matcher and hunks | ||
filter. | ||||
|
r35903 | """ | ||
wctx = repo[None] | ||||
# Two-levels map of "rev -> file ctx -> [line range]". | ||||
linerangesbyrev = {} | ||||
|
r35905 | for fname, (fromline, toline) in _parselinerangeopt(repo, opts): | ||
|
r35903 | if fname not in wctx: | ||
|
r43346 | raise error.Abort( | ||
|
r43387 | _(b'cannot follow file not in parent revision: "%s"') % fname | ||
|
r43346 | ) | ||
|
r35903 | fctx = wctx.filectx(fname) | ||
for fctx, linerange in dagop.blockancestors(fctx, fromline, toline): | ||||
rev = fctx.introrev() | ||||
if rev not in userrevs: | ||||
continue | ||||
|
r43346 | linerangesbyrev.setdefault(rev, {}).setdefault( | ||
fctx.path(), [] | ||||
).append(linerange) | ||||
|
r35903 | |||
|
r36022 | def nofilterhunksfn(fctx, hunks): | ||
return hunks | ||||
|
r35903 | |||
|
r36022 | def hunksfilter(ctx): | ||
fctxlineranges = linerangesbyrev.get(ctx.rev()) | ||||
if fctxlineranges is None: | ||||
return nofilterhunksfn | ||||
|
r35903 | |||
|
r36022 | def filterfn(fctx, hunks): | ||
lineranges = fctxlineranges.get(fctx.path()) | ||||
if lineranges is not None: | ||||
for hr, lines in hunks: | ||||
|
r43346 | if hr is None: # binary | ||
|
r36022 | yield hr, lines | ||
continue | ||||
|
r43346 | if any(mdiff.hunkinrange(hr[2:], lr) for lr in lineranges): | ||
|
r36022 | yield hr, lines | ||
else: | ||||
for hunk in hunks: | ||||
yield hunk | ||||
|
r35903 | |||
|
r36022 | return filterfn | ||
|
r35903 | |||
|
r36022 | def filematcher(ctx): | ||
files = list(linerangesbyrev.get(ctx.rev(), [])) | ||||
return scmutil.matchfiles(repo, files) | ||||
|
r35903 | |||
revs = sorted(linerangesbyrev, reverse=True) | ||||
|
r36024 | differ = changesetdiffer() | ||
differ._makefilematcher = filematcher | ||||
differ._makehunksfilter = hunksfilter | ||||
|
r44029 | return smartset.baseset(revs), differ | ||
|
r35903 | |||
|
r43346 | |||
|
r35903 | def _graphnodeformatter(ui, displayer): | ||
|
r43347 | spec = ui.config(b'ui', b'graphnodetemplate') | ||
|
r35903 | if not spec: | ||
|
r36530 | return templatekw.getgraphnode # fast path for "{graphnode}" | ||
|
r35903 | |||
spec = templater.unquotestring(spec) | ||||
|
r35904 | if isinstance(displayer, changesettemplater): | ||
|
r36997 | # reuse cache of slow templates | ||
|
r37090 | tres = displayer._tresources | ||
else: | ||||
tres = formatter.templateresources(ui) | ||||
|
r43346 | templ = formatter.maketemplater( | ||
ui, spec, defaults=templatekw.keywords, resources=tres | ||||
) | ||||
|
r35903 | def formatnode(repo, ctx): | ||
|
r43347 | props = {b'ctx': ctx, b'repo': repo} | ||
|
r37003 | return templ.renderdefault(props) | ||
|
r43346 | |||
|
r35903 | return formatnode | ||
|
r43346 | |||
|
r42704 | def displaygraph(ui, repo, dag, displayer, edgefn, getcopies=None, props=None): | ||
|
r35903 | props = props or {} | ||
formatnode = _graphnodeformatter(ui, displayer) | ||||
state = graphmod.asciistate() | ||||
|
r44165 | styles = state.styles | ||
|
r35903 | |||
# only set graph styling if HGPLAIN is not set. | ||||
|
r43347 | if ui.plain(b'graph'): | ||
|
r35903 | # set all edge styles to |, the default pre-3.8 behaviour | ||
|
r43347 | styles.update(dict.fromkeys(styles, b'|')) | ||
|
r35903 | else: | ||
edgetypes = { | ||||
|
r43347 | b'parent': graphmod.PARENT, | ||
b'grandparent': graphmod.GRANDPARENT, | ||||
b'missing': graphmod.MISSINGPARENT, | ||||
|
r35903 | } | ||
for name, key in edgetypes.items(): | ||||
# experimental config: experimental.graphstyle.* | ||||
|
r43346 | styles[key] = ui.config( | ||
|
r43347 | b'experimental', b'graphstyle.%s' % name, styles[key] | ||
|
r43346 | ) | ||
|
r35903 | if not styles[key]: | ||
styles[key] = None | ||||
# experimental config: experimental.graphshorten | ||||
|
r44165 | state.graphshorten = ui.configbool(b'experimental', b'graphshorten') | ||
|
r35903 | |||
for rev, type, ctx, parents in dag: | ||||
char = formatnode(repo, ctx) | ||||
|
r42704 | copies = getcopies(ctx) if getcopies else None | ||
|
r35903 | edges = edgefn(type, char, state, rev, parents) | ||
firstedge = next(edges) | ||||
width = firstedge[2] | ||||
|
r43346 | displayer.show( | ||
ctx, copies=copies, graphwidth=width, **pycompat.strkwargs(props) | ||||
) | ||||
|
r43347 | lines = displayer.hunk.pop(rev).split(b'\n') | ||
|
r35903 | if not lines[-1]: | ||
del lines[-1] | ||||
displayer.flush(ctx) | ||||
for type, char, width, coldata in itertools.chain([firstedge], edges): | ||||
graphmod.ascii(ui, state, type, char, lines, coldata) | ||||
lines = [] | ||||
displayer.close() | ||||
|
r43346 | |||
|
r36216 | def displaygraphrevs(ui, repo, revs, displayer, getrenamed): | ||
|
r35903 | revdag = graphmod.dagwalker(repo, revs) | ||
|
r36020 | displaygraph(ui, repo, revdag, displayer, graphmod.asciiedges, getrenamed) | ||
|
r35903 | |||
|
r43346 | |||
|
r42704 | def displayrevs(ui, repo, revs, displayer, getcopies): | ||
|
r36216 | for rev in revs: | ||
ctx = repo[rev] | ||||
|
r42704 | copies = getcopies(ctx) if getcopies else None | ||
|
r36216 | displayer.show(ctx, copies=copies) | ||
displayer.flush(ctx) | ||||
displayer.close() | ||||
|
r43346 | |||
|
r35903 | def checkunsupportedgraphflags(pats, opts): | ||
|
r43347 | for op in [b"newest_first"]: | ||
|
r35903 | if op in opts and opts[op]: | ||
|
r43346 | raise error.Abort( | ||
|
r43347 | _(b"-G/--graph option is incompatible with --%s") | ||
% op.replace(b"_", b"-") | ||||
|
r43346 | ) | ||
|
r35903 | |||
def graphrevs(repo, nodes, opts): | ||||
|
r35905 | limit = getlimit(opts) | ||
|
r35903 | nodes.reverse() | ||
if limit is not None: | ||||
nodes = nodes[:limit] | ||||
return graphmod.nodes(repo, nodes) | ||||