sapling/eden/scm/edenscm/hgext/fbscmquery.py

226 lines
6.6 KiB
Python
Raw Normal View History

# Copyright (c) Facebook, Inc. and its affiliates.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2.
# scmquery.py
# An extension to augement hg with information obtained from SCMQuery
import json
import re
from urllib import urlencode
from edenscm.mercurial import (
extensions,
namespaces,
node,
registrar,
revset,
smartset,
templater,
)
from edenscm.mercurial.i18n import _
from edenscm.mercurial.node import bin
from edenscm.mercurial.pycompat import range
from edenscm.mercurial.util import httplib
from .extlib.phabricator import arcconfig, graphql
namespacepredicate = registrar.namespacepredicate()
DEFAULT_TIMEOUT = 60
MAX_CONNECT_RETRIES = 3
githashre = re.compile("g([0-9a-f]{40})")
svnrevre = re.compile("^r[A-Z]+(\d+)$")
phabhashre = re.compile("^r([A-Z]+)([0-9a-f]{12,40})$")
def uisetup(ui):
def _globalrevswrapper(loaded):
if loaded:
globalrevsmod = extensions.find("globalrevs")
extensions.wrapfunction(
globalrevsmod, "_lookupglobalrev", _scmquerylookupglobalrev
)
if ui.configbool("globalrevs", "scmquerylookup"):
extensions.afterloaded("globalrevs", _globalrevswrapper)
revset.symbols["gitnode"] = gitnode
gitnode._weight = 10
@templater.templatefunc("mirrornode")
def mirrornode(ctx, mapping, args):
"""template: find this commit in other repositories"""
reponame = mapping["repo"].ui.config("fbscmquery", "reponame")
if not reponame:
# We don't know who we are, so we can't ask for a translation
return ""
if mapping["ctx"].mutable():
# Local commits don't have translations
return ""
node = mapping["ctx"].hex()
args = [f(ctx, mapping, a) for f, a in args]
if len(args) == 1:
torepo, totype = reponame, args[0]
else:
torepo, totype = args
try:
client = graphql.Client(repo=mapping["repo"])
return client.getmirroredrev(reponame, "hg", torepo, totype, node)
except arcconfig.ArcConfigError:
mapping["repo"].ui.warn(_("couldn't read .arcconfig or .arcrc"))
return ""
except graphql.ClientError as e:
mapping["repo"].ui.warn((str(e.msg) + "\n"))
return ""
templatekeyword = registrar.templatekeyword()
@templatekeyword("gitnode")
def showgitnode(repo, ctx, templ, **args):
"""Return the git revision corresponding to a given hg rev"""
# Try reading from commit extra first.
extra = ctx.extra()
if "hg-git-rename-source" in extra:
hexnode = extra.get("convert_revision")
if hexnode:
return hexnode
reponame = repo.ui.config("fbscmquery", "reponame")
if not reponame:
# We don't know who we are, so we can't ask for a translation
return ""
backingrepos = repo.ui.configlist("fbscmquery", "backingrepos", default=[reponame])
if ctx.mutable():
# Local commits don't have translations
return ""
matches = []
for backingrepo in backingrepos:
try:
client = graphql.Client(repo=repo)
githash = client.getmirroredrev(
reponame, "hg", backingrepo, "git", ctx.hex()
)
if githash != "":
matches.append((backingrepo, githash))
except (graphql.ClientError, arcconfig.ArcConfigError):
pass
if len(matches) == 0:
return ""
elif len(backingrepos) == 1:
return matches[0][1]
else:
# in case it's not clear, the sort() is to ensure the output is in a
# deterministic order.
matches.sort()
return "; ".join(["{0}: {1}".format(*match) for match in matches])
def gitnode(repo, subset, x):
"""``gitnode(id)``
Return the hg revision corresponding to a given git rev."""
l = revset.getargs(x, 1, 1, _("id requires one argument"))
n = revset.getstring(l[0], _("id requires a string"))
reponame = repo.ui.config("fbscmquery", "reponame")
if not reponame:
# We don't know who we are, so we can't ask for a translation
return subset.filter(lambda r: False)
backingrepos = repo.ui.configlist("fbscmquery", "backingrepos", default=[reponame])
lasterror = None
hghash = None
for backingrepo in backingrepos:
try:
client = graphql.Client(repo=repo)
hghash = client.getmirroredrev(backingrepo, "git", reponame, "hg", n)
if hghash != "":
break
except Exception as ex:
lasterror = ex
if not hghash:
if lasterror:
repo.ui.warn(
("Could not translate revision {0}: {1}\n".format(n, lasterror))
)
else:
repo.ui.warn(("Could not translate revision {0}\n".format(n)))
return subset.filter(lambda r: False)
rn = repo[node.bin(hghash)].rev()
return subset & smartset.baseset([rn])
@namespacepredicate("conduit", priority=70)
def _getnamespace(_repo):
return namespaces.namespace(
listnames=lambda repo: [], namemap=_phablookup, nodemap=lambda repo, node: []
)
def _phablookup(repo, phabrev):
fbconduit: de-phabricator-ize hg and git hashes Summary: Remove the need to ommit the rREPO prefix when copy pasting from phabricator and looking up a commit. Test Plan: Ran the following in phabricator enabled HG repos 11/13 12:15 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBS27aa00fb74d9a3b82756dad6ff26fe253f1e9a70 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 992495:27aa00fb74d9 user: Peng Li <pengli@fb.com> date: Tue Nov 03 10:29:01 2015 -0800 summary: Add a simple root dir arc library 11/13 12:21 cdelahousse@dev4253 ~/fbjava $ hg log -r rFBA8f1335e6d588 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 18654:8f1335e6d588 user: Ryan Menezes <ryandm@fb.com> date: Thu Oct 22 09:59:27 2015 -0700 summary: move jenkins hook into fbjava/arcanist (fbjava changes) Tried the same on a repo with a git mirror that exists in phabricator: 11/16 11:40 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBCODE8272d25d65869ce059024ff38c7051388ad7b802 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 1027287:f26a9c32ae08 user: Oleksandr Kuvshynov <oleksandr@fb.com> date: Sun Nov 15 19:16:39 2015 -0800 summary: [mf] simple cleanup of feed story view If a git hash is too small, abort: $ hg log -r rFBCODE8272d25d65869ce059024f --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py abort: git hash must be 40 characters The previous commands depends on a list of repos set in my hgrc. See (D2660070) [fbconduit] gitcallsigns=LK, CFPUMA, CFSV, CFGK, CFMF, CFGMON, CF, RSIGMA, FA,· WAWEBCLIENT, SKY, FBCODE, FBOBJC, WAWP, SV, OVRMOBILEMAIN, WAANDROID, IGSRV, CPE, MSS, ANDROIDSDK, BUCK, IT-CHEF, WABB, ITINFRA, SIGMA, IOSSDK Reviewers: #sourcecontrol, durham, ericsumner Reviewed By: durham, ericsumner Subscribers: rmcelroy, ericsumner Differential Revision: https://phabricator.fb.com/D2653497 Tasks: 8361368 Signature: t1:2653497:1447480768:295079a7793e182ddea3aeece9cfaead1bfc1a57
2015-11-18 02:22:08 +03:00
# Is the given revset a phabricator hg hash (ie: rHGEXTaaacb34aacb34aa)
cl = repo.changelog
tonode = cl.node
def gittohg(githash):
return [tonode(rev) for rev in repo.revs("gitnode(%s)" % githash)]
phabmatch = phabhashre.match(phabrev)
fbconduit: de-phabricator-ize hg and git hashes Summary: Remove the need to ommit the rREPO prefix when copy pasting from phabricator and looking up a commit. Test Plan: Ran the following in phabricator enabled HG repos 11/13 12:15 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBS27aa00fb74d9a3b82756dad6ff26fe253f1e9a70 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 992495:27aa00fb74d9 user: Peng Li <pengli@fb.com> date: Tue Nov 03 10:29:01 2015 -0800 summary: Add a simple root dir arc library 11/13 12:21 cdelahousse@dev4253 ~/fbjava $ hg log -r rFBA8f1335e6d588 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 18654:8f1335e6d588 user: Ryan Menezes <ryandm@fb.com> date: Thu Oct 22 09:59:27 2015 -0700 summary: move jenkins hook into fbjava/arcanist (fbjava changes) Tried the same on a repo with a git mirror that exists in phabricator: 11/16 11:40 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBCODE8272d25d65869ce059024ff38c7051388ad7b802 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 1027287:f26a9c32ae08 user: Oleksandr Kuvshynov <oleksandr@fb.com> date: Sun Nov 15 19:16:39 2015 -0800 summary: [mf] simple cleanup of feed story view If a git hash is too small, abort: $ hg log -r rFBCODE8272d25d65869ce059024f --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py abort: git hash must be 40 characters The previous commands depends on a list of repos set in my hgrc. See (D2660070) [fbconduit] gitcallsigns=LK, CFPUMA, CFSV, CFGK, CFMF, CFGMON, CF, RSIGMA, FA,· WAWEBCLIENT, SKY, FBCODE, FBOBJC, WAWP, SV, OVRMOBILEMAIN, WAANDROID, IGSRV, CPE, MSS, ANDROIDSDK, BUCK, IT-CHEF, WABB, ITINFRA, SIGMA, IOSSDK Reviewers: #sourcecontrol, durham, ericsumner Reviewed By: durham, ericsumner Subscribers: rmcelroy, ericsumner Differential Revision: https://phabricator.fb.com/D2653497 Tasks: 8361368 Signature: t1:2653497:1447480768:295079a7793e182ddea3aeece9cfaead1bfc1a57
2015-11-18 02:22:08 +03:00
if phabmatch:
phabrepo = phabmatch.group(1)
phabhash = phabmatch.group(2)
fbconduit: de-phabricator-ize hg and git hashes Summary: Remove the need to ommit the rREPO prefix when copy pasting from phabricator and looking up a commit. Test Plan: Ran the following in phabricator enabled HG repos 11/13 12:15 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBS27aa00fb74d9a3b82756dad6ff26fe253f1e9a70 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 992495:27aa00fb74d9 user: Peng Li <pengli@fb.com> date: Tue Nov 03 10:29:01 2015 -0800 summary: Add a simple root dir arc library 11/13 12:21 cdelahousse@dev4253 ~/fbjava $ hg log -r rFBA8f1335e6d588 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 18654:8f1335e6d588 user: Ryan Menezes <ryandm@fb.com> date: Thu Oct 22 09:59:27 2015 -0700 summary: move jenkins hook into fbjava/arcanist (fbjava changes) Tried the same on a repo with a git mirror that exists in phabricator: 11/16 11:40 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBCODE8272d25d65869ce059024ff38c7051388ad7b802 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 1027287:f26a9c32ae08 user: Oleksandr Kuvshynov <oleksandr@fb.com> date: Sun Nov 15 19:16:39 2015 -0800 summary: [mf] simple cleanup of feed story view If a git hash is too small, abort: $ hg log -r rFBCODE8272d25d65869ce059024f --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py abort: git hash must be 40 characters The previous commands depends on a list of repos set in my hgrc. See (D2660070) [fbconduit] gitcallsigns=LK, CFPUMA, CFSV, CFGK, CFMF, CFGMON, CF, RSIGMA, FA,· WAWEBCLIENT, SKY, FBCODE, FBOBJC, WAWP, SV, OVRMOBILEMAIN, WAANDROID, IGSRV, CPE, MSS, ANDROIDSDK, BUCK, IT-CHEF, WABB, ITINFRA, SIGMA, IOSSDK Reviewers: #sourcecontrol, durham, ericsumner Reviewed By: durham, ericsumner Subscribers: rmcelroy, ericsumner Differential Revision: https://phabricator.fb.com/D2653497 Tasks: 8361368 Signature: t1:2653497:1447480768:295079a7793e182ddea3aeece9cfaead1bfc1a57
2015-11-18 02:22:08 +03:00
# The hash may be a git hash
if phabrepo in repo.ui.configlist("fbscmquery", "gitcallsigns", []):
return gittohg(phabhash)
fbconduit: de-phabricator-ize hg and git hashes Summary: Remove the need to ommit the rREPO prefix when copy pasting from phabricator and looking up a commit. Test Plan: Ran the following in phabricator enabled HG repos 11/13 12:15 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBS27aa00fb74d9a3b82756dad6ff26fe253f1e9a70 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 992495:27aa00fb74d9 user: Peng Li <pengli@fb.com> date: Tue Nov 03 10:29:01 2015 -0800 summary: Add a simple root dir arc library 11/13 12:21 cdelahousse@dev4253 ~/fbjava $ hg log -r rFBA8f1335e6d588 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 18654:8f1335e6d588 user: Ryan Menezes <ryandm@fb.com> date: Thu Oct 22 09:59:27 2015 -0700 summary: move jenkins hook into fbjava/arcanist (fbjava changes) Tried the same on a repo with a git mirror that exists in phabricator: 11/16 11:40 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBCODE8272d25d65869ce059024ff38c7051388ad7b802 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 1027287:f26a9c32ae08 user: Oleksandr Kuvshynov <oleksandr@fb.com> date: Sun Nov 15 19:16:39 2015 -0800 summary: [mf] simple cleanup of feed story view If a git hash is too small, abort: $ hg log -r rFBCODE8272d25d65869ce059024f --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py abort: git hash must be 40 characters The previous commands depends on a list of repos set in my hgrc. See (D2660070) [fbconduit] gitcallsigns=LK, CFPUMA, CFSV, CFGK, CFMF, CFGMON, CF, RSIGMA, FA,· WAWEBCLIENT, SKY, FBCODE, FBOBJC, WAWP, SV, OVRMOBILEMAIN, WAANDROID, IGSRV, CPE, MSS, ANDROIDSDK, BUCK, IT-CHEF, WABB, ITINFRA, SIGMA, IOSSDK Reviewers: #sourcecontrol, durham, ericsumner Reviewed By: durham, ericsumner Subscribers: rmcelroy, ericsumner Differential Revision: https://phabricator.fb.com/D2653497 Tasks: 8361368 Signature: t1:2653497:1447480768:295079a7793e182ddea3aeece9cfaead1bfc1a57
2015-11-18 02:22:08 +03:00
return [repo[phabhash].node()]
fbconduit: de-phabricator-ize hg and git hashes Summary: Remove the need to ommit the rREPO prefix when copy pasting from phabricator and looking up a commit. Test Plan: Ran the following in phabricator enabled HG repos 11/13 12:15 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBS27aa00fb74d9a3b82756dad6ff26fe253f1e9a70 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 992495:27aa00fb74d9 user: Peng Li <pengli@fb.com> date: Tue Nov 03 10:29:01 2015 -0800 summary: Add a simple root dir arc library 11/13 12:21 cdelahousse@dev4253 ~/fbjava $ hg log -r rFBA8f1335e6d588 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 18654:8f1335e6d588 user: Ryan Menezes <ryandm@fb.com> date: Thu Oct 22 09:59:27 2015 -0700 summary: move jenkins hook into fbjava/arcanist (fbjava changes) Tried the same on a repo with a git mirror that exists in phabricator: 11/16 11:40 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBCODE8272d25d65869ce059024ff38c7051388ad7b802 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 1027287:f26a9c32ae08 user: Oleksandr Kuvshynov <oleksandr@fb.com> date: Sun Nov 15 19:16:39 2015 -0800 summary: [mf] simple cleanup of feed story view If a git hash is too small, abort: $ hg log -r rFBCODE8272d25d65869ce059024f --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py abort: git hash must be 40 characters The previous commands depends on a list of repos set in my hgrc. See (D2660070) [fbconduit] gitcallsigns=LK, CFPUMA, CFSV, CFGK, CFMF, CFGMON, CF, RSIGMA, FA,· WAWEBCLIENT, SKY, FBCODE, FBOBJC, WAWP, SV, OVRMOBILEMAIN, WAANDROID, IGSRV, CPE, MSS, ANDROIDSDK, BUCK, IT-CHEF, WABB, ITINFRA, SIGMA, IOSSDK Reviewers: #sourcecontrol, durham, ericsumner Reviewed By: durham, ericsumner Subscribers: rmcelroy, ericsumner Differential Revision: https://phabricator.fb.com/D2653497 Tasks: 8361368 Signature: t1:2653497:1447480768:295079a7793e182ddea3aeece9cfaead1bfc1a57
2015-11-18 02:22:08 +03:00
# TODO: 's/svnrev/globalrev' after turning off Subversion servers. We will
# know about this when we remove the `svnrev` revset.
svnrevmatch = svnrevre.match(phabrev)
if svnrevmatch is not None:
svnrev = svnrevmatch.group(1)
return [tonode(rev) for rev in repo.revs("svnrev(%s)" % svnrev)]
m = githashre.match(phabrev)
if m is not None:
fbconduit: de-phabricator-ize hg and git hashes Summary: Remove the need to ommit the rREPO prefix when copy pasting from phabricator and looking up a commit. Test Plan: Ran the following in phabricator enabled HG repos 11/13 12:15 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBS27aa00fb74d9a3b82756dad6ff26fe253f1e9a70 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 992495:27aa00fb74d9 user: Peng Li <pengli@fb.com> date: Tue Nov 03 10:29:01 2015 -0800 summary: Add a simple root dir arc library 11/13 12:21 cdelahousse@dev4253 ~/fbjava $ hg log -r rFBA8f1335e6d588 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 18654:8f1335e6d588 user: Ryan Menezes <ryandm@fb.com> date: Thu Oct 22 09:59:27 2015 -0700 summary: move jenkins hook into fbjava/arcanist (fbjava changes) Tried the same on a repo with a git mirror that exists in phabricator: 11/16 11:40 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBCODE8272d25d65869ce059024ff38c7051388ad7b802 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 1027287:f26a9c32ae08 user: Oleksandr Kuvshynov <oleksandr@fb.com> date: Sun Nov 15 19:16:39 2015 -0800 summary: [mf] simple cleanup of feed story view If a git hash is too small, abort: $ hg log -r rFBCODE8272d25d65869ce059024f --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py abort: git hash must be 40 characters The previous commands depends on a list of repos set in my hgrc. See (D2660070) [fbconduit] gitcallsigns=LK, CFPUMA, CFSV, CFGK, CFMF, CFGMON, CF, RSIGMA, FA,· WAWEBCLIENT, SKY, FBCODE, FBOBJC, WAWP, SV, OVRMOBILEMAIN, WAANDROID, IGSRV, CPE, MSS, ANDROIDSDK, BUCK, IT-CHEF, WABB, ITINFRA, SIGMA, IOSSDK Reviewers: #sourcecontrol, durham, ericsumner Reviewed By: durham, ericsumner Subscribers: rmcelroy, ericsumner Differential Revision: https://phabricator.fb.com/D2653497 Tasks: 8361368 Signature: t1:2653497:1447480768:295079a7793e182ddea3aeece9cfaead1bfc1a57
2015-11-18 02:22:08 +03:00
githash = m.group(1)
if len(githash) == 40:
return gittohg(githash)
fbconduit: de-phabricator-ize hg and git hashes Summary: Remove the need to ommit the rREPO prefix when copy pasting from phabricator and looking up a commit. Test Plan: Ran the following in phabricator enabled HG repos 11/13 12:15 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBS27aa00fb74d9a3b82756dad6ff26fe253f1e9a70 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 992495:27aa00fb74d9 user: Peng Li <pengli@fb.com> date: Tue Nov 03 10:29:01 2015 -0800 summary: Add a simple root dir arc library 11/13 12:21 cdelahousse@dev4253 ~/fbjava $ hg log -r rFBA8f1335e6d588 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 18654:8f1335e6d588 user: Ryan Menezes <ryandm@fb.com> date: Thu Oct 22 09:59:27 2015 -0700 summary: move jenkins hook into fbjava/arcanist (fbjava changes) Tried the same on a repo with a git mirror that exists in phabricator: 11/16 11:40 cdelahousse@dev4253 ~/fbsource/fbcode $ hg log -r rFBCODE8272d25d65869ce059024ff38c7051388ad7b802 --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py changeset: 1027287:f26a9c32ae08 user: Oleksandr Kuvshynov <oleksandr@fb.com> date: Sun Nov 15 19:16:39 2015 -0800 summary: [mf] simple cleanup of feed story view If a git hash is too small, abort: $ hg log -r rFBCODE8272d25d65869ce059024f --config extensions.fbconduit=~/local/fb-hgext/fbconduit.py abort: git hash must be 40 characters The previous commands depends on a list of repos set in my hgrc. See (D2660070) [fbconduit] gitcallsigns=LK, CFPUMA, CFSV, CFGK, CFMF, CFGMON, CF, RSIGMA, FA,· WAWEBCLIENT, SKY, FBCODE, FBOBJC, WAWP, SV, OVRMOBILEMAIN, WAANDROID, IGSRV, CPE, MSS, ANDROIDSDK, BUCK, IT-CHEF, WABB, ITINFRA, SIGMA, IOSSDK Reviewers: #sourcecontrol, durham, ericsumner Reviewed By: durham, ericsumner Subscribers: rmcelroy, ericsumner Differential Revision: https://phabricator.fb.com/D2653497 Tasks: 8361368 Signature: t1:2653497:1447480768:295079a7793e182ddea3aeece9cfaead1bfc1a57
2015-11-18 02:22:08 +03:00
else:
return []
def _scmquerylookupglobalrev(orig, repo, rev):
reponame = repo.ui.config("fbscmquery", "reponame")
if reponame:
try:
client = graphql.Client(repo=repo)
hghash = str(
client.getmirroredrev(reponame, "GLOBAL_REV", reponame, "hg", str(rev))
)
matchedrevs = []
if hghash:
matchedrevs.append(bin(hghash))
return matchedrevs
except Exception:
pass
return orig(repo, rev)