phabricator.py
2401 lines
| 80.6 KiB
| text/x-python
|
PythonLexer
/ hgext / phabricator.py
|
r39688 | # phabricator.py - simple Phabricator integration | ||
# | ||||
# Copyright 2017 Facebook, Inc. | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
|
r39691 | """simple Phabricator integration (EXPERIMENTAL) | ||
|
r39688 | |||
This extension provides a ``phabsend`` command which sends a stack of | ||||
changesets to Phabricator, and a ``phabread`` command which prints a stack of | ||||
revisions in a format suitable for :hg:`import`, and a ``phabupdate`` command | ||||
to update statuses in batch. | ||||
|
r44291 | A "phabstatus" view for :hg:`show` is also provided; it displays status | ||
information of Phabricator differentials associated with unfinished | ||||
changesets. | ||||
|
r39688 | By default, Phabricator requires ``Test Plan`` which might prevent some | ||
changeset from being sent. The requirement could be disabled by changing | ||||
``differential.require-test-plan-field`` config server side. | ||||
Config:: | ||||
[phabricator] | ||||
# Phabricator URL | ||||
url = https://phab.example.com/ | ||||
# Repo callsign. If a repo has a URL https://$HOST/diffusion/FOO, then its | ||||
# callsign is "FOO". | ||||
callsign = FOO | ||||
# curl command to use. If not set (default), use builtin HTTP library to | ||||
# communicate. If set, use the specified curl command. This could be useful | ||||
# if you need to specify advanced options that is not easily supported by | ||||
# the internal library. | ||||
curlcmd = curl --connect-timeout 2 --retry 3 --silent | ||||
r46583 | # retry failed command N time (default 0). Useful when using the extension | |||
# over flakly connection. | ||||
# | ||||
# We wait `retry.interval` between each retry, in seconds. | ||||
# (default 1 second). | ||||
retry = 3 | ||||
retry.interval = 10 | ||||
r46584 | # the retry option can combine well with the http.timeout one. | |||
# | ||||
# For example to give up on http request after 20 seconds: | ||||
[http] | ||||
timeout=20 | ||||
|
r39688 | [auth] | ||
example.schemes = https | ||||
example.prefix = phab.example.com | ||||
# API token. Get it from https://$HOST/conduit/login/ | ||||
example.phabtoken = cli-xxxxxxxxxxxxxxxxxxxxxxxxxxxx | ||||
""" | ||||
from __future__ import absolute_import | ||||
|
r43457 | import base64 | ||
|
r41111 | import contextlib | ||
|
r43458 | import hashlib | ||
|
r39688 | import itertools | ||
import json | ||||
|
r43459 | import mimetypes | ||
|
r39688 | import operator | ||
import re | ||||
r46583 | import time | |||
|
r39688 | |||
|
r47771 | from mercurial.node import bin, short | ||
|
r39688 | from mercurial.i18n import _ | ||
|
r43359 | from mercurial.pycompat import getattr | ||
|
r43453 | from mercurial.thirdparty import attr | ||
|
r39688 | from mercurial import ( | ||
cmdutil, | ||||
context, | ||||
|
r45101 | copies, | ||
|
r39688 | encoding, | ||
error, | ||||
|
r43243 | exthelper, | ||
|
r44291 | graphmod, | ||
|
r39688 | httpconnection as httpconnectionmod, | ||
|
r44586 | localrepo, | ||
|
r44291 | logcmdutil, | ||
|
r43456 | match, | ||
|
r39688 | mdiff, | ||
obsutil, | ||||
parser, | ||||
patch, | ||||
|
r41198 | phases, | ||
|
r42063 | pycompat, | ||
|
r45995 | rewriteutil, | ||
|
r39688 | scmutil, | ||
smartset, | ||||
tags, | ||||
|
r42369 | templatefilters, | ||
|
r39690 | templateutil, | ||
|
r39688 | url as urlmod, | ||
util, | ||||
) | ||||
from mercurial.utils import ( | ||||
procutil, | ||||
stringutil, | ||||
r47669 | urlutil, | |||
|
r39688 | ) | ||
|
r44291 | from . import show | ||
|
r39688 | |||
|
r39771 | # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for | ||
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should | ||||
# be specifying the version(s) of Mercurial they are tested with, or | ||||
# leave the attribute unspecified. | ||||
|
r43347 | testedwith = b'ships-with-hg-core' | ||
|
r39771 | |||
|
r43243 | eh = exthelper.exthelper() | ||
|
r39688 | |||
|
r43243 | cmdtable = eh.cmdtable | ||
command = eh.command | ||||
configtable = eh.configtable | ||||
templatekeyword = eh.templatekeyword | ||||
|
r44586 | uisetup = eh.finaluisetup | ||
|
r39688 | |||
# developer config: phabricator.batchsize | ||||
|
r43346 | eh.configitem( | ||
|
r46554 | b'phabricator', | ||
b'batchsize', | ||||
default=12, | ||||
|
r39688 | ) | ||
|
r43346 | eh.configitem( | ||
|
r46554 | b'phabricator', | ||
b'callsign', | ||||
default=None, | ||||
|
r39688 | ) | ||
|
r43346 | eh.configitem( | ||
|
r46554 | b'phabricator', | ||
b'curlcmd', | ||||
default=None, | ||||
|
r39688 | ) | ||
|
r45209 | # developer config: phabricator.debug | ||
eh.configitem( | ||||
|
r46554 | b'phabricator', | ||
b'debug', | ||||
default=False, | ||||
|
r45209 | ) | ||
|
r39688 | # developer config: phabricator.repophid | ||
|
r43346 | eh.configitem( | ||
|
r46554 | b'phabricator', | ||
b'repophid', | ||||
default=None, | ||||
|
r39688 | ) | ||
|
r43346 | eh.configitem( | ||
|
r46554 | b'phabricator', | ||
r46583 | b'retry', | |||
default=0, | ||||
) | ||||
eh.configitem( | ||||
b'phabricator', | ||||
b'retry.interval', | ||||
default=1, | ||||
) | ||||
eh.configitem( | ||||
b'phabricator', | ||||
|
r46554 | b'url', | ||
default=None, | ||||
|
r39688 | ) | ||
|
r43346 | eh.configitem( | ||
|
r46554 | b'phabsend', | ||
b'confirm', | ||||
default=False, | ||||
|
r39688 | ) | ||
|
r45040 | eh.configitem( | ||
|
r46554 | b'phabimport', | ||
b'secret', | ||||
default=False, | ||||
|
r45040 | ) | ||
|
r45041 | eh.configitem( | ||
|
r46554 | b'phabimport', | ||
b'obsolete', | ||||
default=False, | ||||
|
r45041 | ) | ||
|
r39688 | |||
colortable = { | ||||
b'phabricator.action.created': b'green', | ||||
b'phabricator.action.skipped': b'magenta', | ||||
b'phabricator.action.updated': b'magenta', | ||||
b'phabricator.drev': b'bold', | ||||
|
r44310 | b'phabricator.status.abandoned': b'magenta dim', | ||
b'phabricator.status.accepted': b'green bold', | ||||
b'phabricator.status.closed': b'green', | ||||
b'phabricator.status.needsreview': b'yellow', | ||||
b'phabricator.status.needsrevision': b'red', | ||||
b'phabricator.status.changesplanned': b'red', | ||||
|
r39688 | } | ||
_VCR_FLAGS = [ | ||||
|
r43346 | ( | ||
b'', | ||||
b'test-vcr', | ||||
b'', | ||||
_( | ||||
b'Path to a vcr file. If nonexistent, will record a new vcr transcript' | ||||
b', otherwise will mock all http requests using the specified vcr file.' | ||||
b' (ADVANCED)' | ||||
), | ||||
), | ||||
|
r39688 | ] | ||
|
r43346 | |||
|
r44586 | @eh.wrapfunction(localrepo, "loadhgrc") | ||
|
r46074 | def _loadhgrc(orig, ui, wdirvfs, hgvfs, requirements, *args, **opts): | ||
|
r46554 | """Load ``.arcconfig`` content into a ui instance on repository open.""" | ||
|
r44586 | result = False | ||
arcconfig = {} | ||||
try: | ||||
# json.loads only accepts bytes from 3.6+ | ||||
rawparams = encoding.unifromlocal(wdirvfs.read(b".arcconfig")) | ||||
# json.loads only returns unicode strings | ||||
arcconfig = pycompat.rapply( | ||||
lambda x: encoding.unitolocal(x) | ||||
if isinstance(x, pycompat.unicode) | ||||
else x, | ||||
pycompat.json_loads(rawparams), | ||||
) | ||||
result = True | ||||
except ValueError: | ||||
ui.warn(_(b"invalid JSON in %s\n") % wdirvfs.join(b".arcconfig")) | ||||
except IOError: | ||||
pass | ||||
|
r44587 | cfg = util.sortdict() | ||
|
r44586 | if b"repository.callsign" in arcconfig: | ||
|
r44587 | cfg[(b"phabricator", b"callsign")] = arcconfig[b"repository.callsign"] | ||
if b"phabricator.uri" in arcconfig: | ||||
cfg[(b"phabricator", b"url")] = arcconfig[b"phabricator.uri"] | ||||
if cfg: | ||||
ui.applyconfig(cfg, source=wdirvfs.join(b".arcconfig")) | ||||
|
r44586 | |||
|
r46057 | return ( | ||
|
r46074 | orig(ui, wdirvfs, hgvfs, requirements, *args, **opts) or result | ||
|
r46057 | ) # Load .hg/hgrc | ||
|
r44586 | |||
|
r42628 | def vcrcommand(name, flags, spec, helpcategory=None, optionalrepo=False): | ||
|
r39688 | fullflags = flags + _VCR_FLAGS | ||
|
r43346 | |||
|
r42454 | def hgmatcher(r1, r2): | ||
if r1.uri != r2.uri or r1.method != r2.method: | ||||
return False | ||||
|
r43558 | r1params = util.urlreq.parseqs(r1.body) | ||
r2params = util.urlreq.parseqs(r2.body) | ||||
for key in r1params: | ||||
if key not in r2params: | ||||
return False | ||||
value = r1params[key][0] | ||||
# we want to compare json payloads without worrying about ordering | ||||
if value.startswith(b'{') and value.endswith(b'}'): | ||||
|
r43697 | r1json = pycompat.json_loads(value) | ||
r2json = pycompat.json_loads(r2params[key][0]) | ||||
|
r43558 | if r1json != r2json: | ||
return False | ||||
elif r2params[key][0] != value: | ||||
return False | ||||
return True | ||||
|
r42454 | |||
|
r42636 | def sanitiserequest(request): | ||
request.body = re.sub( | ||||
|
r43346 | br'cli-[a-z0-9]+', br'cli-hahayouwish', request.body | ||
|
r42636 | ) | ||
return request | ||||
def sanitiseresponse(response): | ||||
|
r43906 | if 'set-cookie' in response['headers']: | ||
del response['headers']['set-cookie'] | ||||
|
r42636 | return response | ||
|
r39688 | def decorate(fn): | ||
def inner(*args, **kwargs): | ||||
|
r45806 | vcr = kwargs.pop('test_vcr') | ||
if vcr: | ||||
cassette = pycompat.fsdecode(vcr) | ||||
|
r39688 | import hgdemandimport | ||
|
r43346 | |||
|
r39688 | with hgdemandimport.deactivated(): | ||
import vcr as vcrmod | ||||
import vcr.stubs as stubs | ||||
|
r43346 | |||
|
r40414 | vcr = vcrmod.VCR( | ||
|
r43906 | serializer='json', | ||
|
r42636 | before_record_request=sanitiserequest, | ||
before_record_response=sanitiseresponse, | ||||
|
r40414 | custom_patches=[ | ||
|
r43346 | ( | ||
urlmod, | ||||
|
r43906 | 'httpconnection', | ||
|
r43346 | stubs.VCRHTTPConnection, | ||
), | ||||
( | ||||
urlmod, | ||||
|
r43906 | 'httpsconnection', | ||
|
r43346 | stubs.VCRHTTPSConnection, | ||
), | ||||
], | ||||
) | ||||
|
r43906 | vcr.register_matcher('hgmatcher', hgmatcher) | ||
with vcr.use_cassette(cassette, match_on=['hgmatcher']): | ||||
|
r40414 | return fn(*args, **kwargs) | ||
|
r39688 | return fn(*args, **kwargs) | ||
|
r43346 | |||
|
r44918 | cmd = util.checksignature(inner, depth=2) | ||
cmd.__name__ = fn.__name__ | ||||
cmd.__doc__ = fn.__doc__ | ||||
|
r43346 | return command( | ||
name, | ||||
fullflags, | ||||
spec, | ||||
helpcategory=helpcategory, | ||||
optionalrepo=optionalrepo, | ||||
|
r44918 | )(cmd) | ||
|
r43346 | |||
|
r39688 | return decorate | ||
|
r43346 | |||
|
r45209 | def _debug(ui, *msg, **opts): | ||
"""write debug output for Phabricator if ``phabricator.debug`` is set | ||||
Specifically, this avoids dumping Conduit and HTTP auth chatter that is | ||||
printed with the --debug argument. | ||||
""" | ||||
if ui.configbool(b"phabricator", b"debug"): | ||||
flag = ui.debugflag | ||||
try: | ||||
ui.debugflag = True | ||||
ui.write(*msg, **opts) | ||||
finally: | ||||
ui.debugflag = flag | ||||
|
r39688 | def urlencodenested(params): | ||
"""like urlencode, but works with nested parameters. | ||||
For example, if params is {'a': ['b', 'c'], 'd': {'e': 'f'}}, it will be | ||||
flattened to {'a[0]': 'b', 'a[1]': 'c', 'd[e]': 'f'} and then passed to | ||||
urlencode. Note: the encoding is consistent with PHP's http_build_query. | ||||
""" | ||||
flatparams = util.sortdict() | ||||
|
r43346 | |||
|
r39688 | def process(prefix, obj): | ||
|
r41073 | if isinstance(obj, bool): | ||
obj = {True: b'true', False: b'false'}[obj] # Python -> PHP form | ||||
|
r42066 | lister = lambda l: [(b'%d' % k, v) for k, v in enumerate(l)] | ||
items = {list: lister, dict: lambda x: x.items()}.get(type(obj)) | ||||
|
r39688 | if items is None: | ||
flatparams[prefix] = obj | ||||
else: | ||||
for k, v in items(obj): | ||||
if prefix: | ||||
process(b'%s[%s]' % (prefix, k), v) | ||||
else: | ||||
process(k, v) | ||||
|
r43346 | |||
|
r39688 | process(b'', params) | ||
r47669 | return urlutil.urlreq.urlencode(flatparams) | |||
|
r39688 | |||
|
r43346 | |||
|
r42626 | def readurltoken(ui): | ||
|
r39688 | """return conduit url, token and make sure they exist | ||
Currently read from [auth] config section. In the future, it might | ||||
make sense to read from .arcconfig and .arcrc as well. | ||||
""" | ||||
|
r42626 | url = ui.config(b'phabricator', b'url') | ||
|
r39688 | if not url: | ||
|
r43346 | raise error.Abort( | ||
_(b'config %s.%s is required') % (b'phabricator', b'url') | ||||
) | ||||
|
r39688 | |||
r47669 | res = httpconnectionmod.readauthforuri(ui, url, urlutil.url(url).user) | |||
|
r39688 | token = None | ||
if res: | ||||
group, auth = res | ||||
|
r42626 | ui.debug(b"using auth.%s.* for authentication\n" % group) | ||
|
r39688 | |||
token = auth.get(b'phabtoken') | ||||
if not token: | ||||
|
r43346 | raise error.Abort( | ||
_(b'Can\'t find conduit token associated to %s') % (url,) | ||||
) | ||||
|
r39688 | |||
return url, token | ||||
|
r43346 | |||
|
r42627 | def callconduit(ui, name, params): | ||
|
r39688 | """call Conduit API, params is a dict. return json.loads result, or None""" | ||
|
r42627 | host, token = readurltoken(ui) | ||
|
r47830 | url, authinfo = urlutil.url(b'/'.join([host, b'api', name])).authinfo() | ||
|
r42627 | ui.debug(b'Conduit Call: %s %s\n' % (url, pycompat.byterepr(params))) | ||
|
r39688 | params = params.copy() | ||
|
r43555 | params[b'__conduit__'] = { | ||
b'token': token, | ||||
} | ||||
rawdata = { | ||||
b'params': templatefilters.json(params), | ||||
b'output': b'json', | ||||
b'__conduit__': 1, | ||||
} | ||||
data = urlencodenested(rawdata) | ||||
|
r42627 | curlcmd = ui.config(b'phabricator', b'curlcmd') | ||
|
r39688 | if curlcmd: | ||
|
r43346 | sin, sout = procutil.popen2( | ||
b'%s -d @- %s' % (curlcmd, procutil.shellquote(url)) | ||||
) | ||||
|
r39688 | sin.write(data) | ||
sin.close() | ||||
body = sout.read() | ||||
else: | ||||
|
r42627 | urlopener = urlmod.opener(ui, authinfo) | ||
|
r42067 | request = util.urlreq.request(pycompat.strurl(url), data=data) | ||
r46583 | max_try = ui.configint(b'phabricator', b'retry') + 1 | |||
r46584 | timeout = ui.configwith(float, b'http', b'timeout') | |||
r46583 | for try_count in range(max_try): | |||
try: | ||||
r46584 | with contextlib.closing( | |||
urlopener.open(request, timeout=timeout) | ||||
) as rsp: | ||||
r46583 | body = rsp.read() | |||
break | ||||
except util.urlerr.urlerror as err: | ||||
if try_count == max_try - 1: | ||||
raise | ||||
ui.debug( | ||||
b'Conduit Request failed (try %d/%d): %r\n' | ||||
% (try_count + 1, max_try, err) | ||||
) | ||||
# failing request might come from overloaded server | ||||
retry_interval = ui.configint(b'phabricator', b'retry.interval') | ||||
time.sleep(retry_interval) | ||||
|
r42627 | ui.debug(b'Conduit Response: %s\n' % body) | ||
|
r42063 | parsed = pycompat.rapply( | ||
|
r43346 | lambda x: encoding.unitolocal(x) | ||
if isinstance(x, pycompat.unicode) | ||||
|
r42063 | else x, | ||
|
r43317 | # json.loads only accepts bytes from py3.6+ | ||
|
r43697 | pycompat.json_loads(encoding.unifromlocal(body)), | ||
|
r42063 | ) | ||
if parsed.get(b'error_code'): | ||||
|
r43346 | msg = _(b'Conduit Error (%s): %s') % ( | ||
parsed[b'error_code'], | ||||
parsed[b'error_info'], | ||||
) | ||||
|
r39688 | raise error.Abort(msg) | ||
|
r42063 | return parsed[b'result'] | ||
|
r39688 | |||
|
r43346 | |||
|
r42628 | @vcrcommand(b'debugcallconduit', [], _(b'METHOD'), optionalrepo=True) | ||
|
r39688 | def debugcallconduit(ui, repo, name): | ||
"""call Conduit API | ||||
Call parameters are read from stdin as a JSON blob. Result will be written | ||||
to stdout as a JSON blob. | ||||
""" | ||||
|
r42137 | # json.loads only accepts bytes from 3.6+ | ||
rawparams = encoding.unifromlocal(ui.fin.read()) | ||||
# json.loads only returns unicode strings | ||||
|
r43346 | params = pycompat.rapply( | ||
lambda x: encoding.unitolocal(x) | ||||
if isinstance(x, pycompat.unicode) | ||||
else x, | ||||
|
r43697 | pycompat.json_loads(rawparams), | ||
|
r42137 | ) | ||
# json.dumps only accepts unicode strings | ||||
|
r43346 | result = pycompat.rapply( | ||
lambda x: encoding.unifromlocal(x) if isinstance(x, bytes) else x, | ||||
callconduit(ui, name, params), | ||||
|
r42137 | ) | ||
s = json.dumps(result, sort_keys=True, indent=2, separators=(u',', u': ')) | ||||
ui.write(b'%s\n' % encoding.unitolocal(s)) | ||||
|
r39688 | |||
|
r43346 | |||
|
r39688 | def getrepophid(repo): | ||
"""given callsign, return repository PHID or None""" | ||||
# developer config: phabricator.repophid | ||||
repophid = repo.ui.config(b'phabricator', b'repophid') | ||||
if repophid: | ||||
return repophid | ||||
callsign = repo.ui.config(b'phabricator', b'callsign') | ||||
if not callsign: | ||||
return None | ||||
|
r43346 | query = callconduit( | ||
repo.ui, | ||||
b'diffusion.repository.search', | ||||
{b'constraints': {b'callsigns': [callsign]}}, | ||||
) | ||||
|
r42063 | if len(query[b'data']) == 0: | ||
|
r39688 | return None | ||
|
r42063 | repophid = query[b'data'][0][b'phid'] | ||
|
r39688 | repo.ui.setconfig(b'phabricator', b'repophid', repophid) | ||
return repophid | ||||
|
r43346 | |||
|
r41673 | _differentialrevisiontagre = re.compile(br'\AD([1-9][0-9]*)\Z') | ||
|
r39688 | _differentialrevisiondescre = re.compile( | ||
|
r43346 | br'^Differential Revision:\s*(?P<url>(?:.*)D(?P<id>[1-9][0-9]*))$', re.M | ||
) | ||||
|
r39688 | |||
def getoldnodedrevmap(repo, nodelist): | ||||
"""find previous nodes that has been sent to Phabricator | ||||
return {node: (oldnode, Differential diff, Differential Revision ID)} | ||||
for node in nodelist with known previous sent versions, or associated | ||||
Differential Revision IDs. ``oldnode`` and ``Differential diff`` could | ||||
be ``None``. | ||||
Examines commit messages like "Differential Revision:" to get the | ||||
association information. | ||||
If such commit message line is not found, examines all precursors and their | ||||
tags. Tags with format like "D1234" are considered a match and the node | ||||
with that tag, and the number after "D" (ex. 1234) will be returned. | ||||
The ``old node``, if not None, is guaranteed to be the last diff of | ||||
corresponding Differential Revision, and exist in the repo. | ||||
""" | ||||
unfi = repo.unfiltered() | ||||
r43949 | has_node = unfi.changelog.index.has_node | |||
|
r39688 | |||
|
r43346 | result = {} # {node: (oldnode?, lastdiff?, drev)} | ||
|
r45209 | # ordered for test stability when printing new -> old mapping below | ||
toconfirm = util.sortdict() # {node: (force, {precnode}, drev)} | ||||
|
r39688 | for node in nodelist: | ||
ctx = unfi[node] | ||||
# For tags like "D123", put them into "toconfirm" to verify later | ||||
precnodes = list(obsutil.allpredecessors(unfi.obsstore, [node])) | ||||
for n in precnodes: | ||||
r43949 | if has_node(n): | |||
|
r39688 | for tag in unfi.nodetags(n): | ||
m = _differentialrevisiontagre.match(tag) | ||||
if m: | ||||
toconfirm[node] = (0, set(precnodes), int(m.group(1))) | ||||
|
r44281 | break | ||
else: | ||||
continue # move to next predecessor | ||||
break # found a tag, stop | ||||
else: | ||||
# Check commit message | ||||
m = _differentialrevisiondescre.search(ctx.description()) | ||||
if m: | ||||
toconfirm[node] = (1, set(precnodes), int(m.group('id'))) | ||||
|
r39688 | |||
# Double check if tags are genuine by collecting all old nodes from | ||||
# Phabricator, and expect precursors overlap with it. | ||||
if toconfirm: | ||||
drevs = [drev for force, precs, drev in toconfirm.values()] | ||||
|
r43346 | alldiffs = callconduit( | ||
unfi.ui, b'differential.querydiffs', {b'revisionIDs': drevs} | ||||
) | ||||
|
r45136 | |||
def getnodes(d, precset): | ||||
# Ignore other nodes that were combined into the Differential | ||||
# that aren't predecessors of the current local node. | ||||
return [n for n in getlocalcommits(d) if n in precset] | ||||
|
r39688 | for newnode, (force, precset, drev) in toconfirm.items(): | ||
|
r43346 | diffs = [ | ||
d for d in alldiffs.values() if int(d[b'revisionID']) == drev | ||||
] | ||||
|
r39688 | |||
|
r45136 | # local predecessors known by Phabricator | ||
phprecset = {n for d in diffs for n in getnodes(d, precset)} | ||||
|
r39688 | |||
# Ignore if precursors (Phabricator and local repo) do not overlap, | ||||
# and force is not set (when commit message says nothing) | ||||
|
r45136 | if not force and not phprecset: | ||
|
r39688 | tagname = b'D%d' % drev | ||
|
r43346 | tags.tag( | ||
repo, | ||||
tagname, | ||||
|
r47771 | repo.nullid, | ||
|
r43346 | message=None, | ||
user=None, | ||||
date=None, | ||||
local=True, | ||||
) | ||||
unfi.ui.warn( | ||||
_( | ||||
|
r43717 | b'D%d: local tag removed - does not match ' | ||
|
r43346 | b'Differential history\n' | ||
) | ||||
% drev | ||||
) | ||||
|
r39688 | continue | ||
# Find the last node using Phabricator metadata, and make sure it | ||||
# exists in the repo | ||||
oldnode = lastdiff = None | ||||
if diffs: | ||||
|
r42063 | lastdiff = max(diffs, key=lambda d: int(d[b'id'])) | ||
|
r45136 | oldnodes = getnodes(lastdiff, precset) | ||
|
r45209 | _debug( | ||
unfi.ui, | ||||
b"%s mapped to old nodes %s\n" | ||||
% ( | ||||
short(newnode), | ||||
stringutil.pprint([short(n) for n in sorted(oldnodes)]), | ||||
), | ||||
) | ||||
|
r45136 | # If this commit was the result of `hg fold` after submission, | ||
# and now resubmitted with --fold, the easiest thing to do is | ||||
# to leave the node clear. This only results in creating a new | ||||
# diff for the _same_ Differential Revision if this commit is | ||||
|
r45211 | # the first or last in the selected range. If we picked a node | ||
# from the list instead, it would have to be the lowest if at | ||||
# the beginning of the --fold range, or the highest at the end. | ||||
# Otherwise, one or more of the nodes wouldn't be considered in | ||||
# the diff, and the Differential wouldn't be properly updated. | ||||
|
r45136 | # If this commit is the result of `hg split` in the same | ||
# scenario, there is a single oldnode here (and multiple | ||||
# newnodes mapped to it). That makes it the same as the normal | ||||
# case, as the edges of the newnode range cleanly maps to one | ||||
# oldnode each. | ||||
if len(oldnodes) == 1: | ||||
oldnode = oldnodes[0] | ||||
r43949 | if oldnode and not has_node(oldnode): | |||
|
r39688 | oldnode = None | ||
result[newnode] = (oldnode, lastdiff, drev) | ||||
return result | ||||
|
r43346 | |||
|
r44291 | def getdrevmap(repo, revs): | ||
"""Return a dict mapping each rev in `revs` to their Differential Revision | ||||
ID or None. | ||||
""" | ||||
result = {} | ||||
for rev in revs: | ||||
result[rev] = None | ||||
ctx = repo[rev] | ||||
# Check commit message | ||||
m = _differentialrevisiondescre.search(ctx.description()) | ||||
if m: | ||||
result[rev] = int(m.group('id')) | ||||
continue | ||||
# Check tags | ||||
for tag in repo.nodetags(ctx.node()): | ||||
m = _differentialrevisiontagre.match(tag) | ||||
if m: | ||||
result[rev] = int(m.group(1)) | ||||
break | ||||
return result | ||||
|
r45100 | def getdiff(basectx, ctx, diffopts): | ||
|
r39688 | """plain-text diff without header (user, commit message, etc)""" | ||
output = util.stringio() | ||||
|
r43346 | for chunk, _label in patch.diffui( | ||
|
r45100 | ctx.repo(), basectx.p1().node(), ctx.node(), None, opts=diffopts | ||
|
r43346 | ): | ||
|
r39688 | output.write(chunk) | ||
return output.getvalue() | ||||
|
r43346 | |||
|
r43452 | class DiffChangeType(object): | ||
ADD = 1 | ||||
CHANGE = 2 | ||||
DELETE = 3 | ||||
MOVE_AWAY = 4 | ||||
COPY_AWAY = 5 | ||||
MOVE_HERE = 6 | ||||
COPY_HERE = 7 | ||||
MULTICOPY = 8 | ||||
class DiffFileType(object): | ||||
TEXT = 1 | ||||
IMAGE = 2 | ||||
BINARY = 3 | ||||
|
r43453 | @attr.s | ||
class phabhunk(dict): | ||||
|
r46554 | """Represents a Differential hunk, which is owned by a Differential change""" | ||
|
r43453 | |||
oldOffset = attr.ib(default=0) # camelcase-required | ||||
oldLength = attr.ib(default=0) # camelcase-required | ||||
newOffset = attr.ib(default=0) # camelcase-required | ||||
newLength = attr.ib(default=0) # camelcase-required | ||||
corpus = attr.ib(default='') | ||||
# These get added to the phabchange's equivalents | ||||
addLines = attr.ib(default=0) # camelcase-required | ||||
delLines = attr.ib(default=0) # camelcase-required | ||||
|
r43454 | @attr.s | ||
class phabchange(object): | ||||
"""Represents a Differential change, owns Differential hunks and owned by a | ||||
Differential diff. Each one represents one file in a diff. | ||||
""" | ||||
currentPath = attr.ib(default=None) # camelcase-required | ||||
oldPath = attr.ib(default=None) # camelcase-required | ||||
awayPaths = attr.ib(default=attr.Factory(list)) # camelcase-required | ||||
metadata = attr.ib(default=attr.Factory(dict)) | ||||
oldProperties = attr.ib(default=attr.Factory(dict)) # camelcase-required | ||||
newProperties = attr.ib(default=attr.Factory(dict)) # camelcase-required | ||||
type = attr.ib(default=DiffChangeType.CHANGE) | ||||
fileType = attr.ib(default=DiffFileType.TEXT) # camelcase-required | ||||
commitHash = attr.ib(default=None) # camelcase-required | ||||
addLines = attr.ib(default=0) # camelcase-required | ||||
delLines = attr.ib(default=0) # camelcase-required | ||||
hunks = attr.ib(default=attr.Factory(list)) | ||||
def copynewmetadatatoold(self): | ||||
for key in list(self.metadata.keys()): | ||||
newkey = key.replace(b'new:', b'old:') | ||||
self.metadata[newkey] = self.metadata[key] | ||||
def addoldmode(self, value): | ||||
self.oldProperties[b'unix:filemode'] = value | ||||
def addnewmode(self, value): | ||||
self.newProperties[b'unix:filemode'] = value | ||||
def addhunk(self, hunk): | ||||
if not isinstance(hunk, phabhunk): | ||||
raise error.Abort(b'phabchange.addhunk only takes phabhunks') | ||||
|
r43553 | self.hunks.append(pycompat.byteskwargs(attr.asdict(hunk))) | ||
|
r43454 | # It's useful to include these stats since the Phab web UI shows them, | ||
# and uses them to estimate how large a change a Revision is. Also used | ||||
# in email subjects for the [+++--] bit. | ||||
self.addLines += hunk.addLines | ||||
self.delLines += hunk.delLines | ||||
|
r43455 | @attr.s | ||
class phabdiff(object): | ||||
"""Represents a Differential diff, owns Differential changes. Corresponds | ||||
to a commit. | ||||
""" | ||||
# Doesn't seem to be any reason to send this (output of uname -n) | ||||
sourceMachine = attr.ib(default=b'') # camelcase-required | ||||
sourcePath = attr.ib(default=b'/') # camelcase-required | ||||
sourceControlBaseRevision = attr.ib(default=b'0' * 40) # camelcase-required | ||||
sourceControlPath = attr.ib(default=b'/') # camelcase-required | ||||
sourceControlSystem = attr.ib(default=b'hg') # camelcase-required | ||||
branch = attr.ib(default=b'default') | ||||
bookmark = attr.ib(default=None) | ||||
creationMethod = attr.ib(default=b'phabsend') # camelcase-required | ||||
lintStatus = attr.ib(default=b'none') # camelcase-required | ||||
unitStatus = attr.ib(default=b'none') # camelcase-required | ||||
changes = attr.ib(default=attr.Factory(dict)) | ||||
repositoryPHID = attr.ib(default=None) # camelcase-required | ||||
def addchange(self, change): | ||||
if not isinstance(change, phabchange): | ||||
raise error.Abort(b'phabdiff.addchange only takes phabchanges') | ||||
|
r43553 | self.changes[change.currentPath] = pycompat.byteskwargs( | ||
attr.asdict(change) | ||||
) | ||||
|
r43455 | |||
|
r45100 | def maketext(pchange, basectx, ctx, fname): | ||
|
r43456 | """populate the phabchange for a text file""" | ||
repo = ctx.repo() | ||||
fmatcher = match.exact([fname]) | ||||
diffopts = mdiff.diffopts(git=True, context=32767) | ||||
_pfctx, _fctx, header, fhunks = next( | ||||
|
r45100 | patch.diffhunks(repo, basectx.p1(), ctx, fmatcher, opts=diffopts) | ||
|
r43456 | ) | ||
for fhunk in fhunks: | ||||
(oldOffset, oldLength, newOffset, newLength), lines = fhunk | ||||
corpus = b''.join(lines[1:]) | ||||
shunk = list(header) | ||||
shunk.extend(lines) | ||||
_mf, _mt, addLines, delLines, _hb = patch.diffstatsum( | ||||
patch.diffstatdata(util.iterlines(shunk)) | ||||
) | ||||
pchange.addhunk( | ||||
phabhunk( | ||||
oldOffset, | ||||
oldLength, | ||||
newOffset, | ||||
newLength, | ||||
corpus, | ||||
addLines, | ||||
delLines, | ||||
) | ||||
) | ||||
|
r43457 | def uploadchunks(fctx, fphid): | ||
"""upload large binary files as separate chunks. | ||||
Phab requests chunking over 8MiB, and splits into 4MiB chunks | ||||
""" | ||||
ui = fctx.repo().ui | ||||
chunks = callconduit(ui, b'file.querychunks', {b'filePHID': fphid}) | ||||
|
r43810 | with ui.makeprogress( | ||
|
r43457 | _(b'uploading file chunks'), unit=_(b'chunks'), total=len(chunks) | ||
|
r43810 | ) as progress: | ||
for chunk in chunks: | ||||
progress.increment() | ||||
if chunk[b'complete']: | ||||
continue | ||||
bstart = int(chunk[b'byteStart']) | ||||
bend = int(chunk[b'byteEnd']) | ||||
callconduit( | ||||
ui, | ||||
b'file.uploadchunk', | ||||
{ | ||||
b'filePHID': fphid, | ||||
b'byteStart': bstart, | ||||
b'data': base64.b64encode(fctx.data()[bstart:bend]), | ||||
b'dataEncoding': b'base64', | ||||
}, | ||||
) | ||||
|
r43457 | |||
|
r43458 | def uploadfile(fctx): | ||
"""upload binary files to Phabricator""" | ||||
repo = fctx.repo() | ||||
ui = repo.ui | ||||
fname = fctx.path() | ||||
size = fctx.size() | ||||
fhash = pycompat.bytestr(hashlib.sha256(fctx.data()).hexdigest()) | ||||
# an allocate call is required first to see if an upload is even required | ||||
# (Phab might already have it) and to determine if chunking is needed | ||||
allocateparams = { | ||||
b'name': fname, | ||||
b'contentLength': size, | ||||
b'contentHash': fhash, | ||||
} | ||||
filealloc = callconduit(ui, b'file.allocate', allocateparams) | ||||
fphid = filealloc[b'filePHID'] | ||||
if filealloc[b'upload']: | ||||
ui.write(_(b'uploading %s\n') % bytes(fctx)) | ||||
if not fphid: | ||||
uploadparams = { | ||||
b'name': fname, | ||||
b'data_base64': base64.b64encode(fctx.data()), | ||||
} | ||||
fphid = callconduit(ui, b'file.upload', uploadparams) | ||||
else: | ||||
uploadchunks(fctx, fphid) | ||||
else: | ||||
ui.debug(b'server already has %s\n' % bytes(fctx)) | ||||
if not fphid: | ||||
raise error.Abort(b'Upload of %s failed.' % bytes(fctx)) | ||||
return fphid | ||||
|
r44911 | def addoldbinary(pchange, oldfctx, fctx): | ||
|
r43459 | """add the metadata for the previous version of a binary file to the | ||
phabchange for the new version | ||||
|
r44911 | |||
``oldfctx`` is the previous version of the file; ``fctx`` is the new | ||||
version of the file, or None if the file is being removed. | ||||
|
r43459 | """ | ||
|
r44911 | if not fctx or fctx.cmp(oldfctx): | ||
|
r43459 | # Files differ, add the old one | ||
pchange.metadata[b'old:file:size'] = oldfctx.size() | ||||
mimeguess, _enc = mimetypes.guess_type( | ||||
encoding.unifromlocal(oldfctx.path()) | ||||
) | ||||
if mimeguess: | ||||
pchange.metadata[b'old:file:mime-type'] = pycompat.bytestr( | ||||
mimeguess | ||||
) | ||||
fphid = uploadfile(oldfctx) | ||||
pchange.metadata[b'old:binary-phid'] = fphid | ||||
else: | ||||
# If it's left as IMAGE/BINARY web UI might try to display it | ||||
pchange.fileType = DiffFileType.TEXT | ||||
pchange.copynewmetadatatoold() | ||||
def makebinary(pchange, fctx): | ||||
"""populate the phabchange for a binary file""" | ||||
pchange.fileType = DiffFileType.BINARY | ||||
fphid = uploadfile(fctx) | ||||
pchange.metadata[b'new:binary-phid'] = fphid | ||||
pchange.metadata[b'new:file:size'] = fctx.size() | ||||
mimeguess, _enc = mimetypes.guess_type(encoding.unifromlocal(fctx.path())) | ||||
if mimeguess: | ||||
mimeguess = pycompat.bytestr(mimeguess) | ||||
pchange.metadata[b'new:file:mime-type'] = mimeguess | ||||
if mimeguess.startswith(b'image/'): | ||||
pchange.fileType = DiffFileType.IMAGE | ||||
|
r43460 | # Copied from mercurial/patch.py | ||
gitmode = {b'l': b'120000', b'x': b'100755', b'': b'100644'} | ||||
|
r43557 | def notutf8(fctx): | ||
"""detect non-UTF-8 text files since Phabricator requires them to be marked | ||||
as binary | ||||
""" | ||||
try: | ||||
fctx.data().decode('utf-8') | ||||
return False | ||||
except UnicodeDecodeError: | ||||
fctx.repo().ui.write( | ||||
_(b'file %s detected as non-UTF-8, marked as binary\n') | ||||
% fctx.path() | ||||
) | ||||
return True | ||||
|
r45100 | def addremoved(pdiff, basectx, ctx, removed): | ||
|
r43460 | """add removed files to the phabdiff. Shouldn't include moves""" | ||
for fname in removed: | ||||
pchange = phabchange( | ||||
currentPath=fname, oldPath=fname, type=DiffChangeType.DELETE | ||||
) | ||||
|
r45100 | oldfctx = basectx.p1()[fname] | ||
|
r45099 | pchange.addoldmode(gitmode[oldfctx.flags()]) | ||
|
r44912 | if not (oldfctx.isbinary() or notutf8(oldfctx)): | ||
|
r45100 | maketext(pchange, basectx, ctx, fname) | ||
|
r43460 | |||
pdiff.addchange(pchange) | ||||
|
r45100 | def addmodified(pdiff, basectx, ctx, modified): | ||
|
r43460 | """add modified files to the phabdiff""" | ||
for fname in modified: | ||||
fctx = ctx[fname] | ||||
|
r45100 | oldfctx = basectx.p1()[fname] | ||
|
r43460 | pchange = phabchange(currentPath=fname, oldPath=fname) | ||
|
r45099 | filemode = gitmode[fctx.flags()] | ||
originalmode = gitmode[oldfctx.flags()] | ||||
|
r43460 | if filemode != originalmode: | ||
pchange.addoldmode(originalmode) | ||||
pchange.addnewmode(filemode) | ||||
|
r44914 | if ( | ||
fctx.isbinary() | ||||
or notutf8(fctx) | ||||
or oldfctx.isbinary() | ||||
or notutf8(oldfctx) | ||||
): | ||||
|
r43460 | makebinary(pchange, fctx) | ||
|
r45099 | addoldbinary(pchange, oldfctx, fctx) | ||
|
r43460 | else: | ||
|
r45100 | maketext(pchange, basectx, ctx, fname) | ||
|
r43460 | |||
pdiff.addchange(pchange) | ||||
|
r45100 | def addadded(pdiff, basectx, ctx, added, removed): | ||
|
r43552 | """add file adds to the phabdiff, both new files and copies/moves""" | ||
# Keep track of files that've been recorded as moved/copied, so if there are | ||||
# additional copies we can mark them (moves get removed from removed) | ||||
copiedchanges = {} | ||||
movedchanges = {} | ||||
|
r45101 | |||
copy = {} | ||||
if basectx != ctx: | ||||
copy = copies.pathcopies(basectx.p1(), ctx) | ||||
|
r43552 | for fname in added: | ||
fctx = ctx[fname] | ||||
|
r44913 | oldfctx = None | ||
|
r43552 | pchange = phabchange(currentPath=fname) | ||
|
r45099 | filemode = gitmode[fctx.flags()] | ||
|
r45101 | |||
if copy: | ||||
originalfname = copy.get(fname, fname) | ||||
else: | ||||
originalfname = fname | ||||
if fctx.renamed(): | ||||
originalfname = fctx.renamed()[0] | ||||
renamed = fname != originalfname | ||||
|
r43552 | |||
if renamed: | ||||
|
r45100 | oldfctx = basectx.p1()[originalfname] | ||
|
r44913 | originalmode = gitmode[oldfctx.flags()] | ||
|
r43552 | pchange.oldPath = originalfname | ||
if originalfname in removed: | ||||
origpchange = phabchange( | ||||
currentPath=originalfname, | ||||
oldPath=originalfname, | ||||
type=DiffChangeType.MOVE_AWAY, | ||||
awayPaths=[fname], | ||||
) | ||||
movedchanges[originalfname] = origpchange | ||||
removed.remove(originalfname) | ||||
pchange.type = DiffChangeType.MOVE_HERE | ||||
elif originalfname in movedchanges: | ||||
movedchanges[originalfname].type = DiffChangeType.MULTICOPY | ||||
movedchanges[originalfname].awayPaths.append(fname) | ||||
pchange.type = DiffChangeType.COPY_HERE | ||||
else: # pure copy | ||||
if originalfname not in copiedchanges: | ||||
origpchange = phabchange( | ||||
currentPath=originalfname, type=DiffChangeType.COPY_AWAY | ||||
) | ||||
copiedchanges[originalfname] = origpchange | ||||
else: | ||||
origpchange = copiedchanges[originalfname] | ||||
origpchange.awayPaths.append(fname) | ||||
pchange.type = DiffChangeType.COPY_HERE | ||||
if filemode != originalmode: | ||||
pchange.addoldmode(originalmode) | ||||
pchange.addnewmode(filemode) | ||||
else: # Brand-new file | ||||
pchange.addnewmode(gitmode[fctx.flags()]) | ||||
pchange.type = DiffChangeType.ADD | ||||
|
r44914 | if ( | ||
fctx.isbinary() | ||||
or notutf8(fctx) | ||||
or (oldfctx and (oldfctx.isbinary() or notutf8(oldfctx))) | ||||
): | ||||
|
r43552 | makebinary(pchange, fctx) | ||
if renamed: | ||||
|
r44913 | addoldbinary(pchange, oldfctx, fctx) | ||
|
r43552 | else: | ||
|
r45100 | maketext(pchange, basectx, ctx, fname) | ||
|
r43552 | |||
pdiff.addchange(pchange) | ||||
for _path, copiedchange in copiedchanges.items(): | ||||
pdiff.addchange(copiedchange) | ||||
for _path, movedchange in movedchanges.items(): | ||||
pdiff.addchange(movedchange) | ||||
|
r45100 | def creatediff(basectx, ctx): | ||
|
r39688 | """create a Differential Diff""" | ||
repo = ctx.repo() | ||||
repophid = getrepophid(repo) | ||||
|
r43556 | # Create a "Differential Diff" via "differential.creatediff" API | ||
pdiff = phabdiff( | ||||
|
r45100 | sourceControlBaseRevision=b'%s' % basectx.p1().hex(), | ||
|
r43556 | branch=b'%s' % ctx.branch(), | ||
) | ||||
|
r45100 | modified, added, removed, _d, _u, _i, _c = basectx.p1().status(ctx) | ||
|
r43556 | # addadded will remove moved files from removed, so addremoved won't get | ||
# them | ||||
|
r45100 | addadded(pdiff, basectx, ctx, added, removed) | ||
addmodified(pdiff, basectx, ctx, modified) | ||||
addremoved(pdiff, basectx, ctx, removed) | ||||
|
r39688 | if repophid: | ||
|
r43556 | pdiff.repositoryPHID = repophid | ||
diff = callconduit( | ||||
repo.ui, | ||||
b'differential.creatediff', | ||||
pycompat.byteskwargs(attr.asdict(pdiff)), | ||||
) | ||||
|
r39688 | if not diff: | ||
|
r45100 | if basectx != ctx: | ||
msg = _(b'cannot create diff for %s::%s') % (basectx, ctx) | ||||
else: | ||||
msg = _(b'cannot create diff for %s') % ctx | ||||
raise error.Abort(msg) | ||||
|
r39688 | return diff | ||
|
r43346 | |||
|
r45133 | def writediffproperties(ctxs, diff): | ||
"""write metadata to diff so patches could be applied losslessly | ||||
``ctxs`` is the list of commits that created the diff, in ascending order. | ||||
The list is generally a single commit, but may be several when using | ||||
``phabsend --fold``. | ||||
""" | ||||
|
r43556 | # creatediff returns with a diffid but query returns with an id | ||
diffid = diff.get(b'diffid', diff.get(b'id')) | ||||
|
r45133 | basectx = ctxs[0] | ||
tipctx = ctxs[-1] | ||||
|
r39688 | params = { | ||
|
r43556 | b'diff_id': diffid, | ||
|
r39688 | b'name': b'hg:meta', | ||
|
r43346 | b'data': templatefilters.json( | ||
{ | ||||
|
r45133 | b'user': tipctx.user(), | ||
b'date': b'%d %d' % tipctx.date(), | ||||
b'branch': tipctx.branch(), | ||||
b'node': tipctx.hex(), | ||||
b'parent': basectx.p1().hex(), | ||||
|
r43346 | } | ||
), | ||||
|
r39688 | } | ||
|
r45133 | callconduit(basectx.repo().ui, b'differential.setdiffproperty', params) | ||
|
r39688 | |||
|
r45133 | commits = {} | ||
for ctx in ctxs: | ||||
commits[ctx.hex()] = { | ||||
b'author': stringutil.person(ctx.user()), | ||||
b'authorEmail': stringutil.email(ctx.user()), | ||||
b'time': int(ctx.date()[0]), | ||||
b'commit': ctx.hex(), | ||||
b'parents': [ctx.p1().hex()], | ||||
b'branch': ctx.branch(), | ||||
} | ||||
|
r39688 | params = { | ||
|
r43556 | b'diff_id': diffid, | ||
|
r39688 | b'name': b'local:commits', | ||
|
r45133 | b'data': templatefilters.json(commits), | ||
|
r39688 | } | ||
|
r45133 | callconduit(basectx.repo().ui, b'differential.setdiffproperty', params) | ||
|
r39688 | |||
|
r43346 | |||
def createdifferentialrevision( | ||||
|
r45135 | ctxs, | ||
|
r43346 | revid=None, | ||
parentrevphid=None, | ||||
|
r45135 | oldbasenode=None, | ||
|
r43346 | oldnode=None, | ||
olddiff=None, | ||||
actions=None, | ||||
comment=None, | ||||
): | ||||
|
r39688 | """create or update a Differential Revision | ||
If revid is None, create a new Differential Revision, otherwise update | ||||
|
r42642 | revid. If parentrevphid is not None, set it as a dependency. | ||
|
r39688 | |||
|
r45135 | If there is a single commit for the new Differential Revision, ``ctxs`` will | ||
be a list of that single context. Otherwise, it is a list that covers the | ||||
range of changes for the differential, where ``ctxs[0]`` is the first change | ||||
to include and ``ctxs[-1]`` is the last. | ||||
|
r39688 | If oldnode is not None, check if the patch content (without commit message | ||
|
r45135 | and metadata) has changed before creating another diff. For a Revision with | ||
a single commit, ``oldbasenode`` and ``oldnode`` have the same value. For a | ||||
Revision covering multiple commits, ``oldbasenode`` corresponds to | ||||
``ctxs[0]`` the previous time this Revision was posted, and ``oldnode`` | ||||
corresponds to ``ctxs[-1]``. | ||||
|
r39688 | |||
If actions is not None, they will be appended to the transaction. | ||||
""" | ||||
|
r45135 | ctx = ctxs[-1] | ||
basectx = ctxs[0] | ||||
|
r39688 | repo = ctx.repo() | ||
if oldnode: | ||||
diffopts = mdiff.diffopts(git=True, context=32767) | ||||
|
r45135 | unfi = repo.unfiltered() | ||
oldctx = unfi[oldnode] | ||||
oldbasectx = unfi[oldbasenode] | ||||
|
r45100 | neednewdiff = getdiff(basectx, ctx, diffopts) != getdiff( | ||
oldbasectx, oldctx, diffopts | ||||
) | ||||
|
r39688 | else: | ||
neednewdiff = True | ||||
transactions = [] | ||||
if neednewdiff: | ||||
|
r45100 | diff = creatediff(basectx, ctx) | ||
|
r42063 | transactions.append({b'type': b'update', b'value': diff[b'phid']}) | ||
|
r42624 | if comment: | ||
transactions.append({b'type': b'comment', b'value': comment}) | ||||
|
r39688 | else: | ||
# Even if we don't need to upload a new diff because the patch content | ||||
# does not change. We might still need to update its metadata so | ||||
# pushers could know the correct node metadata. | ||||
assert olddiff | ||||
diff = olddiff | ||||
|
r45135 | writediffproperties(ctxs, diff) | ||
|
r39688 | |||
|
r42642 | # Set the parent Revision every time, so commit re-ordering is picked-up | ||
if parentrevphid: | ||||
|
r43346 | transactions.append( | ||
{b'type': b'parents.set', b'value': [parentrevphid]} | ||||
) | ||||
|
r39688 | |||
if actions: | ||||
transactions += actions | ||||
|
r45134 | # When folding multiple local commits into a single review, arcanist will | ||
# take the summary line of the first commit as the title, and then | ||||
# concatenate the rest of the remaining messages (including each of their | ||||
# first lines) to the rest of the first commit message (each separated by | ||||
# an empty line), and use that as the summary field. Do the same here. | ||||
# For commits with only a one line message, there is no summary field, as | ||||
# this gets assigned to the title. | ||||
fields = util.sortdict() # sorted for stable wire protocol in tests | ||||
|
r45135 | for i, _ctx in enumerate(ctxs): | ||
|
r45134 | # Parse commit message and update related fields. | ||
desc = _ctx.description() | ||||
info = callconduit( | ||||
repo.ui, b'differential.parsecommitmessage', {b'corpus': desc} | ||||
) | ||||
for k in [b'title', b'summary', b'testPlan']: | ||||
v = info[b'fields'].get(k) | ||||
if not v: | ||||
continue | ||||
if i == 0: | ||||
# Title, summary and test plan (if present) are taken verbatim | ||||
# for the first commit. | ||||
fields[k] = v.rstrip() | ||||
continue | ||||
elif k == b'title': | ||||
# Add subsequent titles (i.e. the first line of the commit | ||||
# message) back to the summary. | ||||
k = b'summary' | ||||
# Append any current field to the existing composite field | ||||
fields[k] = b'\n\n'.join(filter(None, [fields.get(k), v.rstrip()])) | ||||
for k, v in fields.items(): | ||||
transactions.append({b'type': k, b'value': v}) | ||||
|
r39688 | |||
params = {b'transactions': transactions} | ||||
if revid is not None: | ||||
# Update an existing Differential Revision | ||||
params[b'objectIdentifier'] = revid | ||||
|
r42627 | revision = callconduit(repo.ui, b'differential.revision.edit', params) | ||
|
r39688 | if not revision: | ||
|
r45135 | if len(ctxs) == 1: | ||
msg = _(b'cannot create revision for %s') % ctx | ||||
else: | ||||
msg = _(b'cannot create revision for %s::%s') % (basectx, ctx) | ||||
raise error.Abort(msg) | ||||
|
r39688 | |||
return revision, diff | ||||
|
r43346 | |||
|
r44907 | def userphids(ui, names): | ||
|
r39688 | """convert user names to PHIDs""" | ||
|
r41854 | names = [name.lower() for name in names] | ||
|
r39688 | query = {b'constraints': {b'usernames': names}} | ||
|
r44907 | result = callconduit(ui, b'user.search', query) | ||
|
r39688 | # username not found is not an error of the API. So check if we have missed | ||
# some names here. | ||||
|
r42063 | data = result[b'data'] | ||
|
r44937 | resolved = {entry[b'fields'][b'username'].lower() for entry in data} | ||
|
r39688 | unresolved = set(names) - resolved | ||
if unresolved: | ||||
|
r43346 | raise error.Abort( | ||
_(b'unknown username: %s') % b' '.join(sorted(unresolved)) | ||||
) | ||||
|
r42063 | return [entry[b'phid'] for entry in data] | ||
|
r39688 | |||
|
r43346 | |||
|
r45138 | def _print_phabsend_action(ui, ctx, newrevid, action): | ||
"""print the ``action`` that occurred when posting ``ctx`` for review | ||||
This is a utility function for the sending phase of ``phabsend``, which | ||||
makes it easier to show a status for all local commits with `--fold``. | ||||
""" | ||||
actiondesc = ui.label( | ||||
{ | ||||
b'created': _(b'created'), | ||||
b'skipped': _(b'skipped'), | ||||
b'updated': _(b'updated'), | ||||
}[action], | ||||
b'phabricator.action.%s' % action, | ||||
) | ||||
drevdesc = ui.label(b'D%d' % newrevid, b'phabricator.drev') | ||||
|
r46357 | summary = cmdutil.format_changeset_summary(ui, ctx, b'phabsend') | ||
ui.write(_(b'%s - %s - %s\n') % (drevdesc, actiondesc, summary)) | ||||
|
r45138 | |||
|
r45137 | def _amend_diff_properties(unfi, drevid, newnodes, diff): | ||
"""update the local commit list for the ``diff`` associated with ``drevid`` | ||||
This is a utility function for the amend phase of ``phabsend``, which | ||||
converts failures to warning messages. | ||||
""" | ||||
|
r45209 | _debug( | ||
unfi.ui, | ||||
b"new commits: %s\n" % stringutil.pprint([short(n) for n in newnodes]), | ||||
) | ||||
|
r45137 | try: | ||
writediffproperties([unfi[newnode] for newnode in newnodes], diff) | ||||
except util.urlerr.urlerror: | ||||
# If it fails just warn and keep going, otherwise the DREV | ||||
# associations will be lost | ||||
unfi.ui.warnnoi18n(b'Failed to update metadata for D%d\n' % drevid) | ||||
|
r43346 | @vcrcommand( | ||
b'phabsend', | ||||
[ | ||||
(b'r', b'rev', [], _(b'revisions to send'), _(b'REV')), | ||||
(b'', b'amend', True, _(b'update commit messages')), | ||||
(b'', b'reviewer', [], _(b'specify reviewers')), | ||||
(b'', b'blocker', [], _(b'specify blocking reviewers')), | ||||
( | ||||
b'm', | ||||
b'comment', | ||||
b'', | ||||
_(b'add a comment to Revisions with new/updated Diffs'), | ||||
), | ||||
(b'', b'confirm', None, _(b'ask for confirmation before sending')), | ||||
|
r45211 | (b'', b'fold', False, _(b'combine the revisions into one review')), | ||
|
r43346 | ], | ||
_(b'REV [OPTIONS]'), | ||||
helpcategory=command.CATEGORY_IMPORT_EXPORT, | ||||
) | ||||
|
r39688 | def phabsend(ui, repo, *revs, **opts): | ||
"""upload changesets to Phabricator | ||||
If there are multiple revisions specified, they will be send as a stack | ||||
with a linear dependencies relationship using the order specified by the | ||||
revset. | ||||
For the first time uploading changesets, local tags will be created to | ||||
maintain the association. After the first time, phabsend will check | ||||
obsstore and tags information so it can figure out whether to update an | ||||
existing Differential Revision, or create a new one. | ||||
If --amend is set, update commit messages so they have the | ||||
``Differential Revision`` URL, remove related tags. This is similar to what | ||||
arcanist will do, and is more desired in author-push workflows. Otherwise, | ||||
use local tags to record the ``Differential Revision`` association. | ||||
The --confirm option lets you confirm changesets before sending them. You | ||||
can also add following to your configuration file to make it default | ||||
behaviour:: | ||||
[phabsend] | ||||
confirm = true | ||||
|
r45211 | By default, a separate review will be created for each commit that is | ||
selected, and will have the same parent/child relationship in Phabricator. | ||||
If ``--fold`` is set, multiple commits are rolled up into a single review | ||||
as if diffed from the parent of the first revision to the last. The commit | ||||
messages are concatenated in the summary field on Phabricator. | ||||
|
r39688 | phabsend will check obsstore and the above association to decide whether to | ||
update an existing Differential Revision, or create a new one. | ||||
""" | ||||
|
r42136 | opts = pycompat.byteskwargs(opts) | ||
|
r39688 | revs = list(revs) + opts.get(b'rev', []) | ||
revs = scmutil.revrange(repo, revs) | ||||
|
r44533 | revs.sort() # ascending order to preserve topological parent/child in phab | ||
|
r39688 | |||
if not revs: | ||||
raise error.Abort(_(b'phabsend requires at least one changeset')) | ||||
if opts.get(b'amend'): | ||||
cmdutil.checkunfinished(repo) | ||||
|
r45211 | ctxs = [repo[rev] for rev in revs] | ||
|
r45213 | if any(c for c in ctxs if c.obsolete()): | ||
raise error.Abort(_(b"obsolete commits cannot be posted for review")) | ||||
|
r45281 | # Ensure the local commits are an unbroken range. The semantics of the | ||
# --fold option implies this, and the auto restacking of orphans requires | ||||
# it. Otherwise A+C in A->B->C will cause B to be orphaned, and C' to | ||||
# get A' as a parent. | ||||
|
r45712 | def _fail_nonlinear_revs(revs, revtype): | ||
badnodes = [repo[r].node() for r in revs] | ||||
|
r45281 | raise error.Abort( | ||
_(b"cannot phabsend multiple %s revisions: %s") | ||||
% (revtype, scmutil.nodesummaries(repo, badnodes)), | ||||
hint=_(b"the revisions must form a linear chain"), | ||||
) | ||||
heads = repo.revs(b'heads(%ld)', revs) | ||||
if len(heads) > 1: | ||||
|
r45712 | _fail_nonlinear_revs(heads, b"head") | ||
|
r45281 | |||
roots = repo.revs(b'roots(%ld)', revs) | ||||
if len(roots) > 1: | ||||
|
r45712 | _fail_nonlinear_revs(roots, b"root") | ||
|
r45281 | |||
|
r45211 | fold = opts.get(b'fold') | ||
if fold: | ||||
if len(revs) == 1: | ||||
# TODO: just switch to --no-fold instead? | ||||
raise error.Abort(_(b"cannot fold a single revision")) | ||||
# There's no clear way to manage multiple commits with a Dxxx tag, so | ||||
# require the amend option. (We could append "_nnn", but then it | ||||
# becomes jumbled if earlier commits are added to an update.) It should | ||||
# lock the repo and ensure that the range is editable, but that would | ||||
# make the code pretty convoluted. The default behavior of `arc` is to | ||||
# create a new review anyway. | ||||
if not opts.get(b"amend"): | ||||
raise error.Abort(_(b"cannot fold with --no-amend")) | ||||
# It might be possible to bucketize the revisions by the DREV value, and | ||||
# iterate over those groups when posting, and then again when amending. | ||||
# But for simplicity, require all selected revisions to be for the same | ||||
# DREV (if present). Adding local revisions to an existing DREV is | ||||
# acceptable. | ||||
drevmatchers = [ | ||||
_differentialrevisiondescre.search(ctx.description()) | ||||
for ctx in ctxs | ||||
] | ||||
if len({m.group('url') for m in drevmatchers if m}) > 1: | ||||
raise error.Abort( | ||||
_(b"cannot fold revisions with different DREV values") | ||||
) | ||||
|
r39688 | # {newnode: (oldnode, olddiff, olddrev} | ||
oldmap = getoldnodedrevmap(repo, [repo[r].node() for r in revs]) | ||||
confirm = ui.configbool(b'phabsend', b'confirm') | ||||
confirm |= bool(opts.get(b'confirm')) | ||||
if confirm: | ||||
confirmed = _confirmbeforesend(repo, revs, oldmap) | ||||
if not confirmed: | ||||
raise error.Abort(_(b'phabsend cancelled')) | ||||
actions = [] | ||||
reviewers = opts.get(b'reviewer', []) | ||||
|
r42637 | blockers = opts.get(b'blocker', []) | ||
phids = [] | ||||
|
r39688 | if reviewers: | ||
|
r44907 | phids.extend(userphids(repo.ui, reviewers)) | ||
|
r42637 | if blockers: | ||
|
r43346 | phids.extend( | ||
|
r44907 | map( | ||
lambda phid: b'blocking(%s)' % phid, | ||||
userphids(repo.ui, blockers), | ||||
) | ||||
|
r43346 | ) | ||
|
r42637 | if phids: | ||
|
r39688 | actions.append({b'type': b'reviewers.add', b'value': phids}) | ||
|
r43346 | drevids = [] # [int] | ||
diffmap = {} # {newnode: diff} | ||||
|
r39688 | |||
|
r42642 | # Send patches one by one so we know their Differential Revision PHIDs and | ||
|
r39688 | # can provide dependency relationship | ||
|
r42642 | lastrevphid = None | ||
|
r45211 | for ctx in ctxs: | ||
if fold: | ||||
ui.debug(b'sending rev %d::%d\n' % (ctx.rev(), ctxs[-1].rev())) | ||||
else: | ||||
ui.debug(b'sending rev %d\n' % ctx.rev()) | ||||
|
r39688 | |||
# Get Differential Revision ID | ||||
oldnode, olddiff, revid = oldmap.get(ctx.node(), (None, None, None)) | ||||
|
r45211 | oldbasenode, oldbasediff, oldbaserevid = oldnode, olddiff, revid | ||
if fold: | ||||
oldbasenode, oldbasediff, oldbaserevid = oldmap.get( | ||||
ctxs[-1].node(), (None, None, None) | ||||
) | ||||
|
r39688 | if oldnode != ctx.node() or opts.get(b'amend'): | ||
# Create or update Differential Revision | ||||
revision, diff = createdifferentialrevision( | ||||
|
r45211 | ctxs if fold else [ctx], | ||
|
r43346 | revid, | ||
lastrevphid, | ||||
|
r45135 | oldbasenode, | ||
|
r43346 | oldnode, | ||
olddiff, | ||||
actions, | ||||
opts.get(b'comment'), | ||||
) | ||||
|
r45211 | |||
if fold: | ||||
for ctx in ctxs: | ||||
diffmap[ctx.node()] = diff | ||||
else: | ||||
diffmap[ctx.node()] = diff | ||||
|
r42063 | newrevid = int(revision[b'object'][b'id']) | ||
|
r42642 | newrevphid = revision[b'object'][b'phid'] | ||
|
r39688 | if revid: | ||
action = b'updated' | ||||
else: | ||||
action = b'created' | ||||
# Create a local tag to note the association, if commit message | ||||
# does not have it already | ||||
|
r45211 | if not fold: | ||
m = _differentialrevisiondescre.search(ctx.description()) | ||||
if not m or int(m.group('id')) != newrevid: | ||||
tagname = b'D%d' % newrevid | ||||
tags.tag( | ||||
repo, | ||||
tagname, | ||||
ctx.node(), | ||||
message=None, | ||||
user=None, | ||||
date=None, | ||||
local=True, | ||||
) | ||||
|
r39688 | else: | ||
|
r42642 | # Nothing changed. But still set "newrevphid" so the next revision | ||
# could depend on this one and "newrevid" for the summary line. | ||||
|
r44906 | newrevphid = querydrev(repo.ui, b'%d' % revid)[0][b'phid'] | ||
|
r39688 | newrevid = revid | ||
action = b'skipped' | ||||
drevids.append(newrevid) | ||||
|
r42642 | lastrevphid = newrevphid | ||
|
r39688 | |||
|
r45211 | if fold: | ||
for c in ctxs: | ||||
if oldmap.get(c.node(), (None, None, None))[2]: | ||||
action = b'updated' | ||||
else: | ||||
action = b'created' | ||||
_print_phabsend_action(ui, c, newrevid, action) | ||||
break | ||||
|
r45138 | _print_phabsend_action(ui, ctx, newrevid, action) | ||
|
r39688 | # Update commit messages and remove tags | ||
if opts.get(b'amend'): | ||||
unfi = repo.unfiltered() | ||||
|
r42627 | drevs = callconduit(ui, b'differential.query', {b'ids': drevids}) | ||
|
r39688 | with repo.wlock(), repo.lock(), repo.transaction(b'phabsend'): | ||
|
r45214 | # Eagerly evaluate commits to restabilize before creating new | ||
# commits. The selected revisions are excluded because they are | ||||
# automatically restacked as part of the submission process. | ||||
restack = [ | ||||
c | ||||
for c in repo.set( | ||||
b"(%ld::) - (%ld) - unstable() - obsolete() - public()", | ||||
revs, | ||||
revs, | ||||
) | ||||
] | ||||
|
r39688 | wnode = unfi[b'.'].node() | ||
|
r43346 | mapping = {} # {oldnode: [newnode]} | ||
|
r45211 | newnodes = [] | ||
drevid = drevids[0] | ||||
|
r39688 | for i, rev in enumerate(revs): | ||
old = unfi[rev] | ||||
|
r45211 | if not fold: | ||
drevid = drevids[i] | ||||
|
r42063 | drev = [d for d in drevs if int(d[b'id']) == drevid][0] | ||
|
r45211 | |||
newdesc = get_amended_desc(drev, old, fold) | ||||
|
r39688 | # Make sure commit message contain "Differential Revision" | ||
|
r45212 | if ( | ||
old.description() != newdesc | ||||
or old.p1().node() in mapping | ||||
or old.p2().node() in mapping | ||||
): | ||||
|
r41198 | if old.phase() == phases.public: | ||
|
r43346 | ui.warn( | ||
|
r43347 | _(b"warning: not updating public commit %s\n") | ||
|
r43346 | % scmutil.formatchangeid(old) | ||
) | ||||
|
r41198 | continue | ||
|
r39688 | parents = [ | ||
mapping.get(old.p1().node(), (old.p1(),))[0], | ||||
mapping.get(old.p2().node(), (old.p2(),))[0], | ||||
] | ||||
|
r45995 | newdesc = rewriteutil.update_hash_refs( | ||
|
r46554 | repo, | ||
newdesc, | ||||
mapping, | ||||
|
r45995 | ) | ||
|
r39688 | new = context.metadataonlyctx( | ||
|
r43346 | repo, | ||
old, | ||||
parents=parents, | ||||
text=newdesc, | ||||
user=old.user(), | ||||
date=old.date(), | ||||
extra=old.extra(), | ||||
) | ||||
|
r39688 | |||
newnode = new.commit() | ||||
mapping[old.node()] = [newnode] | ||||
|
r45137 | |||
|
r45211 | if fold: | ||
# Defer updating the (single) Diff until all nodes are | ||||
# collected. No tags were created, so none need to be | ||||
# removed. | ||||
newnodes.append(newnode) | ||||
continue | ||||
|
r45137 | _amend_diff_properties( | ||
unfi, drevid, [newnode], diffmap[old.node()] | ||||
) | ||||
|
r45211 | |||
# Remove local tags since it's no longer necessary | ||||
tagname = b'D%d' % drevid | ||||
if tagname in repo.tags(): | ||||
tags.tag( | ||||
repo, | ||||
tagname, | ||||
|
r47771 | repo.nullid, | ||
|
r45211 | message=None, | ||
user=None, | ||||
date=None, | ||||
local=True, | ||||
) | ||||
elif fold: | ||||
# When folding multiple commits into one review with | ||||
# --fold, track even the commits that weren't amended, so | ||||
# that their association isn't lost if the properties are | ||||
# rewritten below. | ||||
newnodes.append(old.node()) | ||||
# If the submitted commits are public, no amend takes place so | ||||
# there are no newnodes and therefore no diff update to do. | ||||
if fold and newnodes: | ||||
diff = diffmap[old.node()] | ||||
# The diff object in diffmap doesn't have the local commits | ||||
# because that could be returned from differential.creatediff, | ||||
# not differential.querydiffs. So use the queried diff (if | ||||
# present), or force the amend (a new revision is being posted.) | ||||
if not olddiff or set(newnodes) != getlocalcommits(olddiff): | ||||
_debug(ui, b"updating local commit list for D%d\n" % drevid) | ||||
_amend_diff_properties(unfi, drevid, newnodes, diff) | ||||
else: | ||||
_debug( | ||||
ui, | ||||
b"local commit list for D%d is already up-to-date\n" | ||||
% drevid, | ||||
|
r43346 | ) | ||
|
r45211 | elif fold: | ||
_debug(ui, b"no newnodes to update\n") | ||||
|
r45214 | # Restack any children of first-time submissions that were orphaned | ||
# in the process. The ctx won't report that it is an orphan until | ||||
# the cleanup takes place below. | ||||
for old in restack: | ||||
parents = [ | ||||
mapping.get(old.p1().node(), (old.p1(),))[0], | ||||
mapping.get(old.p2().node(), (old.p2(),))[0], | ||||
] | ||||
new = context.metadataonlyctx( | ||||
repo, | ||||
old, | ||||
parents=parents, | ||||
|
r45995 | text=rewriteutil.update_hash_refs( | ||
repo, old.description(), mapping | ||||
), | ||||
|
r45214 | user=old.user(), | ||
date=old.date(), | ||||
extra=old.extra(), | ||||
) | ||||
newnode = new.commit() | ||||
# Don't obsolete unselected descendants of nodes that have not | ||||
# been changed in this transaction- that results in an error. | ||||
if newnode != old.node(): | ||||
mapping[old.node()] = [newnode] | ||||
_debug( | ||||
ui, | ||||
b"restabilizing %s as %s\n" | ||||
% (short(old.node()), short(newnode)), | ||||
) | ||||
else: | ||||
_debug( | ||||
ui, | ||||
b"not restabilizing unchanged %s\n" % short(old.node()), | ||||
) | ||||
|
r39688 | scmutil.cleanupnodes(repo, mapping, b'phabsend', fixphase=True) | ||
if wnode in mapping: | ||||
unfi.setparents(mapping[wnode][0]) | ||||
|
r43346 | |||
|
r39688 | # Map from "hg:meta" keys to header understood by "hg import". The order is | ||
# consistent with "hg export" output. | ||||
|
r43346 | _metanamemap = util.sortdict( | ||
[ | ||||
(b'user', b'User'), | ||||
(b'date', b'Date'), | ||||
(b'branch', b'Branch'), | ||||
(b'node', b'Node ID'), | ||||
(b'parent', b'Parent '), | ||||
] | ||||
) | ||||
|
r39688 | |||
def _confirmbeforesend(repo, revs, oldmap): | ||||
|
r42626 | url, token = readurltoken(repo.ui) | ||
|
r39688 | ui = repo.ui | ||
for rev in revs: | ||||
ctx = repo[rev] | ||||
oldnode, olddiff, drevid = oldmap.get(ctx.node(), (None, None, None)) | ||||
if drevid: | ||||
|
r43628 | drevdesc = ui.label(b'D%d' % drevid, b'phabricator.drev') | ||
|
r39688 | else: | ||
drevdesc = ui.label(_(b'NEW'), b'phabricator.drev') | ||||
|
r43346 | ui.write( | ||
|
r46357 | _(b'%s - %s\n') | ||
|
r43346 | % ( | ||
drevdesc, | ||||
|
r46357 | cmdutil.format_changeset_summary(ui, ctx, b'phabsend'), | ||
|
r43346 | ) | ||
) | ||||
|
r39688 | |||
|
r43346 | if ui.promptchoice( | ||
|
r45471 | _(b'Send the above changes to %s (Y/n)?$$ &Yes $$ &No') % url | ||
|
r43346 | ): | ||
|
r39688 | return False | ||
return True | ||||
|
r43346 | |||
_knownstatusnames = { | ||||
b'accepted', | ||||
b'needsreview', | ||||
b'needsrevision', | ||||
b'closed', | ||||
b'abandoned', | ||||
|
r44309 | b'changesplanned', | ||
|
r43346 | } | ||
|
r39688 | |||
def _getstatusname(drev): | ||||
"""get normalized status name from a Differential Revision""" | ||||
|
r42063 | return drev[b'statusName'].replace(b' ', b'').lower() | ||
|
r39688 | |||
|
r43346 | |||
|
r39688 | # Small language to specify differential revisions. Support symbols: (), :X, | ||
# +, and -. | ||||
_elements = { | ||||
# token-type: binding-strength, primary, prefix, infix, suffix | ||||
|
r43346 | b'(': (12, None, (b'group', 1, b')'), None, None), | ||
b':': (8, None, (b'ancestors', 8), None, None), | ||||
b'&': (5, None, None, (b'and_', 5), None), | ||||
b'+': (4, None, None, (b'add', 4), None), | ||||
b'-': (4, None, None, (b'sub', 4), None), | ||||
b')': (0, None, None, None, None), | ||||
|
r39688 | b'symbol': (0, b'symbol', None, None, None), | ||
|
r43346 | b'end': (0, None, None, None, None), | ||
|
r39688 | } | ||
|
r43346 | |||
|
r39688 | def _tokenize(text): | ||
|
r43346 | view = memoryview(text) # zero-copy slice | ||
|
r39688 | special = b'():+-& ' | ||
pos = 0 | ||||
length = len(text) | ||||
while pos < length: | ||||
|
r43346 | symbol = b''.join( | ||
itertools.takewhile( | ||||
lambda ch: ch not in special, pycompat.iterbytestr(view[pos:]) | ||||
) | ||||
) | ||||
|
r39688 | if symbol: | ||
yield (b'symbol', symbol, pos) | ||||
pos += len(symbol) | ||||
|
r43346 | else: # special char, ignore space | ||
|
r43576 | if text[pos : pos + 1] != b' ': | ||
yield (text[pos : pos + 1], None, pos) | ||||
|
r39688 | pos += 1 | ||
yield (b'end', None, pos) | ||||
|
r43346 | |||
|
r39688 | def _parse(text): | ||
tree, pos = parser.parser(_elements).parse(_tokenize(text)) | ||||
if pos != len(text): | ||||
raise error.ParseError(b'invalid token', pos) | ||||
return tree | ||||
|
r43346 | |||
|
r39688 | def _parsedrev(symbol): | ||
"""str -> int or None, ex. 'D45' -> 45; '12' -> 12; 'x' -> None""" | ||||
if symbol.startswith(b'D') and symbol[1:].isdigit(): | ||||
return int(symbol[1:]) | ||||
if symbol.isdigit(): | ||||
return int(symbol) | ||||
|
r43346 | |||
|
r39688 | def _prefetchdrevs(tree): | ||
"""return ({single-drev-id}, {ancestor-drev-id}) to prefetch""" | ||||
drevs = set() | ||||
ancestordrevs = set() | ||||
op = tree[0] | ||||
if op == b'symbol': | ||||
r = _parsedrev(tree[1]) | ||||
if r: | ||||
drevs.add(r) | ||||
elif op == b'ancestors': | ||||
r, a = _prefetchdrevs(tree[1]) | ||||
drevs.update(r) | ||||
ancestordrevs.update(r) | ||||
ancestordrevs.update(a) | ||||
else: | ||||
for t in tree[1:]: | ||||
r, a = _prefetchdrevs(t) | ||||
drevs.update(r) | ||||
ancestordrevs.update(a) | ||||
return drevs, ancestordrevs | ||||
|
r43346 | |||
|
r44906 | def querydrev(ui, spec): | ||
|
r39688 | """return a list of "Differential Revision" dicts | ||
spec is a string using a simple query language, see docstring in phabread | ||||
for details. | ||||
A "Differential Revision dict" looks like: | ||||
{ | ||||
|
r44877 | "activeDiffPHID": "PHID-DIFF-xoqnjkobbm6k4dk6hi72", | ||
"authorPHID": "PHID-USER-tv3ohwc4v4jeu34otlye", | ||||
"auxiliary": { | ||||
"phabricator:depends-on": [ | ||||
"PHID-DREV-gbapp366kutjebt7agcd" | ||||
] | ||||
"phabricator:projects": [], | ||||
}, | ||||
"branch": "default", | ||||
"ccs": [], | ||||
"commits": [], | ||||
|
r39688 | "dateCreated": "1499181406", | ||
"dateModified": "1499182103", | ||||
"diffs": [ | ||||
"3", | ||||
"4", | ||||
], | ||||
|
r44877 | "hashes": [], | ||
"id": "2", | ||||
"lineCount": "2", | ||||
"phid": "PHID-DREV-672qvysjcczopag46qty", | ||||
"properties": {}, | ||||
"repositoryPHID": "PHID-REPO-hub2hx62ieuqeheznasv", | ||||
|
r39688 | "reviewers": [], | ||
"sourcePath": null | ||||
|
r44877 | "status": "0", | ||
"statusName": "Needs Review", | ||||
"summary": "", | ||||
"testPlan": "", | ||||
"title": "example", | ||||
"uri": "https://phab.example.com/D2", | ||||
|
r39688 | } | ||
""" | ||||
|
r44877 | # TODO: replace differential.query and differential.querydiffs with | ||
# differential.diff.search because the former (and their output) are | ||||
# frozen, and planned to be deprecated and removed. | ||||
|
r43346 | |||
|
r39688 | def fetch(params): | ||
"""params -> single drev or None""" | ||||
|
r42068 | key = (params.get(b'ids') or params.get(b'phids') or [None])[0] | ||
|
r39688 | if key in prefetched: | ||
return prefetched[key] | ||||
|
r44906 | drevs = callconduit(ui, b'differential.query', params) | ||
|
r39688 | # Fill prefetched with the result | ||
for drev in drevs: | ||||
|
r42063 | prefetched[drev[b'phid']] = drev | ||
prefetched[int(drev[b'id'])] = drev | ||||
|
r39688 | if key not in prefetched: | ||
|
r43346 | raise error.Abort( | ||
_(b'cannot get Differential Revision %r') % params | ||||
) | ||||
|
r39688 | return prefetched[key] | ||
def getstack(topdrevids): | ||||
"""given a top, get a stack from the bottom, [id] -> [id]""" | ||||
visited = set() | ||||
result = [] | ||||
|
r42068 | queue = [{b'ids': [i]} for i in topdrevids] | ||
|
r39688 | while queue: | ||
params = queue.pop() | ||||
drev = fetch(params) | ||||
|
r42063 | if drev[b'id'] in visited: | ||
|
r39688 | continue | ||
|
r42063 | visited.add(drev[b'id']) | ||
result.append(int(drev[b'id'])) | ||||
auxiliary = drev.get(b'auxiliary', {}) | ||||
depends = auxiliary.get(b'phabricator:depends-on', []) | ||||
|
r39688 | for phid in depends: | ||
queue.append({b'phids': [phid]}) | ||||
result.reverse() | ||||
return smartset.baseset(result) | ||||
# Initialize prefetch cache | ||||
|
r43346 | prefetched = {} # {id or phid: drev} | ||
|
r39688 | |||
tree = _parse(spec) | ||||
drevs, ancestordrevs = _prefetchdrevs(tree) | ||||
# developer config: phabricator.batchsize | ||||
|
r44906 | batchsize = ui.configint(b'phabricator', b'batchsize') | ||
|
r39688 | |||
# Prefetch Differential Revisions in batch | ||||
tofetch = set(drevs) | ||||
for r in ancestordrevs: | ||||
tofetch.update(range(max(1, r - batchsize), r + 1)) | ||||
if drevs: | ||||
|
r42063 | fetch({b'ids': list(tofetch)}) | ||
|
r39688 | validids = sorted(set(getstack(list(ancestordrevs))) | set(drevs)) | ||
# Walk through the tree, return smartsets | ||||
def walk(tree): | ||||
op = tree[0] | ||||
if op == b'symbol': | ||||
drev = _parsedrev(tree[1]) | ||||
if drev: | ||||
return smartset.baseset([drev]) | ||||
elif tree[1] in _knownstatusnames: | ||||
|
r43346 | drevs = [ | ||
r | ||||
for r in validids | ||||
if _getstatusname(prefetched[r]) == tree[1] | ||||
] | ||||
|
r39688 | return smartset.baseset(drevs) | ||
else: | ||||
raise error.Abort(_(b'unknown symbol: %s') % tree[1]) | ||||
elif op in {b'and_', b'add', b'sub'}: | ||||
assert len(tree) == 3 | ||||
return getattr(operator, op)(walk(tree[1]), walk(tree[2])) | ||||
elif op == b'group': | ||||
return walk(tree[1]) | ||||
elif op == b'ancestors': | ||||
return getstack(walk(tree[1])) | ||||
else: | ||||
raise error.ProgrammingError(b'illegal tree: %r' % tree) | ||||
return [prefetched[r] for r in walk(tree)] | ||||
|
r43346 | |||
|
r39688 | def getdescfromdrev(drev): | ||
"""get description (commit message) from "Differential Revision" | ||||
This is similar to differential.getcommitmessage API. But we only care | ||||
about limited fields: title, summary, test plan, and URL. | ||||
""" | ||||
|
r42063 | title = drev[b'title'] | ||
summary = drev[b'summary'].rstrip() | ||||
testplan = drev[b'testPlan'].rstrip() | ||||
|
r39688 | if testplan: | ||
testplan = b'Test Plan:\n%s' % testplan | ||||
|
r42063 | uri = b'Differential Revision: %s' % drev[b'uri'] | ||
|
r39688 | return b'\n\n'.join(filter(None, [title, summary, testplan, uri])) | ||
|
r43346 | |||
|
r45134 | def get_amended_desc(drev, ctx, folded): | ||
"""similar to ``getdescfromdrev``, but supports a folded series of commits | ||||
This is used when determining if an individual commit needs to have its | ||||
message amended after posting it for review. The determination is made for | ||||
each individual commit, even when they were folded into one review. | ||||
""" | ||||
if not folded: | ||||
return getdescfromdrev(drev) | ||||
uri = b'Differential Revision: %s' % drev[b'uri'] | ||||
# Since the commit messages were combined when posting multiple commits | ||||
# with --fold, the fields can't be read from Phabricator here, or *all* | ||||
# affected local revisions will end up with the same commit message after | ||||
# the URI is amended in. Append in the DREV line, or update it if it | ||||
# exists. At worst, this means commit message or test plan updates on | ||||
# Phabricator aren't propagated back to the repository, but that seems | ||||
# reasonable for the case where local commits are effectively combined | ||||
# in Phabricator. | ||||
m = _differentialrevisiondescre.search(ctx.description()) | ||||
if not m: | ||||
return b'\n\n'.join([ctx.description(), uri]) | ||||
return _differentialrevisiondescre.sub(uri, ctx.description()) | ||||
|
r45136 | def getlocalcommits(diff): | ||
"""get the set of local commits from a diff object | ||||
See ``getdiffmeta()`` for an example diff object. | ||||
""" | ||||
props = diff.get(b'properties') or {} | ||||
commits = props.get(b'local:commits') or {} | ||||
if len(commits) > 1: | ||||
return {bin(c) for c in commits.keys()} | ||||
# Storing the diff metadata predates storing `local:commits`, so continue | ||||
# to use that in the --no-fold case. | ||||
return {bin(getdiffmeta(diff).get(b'node', b'')) or None} | ||||
|
r39688 | def getdiffmeta(diff): | ||
"""get commit metadata (date, node, user, p1) from a diff object | ||||
The metadata could be "hg:meta", sent by phabsend, like: | ||||
"properties": { | ||||
"hg:meta": { | ||||
|
r44877 | "branch": "default", | ||
|
r39688 | "date": "1499571514 25200", | ||
"node": "98c08acae292b2faf60a279b4189beb6cff1414d", | ||||
"user": "Foo Bar <foo@example.com>", | ||||
"parent": "6d0abad76b30e4724a37ab8721d630394070fe16" | ||||
} | ||||
} | ||||
Or converted from "local:commits", sent by "arc", like: | ||||
"properties": { | ||||
"local:commits": { | ||||
"98c08acae292b2faf60a279b4189beb6cff1414d": { | ||||
"author": "Foo Bar", | ||||
|
r44877 | "authorEmail": "foo@example.com" | ||
|
r39688 | "branch": "default", | ||
"commit": "98c08acae292b2faf60a279b4189beb6cff1414d", | ||||
|
r44877 | "local": "1000", | ||
"message": "...", | ||||
"parents": ["6d0abad76b30e4724a37ab8721d630394070fe16"], | ||||
|
r39688 | "rev": "98c08acae292b2faf60a279b4189beb6cff1414d", | ||
"summary": "...", | ||||
|
r44877 | "tag": "", | ||
"time": 1499546314, | ||||
|
r39688 | } | ||
} | ||||
} | ||||
Note: metadata extracted from "local:commits" will lose time zone | ||||
information. | ||||
""" | ||||
|
r42063 | props = diff.get(b'properties') or {} | ||
meta = props.get(b'hg:meta') | ||||
|
r42442 | if not meta: | ||
if props.get(b'local:commits'): | ||||
commit = sorted(props[b'local:commits'].values())[0] | ||||
meta = {} | ||||
if b'author' in commit and b'authorEmail' in commit: | ||||
|
r43346 | meta[b'user'] = b'%s <%s>' % ( | ||
commit[b'author'], | ||||
commit[b'authorEmail'], | ||||
) | ||||
|
r42442 | if b'time' in commit: | ||
|
r42837 | meta[b'date'] = b'%d 0' % int(commit[b'time']) | ||
|
r42442 | if b'branch' in commit: | ||
meta[b'branch'] = commit[b'branch'] | ||||
node = commit.get(b'commit', commit.get(b'rev')) | ||||
if node: | ||||
meta[b'node'] = node | ||||
if len(commit.get(b'parents', ())) >= 1: | ||||
meta[b'parent'] = commit[b'parents'][0] | ||||
else: | ||||
meta = {} | ||||
if b'date' not in meta and b'dateCreated' in diff: | ||||
meta[b'date'] = b'%s 0' % diff[b'dateCreated'] | ||||
if b'branch' not in meta and diff.get(b'branch'): | ||||
meta[b'branch'] = diff[b'branch'] | ||||
if b'parent' not in meta and diff.get(b'sourceControlBaseRevision'): | ||||
meta[b'parent'] = diff[b'sourceControlBaseRevision'] | ||||
return meta | ||||
|
r39688 | |||
|
r43346 | |||
|
r45076 | def _getdrevs(ui, stack, specs): | ||
|
r45069 | """convert user supplied DREVSPECs into "Differential Revision" dicts | ||
See ``hg help phabread`` for how to specify each DREVSPEC. | ||||
""" | ||||
|
r45076 | if len(specs) > 0: | ||
|
r45069 | |||
def _formatspec(s): | ||||
if stack: | ||||
s = b':(%s)' % s | ||||
return b'(%s)' % s | ||||
|
r45076 | spec = b'+'.join(pycompat.maplist(_formatspec, specs)) | ||
|
r45069 | |||
drevs = querydrev(ui, spec) | ||||
if drevs: | ||||
return drevs | ||||
raise error.Abort(_(b"empty DREVSPEC set")) | ||||
|
r44905 | def readpatch(ui, drevs, write): | ||
|
r39688 | """generate plain-text patch readable by 'hg import' | ||
|
r44909 | write takes a list of (DREV, bytes), where DREV is the differential number | ||
(as bytes, without the "D" prefix) and the bytes are the text of a patch | ||||
to be imported. drevs is what "querydrev" returns, results of | ||||
|
r39688 | "differential.query". | ||
""" | ||||
# Prefetch hg:meta property for all diffs | ||||
|
r44937 | diffids = sorted({max(int(v) for v in drev[b'diffs']) for drev in drevs}) | ||
|
r44905 | diffs = callconduit(ui, b'differential.querydiffs', {b'ids': diffids}) | ||
|
r39688 | |||
|
r44909 | patches = [] | ||
|
r39688 | # Generate patch for each drev | ||
for drev in drevs: | ||||
|
r44905 | ui.note(_(b'reading D%s\n') % drev[b'id']) | ||
|
r39688 | |||
|
r42063 | diffid = max(int(v) for v in drev[b'diffs']) | ||
|
r44905 | body = callconduit(ui, b'differential.getrawdiff', {b'diffID': diffid}) | ||
|
r39688 | desc = getdescfromdrev(drev) | ||
header = b'# HG changeset patch\n' | ||||
# Try to preserve metadata from hg:meta property. Write hg patch | ||||
# headers that can be read by the "import" command. See patchheadermap | ||||
# and extract in mercurial/patch.py for supported headers. | ||||
|
r42068 | meta = getdiffmeta(diffs[b'%d' % diffid]) | ||
|
r39688 | for k in _metanamemap.keys(): | ||
if k in meta: | ||||
header += b'# %s %s\n' % (_metanamemap[k], meta[k]) | ||||
content = b'%s%s\n%s' % (header, desc, body) | ||||
|
r44909 | patches.append((drev[b'id'], content)) | ||
# Write patches to the supplied callback | ||||
write(patches) | ||||
|
r39688 | |||
|
r43346 | |||
@vcrcommand( | ||||
b'phabread', | ||||
[(b'', b'stack', False, _(b'read dependencies'))], | ||||
|
r45071 | _(b'DREVSPEC... [OPTIONS]'), | ||
|
r43346 | helpcategory=command.CATEGORY_IMPORT_EXPORT, | ||
|
r44910 | optionalrepo=True, | ||
|
r43346 | ) | ||
|
r45071 | def phabread(ui, repo, *specs, **opts): | ||
|
r39688 | """print patches from Phabricator suitable for importing | ||
DREVSPEC could be a Differential Revision identity, like ``D123``, or just | ||||
the number ``123``. It could also have common operators like ``+``, ``-``, | ||||
``&``, ``(``, ``)`` for complex queries. Prefix ``:`` could be used to | ||||
|
r45071 | select a stack. If multiple DREVSPEC values are given, the result is the | ||
union of each individually evaluated value. No attempt is currently made | ||||
to reorder the values to run from parent to child. | ||||
|
r39688 | |||
``abandoned``, ``accepted``, ``closed``, ``needsreview``, ``needsrevision`` | ||||
could be used to filter patches by status. For performance reason, they | ||||
only represent a subset of non-status selections and cannot be used alone. | ||||
For example, ``:D6+8-(2+D4)`` selects a stack up to D6, plus D8 and exclude | ||||
D2 and D4. ``:D9 & needsreview`` selects "Needs Review" revisions in a | ||||
stack up to D9. | ||||
If --stack is given, follow dependencies information and read all patches. | ||||
It is equivalent to the ``:`` operator. | ||||
""" | ||||
|
r42136 | opts = pycompat.byteskwargs(opts) | ||
|
r45071 | drevs = _getdrevs(ui, opts.get(b'stack'), specs) | ||
|
r44909 | |||
def _write(patches): | ||||
for drev, content in patches: | ||||
ui.write(content) | ||||
|
r44910 | readpatch(ui, drevs, _write) | ||
|
r39688 | |||
|
r43346 | |||
@vcrcommand( | ||||
|
r45039 | b'phabimport', | ||
[(b'', b'stack', False, _(b'import dependencies as well'))], | ||||
|
r45071 | _(b'DREVSPEC... [OPTIONS]'), | ||
|
r45039 | helpcategory=command.CATEGORY_IMPORT_EXPORT, | ||
) | ||||
|
r45071 | def phabimport(ui, repo, *specs, **opts): | ||
|
r45039 | """import patches from Phabricator for the specified Differential Revisions | ||
The patches are read and applied starting at the parent of the working | ||||
directory. | ||||
See ``hg help phabread`` for how to specify DREVSPEC. | ||||
""" | ||||
opts = pycompat.byteskwargs(opts) | ||||
# --bypass avoids losing exec and symlink bits when importing on Windows, | ||||
# and allows importing with a dirty wdir. It also aborts instead of leaving | ||||
# rejects. | ||||
opts[b'bypass'] = True | ||||
# Mandatory default values, synced with commands.import | ||||
opts[b'strip'] = 1 | ||||
opts[b'prefix'] = b'' | ||||
# Evolve 9.3.0 assumes this key is present in cmdutil.tryimportone() | ||||
opts[b'obsolete'] = False | ||||
|
r45040 | if ui.configbool(b'phabimport', b'secret'): | ||
opts[b'secret'] = True | ||||
|
r45041 | if ui.configbool(b'phabimport', b'obsolete'): | ||
opts[b'obsolete'] = True # Handled by evolve wrapping tryimportone() | ||||
|
r45040 | |||
|
r45039 | def _write(patches): | ||
parents = repo[None].parents() | ||||
with repo.wlock(), repo.lock(), repo.transaction(b'phabimport'): | ||||
for drev, contents in patches: | ||||
ui.status(_(b'applying patch from D%s\n') % drev) | ||||
with patch.extract(ui, pycompat.bytesio(contents)) as patchdata: | ||||
msg, node, rej = cmdutil.tryimportone( | ||||
ui, | ||||
repo, | ||||
patchdata, | ||||
parents, | ||||
opts, | ||||
[], | ||||
None, # Never update wdir to another revision | ||||
) | ||||
if not node: | ||||
raise error.Abort(_(b'D%s: no diffs found') % drev) | ||||
ui.note(msg + b'\n') | ||||
parents = [repo[node]] | ||||
|
r45071 | drevs = _getdrevs(ui, opts.get(b'stack'), specs) | ||
|
r45039 | |||
readpatch(repo.ui, drevs, _write) | ||||
@vcrcommand( | ||||
|
r43346 | b'phabupdate', | ||
[ | ||||
(b'', b'accept', False, _(b'accept revisions')), | ||||
(b'', b'reject', False, _(b'reject revisions')), | ||||
|
r45696 | (b'', b'request-review', False, _(b'request review on revisions')), | ||
|
r43346 | (b'', b'abandon', False, _(b'abandon revisions')), | ||
(b'', b'reclaim', False, _(b'reclaim revisions')), | ||||
|
r45697 | (b'', b'close', False, _(b'close revisions')), | ||
|
r45698 | (b'', b'reopen', False, _(b'reopen revisions')), | ||
|
r45695 | (b'', b'plan-changes', False, _(b'plan changes for revisions')), | ||
|
r45700 | (b'', b'resign', False, _(b'resign as a reviewer from revisions')), | ||
|
r45699 | (b'', b'commandeer', False, _(b'commandeer revisions')), | ||
|
r43346 | (b'm', b'comment', b'', _(b'comment on the last revision')), | ||
|
r46651 | (b'r', b'rev', b'', _(b'local revision to update'), _(b'REV')), | ||
|
r43346 | ], | ||
|
r46651 | _(b'[DREVSPEC...| -r REV...] [OPTIONS]'), | ||
|
r43346 | helpcategory=command.CATEGORY_IMPORT_EXPORT, | ||
|
r44908 | optionalrepo=True, | ||
|
r43346 | ) | ||
|
r45071 | def phabupdate(ui, repo, *specs, **opts): | ||
|
r39688 | """update Differential Revision in batch | ||
DREVSPEC selects revisions. See :hg:`help phabread` for its usage. | ||||
""" | ||||
|
r42136 | opts = pycompat.byteskwargs(opts) | ||
|
r45695 | transactions = [ | ||
b'abandon', | ||||
b'accept', | ||||
|
r45697 | b'close', | ||
|
r45699 | b'commandeer', | ||
|
r45695 | b'plan-changes', | ||
b'reclaim', | ||||
b'reject', | ||||
|
r45698 | b'reopen', | ||
|
r45696 | b'request-review', | ||
|
r45700 | b'resign', | ||
|
r45695 | ] | ||
flags = [n for n in transactions if opts.get(n.replace(b'-', b'_'))] | ||||
|
r39688 | if len(flags) > 1: | ||
raise error.Abort(_(b'%s cannot be used together') % b', '.join(flags)) | ||||
actions = [] | ||||
for f in flags: | ||||
|
r43667 | actions.append({b'type': f, b'value': True}) | ||
|
r39688 | |||
|
r46651 | revs = opts.get(b'rev') | ||
if revs: | ||||
if not repo: | ||||
raise error.InputError(_(b'--rev requires a repository')) | ||||
if specs: | ||||
raise error.InputError(_(b'cannot specify both DREVSPEC and --rev')) | ||||
drevmap = getdrevmap(repo, scmutil.revrange(repo, [revs])) | ||||
specs = [] | ||||
unknown = [] | ||||
for r, d in pycompat.iteritems(drevmap): | ||||
if d is None: | ||||
unknown.append(repo[r]) | ||||
else: | ||||
specs.append(b'D%d' % d) | ||||
if unknown: | ||||
raise error.InputError( | ||||
_(b'selected revisions without a Differential: %s') | ||||
% scmutil.nodesummaries(repo, unknown) | ||||
) | ||||
|
r45071 | drevs = _getdrevs(ui, opts.get(b'stack'), specs) | ||
|
r39688 | for i, drev in enumerate(drevs): | ||
if i + 1 == len(drevs) and opts.get(b'comment'): | ||||
actions.append({b'type': b'comment', b'value': opts[b'comment']}) | ||||
if actions: | ||||
|
r43346 | params = { | ||
b'objectIdentifier': drev[b'phid'], | ||||
b'transactions': actions, | ||||
} | ||||
|
r42627 | callconduit(ui, b'differential.revision.edit', params) | ||
|
r39688 | |||
|
r43346 | |||
|
r43243 | @eh.templatekeyword(b'phabreview', requires={b'ctx'}) | ||
|
r39688 | def template_review(context, mapping): | ||
""":phabreview: Object describing the review for this changeset. | ||||
Has attributes `url` and `id`. | ||||
""" | ||||
ctx = context.resource(mapping, b'ctx') | ||||
m = _differentialrevisiondescre.search(ctx.description()) | ||||
if m: | ||||
|
r43346 | return templateutil.hybriddict( | ||
|
r46554 | { | ||
b'url': m.group('url'), | ||||
b'id': b"D%s" % m.group('id'), | ||||
} | ||||
|
r43346 | ) | ||
|
r41199 | else: | ||
tags = ctx.repo().nodetags(ctx.node()) | ||||
for t in tags: | ||||
if _differentialrevisiontagre.match(t): | ||||
url = ctx.repo().ui.config(b'phabricator', b'url') | ||||
if not url.endswith(b'/'): | ||||
url += b'/' | ||||
url += t | ||||
|
r46554 | return templateutil.hybriddict( | ||
{ | ||||
b'url': url, | ||||
b'id': t, | ||||
} | ||||
) | ||||
|
r41199 | return None | ||
|
r44291 | |||
|
r44292 | @eh.templatekeyword(b'phabstatus', requires={b'ctx', b'repo', b'ui'}) | ||
def template_status(context, mapping): | ||||
|
r46554 | """:phabstatus: String. Status of Phabricator differential.""" | ||
|
r44292 | ctx = context.resource(mapping, b'ctx') | ||
repo = context.resource(mapping, b'repo') | ||||
ui = context.resource(mapping, b'ui') | ||||
rev = ctx.rev() | ||||
try: | ||||
drevid = getdrevmap(repo, [rev])[rev] | ||||
except KeyError: | ||||
return None | ||||
drevs = callconduit(ui, b'differential.query', {b'ids': [drevid]}) | ||||
for drev in drevs: | ||||
if int(drev[b'id']) == drevid: | ||||
return templateutil.hybriddict( | ||||
|
r46554 | { | ||
b'url': drev[b'uri'], | ||||
b'status': drev[b'statusName'], | ||||
} | ||||
|
r44292 | ) | ||
return None | ||||
|
r44291 | @show.showview(b'phabstatus', csettopic=b'work') | ||
def phabstatusshowview(ui, repo, displayer): | ||||
"""Phabricator differiential status""" | ||||
revs = repo.revs('sort(_underway(), topo)') | ||||
drevmap = getdrevmap(repo, revs) | ||||
|
r44937 | unknownrevs, drevids, revsbydrevid = [], set(), {} | ||
|
r44291 | for rev, drevid in pycompat.iteritems(drevmap): | ||
if drevid is not None: | ||||
drevids.add(drevid) | ||||
|
r44937 | revsbydrevid.setdefault(drevid, set()).add(rev) | ||
|
r44291 | else: | ||
unknownrevs.append(rev) | ||||
drevs = callconduit(ui, b'differential.query', {b'ids': list(drevids)}) | ||||
drevsbyrev = {} | ||||
for drev in drevs: | ||||
for rev in revsbydrevid[int(drev[b'id'])]: | ||||
drevsbyrev[rev] = drev | ||||
def phabstatus(ctx): | ||||
drev = drevsbyrev[ctx.rev()] | ||||
|
r44310 | status = ui.label( | ||
b'%(statusName)s' % drev, | ||||
b'phabricator.status.%s' % _getstatusname(drev), | ||||
) | ||||
ui.write(b"\n%s %s\n" % (drev[b'uri'], status)) | ||||
|
r44291 | |||
revs -= smartset.baseset(unknownrevs) | ||||
revdag = graphmod.dagwalker(repo, revs) | ||||
ui.setconfig(b'experimental', b'graphshorten', True) | ||||
displayer._exthook = phabstatus | ||||
nodelen = show.longestshortest(repo, revs) | ||||
logcmdutil.displaygraph( | ||||
ui, | ||||
repo, | ||||
revdag, | ||||
displayer, | ||||
graphmod.asciiedges, | ||||
props={b'nodelen': nodelen}, | ||||
) | ||||