comparison hgext/remotenames.py @ 40059:fda1df3d4e06

remotenames: add names argument to remotenames revset This patch adds names argument to the revsets provided by the remotenames extension. The revsets are remotenames(), remotebranches() and remotebookmarks(). names can be a single names, list of names or can be empty too which means it's an optional argument. If names is/are passed, changesets which have those remotenames will be returned. If names are not passed, changesets from all the remotenames are shown. Passing an invalid remotename does not throw error. The name argument also supports pattern matching. Tests are added for the argument in tests/test-logexchange.t Differential Revision: https://phab.mercurial-scm.org/D3639
author Pulkit Goyal <7895pulkit@gmail.com>
date Thu, 12 Jul 2018 03:12:09 +0530
parents 25cc5616adc9
children 6346e21eecc8
comparison
equal deleted inserted replaced
40058:cf01616f8d96 40059:fda1df3d4e06
39 pycompat, 39 pycompat,
40 registrar, 40 registrar,
41 revsetlang, 41 revsetlang,
42 smartset, 42 smartset,
43 templateutil, 43 templateutil,
44 )
45
46 from mercurial.utils import (
47 stringutil,
44 ) 48 )
45 49
46 if pycompat.ispy3: 50 if pycompat.ispy3:
47 import collections.abc 51 import collections.abc
48 mutablemapping = collections.abc.MutableMapping 52 mutablemapping = collections.abc.MutableMapping
341 remotebranches = repo.names['remotebranches'].names(repo, ctx.node()) 345 remotebranches = repo.names['remotebranches'].names(repo, ctx.node())
342 346
343 return templateutil.compatlist(context, mapping, 'remotebranch', 347 return templateutil.compatlist(context, mapping, 'remotebranch',
344 remotebranches, plural='remotebranches') 348 remotebranches, plural='remotebranches')
345 349
346 def _revsetutil(repo, subset, x, rtypes): 350 def _revsetutil(repo, subset, x, rtypes, args):
347 """utility function to return a set of revs based on the rtypes""" 351 """utility function to return a set of revs based on the rtypes"""
348 352
349 revs = set() 353 revs = set()
350 cl = repo.changelog 354 cl = repo.changelog
355 literals, matchers = args
356 # whether arguments were passed or not
357 argspassed = bool(literals or matchers)
351 for rtype in rtypes: 358 for rtype in rtypes:
352 if rtype in repo.names: 359 if rtype in repo.names:
353 ns = repo.names[rtype] 360 ns = repo.names[rtype]
354 for name in ns.listnames(repo): 361 for name in ns.listnames(repo):
355 revs.update(ns.nodes(repo, name)) 362 if argspassed:
363 if name in literals:
364 revs.update(ns.nodes(repo, name))
365 continue
366 for matcher in matchers:
367 if matcher(name):
368 revs.update(ns.nodes(repo, name))
369 break
370 else:
371 revs.update(ns.nodes(repo, name))
356 372
357 results = (cl.rev(n) for n in revs if cl.hasnode(n)) 373 results = (cl.rev(n) for n in revs if cl.hasnode(n))
358 return subset & smartset.baseset(sorted(results)) 374 return subset & smartset.baseset(sorted(results))
359 375
360 @revsetpredicate('remotenames()') 376 def _parseargs(x):
377 """parses the argument passed in revsets
378
379 returns (literals, matchers) where,
380 literals is a set of literals passed by user
381 matchers is a list of matcher objects for patterns passed by user
382 """
383
384 # set of paths passed as literals
385 literals = set()
386 # list of matcher to match the patterns passed as names
387 matchers = []
388
389 if not x:
390 return literals, matchers
391
392 args = set()
393 lx = revsetlang.getlist(x)
394 err = _('the argument must be a string')
395 for entry in lx:
396 args.add(revsetlang.getstring(entry, err))
397 for p in args:
398 kind, pattern, matcher = stringutil.stringmatcher(p)
399 if kind == 'literal':
400 literals.add(pattern)
401 else:
402 matchers.append(matcher)
403 return literals, matchers
404
405 @revsetpredicate('remotenames([name, ...])')
361 def remotenamesrevset(repo, subset, x): 406 def remotenamesrevset(repo, subset, x):
362 """All changesets which have a remotename on them.""" 407 """All changesets which have a remotename on them. If paths are specified,
363 revsetlang.getargs(x, 0, 0, _("remotenames takes no arguments")) 408 remotenames of those remote paths are only considered.
364 return _revsetutil(repo, subset, x, ('remotebookmarks', 'remotebranches')) 409
365 410 Pattern matching is supported for `name`. See :hg:`help revisions.patterns`.
366 @revsetpredicate('remotebranches()') 411 """
412
413 args = _parseargs(x)
414 return _revsetutil(repo, subset, x, ('remotebookmarks', 'remotebranches'),
415 args)
416
417 @revsetpredicate('remotebranches([name, ...])')
367 def remotebranchesrevset(repo, subset, x): 418 def remotebranchesrevset(repo, subset, x):
368 """All changesets which are branch heads on remotes.""" 419 """All changesets which are branch heads on remotes. If paths are specified,
369 revsetlang.getargs(x, 0, 0, _("remotebranches takes no arguments")) 420 only those remotes paths are considered.
370 return _revsetutil(repo, subset, x, ('remotebranches',)) 421
371 422 Pattern matching is supported for `name`. See :hg:`help revisions.patterns`.
372 @revsetpredicate('remotebookmarks()') 423 """
424
425 args = _parseargs(x)
426 return _revsetutil(repo, subset, x, ('remotebranches',), args)
427
428 @revsetpredicate('remotebookmarks([name, ...])')
373 def remotebmarksrevset(repo, subset, x): 429 def remotebmarksrevset(repo, subset, x):
374 """All changesets which have bookmarks on remotes.""" 430 """All changesets which have bookmarks on remotes. If paths are specified,
375 revsetlang.getargs(x, 0, 0, _("remotebookmarks takes no arguments")) 431 only those remote paths are considered.
376 return _revsetutil(repo, subset, x, ('remotebookmarks',)) 432
433 Pattern matching is supported for `name`. See :hg:`help revisions.patterns`.
434 """
435
436 args = _parseargs(x)
437 return _revsetutil(repo, subset, x, ('remotebookmarks',), args)