hgext3rd/topic/revset.py
author Anton Shestakov <av6@dwimlabs.net>
Thu, 30 Aug 2018 21:05:17 +0800
changeset 4061 ad4194399b47
parent 4060 54eade86ac31
child 4063 00c65abf99cd
permissions -rw-r--r--
topic: handle ambiguous arguments to topic() revset These arguments can be interpreted as either string or a revset. The decision is made based on existence of topic with such a name. This matches the behavior of branch() revset. The code needs to know all topics that ever existed in the repo, because some commands report "disappearance" of topics after certain operations, using this revset (e.g. via stack.stack or repo.revs).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1935
11d740319280 revset: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1923
diff changeset
     1
from __future__ import absolute_import
11d740319280 revset: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1923
diff changeset
     2
11d740319280 revset: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1923
diff changeset
     3
from mercurial import (
4058
90783c9c8609 topic: prepare to handle non-string arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4057
diff changeset
     4
    error,
2924
430fb1758d28 topic: use registrar.revsetpredicate to register revset predicate functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 2915
diff changeset
     5
    registrar,
1935
11d740319280 revset: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1923
diff changeset
     6
    revset,
11d740319280 revset: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1923
diff changeset
     7
    util,
11d740319280 revset: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1923
diff changeset
     8
)
1843
0ba067a97d06 revset: add a topic() revset for querying topics
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
1935
11d740319280 revset: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1923
diff changeset
    10
from . import (
11d740319280 revset: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1923
diff changeset
    11
    destination,
11d740319280 revset: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1923
diff changeset
    12
    stack,
11d740319280 revset: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1923
diff changeset
    13
)
1845
24d8053020a2 constants: extract key for extra into a constant
Augie Fackler <augie@google.com>
parents: 1843
diff changeset
    14
1865
558dd43b599d topic: work around stringmatcher moving during development of hg 3.6
Augie Fackler <raf@durin42.com>
parents: 1864
diff changeset
    15
try:
558dd43b599d topic: work around stringmatcher moving during development of hg 3.6
Augie Fackler <raf@durin42.com>
parents: 1864
diff changeset
    16
    mkmatcher = revset._stringmatcher
558dd43b599d topic: work around stringmatcher moving during development of hg 3.6
Augie Fackler <raf@durin42.com>
parents: 1864
diff changeset
    17
except AttributeError:
3613
bf583a8dc637 compat: search for stringmatcher in the new location
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3156
diff changeset
    18
    try:
bf583a8dc637 compat: search for stringmatcher in the new location
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3156
diff changeset
    19
        from mercurial.utils import stringutil
bf583a8dc637 compat: search for stringmatcher in the new location
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3156
diff changeset
    20
        mkmatcher = stringutil.stringmatcher
bf583a8dc637 compat: search for stringmatcher in the new location
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3156
diff changeset
    21
    except (ImportError, AttributeError):
bf583a8dc637 compat: search for stringmatcher in the new location
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3156
diff changeset
    22
        mkmatcher = util.stringmatcher
1865
558dd43b599d topic: work around stringmatcher moving during development of hg 3.6
Augie Fackler <raf@durin42.com>
parents: 1864
diff changeset
    23
2924
430fb1758d28 topic: use registrar.revsetpredicate to register revset predicate functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 2915
diff changeset
    24
revsetpredicate = registrar.revsetpredicate()
1865
558dd43b599d topic: work around stringmatcher moving during development of hg 3.6
Augie Fackler <raf@durin42.com>
parents: 1864
diff changeset
    25
4060
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    26
@revsetpredicate('topic([string or set])')
1843
0ba067a97d06 revset: add a topic() revset for querying topics
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
def topicset(repo, subset, x):
4060
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    28
    """All changesets with the specified topic or the topics of the given
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    29
    changesets. Without the argument, all changesets with any topic specified.
1843
0ba067a97d06 revset: add a topic() revset for querying topics
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
4060
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    31
    If `string` starts with `re:` the remainder of the name is treated
1843
0ba067a97d06 revset: add a topic() revset for querying topics
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
    as a regular expression.
0ba067a97d06 revset: add a topic() revset for querying topics
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
    """
0ba067a97d06 revset: add a topic() revset for querying topics
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
    args = revset.getargs(x, 0, 1, 'topic takes one or no arguments')
4057
054d288680b4 topic: return result early if there are no arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 3613
diff changeset
    35
054d288680b4 topic: return result early if there are no arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 3613
diff changeset
    36
    mutable = revset._notpublic(repo, revset.fullreposet(repo), ())
054d288680b4 topic: return result early if there are no arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 3613
diff changeset
    37
054d288680b4 topic: return result early if there are no arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 3613
diff changeset
    38
    if not args:
054d288680b4 topic: return result early if there are no arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 3613
diff changeset
    39
        return (subset & mutable).filter(lambda r: bool(repo[r].topic()))
4058
90783c9c8609 topic: prepare to handle non-string arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4057
diff changeset
    40
90783c9c8609 topic: prepare to handle non-string arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4057
diff changeset
    41
    try:
4060
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    42
        topic = revset.getstring(args[0], '')
4058
90783c9c8609 topic: prepare to handle non-string arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4057
diff changeset
    43
    except error.ParseError:
90783c9c8609 topic: prepare to handle non-string arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4057
diff changeset
    44
        # not a string, but another revset
4060
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    45
        pass
4057
054d288680b4 topic: return result early if there are no arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 3613
diff changeset
    46
    else:
4061
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    47
        kind, pattern, matcher = mkmatcher(topic)
2651
6a3df2404472 topic-revset: update the revset to no longer build changectx
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2650
diff changeset
    48
4059
1914a53fe792 topic: handle string argument to topic() revset earlier
Anton Shestakov <av6@dwimlabs.net>
parents: 4058
diff changeset
    49
        def matches(r):
1914a53fe792 topic: handle string argument to topic() revset earlier
Anton Shestakov <av6@dwimlabs.net>
parents: 4058
diff changeset
    50
            topic = repo[r].topic()
1914a53fe792 topic: handle string argument to topic() revset earlier
Anton Shestakov <av6@dwimlabs.net>
parents: 4058
diff changeset
    51
            if not topic:
1914a53fe792 topic: handle string argument to topic() revset earlier
Anton Shestakov <av6@dwimlabs.net>
parents: 4058
diff changeset
    52
                return False
1914a53fe792 topic: handle string argument to topic() revset earlier
Anton Shestakov <av6@dwimlabs.net>
parents: 4058
diff changeset
    53
            return matcher(topic)
1914a53fe792 topic: handle string argument to topic() revset earlier
Anton Shestakov <av6@dwimlabs.net>
parents: 4058
diff changeset
    54
4061
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    55
        if kind == 'literal':
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    56
            # note: falls through to the revset case if no topic with this name
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    57
            # exists and pattern kind is not specified explicitly
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    58
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    59
            alltopics = set([repo.currenttopic])
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    60
            for r in repo.unfiltered().set('all()'):
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    61
                alltopics.add(r.topic(force=True))
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    62
            alltopics.discard('')
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    63
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    64
            if pattern in alltopics:
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    65
                return (subset & mutable).filter(matches)
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    66
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    67
            if topic.startswith('literal:'):
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    68
                raise error.RepoLookupError("topic '%s' does not exist"
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    69
                                            % pattern)
ad4194399b47 topic: handle ambiguous arguments to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4060
diff changeset
    70
        else:
4059
1914a53fe792 topic: handle string argument to topic() revset earlier
Anton Shestakov <av6@dwimlabs.net>
parents: 4058
diff changeset
    71
            return (subset & mutable).filter(matches)
1914a53fe792 topic: handle string argument to topic() revset earlier
Anton Shestakov <av6@dwimlabs.net>
parents: 4058
diff changeset
    72
4060
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    73
    s = revset.getset(repo, revset.fullreposet(repo), x)
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    74
    topics = set(repo[r].topic() for r in s)
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    75
    topics.discard('')
2651
6a3df2404472 topic-revset: update the revset to no longer build changectx
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2650
diff changeset
    76
4060
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    77
    def matches(r):
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    78
        if r in s:
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    79
            return True
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    80
        topic = repo[r].topic()
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    81
        if not topic:
2651
6a3df2404472 topic-revset: update the revset to no longer build changectx
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2650
diff changeset
    82
            return False
4060
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    83
        return topic in topics
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    84
54eade86ac31 topic: handle revsets passed to topic() revset
Anton Shestakov <av6@dwimlabs.net>
parents: 4059
diff changeset
    85
    return (subset & mutable).filter(matches)
1843
0ba067a97d06 revset: add a topic() revset for querying topics
Augie Fackler <augie@google.com>
parents:
diff changeset
    86
2924
430fb1758d28 topic: use registrar.revsetpredicate to register revset predicate functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 2915
diff changeset
    87
@revsetpredicate('ngtip([branch])')
1870
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1865
diff changeset
    88
def ngtipset(repo, subset, x):
2924
430fb1758d28 topic: use registrar.revsetpredicate to register revset predicate functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 2915
diff changeset
    89
    """The untopiced tip.
1870
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1865
diff changeset
    90
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1865
diff changeset
    91
    Name is horrible so that people change it.
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1865
diff changeset
    92
    """
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1865
diff changeset
    93
    args = revset.getargs(x, 1, 1, 'topic takes one')
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1865
diff changeset
    94
    # match a specific topic
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1865
diff changeset
    95
    branch = revset.getstring(args[0], 'ngtip() argument must be a string')
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1865
diff changeset
    96
    if branch == '.':
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1865
diff changeset
    97
        branch = repo['.'].branch()
1986
042356d5ba59 ngtip: rely on topicmap for 'ngtip'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1943
diff changeset
    98
    return subset & revset.baseset(destination.ngtip(repo, branch))
1870
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1865
diff changeset
    99
2924
430fb1758d28 topic: use registrar.revsetpredicate to register revset predicate functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 2915
diff changeset
   100
@revsetpredicate('stack()')
1910
24986e5a537c stack: add a 'stack()' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
   101
def stackset(repo, subset, x):
2924
430fb1758d28 topic: use registrar.revsetpredicate to register revset predicate functions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 2915
diff changeset
   102
    """All relevant changes in the current topic,
1910
24986e5a537c stack: add a 'stack()' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
   103
24986e5a537c stack: add a 'stack()' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
   104
    This is roughly equivalent to 'topic(.) - obsolete' with a sorting moving
24986e5a537c stack: add a 'stack()' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
   105
    unstable changeset after there future parent (as if evolve where already
24986e5a537c stack: add a 'stack()' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
   106
    run)."""
2681
aa4db71a6224 topics: return a parse error if stack() revset is passed with argument
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2669
diff changeset
   107
    err = 'stack() takes no argument, it works on current topic'
aa4db71a6224 topics: return a parse error if stack() revset is passed with argument
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2669
diff changeset
   108
    revset.getargs(x, 0, 0, err)
2669
b933a8068c17 topic: add some initial support for using stack on named branch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2668
diff changeset
   109
    topic = None
b933a8068c17 topic: add some initial support for using stack on named branch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2668
diff changeset
   110
    branch = None
3156
31493a1b0e39 revset: clean up some messy logic
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2924
diff changeset
   111
    if repo.currenttopic:
2669
b933a8068c17 topic: add some initial support for using stack on named branch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2668
diff changeset
   112
        topic = repo.currenttopic
3156
31493a1b0e39 revset: clean up some messy logic
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2924
diff changeset
   113
    else:
2669
b933a8068c17 topic: add some initial support for using stack on named branch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2668
diff changeset
   114
        branch = repo[None].branch()
2915
b3abdb3d819e stack: replace 'getstack' with direct call to 'stack'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2712
diff changeset
   115
    return revset.baseset(stack.stack(repo, branch=branch, topic=topic)[1:]) & subset