remotenames: move revset and templatekw to core

Summary:
This makes it possible to use template like `{remotenames}` or revset like
`remotenames()` without enabling the remotename extension.

Rarely used revsets like `upstream()` and `pushed()` are not moved.

Reviewed By: markbt

Differential Revision: D20529360

fbshipit-source-id: ea95b3324f974e112909cdd79ce662940a4f9b7c
This commit is contained in:
Jun Wu 2020-03-26 08:23:49 -07:00 committed by Facebook GitHub Bot
parent cc1b1bd6a9
commit baae0138ff
5 changed files with 72 additions and 85 deletions

View File

@ -51,7 +51,6 @@ from edenscm.mercurial import (
scmutil, scmutil,
setdiscovery, setdiscovery,
smartset, smartset,
templatekw,
ui as uimod, ui as uimod,
url, url,
util, util,
@ -114,7 +113,6 @@ configitem("remotenames", "upstream", default=[])
# always update remote bookmarks! The config option exists for testing purpose. # always update remote bookmarks! The config option exists for testing purpose.
configitem("remotenames", "racy-pull-on-push", default=True) configitem("remotenames", "racy-pull-on-push", default=True)
templatekeyword = registrar.templatekeyword()
revsetpredicate = registrar.revsetpredicate() revsetpredicate = registrar.revsetpredicate()
@ -1720,82 +1718,3 @@ def pushed(repo, subset, x):
"""Select changesets in any remote repository according to remotenames.""" """Select changesets in any remote repository according to remotenames."""
revset.getargs(x, 0, 0, "pushed takes no arguments") revset.getargs(x, 0, 0, "pushed takes no arguments")
return upstream_revs(lambda x: True, repo, subset, x) return upstream_revs(lambda x: True, repo, subset, x)
def getremoterevs(repo, namespacename, matchpattern=None):
try:
ns = repo.names[namespacename]
except KeyError:
return set()
if matchpattern is None:
nodes = set()
for name in ns.listnames(repo):
nodes.update(ns.namemap(repo, name))
else:
kind, pattern, matcher = util.stringmatcher(matchpattern)
if kind == "literal":
nodes = ns.namemap(repo, pattern)
else:
nodes = set()
for name in ns.listnames(repo):
if matcher(name):
nodes.update(ns.namemap(repo, name))
return {repo[node].rev() for node in nodes if node in repo}
@revsetpredicate("remotenames()")
def remotenamesrevset(repo, subset, x):
"""All remote bookmarks and branches."""
if not util.safehasattr(repo, "_remotenames"):
# If this repository does not have remotenames enabled just evaluate to the
# empty set.
return smartset.baseset([])
revset.getargs(x, 0, 0, "remotenames takes no arguments")
remoterevs = set()
for rname in repo._remotenames.keys():
remoterevs.update(getremoterevs(repo, "remote" + rname))
return subset & smartset.baseset(sorted(remoterevs))
@revsetpredicate("remotebookmark([name])")
def remotebookmarkrevset(repo, subset, x):
"""The named remote bookmark, or all remote bookmarks.
Pattern matching is supported for `name`. See :hg:`help revisions.patterns`.
"""
args = revset.getargs(x, 0, 1, _("remotebookmark takes one or no arguments"))
if args:
bookmarkname = revset.getstring(
args[0], _("the argument to remotebookmark must be a string")
)
else:
bookmarkname = None
remoterevs = getremoterevs(repo, "remotebookmarks", bookmarkname)
if not remoterevs and bookmarkname is not None:
raise error.RepoLookupError(
_("no remote bookmarks exist that match '%s'") % bookmarkname
)
return subset & smartset.baseset(sorted(remoterevs))
###########
# templates
###########
@templatekeyword("remotenames")
def remotenameskw(**args):
""":remotenames: List of strings. List of remote names associated with the
changeset. If remotenames.suppressbranches is True then branch names will
be hidden if there is a bookmark at the same changeset.
"""
repo, ctx = args["repo"], args["ctx"]
remotenames = []
if "remotebookmarks" in repo.names:
remotenames = repo.names["remotebookmarks"].names(repo, ctx.node())
return templatekw.showlist("remotename", remotenames, args, plural="remotenames")

View File

@ -2353,6 +2353,59 @@ def wdir(repo, subset, x):
return baseset() return baseset()
@predicate("remotenames()")
def remotenamesrevset(repo, subset, x):
"""All remote bookmarks and branches."""
getargs(x, 0, 0, "remotenames takes no arguments")
remoterevs = set()
for rname in repo._remotenames.keys():
remoterevs.update(_getremoterevs(repo, "remote" + rname))
return subset & smartset.baseset(sorted(remoterevs))
@predicate("remotebookmark([name])")
def remotebookmarkrevset(repo, subset, x):
"""The named remote bookmark, or all remote bookmarks.
Pattern matching is supported for `name`. See :hg:`help revisions.patterns`.
"""
args = getargs(x, 0, 1, _("remotebookmark takes one or no arguments"))
if args:
bookmarkname = getstring(
args[0], _("the argument to remotebookmark must be a string")
)
else:
bookmarkname = None
remoterevs = _getremoterevs(repo, "remotebookmarks", bookmarkname)
if not remoterevs and bookmarkname is not None:
raise error.RepoLookupError(
_("no remote bookmarks exist that match '%s'") % bookmarkname
)
return subset & smartset.baseset(sorted(remoterevs))
def _getremoterevs(repo, namespacename, matchpattern=None):
try:
ns = repo.names[namespacename]
except KeyError:
return set()
if matchpattern is None:
nodes = set()
for name in ns.listnames(repo):
nodes.update(ns.namemap(repo, name))
else:
kind, pattern, matcher = util.stringmatcher(matchpattern)
if kind == "literal":
nodes = ns.namemap(repo, pattern)
else:
nodes = set()
for name in ns.listnames(repo):
if matcher(name):
nodes.update(ns.namemap(repo, name))
return {repo[node].rev() for node in nodes if node in repo}
def _orderedlist(repo, subset, x): def _orderedlist(repo, subset, x):
s = getstring(x, "internal error") s = getstring(x, "internal error")
if not s: if not s:

View File

@ -1013,6 +1013,20 @@ def showverbosity(ui, **args):
return "" return ""
@templatekeyword("remotenames")
def remotenameskw(**args):
""":remotenames: List of strings. List of remote names associated with the
changeset.
"""
repo, ctx = args["repo"], args["ctx"]
remotenames = []
if "remotebookmarks" in repo.names:
remotenames = repo.names["remotebookmarks"].names(repo, ctx.node())
return showlist("remotename", remotenames, args, plural="remotenames")
def loadkeyword(ui, extname, registrarobj): def loadkeyword(ui, extname, registrarobj):
"""Load template keyword from specified registrarobj """Load template keyword from specified registrarobj
""" """

View File

@ -158,6 +158,7 @@ bookmarks revset
$ hg help revsets | grep 'bookmark(' $ hg help revsets | grep 'bookmark('
"bookmark([name])" "bookmark([name])"
"remotebookmark([name])"
bookmarks X and X2 moved to rev 1, Y at rev -1 bookmarks X and X2 moved to rev 1, Y at rev -1

View File

@ -81,7 +81,7 @@ Make some non-conflicting commits in in the client repos.
$ cd ../client2 $ cd ../client2
$ hg pull -q $ hg pull -q
$ log $ log
o second commit [public:0a57cb610829] master o second commit [public:0a57cb610829] master default/master
| |
o first commit [public:679b2ce82944] o first commit [public:679b2ce82944]
| |
@ -93,7 +93,7 @@ Make some non-conflicting commits in in the client repos.
$ log $ log
@ third commit [draft:8ee8e01cbc17] @ third commit [draft:8ee8e01cbc17]
| |
| o second commit [public:0a57cb610829] master | o second commit [public:0a57cb610829] master default/master
| | | |
| o first commit [public:679b2ce82944] | o first commit [public:679b2ce82944]
|/ |/
@ -119,7 +119,7 @@ Make some non-conflicting commits in in the client repos.
| |
| @ third commit [draft:8ee8e01cbc17] | @ third commit [draft:8ee8e01cbc17]
| | | |
o | second commit [public:0a57cb610829] master o | second commit [public:0a57cb610829] master default/master
| | | |
o | first commit [public:679b2ce82944] o | first commit [public:679b2ce82944]
|/ |/
@ -189,7 +189,7 @@ Meanwhile, push from client2 -> server2.
| | | |
o | third commit [draft:8ee8e01cbc17] o | third commit [draft:8ee8e01cbc17]
| | | |
| o second commit [public:0a57cb610829] master | o second commit [public:0a57cb610829] master default/master
| | | |
| o first commit [public:679b2ce82944] | o first commit [public:679b2ce82944]
|/ |/