| 1 |
# remotenames.py - extension to display remotenames |
| 2 |
# |
| 3 |
# Copyright 2017 Augie Fackler <[email protected]> |
| 4 |
# Copyright 2017 Sean Farley <[email protected]> |
| 5 |
# |
| 6 |
# This software may be used and distributed according to the terms of the |
| 7 |
# GNU General Public License version 2 or any later version. |
| 8 |
|
| 9 |
""" showing remotebookmarks and remotebranches in UI (EXPERIMENTAL) |
| 10 |
|
| 11 |
By default both remotebookmarks and remotebranches are turned on. Config knob to |
| 12 |
control the individually are as follows. |
| 13 |
|
| 14 |
Config options to tweak the default behaviour: |
| 15 |
|
| 16 |
remotenames.bookmarks |
| 17 |
Boolean value to enable or disable showing of remotebookmarks (default: True) |
| 18 |
|
| 19 |
remotenames.branches |
| 20 |
Boolean value to enable or disable showing of remotebranches (default: True) |
| 21 |
|
| 22 |
remotenames.hoistedpeer |
| 23 |
Name of the peer whose remotebookmarks should be hoisted into the top-level |
| 24 |
namespace (default: 'default') |
| 25 |
""" |
| 26 |
|
| 27 |
from __future__ import annotations |
| 28 |
|
| 29 |
import collections.abc |
| 30 |
|
| 31 |
from mercurial.i18n import _ |
| 32 |
|
| 33 |
from mercurial.node import bin |
| 34 |
from mercurial import ( |
| 35 |
bookmarks, |
| 36 |
error, |
| 37 |
extensions, |
| 38 |
logexchange, |
| 39 |
namespaces, |
| 40 |
registrar, |
| 41 |
revsetlang, |
| 42 |
smartset, |
| 43 |
templateutil, |
| 44 |
util, |
| 45 |
) |
| 46 |
|
| 47 |
from mercurial.utils import stringutil |
| 48 |
|
| 49 |
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
| 50 |
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
| 51 |
# be specifying the version(s) of Mercurial they are tested with, or |
| 52 |
# leave the attribute unspecified. |
| 53 |
testedwith = b'ships-with-hg-core' |
| 54 |
|
| 55 |
configtable = {} |
| 56 |
configitem = registrar.configitem(configtable) |
| 57 |
templatekeyword = registrar.templatekeyword() |
| 58 |
revsetpredicate = registrar.revsetpredicate() |
| 59 |
|
| 60 |
configitem( |
| 61 |
b'remotenames', |
| 62 |
b'bookmarks', |
| 63 |
default=True, |
| 64 |
) |
| 65 |
configitem( |
| 66 |
b'remotenames', |
| 67 |
b'branches', |
| 68 |
default=True, |
| 69 |
) |
| 70 |
configitem( |
| 71 |
b'remotenames', |
| 72 |
b'hoistedpeer', |
| 73 |
default=b'default', |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
class lazyremotenamedict(collections.abc.MutableMapping): |
| 78 |
""" |
| 79 |
Read-only dict-like Class to lazily resolve remotename entries |
| 80 |
|
| 81 |
We are doing that because remotenames startup was slow. |
| 82 |
We lazily read the remotenames file once to figure out the potential entries |
| 83 |
and store them in self.potentialentries. Then when asked to resolve an |
| 84 |
entry, if it is not in self.potentialentries, then it isn't there, if it |
| 85 |
is in self.potentialentries we resolve it and store the result in |
| 86 |
self.cache. We cannot be lazy is when asked all the entries (keys). |
| 87 |
""" |
| 88 |
|
| 89 |
def __init__(self, kind, repo): |
| 90 |
self.cache = {} |
| 91 |
self.potentialentries = {} |
| 92 |
self._kind = kind # bookmarks or branches |
| 93 |
self._repo = repo |
| 94 |
self.loaded = False |
| 95 |
|
| 96 |
def _load(self): |
| 97 |
"""Read the remotenames file, store entries matching selected kind""" |
| 98 |
self.loaded = True |
| 99 |
repo = self._repo |
| 100 |
for node, rpath, rname in logexchange.readremotenamefile( |
| 101 |
repo, self._kind |
| 102 |
): |
| 103 |
name = rpath + b'/' + rname |
| 104 |
self.potentialentries[name] = (node, rpath, name) |
| 105 |
|
| 106 |
def _resolvedata(self, potentialentry): |
| 107 |
"""Check that the node for potentialentry exists and return it""" |
| 108 |
if not potentialentry in self.potentialentries: |
| 109 |
return None |
| 110 |
node, remote, name = self.potentialentries[potentialentry] |
| 111 |
repo = self._repo |
| 112 |
binnode = bin(node) |
| 113 |
# if the node doesn't exist, skip it |
| 114 |
try: |
| 115 |
repo.changelog.rev(binnode) |
| 116 |
except LookupError: |
| 117 |
return None |
| 118 |
# Skip closed branches |
| 119 |
if self._kind == b'branches' and repo[binnode].closesbranch(): |
| 120 |
return None |
| 121 |
return [binnode] |
| 122 |
|
| 123 |
def __getitem__(self, key): |
| 124 |
if not self.loaded: |
| 125 |
self._load() |
| 126 |
val = self._fetchandcache(key) |
| 127 |
if val is not None: |
| 128 |
return val |
| 129 |
else: |
| 130 |
raise KeyError() |
| 131 |
|
| 132 |
def __iter__(self): |
| 133 |
return iter(self.potentialentries) |
| 134 |
|
| 135 |
def __len__(self): |
| 136 |
return len(self.potentialentries) |
| 137 |
|
| 138 |
def __setitem__(self, k, v): |
| 139 |
raise NotImplementedError |
| 140 |
|
| 141 |
def __delitem__(self, k): |
| 142 |
raise NotImplementedError |
| 143 |
|
| 144 |
def _fetchandcache(self, key): |
| 145 |
if key in self.cache: |
| 146 |
return self.cache[key] |
| 147 |
val = self._resolvedata(key) |
| 148 |
if val is not None: |
| 149 |
self.cache[key] = val |
| 150 |
return val |
| 151 |
else: |
| 152 |
return None |
| 153 |
|
| 154 |
def keys(self): |
| 155 |
"""Get a list of bookmark or branch names""" |
| 156 |
if not self.loaded: |
| 157 |
self._load() |
| 158 |
return self.potentialentries.keys() |
| 159 |
|
| 160 |
def iteritems(self): |
| 161 |
"""Iterate over (name, node) tuples""" |
| 162 |
|
| 163 |
if not self.loaded: |
| 164 |
self._load() |
| 165 |
|
| 166 |
for k, vtup in self.potentialentries.items(): |
| 167 |
yield (k, [bin(vtup[0])]) |
| 168 |
|
| 169 |
items = iteritems |
| 170 |
|
| 171 |
|
| 172 |
class remotenames: |
| 173 |
""" |
| 174 |
This class encapsulates all the remotenames state. It also contains |
| 175 |
methods to access that state in convenient ways. Remotenames are lazy |
| 176 |
loaded. Whenever client code needs to ensure the freshest copy of |
| 177 |
remotenames, use the `clearnames` method to force an eventual load. |
| 178 |
""" |
| 179 |
|
| 180 |
def __init__(self, repo, *args): |
| 181 |
self._repo = repo |
| 182 |
self.clearnames() |
| 183 |
|
| 184 |
def clearnames(self): |
| 185 |
"""Clear all remote names state""" |
| 186 |
self.bookmarks = lazyremotenamedict(b"bookmarks", self._repo) |
| 187 |
self.branches = lazyremotenamedict(b"branches", self._repo) |
| 188 |
self._invalidatecache() |
| 189 |
|
| 190 |
def _invalidatecache(self): |
| 191 |
self._nodetobmarks = None |
| 192 |
self._nodetobranch = None |
| 193 |
self._hoisttonodes = None |
| 194 |
self._nodetohoists = None |
| 195 |
|
| 196 |
def bmarktonodes(self): |
| 197 |
return self.bookmarks |
| 198 |
|
| 199 |
def nodetobmarks(self): |
| 200 |
if not self._nodetobmarks: |
| 201 |
bmarktonodes = self.bmarktonodes() |
| 202 |
self._nodetobmarks = {} |
| 203 |
for name, node in bmarktonodes.items(): |
| 204 |
self._nodetobmarks.setdefault(node[0], []).append(name) |
| 205 |
return self._nodetobmarks |
| 206 |
|
| 207 |
def branchtonodes(self): |
| 208 |
return self.branches |
| 209 |
|
| 210 |
def nodetobranch(self): |
| 211 |
if not self._nodetobranch: |
| 212 |
branchtonodes = self.branchtonodes() |
| 213 |
self._nodetobranch = {} |
| 214 |
for name, nodes in branchtonodes.items(): |
| 215 |
for node in nodes: |
| 216 |
self._nodetobranch.setdefault(node, []).append(name) |
| 217 |
return self._nodetobranch |
| 218 |
|
| 219 |
def hoisttonodes(self, hoist): |
| 220 |
if not self._hoisttonodes: |
| 221 |
marktonodes = self.bmarktonodes() |
| 222 |
self._hoisttonodes = {} |
| 223 |
hoist += b'/' |
| 224 |
for name, node in marktonodes.items(): |
| 225 |
if name.startswith(hoist): |
| 226 |
name = name[len(hoist) :] |
| 227 |
self._hoisttonodes[name] = node |
| 228 |
return self._hoisttonodes |
| 229 |
|
| 230 |
def nodetohoists(self, hoist): |
| 231 |
if not self._nodetohoists: |
| 232 |
marktonodes = self.bmarktonodes() |
| 233 |
self._nodetohoists = {} |
| 234 |
hoist += b'/' |
| 235 |
for name, node in marktonodes.items(): |
| 236 |
if name.startswith(hoist): |
| 237 |
name = name[len(hoist) :] |
| 238 |
self._nodetohoists.setdefault(node[0], []).append(name) |
| 239 |
return self._nodetohoists |
| 240 |
|
| 241 |
|
| 242 |
def wrapprintbookmarks(orig, ui, repo, fm, bmarks): |
| 243 |
if b'remotebookmarks' not in repo.names: |
| 244 |
return |
| 245 |
ns = repo.names[b'remotebookmarks'] |
| 246 |
|
| 247 |
for name in ns.listnames(repo): |
| 248 |
nodes = ns.nodes(repo, name) |
| 249 |
if not nodes: |
| 250 |
continue |
| 251 |
node = nodes[0] |
| 252 |
|
| 253 |
bmarks[name] = (node, b' ', b'') |
| 254 |
|
| 255 |
return orig(ui, repo, fm, bmarks) |
| 256 |
|
| 257 |
|
| 258 |
def extsetup(ui): |
| 259 |
extensions.wrapfunction(bookmarks, '_printbookmarks', wrapprintbookmarks) |
| 260 |
|
| 261 |
|
| 262 |
def reposetup(ui, repo): |
| 263 |
# set the config option to store remotenames |
| 264 |
repo.ui.setconfig(b'experimental', b'remotenames', True, b'remotenames-ext') |
| 265 |
|
| 266 |
if not repo.local(): |
| 267 |
return |
| 268 |
|
| 269 |
repo._remotenames = remotenames(repo) |
| 270 |
ns = namespaces.namespace |
| 271 |
|
| 272 |
if ui.configbool(b'remotenames', b'bookmarks'): |
| 273 |
remotebookmarkns = ns( |
| 274 |
b'remotebookmarks', |
| 275 |
templatename=b'remotebookmarks', |
| 276 |
colorname=b'remotebookmark', |
| 277 |
logfmt=b'remote bookmark: %s\n', |
| 278 |
listnames=lambda repo: repo._remotenames.bmarktonodes().keys(), |
| 279 |
namemap=lambda repo, name: repo._remotenames.bmarktonodes().get( |
| 280 |
name, [] |
| 281 |
), |
| 282 |
nodemap=lambda repo, node: repo._remotenames.nodetobmarks().get( |
| 283 |
node, [] |
| 284 |
), |
| 285 |
) |
| 286 |
repo.names.addnamespace(remotebookmarkns) |
| 287 |
|
| 288 |
# hoisting only works if there are remote bookmarks |
| 289 |
hoist = ui.config(b'remotenames', b'hoistedpeer') |
| 290 |
if hoist: |
| 291 |
hoistednamens = ns( |
| 292 |
b'hoistednames', |
| 293 |
templatename=b'hoistednames', |
| 294 |
colorname=b'hoistedname', |
| 295 |
logfmt=b'hoisted name: %s\n', |
| 296 |
listnames=lambda repo: repo._remotenames.hoisttonodes( |
| 297 |
hoist |
| 298 |
).keys(), |
| 299 |
namemap=lambda repo, name: repo._remotenames.hoisttonodes( |
| 300 |
hoist |
| 301 |
).get(name, []), |
| 302 |
nodemap=lambda repo, node: repo._remotenames.nodetohoists( |
| 303 |
hoist |
| 304 |
).get(node, []), |
| 305 |
) |
| 306 |
repo.names.addnamespace(hoistednamens) |
| 307 |
|
| 308 |
if ui.configbool(b'remotenames', b'branches'): |
| 309 |
remotebranchns = ns( |
| 310 |
b'remotebranches', |
| 311 |
templatename=b'remotebranches', |
| 312 |
colorname=b'remotebranch', |
| 313 |
logfmt=b'remote branch: %s\n', |
| 314 |
listnames=lambda repo: repo._remotenames.branchtonodes().keys(), |
| 315 |
namemap=lambda repo, name: repo._remotenames.branchtonodes().get( |
| 316 |
name, [] |
| 317 |
), |
| 318 |
nodemap=lambda repo, node: repo._remotenames.nodetobranch().get( |
| 319 |
node, [] |
| 320 |
), |
| 321 |
) |
| 322 |
repo.names.addnamespace(remotebranchns) |
| 323 |
|
| 324 |
|
| 325 |
@templatekeyword(b'remotenames', requires={b'repo', b'ctx'}) |
| 326 |
def remotenameskw(context, mapping): |
| 327 |
"""List of strings. Remote names associated with the changeset.""" |
| 328 |
repo = context.resource(mapping, b'repo') |
| 329 |
ctx = context.resource(mapping, b'ctx') |
| 330 |
|
| 331 |
remotenames = [] |
| 332 |
if b'remotebookmarks' in repo.names: |
| 333 |
remotenames = repo.names[b'remotebookmarks'].names(repo, ctx.node()) |
| 334 |
|
| 335 |
if b'remotebranches' in repo.names: |
| 336 |
remotenames += repo.names[b'remotebranches'].names(repo, ctx.node()) |
| 337 |
|
| 338 |
return templateutil.compatlist( |
| 339 |
context, mapping, b'remotename', remotenames, plural=b'remotenames' |
| 340 |
) |
| 341 |
|
| 342 |
|
| 343 |
@templatekeyword(b'remotebookmarks', requires={b'repo', b'ctx'}) |
| 344 |
def remotebookmarkskw(context, mapping): |
| 345 |
"""List of strings. Remote bookmarks associated with the changeset.""" |
| 346 |
repo = context.resource(mapping, b'repo') |
| 347 |
ctx = context.resource(mapping, b'ctx') |
| 348 |
|
| 349 |
remotebmarks = [] |
| 350 |
if b'remotebookmarks' in repo.names: |
| 351 |
remotebmarks = repo.names[b'remotebookmarks'].names(repo, ctx.node()) |
| 352 |
|
| 353 |
return templateutil.compatlist( |
| 354 |
context, |
| 355 |
mapping, |
| 356 |
b'remotebookmark', |
| 357 |
remotebmarks, |
| 358 |
plural=b'remotebookmarks', |
| 359 |
) |
| 360 |
|
| 361 |
|
| 362 |
@templatekeyword(b'remotebranches', requires={b'repo', b'ctx'}) |
| 363 |
def remotebrancheskw(context, mapping): |
| 364 |
"""List of strings. Remote branches associated with the changeset.""" |
| 365 |
repo = context.resource(mapping, b'repo') |
| 366 |
ctx = context.resource(mapping, b'ctx') |
| 367 |
|
| 368 |
remotebranches = [] |
| 369 |
if b'remotebranches' in repo.names: |
| 370 |
remotebranches = repo.names[b'remotebranches'].names(repo, ctx.node()) |
| 371 |
|
| 372 |
return templateutil.compatlist( |
| 373 |
context, |
| 374 |
mapping, |
| 375 |
b'remotebranch', |
| 376 |
remotebranches, |
| 377 |
plural=b'remotebranches', |
| 378 |
) |
| 379 |
|
| 380 |
|
| 381 |
def _revsetutil(repo, subset, x, rtypes): |
| 382 |
"""utility function to return a set of revs based on the rtypes""" |
| 383 |
args = revsetlang.getargs(x, 0, 1, _(b'only one argument accepted')) |
| 384 |
if args: |
| 385 |
kind, pattern, matcher = stringutil.stringmatcher( |
| 386 |
revsetlang.getstring(args[0], _(b'argument must be a string')) |
| 387 |
) |
| 388 |
else: |
| 389 |
kind = pattern = None |
| 390 |
matcher = util.always |
| 391 |
|
| 392 |
nodes = set() |
| 393 |
cl = repo.changelog |
| 394 |
for rtype in rtypes: |
| 395 |
if rtype in repo.names: |
| 396 |
ns = repo.names[rtype] |
| 397 |
for name in ns.listnames(repo): |
| 398 |
if not matcher(name): |
| 399 |
continue |
| 400 |
nodes.update(ns.nodes(repo, name)) |
| 401 |
if kind == b'literal' and not nodes: |
| 402 |
raise error.RepoLookupError( |
| 403 |
_(b"remote name '%s' does not exist") % pattern |
| 404 |
) |
| 405 |
|
| 406 |
revs = (cl.rev(n) for n in nodes if cl.hasnode(n)) |
| 407 |
return subset & smartset.baseset(revs) |
| 408 |
|
| 409 |
|
| 410 |
@revsetpredicate(b'remotenames([name])') |
| 411 |
def remotenamesrevset(repo, subset, x): |
| 412 |
"""All changesets which have a remotename on them. If `name` is |
| 413 |
specified, only remotenames of matching remote paths are considered. |
| 414 |
|
| 415 |
Pattern matching is supported for `name`. See :hg:`help revisions.patterns`. |
| 416 |
""" |
| 417 |
return _revsetutil(repo, subset, x, (b'remotebookmarks', b'remotebranches')) |
| 418 |
|
| 419 |
|
| 420 |
@revsetpredicate(b'remotebranches([name])') |
| 421 |
def remotebranchesrevset(repo, subset, x): |
| 422 |
"""All changesets which are branch heads on remotes. If `name` is |
| 423 |
specified, only remotenames of matching remote paths are considered. |
| 424 |
|
| 425 |
Pattern matching is supported for `name`. See :hg:`help revisions.patterns`. |
| 426 |
""" |
| 427 |
return _revsetutil(repo, subset, x, (b'remotebranches',)) |
| 428 |
|
| 429 |
|
| 430 |
@revsetpredicate(b'remotebookmarks([name])') |
| 431 |
def remotebmarksrevset(repo, subset, x): |
| 432 |
"""All changesets which have bookmarks on remotes. If `name` is |
| 433 |
specified, only remotenames of matching remote paths are considered. |
| 434 |
|
| 435 |
Pattern matching is supported for `name`. See :hg:`help revisions.patterns`. |
| 436 |
""" |
| 437 |
return _revsetutil(repo, subset, x, (b'remotebookmarks',)) |