mercurial/revset.py
author Gábor Stefanik <gabor.stefanik@nng.com>
Thu, 18 Aug 2016 17:25:10 +0200
changeset 29814 cbf9984a7957
parent 29780 531e85eec23c
child 29896 104914b03b83
permissions -rw-r--r--
revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334) v2: fixes from review
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     1
# revset.py - revision set queries for mercurial
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     2
#
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     3
# Copyright 2010 Matt Mackall <mpm@selenic.com>
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     4
#
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     7
25971
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
     8
from __future__ import absolute_import
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
     9
20690
13c0327eeb6f revset: changed ancestors revset to return lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20659
diff changeset
    10
import heapq
25971
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    11
import re
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    12
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    13
from .i18n import _
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    14
from . import (
26713
a271925699d6 revset: reintroduce and experimental revset for update destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26638
diff changeset
    15
    destutil,
25971
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    16
    encoding,
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    17
    error,
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    18
    hbisect,
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    19
    match as matchmod,
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    20
    node,
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    21
    obsolete as obsmod,
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    22
    parser,
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    23
    pathutil,
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    24
    phases,
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
    25
    registrar,
25971
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    26
    repoview,
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    27
    util,
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
    28
)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    29
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
    30
def _revancestors(repo, revs, followfirst):
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
    31
    """Like revlog.ancestors(), but supports followfirst."""
24306
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24219
diff changeset
    32
    if followfirst:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24219
diff changeset
    33
        cut = 1
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24219
diff changeset
    34
    else:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24219
diff changeset
    35
        cut = None
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
    36
    cl = repo.changelog
20690
13c0327eeb6f revset: changed ancestors revset to return lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20659
diff changeset
    37
13c0327eeb6f revset: changed ancestors revset to return lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20659
diff changeset
    38
    def iterate():
22832
2f1d2a42f040 revancestors: replace `descending` with `sort(reverse=False)`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22831
diff changeset
    39
        revs.sort(reverse=True)
24939
85544a52ee84 revset: use an iterator instead of a dequeue in ancestors()
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24938
diff changeset
    40
        irevs = iter(revs)
20691
c1f666e27345 revset: optimized _revancestors method based on order of revisions
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20690
diff changeset
    41
        h = []
c1f666e27345 revset: optimized _revancestors method based on order of revisions
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20690
diff changeset
    42
25143
91c49621b2b8 _revancestors: use 'next' to remove the verbose try except clauses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25131
diff changeset
    43
        inputrev = next(irevs, None)
91c49621b2b8 _revancestors: use 'next' to remove the verbose try except clauses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25131
diff changeset
    44
        if inputrev is not None:
24939
85544a52ee84 revset: use an iterator instead of a dequeue in ancestors()
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24938
diff changeset
    45
            heapq.heappush(h, -inputrev)
20691
c1f666e27345 revset: optimized _revancestors method based on order of revisions
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20690
diff changeset
    46
23956
b1e026c25552 revset: fix ancestors(null) to include null revision (issue4512)
Yuya Nishihara <yuya@tcha.org>
parents: 23954
diff changeset
    47
        seen = set()
20690
13c0327eeb6f revset: changed ancestors revset to return lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20659
diff changeset
    48
        while h:
13c0327eeb6f revset: changed ancestors revset to return lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20659
diff changeset
    49
            current = -heapq.heappop(h)
24940
6b54f749659b revset: avoid returning duplicates when returning ancestors
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24939
diff changeset
    50
            if current == inputrev:
25143
91c49621b2b8 _revancestors: use 'next' to remove the verbose try except clauses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25131
diff changeset
    51
                inputrev = next(irevs, None)
91c49621b2b8 _revancestors: use 'next' to remove the verbose try except clauses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25131
diff changeset
    52
                if inputrev is not None:
24940
6b54f749659b revset: avoid returning duplicates when returning ancestors
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24939
diff changeset
    53
                    heapq.heappush(h, -inputrev)
20690
13c0327eeb6f revset: changed ancestors revset to return lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20659
diff changeset
    54
            if current not in seen:
13c0327eeb6f revset: changed ancestors revset to return lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20659
diff changeset
    55
                seen.add(current)
13c0327eeb6f revset: changed ancestors revset to return lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20659
diff changeset
    56
                yield current
13c0327eeb6f revset: changed ancestors revset to return lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20659
diff changeset
    57
                for parent in cl.parentrevs(current)[:cut]:
13c0327eeb6f revset: changed ancestors revset to return lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20659
diff changeset
    58
                    if parent != node.nullrev:
13c0327eeb6f revset: changed ancestors revset to return lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20659
diff changeset
    59
                        heapq.heappush(h, -parent)
13c0327eeb6f revset: changed ancestors revset to return lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20659
diff changeset
    60
22795
c21342159fad generatorset: drop the leading underscore in the class name
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22794
diff changeset
    61
    return generatorset(iterate(), iterasc=False)
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
    62
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
    63
def _revdescendants(repo, revs, followfirst):
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
    64
    """Like revlog.descendants() but supports followfirst."""
24306
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24219
diff changeset
    65
    if followfirst:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24219
diff changeset
    66
        cut = 1
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24219
diff changeset
    67
    else:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24219
diff changeset
    68
        cut = None
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
    69
20692
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    70
    def iterate():
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    71
        cl = repo.changelog
25549
f93ff3ab8d14 revset: mark spots that should use 'smartset.min()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25548
diff changeset
    72
        # XXX this should be 'parentset.min()' assuming 'parentset' is a
f93ff3ab8d14 revset: mark spots that should use 'smartset.min()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25548
diff changeset
    73
        # smartset (and if it is not, it should.)
20692
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    74
        first = min(revs)
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    75
        nullrev = node.nullrev
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    76
        if first == nullrev:
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    77
            # Are there nodes with a null first parent and a non-null
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    78
            # second one? Maybe. Do we care? Probably not.
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    79
            for i in cl:
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
    80
                yield i
20692
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    81
        else:
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    82
            seen = set(revs)
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    83
            for i in cl.revs(first + 1):
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    84
                for x in cl.parentrevs(i)[:cut]:
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    85
                    if x != nullrev and x in seen:
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    86
                        seen.add(i)
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    87
                        yield i
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    88
                        break
7af341082b76 revset: changed descendants revset to use lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20691
diff changeset
    89
22795
c21342159fad generatorset: drop the leading underscore in the class name
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22794
diff changeset
    90
    return generatorset(iterate(), iterasc=True)
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
    91
26095
6eed95ca4c03 revset: mark reachablerootspure as private
Yuya Nishihara <yuya@tcha.org>
parents: 26094
diff changeset
    92
def _reachablerootspure(repo, minroot, roots, heads, includepath):
26002
fd92bfbbe02d revset: rename revsbetween to reachableroots and add an argument
Laurent Charignon <lcharignon@fb.com>
parents: 26001
diff changeset
    93
    """return (heads(::<roots> and ::<heads>))
fd92bfbbe02d revset: rename revsbetween to reachableroots and add an argument
Laurent Charignon <lcharignon@fb.com>
parents: 26001
diff changeset
    94
fd92bfbbe02d revset: rename revsbetween to reachableroots and add an argument
Laurent Charignon <lcharignon@fb.com>
parents: 26001
diff changeset
    95
    If includepath is True, return (<roots>::<heads>)."""
16862
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
    96
    if not roots:
26094
df41c7be16d6 reachableroots: construct and sort baseset in revset module
Yuya Nishihara <yuya@tcha.org>
parents: 26093
diff changeset
    97
        return []
16862
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
    98
    parentrevs = repo.changelog.parentrevs
26053
b68c9d232db6 reachableroots: use internal "revstates" array to test if rev is a root
Yuya Nishihara <yuya@tcha.org>
parents: 26006
diff changeset
    99
    roots = set(roots)
22487
e40bb83d0989 revset: stop using a baseset instead of a plain list in _revsbetween
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22483
diff changeset
   100
    visit = list(heads)
16862
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   101
    reachable = set()
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   102
    seen = {}
25566
15412bba5a68 revset: prefetch all attributes before loop in _revsbetween
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25554
diff changeset
   103
    # prefetch all the things! (because python is slow)
15412bba5a68 revset: prefetch all attributes before loop in _revsbetween
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25554
diff changeset
   104
    reached = reachable.add
15412bba5a68 revset: prefetch all attributes before loop in _revsbetween
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25554
diff changeset
   105
    dovisit = visit.append
15412bba5a68 revset: prefetch all attributes before loop in _revsbetween
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25554
diff changeset
   106
    nextvisit = visit.pop
16862
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   107
    # open-code the post-order traversal due to the tiny size of
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   108
    # sys.getrecursionlimit()
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   109
    while visit:
25566
15412bba5a68 revset: prefetch all attributes before loop in _revsbetween
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25554
diff changeset
   110
        rev = nextvisit()
16862
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   111
        if rev in roots:
25566
15412bba5a68 revset: prefetch all attributes before loop in _revsbetween
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25554
diff changeset
   112
            reached(rev)
26002
fd92bfbbe02d revset: rename revsbetween to reachableroots and add an argument
Laurent Charignon <lcharignon@fb.com>
parents: 26001
diff changeset
   113
            if not includepath:
fd92bfbbe02d revset: rename revsbetween to reachableroots and add an argument
Laurent Charignon <lcharignon@fb.com>
parents: 26001
diff changeset
   114
                continue
16862
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   115
        parents = parentrevs(rev)
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   116
        seen[rev] = parents
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   117
        for parent in parents:
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   118
            if parent >= minroot and parent not in seen:
25566
15412bba5a68 revset: prefetch all attributes before loop in _revsbetween
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25554
diff changeset
   119
                dovisit(parent)
16862
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   120
    if not reachable:
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
   121
        return baseset()
26002
fd92bfbbe02d revset: rename revsbetween to reachableroots and add an argument
Laurent Charignon <lcharignon@fb.com>
parents: 26001
diff changeset
   122
    if not includepath:
fd92bfbbe02d revset: rename revsbetween to reachableroots and add an argument
Laurent Charignon <lcharignon@fb.com>
parents: 26001
diff changeset
   123
        return reachable
16862
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   124
    for rev in sorted(seen):
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   125
        for parent in seen[rev]:
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   126
            if parent in reachable:
25566
15412bba5a68 revset: prefetch all attributes before loop in _revsbetween
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25554
diff changeset
   127
                reached(rev)
26091
60bbd4f9abd1 reachableroots: sort the smartset in the pure version too
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26060
diff changeset
   128
    return reachable
16862
b6efeb27e733 revset: introduce and use _revsbetween
Bryan O'Sullivan <bryano@fb.com>
parents: 16861
diff changeset
   129
26006
1ffd97cbf9a2 reachableroots: default to the C implementation
Laurent Charignon <lcharignon@fb.com>
parents: 26002
diff changeset
   130
def reachableroots(repo, roots, heads, includepath=False):
1ffd97cbf9a2 reachableroots: default to the C implementation
Laurent Charignon <lcharignon@fb.com>
parents: 26002
diff changeset
   131
    """return (heads(::<roots> and ::<heads>))
1ffd97cbf9a2 reachableroots: default to the C implementation
Laurent Charignon <lcharignon@fb.com>
parents: 26002
diff changeset
   132
1ffd97cbf9a2 reachableroots: default to the C implementation
Laurent Charignon <lcharignon@fb.com>
parents: 26002
diff changeset
   133
    If includepath is True, return (<roots>::<heads>)."""
1ffd97cbf9a2 reachableroots: default to the C implementation
Laurent Charignon <lcharignon@fb.com>
parents: 26002
diff changeset
   134
    if not roots:
1ffd97cbf9a2 reachableroots: default to the C implementation
Laurent Charignon <lcharignon@fb.com>
parents: 26002
diff changeset
   135
        return baseset()
26093
204131131766 reachableroots: use smartset min
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26091
diff changeset
   136
    minroot = roots.min()
26053
b68c9d232db6 reachableroots: use internal "revstates" array to test if rev is a root
Yuya Nishihara <yuya@tcha.org>
parents: 26006
diff changeset
   137
    roots = list(roots)
26006
1ffd97cbf9a2 reachableroots: default to the C implementation
Laurent Charignon <lcharignon@fb.com>
parents: 26002
diff changeset
   138
    heads = list(heads)
1ffd97cbf9a2 reachableroots: default to the C implementation
Laurent Charignon <lcharignon@fb.com>
parents: 26002
diff changeset
   139
    try:
26094
df41c7be16d6 reachableroots: construct and sort baseset in revset module
Yuya Nishihara <yuya@tcha.org>
parents: 26093
diff changeset
   140
        revs = repo.changelog.reachableroots(minroot, heads, roots, includepath)
26006
1ffd97cbf9a2 reachableroots: default to the C implementation
Laurent Charignon <lcharignon@fb.com>
parents: 26002
diff changeset
   141
    except AttributeError:
26095
6eed95ca4c03 revset: mark reachablerootspure as private
Yuya Nishihara <yuya@tcha.org>
parents: 26094
diff changeset
   142
        revs = _reachablerootspure(repo, minroot, roots, heads, includepath)
26094
df41c7be16d6 reachableroots: construct and sort baseset in revset module
Yuya Nishihara <yuya@tcha.org>
parents: 26093
diff changeset
   143
    revs = baseset(revs)
df41c7be16d6 reachableroots: construct and sort baseset in revset module
Yuya Nishihara <yuya@tcha.org>
parents: 26093
diff changeset
   144
    revs.sort()
df41c7be16d6 reachableroots: construct and sort baseset in revset module
Yuya Nishihara <yuya@tcha.org>
parents: 26093
diff changeset
   145
    return revs
26006
1ffd97cbf9a2 reachableroots: default to the C implementation
Laurent Charignon <lcharignon@fb.com>
parents: 26002
diff changeset
   146
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   147
elements = {
25815
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   148
    # token-type: binding-strength, primary, prefix, infix, suffix
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   149
    "(": (21, None, ("group", 1, ")"), ("func", 1, ")"), None),
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   150
    "##": (20, None, None, ("_concat", 20), None),
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   151
    "~": (18, None, None, ("ancestor", 18), None),
29767
e5b794063fd4 parser: remove unused binding parameter from suffix action
Yuya Nishihara <yuya@tcha.org>
parents: 29646
diff changeset
   152
    "^": (18, None, None, ("parent", 18), "parentpost"),
25815
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   153
    "-": (5, None, ("negate", 19), ("minus", 5), None),
29767
e5b794063fd4 parser: remove unused binding parameter from suffix action
Yuya Nishihara <yuya@tcha.org>
parents: 29646
diff changeset
   154
    "::": (17, None, ("dagrangepre", 17), ("dagrange", 17), "dagrangepost"),
e5b794063fd4 parser: remove unused binding parameter from suffix action
Yuya Nishihara <yuya@tcha.org>
parents: 29646
diff changeset
   155
    "..": (17, None, ("dagrangepre", 17), ("dagrange", 17), "dagrangepost"),
e5b794063fd4 parser: remove unused binding parameter from suffix action
Yuya Nishihara <yuya@tcha.org>
parents: 29646
diff changeset
   156
    ":": (15, "rangeall", ("rangepre", 15), ("range", 15), "rangepost"),
25815
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   157
    "not": (10, None, ("not", 10), None, None),
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   158
    "!": (10, None, ("not", 10), None, None),
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   159
    "and": (5, None, None, ("and", 5), None),
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   160
    "&": (5, None, None, ("and", 5), None),
29767
e5b794063fd4 parser: remove unused binding parameter from suffix action
Yuya Nishihara <yuya@tcha.org>
parents: 29646
diff changeset
   161
    "%": (5, None, None, ("only", 5), "onlypost"),
25815
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   162
    "or": (4, None, None, ("or", 4), None),
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   163
    "|": (4, None, None, ("or", 4), None),
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   164
    "+": (4, None, None, ("or", 4), None),
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   165
    "=": (3, None, None, ("keyvalue", 3), None),
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   166
    ",": (2, None, None, ("list", 2), None),
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   167
    ")": (0, None, None, None, None),
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   168
    "symbol": (0, "symbol", None, None, None),
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   169
    "string": (0, "string", None, None, None),
e71e5629e006 parser: separate actions for primary expression and prefix operator
Yuya Nishihara <yuya@tcha.org>
parents: 25801
diff changeset
   170
    "end": (0, None, None, None, None),
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   171
}
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   172
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   173
keywords = set(['and', 'or', 'not'])
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   174
23842
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   175
# default set of valid characters for the initial letter of symbols
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   176
_syminitletters = set(c for c in [chr(i) for i in xrange(256)]
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   177
                      if c.isalnum() or c in '._@' or ord(c) > 127)
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   178
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   179
# default set of valid characters for non-initial letters of symbols
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   180
_symletters = set(c for c in  [chr(i) for i in xrange(256)]
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   181
                  if c.isalnum() or c in '-._/@' or ord(c) > 127)
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   182
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   183
def tokenize(program, lookup=None, syminitletters=None, symletters=None):
17886
d8905e2c1301 revset: accept @ in unquoted symbols (issue3686)
Matt Mackall <mpm@selenic.com>
parents: 17829
diff changeset
   184
    '''
d8905e2c1301 revset: accept @ in unquoted symbols (issue3686)
Matt Mackall <mpm@selenic.com>
parents: 17829
diff changeset
   185
    Parse a revset statement into a stream of tokens
d8905e2c1301 revset: accept @ in unquoted symbols (issue3686)
Matt Mackall <mpm@selenic.com>
parents: 17829
diff changeset
   186
23842
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   187
    ``syminitletters`` is the set of valid characters for the initial
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   188
    letter of symbols.
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   189
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   190
    By default, character ``c`` is recognized as valid for initial
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   191
    letter of symbols, if ``c.isalnum() or c in '._@' or ord(c) > 127``.
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   192
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   193
    ``symletters`` is the set of valid characters for non-initial
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   194
    letters of symbols.
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   195
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   196
    By default, character ``c`` is recognized as valid for non-initial
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   197
    letters of symbols, if ``c.isalnum() or c in '-._/@' or ord(c) > 127``.
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   198
17886
d8905e2c1301 revset: accept @ in unquoted symbols (issue3686)
Matt Mackall <mpm@selenic.com>
parents: 17829
diff changeset
   199
    Check that @ is a valid unquoted token character (issue3686):
d8905e2c1301 revset: accept @ in unquoted symbols (issue3686)
Matt Mackall <mpm@selenic.com>
parents: 17829
diff changeset
   200
    >>> list(tokenize("@::"))
d8905e2c1301 revset: accept @ in unquoted symbols (issue3686)
Matt Mackall <mpm@selenic.com>
parents: 17829
diff changeset
   201
    [('symbol', '@', 0), ('::', None, 1), ('end', None, 3)]
d8905e2c1301 revset: accept @ in unquoted symbols (issue3686)
Matt Mackall <mpm@selenic.com>
parents: 17829
diff changeset
   202
d8905e2c1301 revset: accept @ in unquoted symbols (issue3686)
Matt Mackall <mpm@selenic.com>
parents: 17829
diff changeset
   203
    '''
23842
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   204
    if syminitletters is None:
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   205
        syminitletters = _syminitletters
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   206
    if symletters is None:
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   207
        symletters = _symletters
17886
d8905e2c1301 revset: accept @ in unquoted symbols (issue3686)
Matt Mackall <mpm@selenic.com>
parents: 17829
diff changeset
   208
25902
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   209
    if program and lookup:
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   210
        # attempt to parse old-style ranges first to deal with
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   211
        # things like old-tag which contain query metacharacters
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   212
        parts = program.split(':', 1)
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   213
        if all(lookup(sym) for sym in parts if sym):
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   214
            if parts[0]:
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   215
                yield ('symbol', parts[0], 0)
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   216
            if len(parts) > 1:
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   217
                s = len(parts[0])
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   218
                yield (':', None, s)
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   219
                if parts[1]:
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   220
                    yield ('symbol', parts[1], s + 1)
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   221
            yield ('end', None, len(program))
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   222
            return
5214cbdc37e5 revset: port parsing rule of old-style ranges from scmutil.revrange()
Yuya Nishihara <yuya@tcha.org>
parents: 25819
diff changeset
   223
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   224
    pos, l = 0, len(program)
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   225
    while pos < l:
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   226
        c = program[pos]
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   227
        if c.isspace(): # skip inter-token whitespace
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   228
            pass
11278
7df88cdf47fd revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents: 11275
diff changeset
   229
        elif c == ':' and program[pos:pos + 2] == '::': # look ahead carefully
11289
4215ce511134 revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents: 11284
diff changeset
   230
            yield ('::', None, pos)
11278
7df88cdf47fd revset: add support for prefix and suffix versions of : and ::
Matt Mackall <mpm@selenic.com>
parents: 11275
diff changeset
   231
            pos += 1 # skip ahead
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   232
        elif c == '.' and program[pos:pos + 2] == '..': # look ahead carefully
11289
4215ce511134 revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents: 11284
diff changeset
   233
            yield ('..', None, pos)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   234
            pos += 1 # skip ahead
23742
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
   235
        elif c == '#' and program[pos:pos + 2] == '##': # look ahead carefully
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
   236
            yield ('##', None, pos)
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
   237
            pos += 1 # skip ahead
25704
70a2082f855a revset: add parsing rule for key=value pair
Yuya Nishihara <yuya@tcha.org>
parents: 25689
diff changeset
   238
        elif c in "():=,-|&+!~^%": # handle simple operators
11289
4215ce511134 revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents: 11284
diff changeset
   239
            yield (c, None, pos)
12408
78a97859b90d revset: support raw string literals
Brodie Rao <brodie@bitheap.org>
parents: 12401
diff changeset
   240
        elif (c in '"\'' or c == 'r' and
78a97859b90d revset: support raw string literals
Brodie Rao <brodie@bitheap.org>
parents: 12401
diff changeset
   241
              program[pos:pos + 2] in ("r'", 'r"')): # handle quoted strings
78a97859b90d revset: support raw string literals
Brodie Rao <brodie@bitheap.org>
parents: 12401
diff changeset
   242
            if c == 'r':
78a97859b90d revset: support raw string literals
Brodie Rao <brodie@bitheap.org>
parents: 12401
diff changeset
   243
                pos += 1
78a97859b90d revset: support raw string literals
Brodie Rao <brodie@bitheap.org>
parents: 12401
diff changeset
   244
                c = program[pos]
78a97859b90d revset: support raw string literals
Brodie Rao <brodie@bitheap.org>
parents: 12401
diff changeset
   245
                decode = lambda x: x
78a97859b90d revset: support raw string literals
Brodie Rao <brodie@bitheap.org>
parents: 12401
diff changeset
   246
            else:
26232
43f9976346e9 revset: handle error of string unescaping
Yuya Nishihara <yuya@tcha.org>
parents: 26212
diff changeset
   247
                decode = parser.unescapestr
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   248
            pos += 1
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   249
            s = pos
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   250
            while pos < l: # find closing quote
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   251
                d = program[pos]
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   252
                if d == '\\': # skip over escaped characters
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   253
                    pos += 2
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   254
                    continue
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   255
                if d == c:
12408
78a97859b90d revset: support raw string literals
Brodie Rao <brodie@bitheap.org>
parents: 12401
diff changeset
   256
                    yield ('string', decode(program[s:pos]), s)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   257
                    break
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   258
                pos += 1
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   259
            else:
11383
de544774ebea revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents: 11349
diff changeset
   260
                raise error.ParseError(_("unterminated string"), s)
16683
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 16661
diff changeset
   261
        # gather up a symbol/keyword
23842
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   262
        elif c in syminitletters:
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   263
            s = pos
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   264
            pos += 1
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   265
            while pos < l: # find end of symbol
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   266
                d = program[pos]
23842
91dbb98b3513 revset: make tokenize extensible to parse alias declarations and definitions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23836
diff changeset
   267
                if d not in symletters:
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   268
                    break
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   269
                if d == '.' and program[pos - 1] == '.': # special case for ..
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   270
                    pos -= 1
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   271
                    break
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   272
                pos += 1
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   273
            sym = program[s:pos]
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   274
            if sym in keywords: # operator keywords
11289
4215ce511134 revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents: 11284
diff changeset
   275
                yield (sym, None, s)
20780
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   276
            elif '-' in sym:
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   277
                # some jerk gave us foo-bar-baz, try to check if it's a symbol
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   278
                if lookup and lookup(sym):
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   279
                    # looks like a real symbol
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   280
                    yield ('symbol', sym, s)
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   281
                else:
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   282
                    # looks like an expression
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   283
                    parts = sym.split('-')
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   284
                    for p in parts[:-1]:
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   285
                        if p: # possible consecutive -
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   286
                            yield ('symbol', p, s)
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   287
                        s += len(p)
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   288
                        yield ('-', None, pos)
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   289
                        s += 1
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   290
                    if parts[-1]: # possible trailing -
403f1f73d30f revset: try to handle hyphenated symbols if lookup callback is available
Matt Mackall <mpm@selenic.com>
parents: 20779
diff changeset
   291
                        yield ('symbol', parts[-1], s)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   292
            else:
11289
4215ce511134 revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents: 11284
diff changeset
   293
                yield ('symbol', sym, s)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   294
            pos -= 1
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   295
        else:
24708
fb47816e1a9c revsets: more informative syntax error message
Ryan McElroy <rmcelroy@fb.com>
parents: 24518
diff changeset
   296
            raise error.ParseError(_("syntax error in revset '%s'") %
fb47816e1a9c revsets: more informative syntax error message
Ryan McElroy <rmcelroy@fb.com>
parents: 24518
diff changeset
   297
                                   program, pos)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   298
        pos += 1
11289
4215ce511134 revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents: 11284
diff changeset
   299
    yield ('end', None, pos)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   300
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   301
# helpers
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   302
29441
9e8d258708bb revset: check invalid function syntax "func-name"() explicitly
Yuya Nishihara <yuya@tcha.org>
parents: 29425
diff changeset
   303
def getsymbol(x):
9e8d258708bb revset: check invalid function syntax "func-name"() explicitly
Yuya Nishihara <yuya@tcha.org>
parents: 29425
diff changeset
   304
    if x and x[0] == 'symbol':
9e8d258708bb revset: check invalid function syntax "func-name"() explicitly
Yuya Nishihara <yuya@tcha.org>
parents: 29425
diff changeset
   305
        return x[1]
9e8d258708bb revset: check invalid function syntax "func-name"() explicitly
Yuya Nishihara <yuya@tcha.org>
parents: 29425
diff changeset
   306
    raise error.ParseError(_('not a symbol'))
9e8d258708bb revset: check invalid function syntax "func-name"() explicitly
Yuya Nishihara <yuya@tcha.org>
parents: 29425
diff changeset
   307
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   308
def getstring(x, err):
11406
42408cd43f55 revset: fix up contains/getstring when no args passed
Matt Mackall <mpm@selenic.com>
parents: 11404
diff changeset
   309
    if x and (x[0] == 'string' or x[0] == 'symbol'):
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   310
        return x[1]
11289
4215ce511134 revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents: 11284
diff changeset
   311
    raise error.ParseError(err)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   312
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   313
def getlist(x):
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   314
    if not x:
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   315
        return []
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   316
    if x[0] == 'list':
27987
b19d8d5d6b51 revset: flatten chained 'list' operations (aka function args) (issue5072)
Yuya Nishihara <yuya@tcha.org>
parents: 27945
diff changeset
   317
        return list(x[1:])
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   318
    return [x]
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   319
11339
744d5b73f776 revset: improve filter argument handling
Matt Mackall <mpm@selenic.com>
parents: 11304
diff changeset
   320
def getargs(x, min, max, err):
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   321
    l = getlist(x)
16161
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
   322
    if len(l) < min or (max >= 0 and len(l) > max):
11289
4215ce511134 revset: raise ParseError exceptions
Matt Mackall <mpm@selenic.com>
parents: 11284
diff changeset
   323
        raise error.ParseError(err)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   324
    return l
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   325
25767
026105c442d7 revset: rename getkwargs() to getargsdict()
Yuya Nishihara <yuya@tcha.org>
parents: 25766
diff changeset
   326
def getargsdict(x, funcname, keys):
25705
48919d246a47 revset: add function to build dict of positional and keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 25704
diff changeset
   327
    return parser.buildargsdict(getlist(x), funcname, keys.split(),
48919d246a47 revset: add function to build dict of positional and keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 25704
diff changeset
   328
                                keyvaluenode='keyvalue', keynode='symbol')
48919d246a47 revset: add function to build dict of positional and keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 25704
diff changeset
   329
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   330
def getset(repo, subset, x):
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   331
    if not x:
11383
de544774ebea revset: all your error messages are belong to _
Martin Geisler <mg@lazybytes.net>
parents: 11349
diff changeset
   332
        raise error.ParseError(_("missing argument"))
20527
bde426f18e0a revset: changed mfunc and getset to work with old style revset methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20526
diff changeset
   333
    s = methods[x[0]](repo, subset, *x[1:])
22884
4584eec9aead getset: check if an object is a baseset using `isascending` instead of `set`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22883
diff changeset
   334
    if util.safehasattr(s, 'isascending'):
20527
bde426f18e0a revset: changed mfunc and getset to work with old style revset methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20526
diff changeset
   335
        return s
29098
2822e9a5263b devel: use the new 'config' argument for the revset develwarn
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29074
diff changeset
   336
    # else case should not happen, because all non-func are internal,
2822e9a5263b devel: use the new 'config' argument for the revset develwarn
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29074
diff changeset
   337
    # ignoring for now.
2822e9a5263b devel: use the new 'config' argument for the revset develwarn
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29074
diff changeset
   338
    if x[0] == 'func' and x[1][0] == 'symbol' and x[1][1] in symbols:
29147
ff07da10de4c devel: fix a typo in a deprecation warning
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29146
diff changeset
   339
        repo.ui.deprecwarn('revset "%s" uses list instead of smartset'
29146
b175d9cc3ee8 devel: officially deprecate old style revset
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29139
diff changeset
   340
                           % x[1][1],
b175d9cc3ee8 devel: officially deprecate old style revset
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29139
diff changeset
   341
                           '3.9')
20527
bde426f18e0a revset: changed mfunc and getset to work with old style revset methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20526
diff changeset
   342
    return baseset(s)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   343
17003
42c472877825 revset: add a utility for obtaining the source of a given rev
Matt Harbison <matt_harbison@yahoo.com>
parents: 17002
diff changeset
   344
def _getrevsource(repo, r):
42c472877825 revset: add a utility for obtaining the source of a given rev
Matt Harbison <matt_harbison@yahoo.com>
parents: 17002
diff changeset
   345
    extra = repo[r].extra()
42c472877825 revset: add a utility for obtaining the source of a given rev
Matt Harbison <matt_harbison@yahoo.com>
parents: 17002
diff changeset
   346
    for label in ('source', 'transplant_source', 'rebase_source'):
42c472877825 revset: add a utility for obtaining the source of a given rev
Matt Harbison <matt_harbison@yahoo.com>
parents: 17002
diff changeset
   347
        if label in extra:
42c472877825 revset: add a utility for obtaining the source of a given rev
Matt Harbison <matt_harbison@yahoo.com>
parents: 17002
diff changeset
   348
            try:
42c472877825 revset: add a utility for obtaining the source of a given rev
Matt Harbison <matt_harbison@yahoo.com>
parents: 17002
diff changeset
   349
                return repo[extra[label]].rev()
42c472877825 revset: add a utility for obtaining the source of a given rev
Matt Harbison <matt_harbison@yahoo.com>
parents: 17002
diff changeset
   350
            except error.RepoLookupError:
42c472877825 revset: add a utility for obtaining the source of a given rev
Matt Harbison <matt_harbison@yahoo.com>
parents: 17002
diff changeset
   351
                pass
42c472877825 revset: add a utility for obtaining the source of a given rev
Matt Harbison <matt_harbison@yahoo.com>
parents: 17002
diff changeset
   352
    return None
42c472877825 revset: add a utility for obtaining the source of a given rev
Matt Harbison <matt_harbison@yahoo.com>
parents: 17002
diff changeset
   353
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   354
# operator methods
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   355
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   356
def stringset(repo, subset, x):
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   357
    x = repo[x].rev()
25265
e16456831516 revset: drop magic of fullreposet membership test (issue4682)
Yuya Nishihara <yuya@tcha.org>
parents: 25094
diff changeset
   358
    if (x in subset
e16456831516 revset: drop magic of fullreposet membership test (issue4682)
Yuya Nishihara <yuya@tcha.org>
parents: 25094
diff changeset
   359
        or x == node.nullrev and isinstance(subset, fullreposet)):
20364
a6cf48b2880d revset: added baseset class (still empty) to improve revset performance
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20289
diff changeset
   360
        return baseset([x])
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
   361
    return baseset()
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   362
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   363
def rangeset(repo, subset, x, y):
23162
69524a05a7fa revset-rangeset: call 'getset' on a 'fullreposet'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23139
diff changeset
   364
    m = getset(repo, fullreposet(repo), x)
69524a05a7fa revset-rangeset: call 'getset' on a 'fullreposet'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23139
diff changeset
   365
    n = getset(repo, fullreposet(repo), y)
11456
88abbb046e66 revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents: 11446
diff changeset
   366
88abbb046e66 revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents: 11446
diff changeset
   367
    if not m or not n:
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
   368
        return baseset()
22817
e4eb4bfc3616 rangeset: use `first` and `last` instead of direct indexing
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22813
diff changeset
   369
    m, n = m.first(), n.last()
11456
88abbb046e66 revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents: 11446
diff changeset
   370
25766
d51dac68ec98 revset: work around x:y range where x or y is wdir()
Yuya Nishihara <yuya@tcha.org>
parents: 25765
diff changeset
   371
    if m == n:
d51dac68ec98 revset: work around x:y range where x or y is wdir()
Yuya Nishihara <yuya@tcha.org>
parents: 25765
diff changeset
   372
        r = baseset([m])
d51dac68ec98 revset: work around x:y range where x or y is wdir()
Yuya Nishihara <yuya@tcha.org>
parents: 25765
diff changeset
   373
    elif n == node.wdirrev:
d51dac68ec98 revset: work around x:y range where x or y is wdir()
Yuya Nishihara <yuya@tcha.org>
parents: 25765
diff changeset
   374
        r = spanset(repo, m, len(repo)) + baseset([n])
d51dac68ec98 revset: work around x:y range where x or y is wdir()
Yuya Nishihara <yuya@tcha.org>
parents: 25765
diff changeset
   375
    elif m == node.wdirrev:
d51dac68ec98 revset: work around x:y range where x or y is wdir()
Yuya Nishihara <yuya@tcha.org>
parents: 25765
diff changeset
   376
        r = baseset([m]) + spanset(repo, len(repo) - 1, n - 1)
d51dac68ec98 revset: work around x:y range where x or y is wdir()
Yuya Nishihara <yuya@tcha.org>
parents: 25765
diff changeset
   377
    elif m < n:
20526
9ad6dae67845 revset: changed revsets to use spanset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20525
diff changeset
   378
        r = spanset(repo, m, n + 1)
11456
88abbb046e66 revset: deal with empty sets in range endpoints
Matt Mackall <mpm@selenic.com>
parents: 11446
diff changeset
   379
    else:
20526
9ad6dae67845 revset: changed revsets to use spanset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20525
diff changeset
   380
        r = spanset(repo, m, n - 1)
25548
9584bcf27637 revset: mark the place where we are combining sets in the wrong direction
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25547
diff changeset
   381
    # XXX We should combine with subset first: 'subset & baseset(...)'. This is
9584bcf27637 revset: mark the place where we are combining sets in the wrong direction
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25547
diff changeset
   382
    # necessary to ensure we preserve the order in subset.
9584bcf27637 revset: mark the place where we are combining sets in the wrong direction
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25547
diff changeset
   383
    #
9584bcf27637 revset: mark the place where we are combining sets in the wrong direction
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25547
diff changeset
   384
    # This has performance implication, carrying the sorting over when possible
9584bcf27637 revset: mark the place where we are combining sets in the wrong direction
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25547
diff changeset
   385
    # would be more efficient.
20526
9ad6dae67845 revset: changed revsets to use spanset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20525
diff changeset
   386
    return r & subset
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   387
16860
e1aa1ed30030 revset: turn dagrange into a function
Bryan O'Sullivan <bryano@fb.com>
parents: 16859
diff changeset
   388
def dagrange(repo, subset, x, y):
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
   389
    r = fullreposet(repo)
26002
fd92bfbbe02d revset: rename revsbetween to reachableroots and add an argument
Laurent Charignon <lcharignon@fb.com>
parents: 26001
diff changeset
   390
    xs = reachableroots(repo, getset(repo, r, x), getset(repo, r, y),
fd92bfbbe02d revset: rename revsbetween to reachableroots and add an argument
Laurent Charignon <lcharignon@fb.com>
parents: 26001
diff changeset
   391
                         includepath=True)
29139
64c1955a0461 revset: make dagrange preserve order of input set
Yuya Nishihara <yuya@tcha.org>
parents: 29119
diff changeset
   392
    return subset & xs
16860
e1aa1ed30030 revset: turn dagrange into a function
Bryan O'Sullivan <bryano@fb.com>
parents: 16859
diff changeset
   393
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   394
def andset(repo, subset, x, y):
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   395
    return getset(repo, getset(repo, subset, x), y)
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   396
28217
d2ac8b57a75d revset: use smartset minus operator
Durham Goode <durham@fb.com>
parents: 28139
diff changeset
   397
def differenceset(repo, subset, x, y):
d2ac8b57a75d revset: use smartset minus operator
Durham Goode <durham@fb.com>
parents: 28139
diff changeset
   398
    return getset(repo, subset, x) - getset(repo, subset, y)
d2ac8b57a75d revset: use smartset minus operator
Durham Goode <durham@fb.com>
parents: 28139
diff changeset
   399
25309
b333ca94403d revset: reduce nesting of chained 'or' operations (issue4624)
Yuya Nishihara <yuya@tcha.org>
parents: 25308
diff changeset
   400
def orset(repo, subset, *xs):
25929
289149111d46 revset: make balanced addsets by orset() without using _combinesets()
Yuya Nishihara <yuya@tcha.org>
parents: 25927
diff changeset
   401
    assert xs
289149111d46 revset: make balanced addsets by orset() without using _combinesets()
Yuya Nishihara <yuya@tcha.org>
parents: 25927
diff changeset
   402
    if len(xs) == 1:
289149111d46 revset: make balanced addsets by orset() without using _combinesets()
Yuya Nishihara <yuya@tcha.org>
parents: 25927
diff changeset
   403
        return getset(repo, subset, xs[0])
289149111d46 revset: make balanced addsets by orset() without using _combinesets()
Yuya Nishihara <yuya@tcha.org>
parents: 25927
diff changeset
   404
    p = len(xs) // 2
289149111d46 revset: make balanced addsets by orset() without using _combinesets()
Yuya Nishihara <yuya@tcha.org>
parents: 25927
diff changeset
   405
    a = orset(repo, subset, *xs[:p])
289149111d46 revset: make balanced addsets by orset() without using _combinesets()
Yuya Nishihara <yuya@tcha.org>
parents: 25927
diff changeset
   406
    b = orset(repo, subset, *xs[p:])
289149111d46 revset: make balanced addsets by orset() without using _combinesets()
Yuya Nishihara <yuya@tcha.org>
parents: 25927
diff changeset
   407
    return a + b
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   408
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   409
def notset(repo, subset, x):
20366
5ec6321f49a9 revset: added substraction to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20365
diff changeset
   410
    return subset - getset(repo, subset, x)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   411
27987
b19d8d5d6b51 revset: flatten chained 'list' operations (aka function args) (issue5072)
Yuya Nishihara <yuya@tcha.org>
parents: 27945
diff changeset
   412
def listset(repo, subset, *xs):
27517
c60a9c16ae25 revset: add hint for list error to use or
timeless <timeless@mozdev.org>
parents: 27440
diff changeset
   413
    raise error.ParseError(_("can't use a list in this context"),
c60a9c16ae25 revset: add hint for list error to use or
timeless <timeless@mozdev.org>
parents: 27440
diff changeset
   414
                           hint=_('see hg help "revsets.x or y"'))
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   415
25704
70a2082f855a revset: add parsing rule for key=value pair
Yuya Nishihara <yuya@tcha.org>
parents: 25689
diff changeset
   416
def keyvaluepair(repo, subset, k, v):
70a2082f855a revset: add parsing rule for key=value pair
Yuya Nishihara <yuya@tcha.org>
parents: 25689
diff changeset
   417
    raise error.ParseError(_("can't use a key-value pair in this context"))
70a2082f855a revset: add parsing rule for key=value pair
Yuya Nishihara <yuya@tcha.org>
parents: 25689
diff changeset
   418
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   419
def func(repo, subset, a, b):
29441
9e8d258708bb revset: check invalid function syntax "func-name"() explicitly
Yuya Nishihara <yuya@tcha.org>
parents: 29425
diff changeset
   420
    f = getsymbol(a)
9e8d258708bb revset: check invalid function syntax "func-name"() explicitly
Yuya Nishihara <yuya@tcha.org>
parents: 29425
diff changeset
   421
    if f in symbols:
9e8d258708bb revset: check invalid function syntax "func-name"() explicitly
Yuya Nishihara <yuya@tcha.org>
parents: 29425
diff changeset
   422
        return symbols[f](repo, subset, b)
25632
015c0d1087a3 revset: don't suggest private or undocumented queries
Matt Harbison <matt_harbison@yahoo.com>
parents: 25630
diff changeset
   423
015c0d1087a3 revset: don't suggest private or undocumented queries
Matt Harbison <matt_harbison@yahoo.com>
parents: 25630
diff changeset
   424
    keep = lambda fn: getattr(fn, '__doc__', None) is not None
015c0d1087a3 revset: don't suggest private or undocumented queries
Matt Harbison <matt_harbison@yahoo.com>
parents: 25630
diff changeset
   425
015c0d1087a3 revset: don't suggest private or undocumented queries
Matt Harbison <matt_harbison@yahoo.com>
parents: 25630
diff changeset
   426
    syms = [s for (s, fn) in symbols.items() if keep(fn)]
29441
9e8d258708bb revset: check invalid function syntax "func-name"() explicitly
Yuya Nishihara <yuya@tcha.org>
parents: 29425
diff changeset
   427
    raise error.UnknownIdentifier(f, syms)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   428
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   429
# functions
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   430
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   431
# symbols are callables like:
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   432
#   fn(repo, subset, x)
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   433
# with:
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   434
#   repo - current repository instance
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   435
#   subset - of revisions to be examined
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   436
#   x - argument in tree form
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   437
symbols = {}
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   438
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   439
# symbols which can't be used for a DoS attack for any given input
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   440
# (e.g. those which accept regexes as plain strings shouldn't be included)
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   441
# functions that just return a lot of changesets (like all) don't count here
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   442
safesymbols = set()
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   443
28395
0383f7a5e86c revset: replace predicate by revsetpredicate of registrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28393
diff changeset
   444
predicate = registrar.revsetpredicate()
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   445
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   446
@predicate('_destupdate')
26713
a271925699d6 revset: reintroduce and experimental revset for update destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26638
diff changeset
   447
def _destupdate(repo, subset, x):
a271925699d6 revset: reintroduce and experimental revset for update destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26638
diff changeset
   448
    # experimental revset for update destination
a271925699d6 revset: reintroduce and experimental revset for update destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26638
diff changeset
   449
    args = getargsdict(x, 'limit', 'clean check')
a271925699d6 revset: reintroduce and experimental revset for update destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26638
diff changeset
   450
    return subset & baseset([destutil.destupdate(repo, **args)[0]])
a271925699d6 revset: reintroduce and experimental revset for update destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26638
diff changeset
   451
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   452
@predicate('_destmerge')
26716
c027641f8a83 revset: rename and test '_destmerge'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26714
diff changeset
   453
def _destmerge(repo, subset, x):
c027641f8a83 revset: rename and test '_destmerge'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26714
diff changeset
   454
    # experimental revset for merge destination
28139
5476a7a039c0 destutil: allow to specify an explicit source for the merge
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28015
diff changeset
   455
    sourceset = None
5476a7a039c0 destutil: allow to specify an explicit source for the merge
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28015
diff changeset
   456
    if x is not None:
5476a7a039c0 destutil: allow to specify an explicit source for the merge
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28015
diff changeset
   457
        sourceset = getset(repo, fullreposet(repo), x)
5476a7a039c0 destutil: allow to specify an explicit source for the merge
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28015
diff changeset
   458
    return subset & baseset([destutil.destmerge(repo, sourceset=sourceset)])
26303
c99b4d6efdd8 merge: move default destination computation in a revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26232
diff changeset
   459
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   460
@predicate('adds(pattern)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   461
def adds(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   462
    """Changesets that add a file matching pattern.
20289
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
   463
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
   464
    The pattern without explicit kind like ``glob:`` is expected to be
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
   465
    relative to the current directory and match against a file or a
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
   466
    directory.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   467
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   468
    # i18n: "adds" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   469
    pat = getstring(x, _("adds requires a pattern"))
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   470
    return checkstatus(repo, subset, pat, 1)
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   471
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   472
@predicate('ancestor(*changeset)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   473
def ancestor(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   474
    """A greatest common ancestor of the changesets.
18536
ae645d4f084c revset: change ancestor to accept 0 or more arguments (issue3750)
Paul Cavallaro <ptc@fb.com>
parents: 18495
diff changeset
   475
ae645d4f084c revset: change ancestor to accept 0 or more arguments (issue3750)
Paul Cavallaro <ptc@fb.com>
parents: 18495
diff changeset
   476
    Accepts 0 or more changesets.
ae645d4f084c revset: change ancestor to accept 0 or more arguments (issue3750)
Paul Cavallaro <ptc@fb.com>
parents: 18495
diff changeset
   477
    Will return empty list when passed no args.
ae645d4f084c revset: change ancestor to accept 0 or more arguments (issue3750)
Paul Cavallaro <ptc@fb.com>
parents: 18495
diff changeset
   478
    Greatest common ancestor of a single changeset is that changeset.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   479
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   480
    # i18n: "ancestor" is a keyword
18536
ae645d4f084c revset: change ancestor to accept 0 or more arguments (issue3750)
Paul Cavallaro <ptc@fb.com>
parents: 18495
diff changeset
   481
    l = getlist(x)
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
   482
    rl = fullreposet(repo)
18536
ae645d4f084c revset: change ancestor to accept 0 or more arguments (issue3750)
Paul Cavallaro <ptc@fb.com>
parents: 18495
diff changeset
   483
    anc = None
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   484
18536
ae645d4f084c revset: change ancestor to accept 0 or more arguments (issue3750)
Paul Cavallaro <ptc@fb.com>
parents: 18495
diff changeset
   485
    # (getset(repo, rl, i) for i in l) generates a list of lists
ae645d4f084c revset: change ancestor to accept 0 or more arguments (issue3750)
Paul Cavallaro <ptc@fb.com>
parents: 18495
diff changeset
   486
    for revs in (getset(repo, rl, i) for i in l):
ae645d4f084c revset: change ancestor to accept 0 or more arguments (issue3750)
Paul Cavallaro <ptc@fb.com>
parents: 18495
diff changeset
   487
        for r in revs:
ae645d4f084c revset: change ancestor to accept 0 or more arguments (issue3750)
Paul Cavallaro <ptc@fb.com>
parents: 18495
diff changeset
   488
            if anc is None:
20991
a05d694599f9 revlog: use context ancestor instead of changelog ancestor
Mads Kiilerich <madski@unity3d.com>
parents: 20895
diff changeset
   489
                anc = repo[r]
18536
ae645d4f084c revset: change ancestor to accept 0 or more arguments (issue3750)
Paul Cavallaro <ptc@fb.com>
parents: 18495
diff changeset
   490
            else:
20991
a05d694599f9 revlog: use context ancestor instead of changelog ancestor
Mads Kiilerich <madski@unity3d.com>
parents: 20895
diff changeset
   491
                anc = anc.ancestor(repo[r])
a05d694599f9 revlog: use context ancestor instead of changelog ancestor
Mads Kiilerich <madski@unity3d.com>
parents: 20895
diff changeset
   492
a05d694599f9 revlog: use context ancestor instead of changelog ancestor
Mads Kiilerich <madski@unity3d.com>
parents: 20895
diff changeset
   493
    if anc is not None and anc.rev() in subset:
a05d694599f9 revlog: use context ancestor instead of changelog ancestor
Mads Kiilerich <madski@unity3d.com>
parents: 20895
diff changeset
   494
        return baseset([anc.rev()])
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
   495
    return baseset()
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   496
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   497
def _ancestors(repo, subset, x, followfirst=False):
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
   498
    heads = getset(repo, fullreposet(repo), x)
22944
5aae3dea8044 revset: better naming of variables containing the value of a single argument
Mads Kiilerich <madski@unity3d.com>
parents: 22891
diff changeset
   499
    if not heads:
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
   500
        return baseset()
22944
5aae3dea8044 revset: better naming of variables containing the value of a single argument
Mads Kiilerich <madski@unity3d.com>
parents: 22891
diff changeset
   501
    s = _revancestors(repo, heads, followfirst)
23003
62d19ce9d7b6 revset-_ancestor: use & instead of filter
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22999
diff changeset
   502
    return subset & s
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   503
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   504
@predicate('ancestors(set)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   505
def ancestors(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   506
    """Changesets that are ancestors of a changeset in set.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   507
    """
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   508
    return _ancestors(repo, subset, x)
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   509
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   510
@predicate('_firstancestors', safe=True)
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   511
def _firstancestors(repo, subset, x):
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   512
    # ``_firstancestors(set)``
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   513
    # Like ``ancestors(set)`` but follows only the first parents.
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   514
    return _ancestors(repo, subset, x, followfirst=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   515
14070
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
   516
def ancestorspec(repo, subset, x, n):
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
   517
    """``set~n``
16683
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 16661
diff changeset
   518
    Changesets that are the Nth ancestor (first parents only) of a changeset
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 16661
diff changeset
   519
    in set.
14070
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
   520
    """
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
   521
    try:
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
   522
        n = int(n[1])
14851
f96c354493d7 revsets: actually catch type error on tip^p1(tip) (issue2884)
Matt Mackall <mpm@selenic.com>
parents: 14842
diff changeset
   523
    except (TypeError, ValueError):
14070
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
   524
        raise error.ParseError(_("~ expects a number"))
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
   525
    ps = set()
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
   526
    cl = repo.changelog
23163
6f1b8b3f12fd revset-ancestorspec: call 'getset' on a 'fullreposet'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23162
diff changeset
   527
    for r in getset(repo, fullreposet(repo), x):
14070
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
   528
        for i in range(n):
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
   529
            r = cl.parentrevs(r)[0]
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
   530
        ps.add(r)
22531
22ba2c0825da revset: use `subset &` in `ancestorspec`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22530
diff changeset
   531
    return subset & ps
14070
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
   532
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   533
@predicate('author(string)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   534
def author(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   535
    """Alias for ``user(string)``.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   536
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   537
    # i18n: "author" is a keyword
15726
9b822edecb4c i18n: use "encoding.lower()" to normalize specified string for revset
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15596
diff changeset
   538
    n = encoding.lower(getstring(x, _("author requires a string")))
16823
b23bacb230c9 revset: add pattern matching to the 'user' revset expression
Simon King <simon@simonking.org.uk>
parents: 16822
diff changeset
   539
    kind, pattern, matcher = _substringmatcher(n)
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   540
    return subset.filter(lambda x: matcher(encoding.lower(repo[x].user())),
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   541
                         condrepr=('<user %r>', n))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   542
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   543
@predicate('bisect(string)', safe=True)
15134
81adf7777f8f revset: rename bisected() to bisect()
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 15133
diff changeset
   544
def bisect(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   545
    """Changesets marked in the specified bisect status:
15136
18219c0789ae revset.bisect: add new 'range' set to the bisect keyword
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 15135
diff changeset
   546
15153
fa0a464e4ca5 hbisect: add two new revset descriptions: 'goods' and 'bads'
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 15147
diff changeset
   547
    - ``good``, ``bad``, ``skip``: csets explicitly marked as good/bad/skip
17424
e7cfe3587ea4 fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 17390
diff changeset
   548
    - ``goods``, ``bads``      : csets topologically good/bad
15153
fa0a464e4ca5 hbisect: add two new revset descriptions: 'goods' and 'bads'
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 15147
diff changeset
   549
    - ``range``              : csets taking part in the bisection
fa0a464e4ca5 hbisect: add two new revset descriptions: 'goods' and 'bads'
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 15147
diff changeset
   550
    - ``pruned``             : csets that are goods, bads or skipped
fa0a464e4ca5 hbisect: add two new revset descriptions: 'goods' and 'bads'
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 15147
diff changeset
   551
    - ``untested``           : csets whose fate is yet unknown
fa0a464e4ca5 hbisect: add two new revset descriptions: 'goods' and 'bads'
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 15147
diff changeset
   552
    - ``ignored``            : csets ignored due to DAG topology
16647
14913fcb30c6 bisect: track the current changeset (issue3382)
Bryan O'Sullivan <bryano@fb.com>
parents: 16640
diff changeset
   553
    - ``current``            : the cset currently being bisected
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   554
    """
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
   555
    # i18n: "bisect" is a keyword
15135
f19de58af225 revset.bisect: move bisect() code to hbisect.py
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 15134
diff changeset
   556
    status = getstring(x, _("bisect requires a string")).lower()
16467
7f59900e3f8b revset: fix O(n**2) behaviour of bisect() (issue3381)
Bryan O'Sullivan <bryano@fb.com>
parents: 16453
diff changeset
   557
    state = set(hbisect.get(repo, status))
22532
0cf46b8298fe revset: use `subset &` in `bisect`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22531
diff changeset
   558
    return subset & state
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   559
15134
81adf7777f8f revset: rename bisected() to bisect()
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 15133
diff changeset
   560
# Backward-compatibility
81adf7777f8f revset: rename bisected() to bisect()
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 15133
diff changeset
   561
# - no help entry so that we do not advertise it any more
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   562
@predicate('bisected', safe=True)
15134
81adf7777f8f revset: rename bisected() to bisect()
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 15133
diff changeset
   563
def bisected(repo, subset, x):
81adf7777f8f revset: rename bisected() to bisect()
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 15133
diff changeset
   564
    return bisect(repo, subset, x)
81adf7777f8f revset: rename bisected() to bisect()
"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
parents: 15133
diff changeset
   565
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   566
@predicate('bookmark([name])', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   567
def bookmark(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   568
    """The named bookmark or all bookmarks.
16822
da55d8a77390 revset: add pattern matching to 'bookmarks' revset expression
Simon King <simon@simonking.org.uk>
parents: 16821
diff changeset
   569
da55d8a77390 revset: add pattern matching to 'bookmarks' revset expression
Simon King <simon@simonking.org.uk>
parents: 16821
diff changeset
   570
    If `name` starts with `re:`, the remainder of the name is treated as
da55d8a77390 revset: add pattern matching to 'bookmarks' revset expression
Simon King <simon@simonking.org.uk>
parents: 16821
diff changeset
   571
    a regular expression. To match a bookmark that actually starts with `re:`,
da55d8a77390 revset: add pattern matching to 'bookmarks' revset expression
Simon King <simon@simonking.org.uk>
parents: 16821
diff changeset
   572
    use the prefix `literal:`.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   573
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   574
    # i18n: "bookmark" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   575
    args = getargs(x, 0, 1, _('bookmark takes one or no arguments'))
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   576
    if args:
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   577
        bm = getstring(args[0],
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   578
                       # i18n: "bookmark" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   579
                       _('the argument to bookmark must be a string'))
26481
7d132557e44a util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents: 26307
diff changeset
   580
        kind, pattern, matcher = util.stringmatcher(bm)
22499
8c9f9e346acc revset: unify code flow in `bookmark`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22498
diff changeset
   581
        bms = set()
16822
da55d8a77390 revset: add pattern matching to 'bookmarks' revset expression
Simon King <simon@simonking.org.uk>
parents: 16821
diff changeset
   582
        if kind == 'literal':
22105
3efe3c2609e0 revset: bookmark revset interprets 'literal:' prefix correctly (issue4329)
Michael O'Connor <mkoconnor@gmail.com>
parents: 21939
diff changeset
   583
            bmrev = repo._bookmarks.get(pattern, None)
16822
da55d8a77390 revset: add pattern matching to 'bookmarks' revset expression
Simon King <simon@simonking.org.uk>
parents: 16821
diff changeset
   584
            if not bmrev:
23978
eeb5d5ab14a6 revset: raise RepoLookupError to make present() predicate continue the query
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23956
diff changeset
   585
                raise error.RepoLookupError(_("bookmark '%s' does not exist")
26538
5c9ec1cc671a revset: strip off "literal:" prefix from bookmark not found error
Yuya Nishihara <yuya@tcha.org>
parents: 26537
diff changeset
   586
                                            % pattern)
22499
8c9f9e346acc revset: unify code flow in `bookmark`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22498
diff changeset
   587
            bms.add(repo[bmrev].rev())
16822
da55d8a77390 revset: add pattern matching to 'bookmarks' revset expression
Simon King <simon@simonking.org.uk>
parents: 16821
diff changeset
   588
        else:
da55d8a77390 revset: add pattern matching to 'bookmarks' revset expression
Simon King <simon@simonking.org.uk>
parents: 16821
diff changeset
   589
            matchrevs = set()
18495
8260fa9f30b9 bookmarks: don't use bookmarks.listbookmarks in local computations
Kevin Bullock <kbullock@ringworld.org>
parents: 18473
diff changeset
   590
            for name, bmrev in repo._bookmarks.iteritems():
16822
da55d8a77390 revset: add pattern matching to 'bookmarks' revset expression
Simon King <simon@simonking.org.uk>
parents: 16821
diff changeset
   591
                if matcher(name):
da55d8a77390 revset: add pattern matching to 'bookmarks' revset expression
Simon King <simon@simonking.org.uk>
parents: 16821
diff changeset
   592
                    matchrevs.add(bmrev)
da55d8a77390 revset: add pattern matching to 'bookmarks' revset expression
Simon King <simon@simonking.org.uk>
parents: 16821
diff changeset
   593
            if not matchrevs:
23978
eeb5d5ab14a6 revset: raise RepoLookupError to make present() predicate continue the query
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23956
diff changeset
   594
                raise error.RepoLookupError(_("no bookmarks exist"
eeb5d5ab14a6 revset: raise RepoLookupError to make present() predicate continue the query
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23956
diff changeset
   595
                                              " that match '%s'") % pattern)
16822
da55d8a77390 revset: add pattern matching to 'bookmarks' revset expression
Simon King <simon@simonking.org.uk>
parents: 16821
diff changeset
   596
            for bmrev in matchrevs:
22499
8c9f9e346acc revset: unify code flow in `bookmark`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22498
diff changeset
   597
                bms.add(repo[bmrev].rev())
8c9f9e346acc revset: unify code flow in `bookmark`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22498
diff changeset
   598
    else:
8c9f9e346acc revset: unify code flow in `bookmark`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22498
diff changeset
   599
        bms = set([repo[r].rev()
8c9f9e346acc revset: unify code flow in `bookmark`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22498
diff changeset
   600
                   for r in repo._bookmarks.values()])
22500
171015d3b30c revset: remove nullrev from the bookmark computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22499
diff changeset
   601
    bms -= set([node.nullrev])
22530
faf4f63533ff revset: use `subset &` in `bookmark`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22529
diff changeset
   602
    return subset & bms
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   603
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   604
@predicate('branch(string or set)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   605
def branch(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   606
    """
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   607
    All changesets belonging to the given branch or the branches of the given
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   608
    changesets.
16821
0946502fd3d5 revset: add pattern matching to 'branch' revset expression
Simon King <simon@simonking.org.uk>
parents: 16820
diff changeset
   609
0946502fd3d5 revset: add pattern matching to 'branch' revset expression
Simon King <simon@simonking.org.uk>
parents: 16820
diff changeset
   610
    If `string` starts with `re:`, the remainder of the name is treated as
0946502fd3d5 revset: add pattern matching to 'branch' revset expression
Simon King <simon@simonking.org.uk>
parents: 16820
diff changeset
   611
    a regular expression. To match a branch that actually starts with `re:`,
0946502fd3d5 revset: add pattern matching to 'branch' revset expression
Simon King <simon@simonking.org.uk>
parents: 16820
diff changeset
   612
    use the prefix `literal:`.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   613
    """
24374
77fd1fb538cd revbranchcache: store repo on the object
Durham Goode <durham@fb.com>
parents: 24366
diff changeset
   614
    getbi = repo.revbranchcache().branchinfo
23787
678f53865c68 revset: use localrepo revbranchcache for branch name filtering
Mads Kiilerich <madski@unity3d.com>
parents: 23765
diff changeset
   615
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   616
    try:
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   617
        b = getstring(x, '')
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   618
    except error.ParseError:
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   619
        # not a string, but another revspec, e.g. tip()
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   620
        pass
16821
0946502fd3d5 revset: add pattern matching to 'branch' revset expression
Simon King <simon@simonking.org.uk>
parents: 16820
diff changeset
   621
    else:
26481
7d132557e44a util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents: 26307
diff changeset
   622
        kind, pattern, matcher = util.stringmatcher(b)
16821
0946502fd3d5 revset: add pattern matching to 'branch' revset expression
Simon King <simon@simonking.org.uk>
parents: 16820
diff changeset
   623
        if kind == 'literal':
0946502fd3d5 revset: add pattern matching to 'branch' revset expression
Simon King <simon@simonking.org.uk>
parents: 16820
diff changeset
   624
            # note: falls through to the revspec case if no branch with
26537
832feae7c986 revset: do not fall through to revspec for literal: branch (issue4838)
Yuya Nishihara <yuya@tcha.org>
parents: 26481
diff changeset
   625
            # this name exists and pattern kind is not specified explicitly
16821
0946502fd3d5 revset: add pattern matching to 'branch' revset expression
Simon King <simon@simonking.org.uk>
parents: 16820
diff changeset
   626
            if pattern in repo.branchmap():
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   627
                return subset.filter(lambda r: matcher(getbi(r)[0]),
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   628
                                     condrepr=('<branch %r>', b))
26537
832feae7c986 revset: do not fall through to revspec for literal: branch (issue4838)
Yuya Nishihara <yuya@tcha.org>
parents: 26481
diff changeset
   629
            if b.startswith('literal:'):
832feae7c986 revset: do not fall through to revspec for literal: branch (issue4838)
Yuya Nishihara <yuya@tcha.org>
parents: 26481
diff changeset
   630
                raise error.RepoLookupError(_("branch '%s' does not exist")
832feae7c986 revset: do not fall through to revspec for literal: branch (issue4838)
Yuya Nishihara <yuya@tcha.org>
parents: 26481
diff changeset
   631
                                            % pattern)
16821
0946502fd3d5 revset: add pattern matching to 'branch' revset expression
Simon King <simon@simonking.org.uk>
parents: 16820
diff changeset
   632
        else:
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   633
            return subset.filter(lambda r: matcher(getbi(r)[0]),
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   634
                                 condrepr=('<branch %r>', b))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   635
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
   636
    s = getset(repo, fullreposet(repo), x)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   637
    b = set()
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   638
    for r in s:
24374
77fd1fb538cd revbranchcache: store repo on the object
Durham Goode <durham@fb.com>
parents: 24366
diff changeset
   639
        b.add(getbi(r)[0])
22867
5ee9b78ce805 revset-branch: remove usage of `set()`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22866
diff changeset
   640
    c = s.__contains__
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   641
    return subset.filter(lambda r: c(r) or getbi(r)[0] in b,
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   642
                         condrepr=lambda: '<branch %r>' % sorted(b))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   643
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   644
@predicate('bumped()', safe=True)
17829
c73f7a28953c revset: add a bumped revset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17825
diff changeset
   645
def bumped(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   646
    """Mutable changesets marked as successors of public changesets.
17829
c73f7a28953c revset: add a bumped revset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17825
diff changeset
   647
c73f7a28953c revset: add a bumped revset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17825
diff changeset
   648
    Only non-public and non-obsolete changesets can be `bumped`.
c73f7a28953c revset: add a bumped revset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17825
diff changeset
   649
    """
c73f7a28953c revset: add a bumped revset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17825
diff changeset
   650
    # i18n: "bumped" is a keyword
c73f7a28953c revset: add a bumped revset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17825
diff changeset
   651
    getargs(x, 0, 0, _("bumped takes no arguments"))
c73f7a28953c revset: add a bumped revset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17825
diff changeset
   652
    bumped = obsmod.getrevs(repo, 'bumped')
20367
2ac278aab2b4 revset: added intersection to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20366
diff changeset
   653
    return subset & bumped
17829
c73f7a28953c revset: add a bumped revset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17825
diff changeset
   654
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   655
@predicate('bundle()', safe=True)
17913
03e552aaae67 bundle: add revset expression to show bundle contents (issue3487)
Tomasz Kleczek <tkleczek@fb.com>
parents: 17886
diff changeset
   656
def bundle(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   657
    """Changesets in the bundle.
17913
03e552aaae67 bundle: add revset expression to show bundle contents (issue3487)
Tomasz Kleczek <tkleczek@fb.com>
parents: 17886
diff changeset
   658
03e552aaae67 bundle: add revset expression to show bundle contents (issue3487)
Tomasz Kleczek <tkleczek@fb.com>
parents: 17886
diff changeset
   659
    Bundle must be specified by the -R option."""
03e552aaae67 bundle: add revset expression to show bundle contents (issue3487)
Tomasz Kleczek <tkleczek@fb.com>
parents: 17886
diff changeset
   660
03e552aaae67 bundle: add revset expression to show bundle contents (issue3487)
Tomasz Kleczek <tkleczek@fb.com>
parents: 17886
diff changeset
   661
    try:
18411
8b0f0dd56cec bundlerepo: improve performance for bundle() revset expression
Mads Kiilerich <madski@unity3d.com>
parents: 18382
diff changeset
   662
        bundlerevs = repo.changelog.bundlerevs
17913
03e552aaae67 bundle: add revset expression to show bundle contents (issue3487)
Tomasz Kleczek <tkleczek@fb.com>
parents: 17886
diff changeset
   663
    except AttributeError:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26571
diff changeset
   664
        raise error.Abort(_("no bundle provided - specify with -R"))
20367
2ac278aab2b4 revset: added intersection to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20366
diff changeset
   665
    return subset & bundlerevs
17913
03e552aaae67 bundle: add revset expression to show bundle contents (issue3487)
Tomasz Kleczek <tkleczek@fb.com>
parents: 17886
diff changeset
   666
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   667
def checkstatus(repo, subset, pat, field):
16521
592701c8eac6 revset: fix adds/modifies/removes and patterns (issue3403)
Patrick Mezard <patrick@mezard.eu>
parents: 16467
diff changeset
   668
    hasset = matchmod.patkind(pat) == 'set'
20457
ed7b674824a3 revset: added lazyset implementation to checkstatus
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20456
diff changeset
   669
23115
c23c03605c59 revset: don't recreate matcher for every revision
Martin von Zweigbergk <martinvonz@google.com>
parents: 23100
diff changeset
   670
    mcache = [None]
20457
ed7b674824a3 revset: added lazyset implementation to checkstatus
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20456
diff changeset
   671
    def matches(x):
ed7b674824a3 revset: added lazyset implementation to checkstatus
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20456
diff changeset
   672
        c = repo[x]
23115
c23c03605c59 revset: don't recreate matcher for every revision
Martin von Zweigbergk <martinvonz@google.com>
parents: 23100
diff changeset
   673
        if not mcache[0] or hasset:
c23c03605c59 revset: don't recreate matcher for every revision
Martin von Zweigbergk <martinvonz@google.com>
parents: 23100
diff changeset
   674
            mcache[0] = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
c23c03605c59 revset: don't recreate matcher for every revision
Martin von Zweigbergk <martinvonz@google.com>
parents: 23100
diff changeset
   675
        m = mcache[0]
c23c03605c59 revset: don't recreate matcher for every revision
Martin von Zweigbergk <martinvonz@google.com>
parents: 23100
diff changeset
   676
        fname = None
c23c03605c59 revset: don't recreate matcher for every revision
Martin von Zweigbergk <martinvonz@google.com>
parents: 23100
diff changeset
   677
        if not m.anypats() and len(m.files()) == 1:
c23c03605c59 revset: don't recreate matcher for every revision
Martin von Zweigbergk <martinvonz@google.com>
parents: 23100
diff changeset
   678
            fname = m.files()[0]
16521
592701c8eac6 revset: fix adds/modifies/removes and patterns (issue3403)
Patrick Mezard <patrick@mezard.eu>
parents: 16467
diff changeset
   679
        if fname is not None:
592701c8eac6 revset: fix adds/modifies/removes and patterns (issue3403)
Patrick Mezard <patrick@mezard.eu>
parents: 16467
diff changeset
   680
            if fname not in c.files():
20457
ed7b674824a3 revset: added lazyset implementation to checkstatus
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20456
diff changeset
   681
                return False
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   682
        else:
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   683
            for f in c.files():
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   684
                if m(f):
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   685
                    break
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   686
            else:
20457
ed7b674824a3 revset: added lazyset implementation to checkstatus
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20456
diff changeset
   687
                return False
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   688
        files = repo.status(c.p1().node(), c.node())[field]
16521
592701c8eac6 revset: fix adds/modifies/removes and patterns (issue3403)
Patrick Mezard <patrick@mezard.eu>
parents: 16467
diff changeset
   689
        if fname is not None:
592701c8eac6 revset: fix adds/modifies/removes and patterns (issue3403)
Patrick Mezard <patrick@mezard.eu>
parents: 16467
diff changeset
   690
            if fname in files:
20457
ed7b674824a3 revset: added lazyset implementation to checkstatus
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20456
diff changeset
   691
                return True
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   692
        else:
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   693
            for f in files:
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   694
                if m(f):
20457
ed7b674824a3 revset: added lazyset implementation to checkstatus
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20456
diff changeset
   695
                    return True
ed7b674824a3 revset: added lazyset implementation to checkstatus
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20456
diff changeset
   696
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   697
    return subset.filter(matches, condrepr=('<status[%r] %r>', field, pat))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   698
29406
c2193e59ef9f revsets: passing a set to baseset() is not wrong
Martin von Zweigbergk <martinvonz@google.com>
parents: 29389
diff changeset
   699
def _children(repo, subset, parentset):
25550
3e9049876ace revset: gratuitous code move in '_children'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25549
diff changeset
   700
    if not parentset:
3e9049876ace revset: gratuitous code move in '_children'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25549
diff changeset
   701
        return baseset()
15899
476a981fdf34 revset: optimize roots and children
Matt Mackall <mpm@selenic.com>
parents: 15898
diff changeset
   702
    cs = set()
476a981fdf34 revset: optimize roots and children
Matt Mackall <mpm@selenic.com>
parents: 15898
diff changeset
   703
    pr = repo.changelog.parentrevs
25567
f140d6207cca revset: use parentsets.min in _children
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25566
diff changeset
   704
    minrev = parentset.min()
29406
c2193e59ef9f revsets: passing a set to baseset() is not wrong
Martin von Zweigbergk <martinvonz@google.com>
parents: 29389
diff changeset
   705
    for r in subset:
18063
34a1a639d835 revset.children: ignore rev numbers that are too low
Siddharth Agarwal <sid0@fb.com>
parents: 17980
diff changeset
   706
        if r <= minrev:
34a1a639d835 revset.children: ignore rev numbers that are too low
Siddharth Agarwal <sid0@fb.com>
parents: 17980
diff changeset
   707
            continue
15899
476a981fdf34 revset: optimize roots and children
Matt Mackall <mpm@selenic.com>
parents: 15898
diff changeset
   708
        for p in pr(r):
16396
03e408a122c4 revset: avoid set duplication in roots()
Patrick Mezard <patrick@mezard.eu>
parents: 16395
diff changeset
   709
            if p in parentset:
15899
476a981fdf34 revset: optimize roots and children
Matt Mackall <mpm@selenic.com>
parents: 15898
diff changeset
   710
                cs.add(r)
20709
71df845d86cf revsets: backout d04aac468bf4 due to performance regressions
Matt Mackall <mpm@selenic.com>
parents: 20708
diff changeset
   711
    return baseset(cs)
15899
476a981fdf34 revset: optimize roots and children
Matt Mackall <mpm@selenic.com>
parents: 15898
diff changeset
   712
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   713
@predicate('children(set)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   714
def children(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   715
    """Child changesets of changesets in set.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   716
    """
23164
7a42e5d4c418 revset-children: call 'getset' on a 'fullreposet'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23163
diff changeset
   717
    s = getset(repo, fullreposet(repo), x)
15899
476a981fdf34 revset: optimize roots and children
Matt Mackall <mpm@selenic.com>
parents: 15898
diff changeset
   718
    cs = _children(repo, subset, s)
20367
2ac278aab2b4 revset: added intersection to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20366
diff changeset
   719
    return subset & cs
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   720
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   721
@predicate('closed()', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   722
def closed(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   723
    """Changeset is closed.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   724
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   725
    # i18n: "closed" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   726
    getargs(x, 0, 0, _("closed takes no arguments"))
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   727
    return subset.filter(lambda r: repo[r].closesbranch(),
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   728
                         condrepr='<branch closed>')
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   729
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   730
@predicate('contains(pattern)')
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   731
def contains(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   732
    """The revision's manifest contains a file matching pattern (but might not
21199
e9c2f76be74b help: clarify distinction among `contains`/`file`/`filelog`
Greg Hurrell <glh@fb.com>
parents: 21173
diff changeset
   733
    modify it). See :hg:`help patterns` for information about file patterns.
20289
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
   734
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
   735
    The pattern without explicit kind like ``glob:`` is expected to be
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
   736
    relative to the current directory and match against a file exactly
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
   737
    for efficiency.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   738
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   739
    # i18n: "contains" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   740
    pat = getstring(x, _("contains requires a pattern"))
20461
abd8e56a1038 revset: added lazyset implementation to contains revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20460
diff changeset
   741
abd8e56a1038 revset: added lazyset implementation to contains revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20460
diff changeset
   742
    def matches(x):
abd8e56a1038 revset: added lazyset implementation to contains revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20460
diff changeset
   743
        if not matchmod.patkind(pat):
abd8e56a1038 revset: added lazyset implementation to contains revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20460
diff changeset
   744
            pats = pathutil.canonpath(repo.root, repo.getcwd(), pat)
abd8e56a1038 revset: added lazyset implementation to contains revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20460
diff changeset
   745
            if pats in repo[x]:
abd8e56a1038 revset: added lazyset implementation to contains revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20460
diff changeset
   746
                return True
abd8e56a1038 revset: added lazyset implementation to contains revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20460
diff changeset
   747
        else:
abd8e56a1038 revset: added lazyset implementation to contains revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20460
diff changeset
   748
            c = repo[x]
abd8e56a1038 revset: added lazyset implementation to contains revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20460
diff changeset
   749
            m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=c)
15964
6e37b8282aa2 revsets: provide contexts for filesets
Matt Mackall <mpm@selenic.com>
parents: 15949
diff changeset
   750
            for f in c.manifest():
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   751
                if m(f):
20461
abd8e56a1038 revset: added lazyset implementation to contains revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20460
diff changeset
   752
                    return True
abd8e56a1038 revset: added lazyset implementation to contains revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20460
diff changeset
   753
        return False
abd8e56a1038 revset: added lazyset implementation to contains revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20460
diff changeset
   754
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   755
    return subset.filter(matches, condrepr=('<contains %r>', pat))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   756
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   757
@predicate('converted([id])', safe=True)
17002
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   758
def converted(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   759
    """Changesets converted from the given identifier in the old repository if
17002
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   760
    present, or all converted changesets if no identifier is specified.
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   761
    """
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   762
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   763
    # There is exactly no chance of resolving the revision, so do a simple
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   764
    # string compare and hope for the best
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   765
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
   766
    rev = None
17002
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   767
    # i18n: "converted" is a keyword
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   768
    l = getargs(x, 0, 1, _('converted takes one or no arguments'))
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   769
    if l:
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
   770
        # i18n: "converted" is a keyword
17002
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   771
        rev = getstring(l[0], _('converted requires a revision'))
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   772
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   773
    def _matchvalue(r):
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   774
        source = repo[r].extra().get('convert_revision', None)
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   775
        return source is not None and (rev is None or source.startswith(rev))
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   776
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   777
    return subset.filter(lambda r: _matchvalue(r),
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   778
                         condrepr=('<converted %r>', rev))
17002
0eb522625eb2 revset: add a predicate for finding converted changesets
Matt Harbison <matt_harbison@yahoo.com>
parents: 16862
diff changeset
   779
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   780
@predicate('date(interval)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   781
def date(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   782
    """Changesets within the interval, see :hg:`help dates`.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   783
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   784
    # i18n: "date" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   785
    ds = getstring(x, _("date requires a string"))
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   786
    dm = util.matchdate(ds)
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   787
    return subset.filter(lambda x: dm(repo[x].date()[0]),
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   788
                         condrepr=('<date %r>', ds))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   789
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   790
@predicate('desc(string)', safe=True)
14650
93731b3efd0d revset: add desc(string) to search in commit messages
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14649
diff changeset
   791
def desc(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   792
    """Search commit message for string. The match is case-insensitive.
14650
93731b3efd0d revset: add desc(string) to search in commit messages
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14649
diff changeset
   793
    """
93731b3efd0d revset: add desc(string) to search in commit messages
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14649
diff changeset
   794
    # i18n: "desc" is a keyword
15726
9b822edecb4c i18n: use "encoding.lower()" to normalize specified string for revset
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15596
diff changeset
   795
    ds = encoding.lower(getstring(x, _("desc requires a string")))
20452
a685d9870eb5 revset: added lazyset implementation to desc revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20451
diff changeset
   796
a685d9870eb5 revset: added lazyset implementation to desc revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20451
diff changeset
   797
    def matches(x):
a685d9870eb5 revset: added lazyset implementation to desc revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20451
diff changeset
   798
        c = repo[x]
a685d9870eb5 revset: added lazyset implementation to desc revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20451
diff changeset
   799
        return ds in encoding.lower(c.description())
a685d9870eb5 revset: added lazyset implementation to desc revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20451
diff changeset
   800
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   801
    return subset.filter(matches, condrepr=('<desc %r>', ds))
14650
93731b3efd0d revset: add desc(string) to search in commit messages
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14649
diff changeset
   802
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   803
def _descendants(repo, subset, x, followfirst=False):
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
   804
    roots = getset(repo, fullreposet(repo), x)
22944
5aae3dea8044 revset: better naming of variables containing the value of a single argument
Mads Kiilerich <madski@unity3d.com>
parents: 22891
diff changeset
   805
    if not roots:
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
   806
        return baseset()
22944
5aae3dea8044 revset: better naming of variables containing the value of a single argument
Mads Kiilerich <madski@unity3d.com>
parents: 22891
diff changeset
   807
    s = _revdescendants(repo, roots, followfirst)
20894
04e1596d5dbd revset: improve _descendants performance
Durham Goode <durham@fb.com>
parents: 20863
diff changeset
   808
04e1596d5dbd revset: improve _descendants performance
Durham Goode <durham@fb.com>
parents: 20863
diff changeset
   809
    # Both sets need to be ascending in order to lazily return the union
04e1596d5dbd revset: improve _descendants performance
Durham Goode <durham@fb.com>
parents: 20863
diff changeset
   810
    # in the correct order.
22944
5aae3dea8044 revset: better naming of variables containing the value of a single argument
Mads Kiilerich <madski@unity3d.com>
parents: 22891
diff changeset
   811
    base = subset & roots
22860
1dd178277cf5 revset-_descendant: rework the whole sorting and combining logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22859
diff changeset
   812
    desc = subset & s
1dd178277cf5 revset-_descendant: rework the whole sorting and combining logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22859
diff changeset
   813
    result = base + desc
1dd178277cf5 revset-_descendant: rework the whole sorting and combining logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22859
diff changeset
   814
    if subset.isascending():
1dd178277cf5 revset-_descendant: rework the whole sorting and combining logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22859
diff changeset
   815
        result.sort()
1dd178277cf5 revset-_descendant: rework the whole sorting and combining logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22859
diff changeset
   816
    elif subset.isdescending():
1dd178277cf5 revset-_descendant: rework the whole sorting and combining logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22859
diff changeset
   817
        result.sort(reverse=True)
1dd178277cf5 revset-_descendant: rework the whole sorting and combining logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22859
diff changeset
   818
    else:
1dd178277cf5 revset-_descendant: rework the whole sorting and combining logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22859
diff changeset
   819
        result = subset & result
22830
1d1da8abe130 _descendants: directly use smartset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22829
diff changeset
   820
    return result
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   821
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   822
@predicate('descendants(set)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   823
def descendants(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   824
    """Changesets which are descendants of changesets in set.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   825
    """
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   826
    return _descendants(repo, subset, x)
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   827
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   828
@predicate('_firstdescendants', safe=True)
16409
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   829
def _firstdescendants(repo, subset, x):
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   830
    # ``_firstdescendants(set)``
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   831
    # Like ``descendants(set)`` but follows only the first parents.
2cbd7dd0cc1f graphlog: fix --follow-first --rev combinations
Patrick Mezard <patrick@mezard.eu>
parents: 16402
diff changeset
   832
    return _descendants(repo, subset, x, followfirst=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
   833
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   834
@predicate('destination([set])', safe=True)
17186
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   835
def destination(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   836
    """Changesets that were created by a graft, transplant or rebase operation,
17186
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   837
    with the given revisions specified as the source.  Omitting the optional set
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   838
    is the same as passing all().
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   839
    """
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   840
    if x is not None:
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
   841
        sources = getset(repo, fullreposet(repo), x)
17186
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   842
    else:
24201
77ef059b3317 revset: drop unnecessary calls of getall() with empty argument
Yuya Nishihara <yuya@tcha.org>
parents: 24163
diff changeset
   843
        sources = fullreposet(repo)
17186
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   844
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   845
    dests = set()
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   846
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   847
    # subset contains all of the possible destinations that can be returned, so
22944
5aae3dea8044 revset: better naming of variables containing the value of a single argument
Mads Kiilerich <madski@unity3d.com>
parents: 22891
diff changeset
   848
    # iterate over them and see if their source(s) were provided in the arg set.
5aae3dea8044 revset: better naming of variables containing the value of a single argument
Mads Kiilerich <madski@unity3d.com>
parents: 22891
diff changeset
   849
    # Even if the immediate src of r is not in the arg set, src's source (or
17186
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   850
    # further back) may be.  Scanning back further than the immediate src allows
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   851
    # transitive transplants and rebases to yield the same results as transitive
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   852
    # grafts.
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   853
    for r in subset:
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   854
        src = _getrevsource(repo, r)
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   855
        lineage = None
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   856
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   857
        while src is not None:
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   858
            if lineage is None:
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   859
                lineage = list()
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   860
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   861
            lineage.append(r)
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   862
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   863
            # The visited lineage is a match if the current source is in the arg
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   864
            # set.  Since every candidate dest is visited by way of iterating
17494
74801685aaa2 spelling: further
timeless@mozdev.org
parents: 17291
diff changeset
   865
            # subset, any dests further back in the lineage will be tested by a
17186
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   866
            # different iteration over subset.  Likewise, if the src was already
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   867
            # selected, the current lineage can be selected without going back
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   868
            # further.
22944
5aae3dea8044 revset: better naming of variables containing the value of a single argument
Mads Kiilerich <madski@unity3d.com>
parents: 22891
diff changeset
   869
            if src in sources or src in dests:
17186
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   870
                dests.update(lineage)
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   871
                break
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   872
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   873
            r = src
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   874
            src = _getrevsource(repo, r)
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   875
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   876
    return subset.filter(dests.__contains__,
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   877
                         condrepr=lambda: '<destination %r>' % sorted(dests))
17186
a3da6f298592 revset: add destination() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17185
diff changeset
   878
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   879
@predicate('divergent()', safe=True)
18071
bea754715961 obsolete: add revset and test for divergent changesets
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18063
diff changeset
   880
def divergent(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   881
    """
18071
bea754715961 obsolete: add revset and test for divergent changesets
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18063
diff changeset
   882
    Final successors of changesets with an alternative set of final successors.
bea754715961 obsolete: add revset and test for divergent changesets
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18063
diff changeset
   883
    """
bea754715961 obsolete: add revset and test for divergent changesets
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18063
diff changeset
   884
    # i18n: "divergent" is a keyword
bea754715961 obsolete: add revset and test for divergent changesets
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18063
diff changeset
   885
    getargs(x, 0, 0, _("divergent takes no arguments"))
bea754715961 obsolete: add revset and test for divergent changesets
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18063
diff changeset
   886
    divergent = obsmod.getrevs(repo, 'divergent')
22533
5b65429721d5 revset: use `subset &` in `divergent`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22532
diff changeset
   887
    return subset & divergent
18071
bea754715961 obsolete: add revset and test for divergent changesets
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18063
diff changeset
   888
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   889
@predicate('extinct()', safe=True)
17173
c621f84dbb35 obsolete: compute extinct changesets
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17171
diff changeset
   890
def extinct(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   891
    """Obsolete changesets with obsolete descendants only.
17291
2d6bbf87f7b4 revset: minor doc fixes on obsolete related revsets
Patrick Mezard <patrick@mezard.eu>
parents: 17272
diff changeset
   892
    """
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
   893
    # i18n: "extinct" is a keyword
17258
5822345e9e46 revset: use appropriate predicate name in error messages
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17244
diff changeset
   894
    getargs(x, 0, 0, _("extinct takes no arguments"))
17825
3cc06457f15e obsolete: rename `getobscache` into `getrevs`
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17804
diff changeset
   895
    extincts = obsmod.getrevs(repo, 'extinct')
20367
2ac278aab2b4 revset: added intersection to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20366
diff changeset
   896
    return subset & extincts
17173
c621f84dbb35 obsolete: compute extinct changesets
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17171
diff changeset
   897
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   898
@predicate('extra(label, [value])', safe=True)
16661
de4b42daf396 revset: add function for matching extra data (issue2767)
Henrik Stuart <hg@hstuart.dk>
parents: 16657
diff changeset
   899
def extra(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   900
    """Changesets with the given label in the extra metadata, with the given
16824
f3b8c82a559c revset: add pattern matching to 'extra' revset expression
Simon King <simon@simonking.org.uk>
parents: 16823
diff changeset
   901
    optional value.
f3b8c82a559c revset: add pattern matching to 'extra' revset expression
Simon King <simon@simonking.org.uk>
parents: 16823
diff changeset
   902
f3b8c82a559c revset: add pattern matching to 'extra' revset expression
Simon King <simon@simonking.org.uk>
parents: 16823
diff changeset
   903
    If `value` starts with `re:`, the remainder of the value is treated as
f3b8c82a559c revset: add pattern matching to 'extra' revset expression
Simon King <simon@simonking.org.uk>
parents: 16823
diff changeset
   904
    a regular expression. To match a value that actually starts with `re:`,
f3b8c82a559c revset: add pattern matching to 'extra' revset expression
Simon King <simon@simonking.org.uk>
parents: 16823
diff changeset
   905
    use the prefix `literal:`.
f3b8c82a559c revset: add pattern matching to 'extra' revset expression
Simon King <simon@simonking.org.uk>
parents: 16823
diff changeset
   906
    """
25767
026105c442d7 revset: rename getkwargs() to getargsdict()
Yuya Nishihara <yuya@tcha.org>
parents: 25766
diff changeset
   907
    args = getargsdict(x, 'extra', 'label value')
25706
b7f53c474e2c revset: port extra() to support keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 25705
diff changeset
   908
    if 'label' not in args:
b7f53c474e2c revset: port extra() to support keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 25705
diff changeset
   909
        # i18n: "extra" is a keyword
b7f53c474e2c revset: port extra() to support keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 25705
diff changeset
   910
        raise error.ParseError(_('extra takes at least 1 argument'))
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
   911
    # i18n: "extra" is a keyword
25706
b7f53c474e2c revset: port extra() to support keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 25705
diff changeset
   912
    label = getstring(args['label'], _('first argument to extra must be '
b7f53c474e2c revset: port extra() to support keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 25705
diff changeset
   913
                                       'a string'))
16661
de4b42daf396 revset: add function for matching extra data (issue2767)
Henrik Stuart <hg@hstuart.dk>
parents: 16657
diff changeset
   914
    value = None
de4b42daf396 revset: add function for matching extra data (issue2767)
Henrik Stuart <hg@hstuart.dk>
parents: 16657
diff changeset
   915
25706
b7f53c474e2c revset: port extra() to support keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 25705
diff changeset
   916
    if 'value' in args:
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
   917
        # i18n: "extra" is a keyword
25706
b7f53c474e2c revset: port extra() to support keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 25705
diff changeset
   918
        value = getstring(args['value'], _('second argument to extra must be '
b7f53c474e2c revset: port extra() to support keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 25705
diff changeset
   919
                                           'a string'))
26481
7d132557e44a util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents: 26307
diff changeset
   920
        kind, value, matcher = util.stringmatcher(value)
16661
de4b42daf396 revset: add function for matching extra data (issue2767)
Henrik Stuart <hg@hstuart.dk>
parents: 16657
diff changeset
   921
de4b42daf396 revset: add function for matching extra data (issue2767)
Henrik Stuart <hg@hstuart.dk>
parents: 16657
diff changeset
   922
    def _matchvalue(r):
de4b42daf396 revset: add function for matching extra data (issue2767)
Henrik Stuart <hg@hstuart.dk>
parents: 16657
diff changeset
   923
        extra = repo[r].extra()
16824
f3b8c82a559c revset: add pattern matching to 'extra' revset expression
Simon King <simon@simonking.org.uk>
parents: 16823
diff changeset
   924
        return label in extra and (value is None or matcher(extra[label]))
16661
de4b42daf396 revset: add function for matching extra data (issue2767)
Henrik Stuart <hg@hstuart.dk>
parents: 16657
diff changeset
   925
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   926
    return subset.filter(lambda r: _matchvalue(r),
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
   927
                         condrepr=('<extra[%r] %r>', label, value))
15819
33ca11b010e2 phases: implements simple revset symbol
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 15791
diff changeset
   928
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   929
@predicate('filelog(pattern)', safe=True)
14342
c0b6a734b4f3 revset: introduce filelog() to emulate log's fast path
Matt Mackall <mpm@selenic.com>
parents: 14318
diff changeset
   930
def filelog(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   931
    """Changesets connected to the specified filelog.
17244
483aa765f6c4 revset: add explanation about difference between 'filelog()' and 'file()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17186
diff changeset
   932
21199
e9c2f76be74b help: clarify distinction among `contains`/`file`/`filelog`
Greg Hurrell <glh@fb.com>
parents: 21173
diff changeset
   933
    For performance reasons, visits only revisions mentioned in the file-level
e9c2f76be74b help: clarify distinction among `contains`/`file`/`filelog`
Greg Hurrell <glh@fb.com>
parents: 21173
diff changeset
   934
    filelog, rather than filtering through all changesets (much faster, but
e9c2f76be74b help: clarify distinction among `contains`/`file`/`filelog`
Greg Hurrell <glh@fb.com>
parents: 21173
diff changeset
   935
    doesn't include deletes or duplicate changes). For a slower, more accurate
e9c2f76be74b help: clarify distinction among `contains`/`file`/`filelog`
Greg Hurrell <glh@fb.com>
parents: 21173
diff changeset
   936
    result, use ``file()``.
20289
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
   937
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
   938
    The pattern without explicit kind like ``glob:`` is expected to be
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
   939
    relative to the current directory and match against a file exactly
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
   940
    for efficiency.
23719
34364a4b25eb linkrev: work around linkrev to filtered entry in 'filelog' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23704
diff changeset
   941
34364a4b25eb linkrev: work around linkrev to filtered entry in 'filelog' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23704
diff changeset
   942
    If some linkrev points to revisions filtered by the current repoview, we'll
34364a4b25eb linkrev: work around linkrev to filtered entry in 'filelog' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23704
diff changeset
   943
    work around it to return a non-filtered value.
14342
c0b6a734b4f3 revset: introduce filelog() to emulate log's fast path
Matt Mackall <mpm@selenic.com>
parents: 14318
diff changeset
   944
    """
c0b6a734b4f3 revset: introduce filelog() to emulate log's fast path
Matt Mackall <mpm@selenic.com>
parents: 14318
diff changeset
   945
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
   946
    # i18n: "filelog" is a keyword
14342
c0b6a734b4f3 revset: introduce filelog() to emulate log's fast path
Matt Mackall <mpm@selenic.com>
parents: 14318
diff changeset
   947
    pat = getstring(x, _("filelog requires a pattern"))
c0b6a734b4f3 revset: introduce filelog() to emulate log's fast path
Matt Mackall <mpm@selenic.com>
parents: 14318
diff changeset
   948
    s = set()
23719
34364a4b25eb linkrev: work around linkrev to filtered entry in 'filelog' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23704
diff changeset
   949
    cl = repo.changelog
14342
c0b6a734b4f3 revset: introduce filelog() to emulate log's fast path
Matt Mackall <mpm@selenic.com>
parents: 14318
diff changeset
   950
15964
6e37b8282aa2 revsets: provide contexts for filesets
Matt Mackall <mpm@selenic.com>
parents: 15949
diff changeset
   951
    if not matchmod.patkind(pat):
20288
b61ad01c4e73 revset: use "canonpath()" for "filelog()" pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20287
diff changeset
   952
        f = pathutil.canonpath(repo.root, repo.getcwd(), pat)
23719
34364a4b25eb linkrev: work around linkrev to filtered entry in 'filelog' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23704
diff changeset
   953
        files = [f]
14342
c0b6a734b4f3 revset: introduce filelog() to emulate log's fast path
Matt Mackall <mpm@selenic.com>
parents: 14318
diff changeset
   954
    else:
20288
b61ad01c4e73 revset: use "canonpath()" for "filelog()" pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20287
diff changeset
   955
        m = matchmod.match(repo.root, repo.getcwd(), [pat], ctx=repo[None])
23719
34364a4b25eb linkrev: work around linkrev to filtered entry in 'filelog' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23704
diff changeset
   956
        files = (f for f in repo[None] if m(f))
34364a4b25eb linkrev: work around linkrev to filtered entry in 'filelog' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23704
diff changeset
   957
34364a4b25eb linkrev: work around linkrev to filtered entry in 'filelog' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23704
diff changeset
   958
    for f in files:
34364a4b25eb linkrev: work around linkrev to filtered entry in 'filelog' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23704
diff changeset
   959
        fl = repo.file(f)
27945
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   960
        known = {}
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   961
        scanpos = 0
23719
34364a4b25eb linkrev: work around linkrev to filtered entry in 'filelog' revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23704
diff changeset
   962
        for fr in list(fl):
27945
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   963
            fn = fl.node(fr)
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   964
            if fn in known:
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   965
                s.add(known[fn])
23821
7a7f437ab63d filelog: remove trailing "form feed" character
Martin von Zweigbergk <martinvonz@google.com>
parents: 23820
diff changeset
   966
                continue
27945
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   967
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   968
            lr = fl.linkrev(fr)
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   969
            if lr in cl:
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   970
                s.add(lr)
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   971
            elif scanpos is not None:
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   972
                # lowest matching changeset is filtered, scan further
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   973
                # ahead in changelog
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   974
                start = max(lr, scanpos) + 1
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   975
                scanpos = None
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   976
                for r in cl.revs(start):
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   977
                    # minimize parsing of non-matching entries
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   978
                    if f in cl.revision(r) and f in cl.readfiles(r):
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   979
                        try:
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   980
                            # try to use manifest delta fastpath
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   981
                            n = repo[r].filenode(f)
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   982
                            if n not in known:
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   983
                                if n == fn:
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   984
                                    s.add(r)
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   985
                                    scanpos = r
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   986
                                    break
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   987
                                else:
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   988
                                    known[n] = r
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   989
                        except error.ManifestLookupError:
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   990
                            # deletion in changelog
4186d359046a log: speed up single file log with hidden revs (issue4747)
Matt Mackall <mpm@selenic.com>
parents: 27637
diff changeset
   991
                            continue
14342
c0b6a734b4f3 revset: introduce filelog() to emulate log's fast path
Matt Mackall <mpm@selenic.com>
parents: 14318
diff changeset
   992
22534
6261b9c549a2 revset: use `subset &` in `filelog`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22533
diff changeset
   993
    return subset & s
14342
c0b6a734b4f3 revset: introduce filelog() to emulate log's fast path
Matt Mackall <mpm@selenic.com>
parents: 14318
diff changeset
   994
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
   995
@predicate('first(set, [n])', safe=True)
15117
0ab1c3a1f3b2 revsets: add first alias for last
Matt Mackall <mpm@selenic.com>
parents: 15116
diff changeset
   996
def first(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
   997
    """An alias for limit().
15117
0ab1c3a1f3b2 revsets: add first alias for last
Matt Mackall <mpm@selenic.com>
parents: 15116
diff changeset
   998
    """
0ab1c3a1f3b2 revsets: add first alias for last
Matt Mackall <mpm@selenic.com>
parents: 15116
diff changeset
   999
    return limit(repo, subset, x)
0ab1c3a1f3b2 revsets: add first alias for last
Matt Mackall <mpm@selenic.com>
parents: 15116
diff changeset
  1000
16185
352053e6cd8e context: add followfirst arg to filectx and workingfilectx
Patrick Mezard <patrick@mezard.eu>
parents: 16181
diff changeset
  1001
def _follow(repo, subset, x, name, followfirst=False):
29814
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1002
    l = getargs(x, 0, 2, _("%s takes no arguments or a pattern "
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1003
                           "and an optional revset") % name)
16185
352053e6cd8e context: add followfirst arg to filectx and workingfilectx
Patrick Mezard <patrick@mezard.eu>
parents: 16181
diff changeset
  1004
    c = repo['.']
352053e6cd8e context: add followfirst arg to filectx and workingfilectx
Patrick Mezard <patrick@mezard.eu>
parents: 16181
diff changeset
  1005
    if l:
26102
5618858dce26 revsets: makes follow() supports file patterns (issue4757) (BC)
liscju <piotr.listkiewicz@gmail.com>
parents: 26099
diff changeset
  1006
        x = getstring(l[0], _("%s expected a pattern") % name)
29814
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1007
        rev = None
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1008
        if len(l) >= 2:
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1009
            rev = getset(repo, fullreposet(repo), l[1]).last()
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1010
            if rev is None:
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1011
                raise error.RepoLookupError(
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1012
                        _("%s: starting revision set cannot be empty") % name)
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1013
            c = repo[rev]
26102
5618858dce26 revsets: makes follow() supports file patterns (issue4757) (BC)
liscju <piotr.listkiewicz@gmail.com>
parents: 26099
diff changeset
  1014
        matcher = matchmod.match(repo.root, repo.getcwd(), [x],
29814
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1015
                                 ctx=repo[rev], default='path')
26102
5618858dce26 revsets: makes follow() supports file patterns (issue4757) (BC)
liscju <piotr.listkiewicz@gmail.com>
parents: 26099
diff changeset
  1016
28008
86c4cbdaffee revset: use manifest.matches in _follow revset
Durham Goode <durham@fb.com>
parents: 27945
diff changeset
  1017
        files = c.manifest().walk(matcher)
86c4cbdaffee revset: use manifest.matches in _follow revset
Durham Goode <durham@fb.com>
parents: 27945
diff changeset
  1018
26102
5618858dce26 revsets: makes follow() supports file patterns (issue4757) (BC)
liscju <piotr.listkiewicz@gmail.com>
parents: 26099
diff changeset
  1019
        s = set()
28008
86c4cbdaffee revset: use manifest.matches in _follow revset
Durham Goode <durham@fb.com>
parents: 27945
diff changeset
  1020
        for fname in files:
86c4cbdaffee revset: use manifest.matches in _follow revset
Durham Goode <durham@fb.com>
parents: 27945
diff changeset
  1021
            fctx = c[fname]
86c4cbdaffee revset: use manifest.matches in _follow revset
Durham Goode <durham@fb.com>
parents: 27945
diff changeset
  1022
            s = s.union(set(c.rev() for c in fctx.ancestors(followfirst)))
86c4cbdaffee revset: use manifest.matches in _follow revset
Durham Goode <durham@fb.com>
parents: 27945
diff changeset
  1023
            # include the revision responsible for the most recent version
86c4cbdaffee revset: use manifest.matches in _follow revset
Durham Goode <durham@fb.com>
parents: 27945
diff changeset
  1024
            s.add(fctx.introrev())
16185
352053e6cd8e context: add followfirst arg to filectx and workingfilectx
Patrick Mezard <patrick@mezard.eu>
parents: 16181
diff changeset
  1025
    else:
20690
13c0327eeb6f revset: changed ancestors revset to return lazy generators
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20659
diff changeset
  1026
        s = _revancestors(repo, baseset([c.rev()]), followfirst)
16185
352053e6cd8e context: add followfirst arg to filectx and workingfilectx
Patrick Mezard <patrick@mezard.eu>
parents: 16181
diff changeset
  1027
22535
44f471102f3a revset: use `subset &` in `follow`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22534
diff changeset
  1028
    return subset & s
16185
352053e6cd8e context: add followfirst arg to filectx and workingfilectx
Patrick Mezard <patrick@mezard.eu>
parents: 16181
diff changeset
  1029
29814
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1030
@predicate('follow([pattern[, startrev]])', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1031
def follow(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1032
    """
24366
e8ea31131705 revset: replace "working copy" with "working directory" in function help
Yuya Nishihara <yuya@tcha.org>
parents: 24306
diff changeset
  1033
    An alias for ``::.`` (ancestors of the working directory's first parent).
26102
5618858dce26 revsets: makes follow() supports file patterns (issue4757) (BC)
liscju <piotr.listkiewicz@gmail.com>
parents: 26099
diff changeset
  1034
    If pattern is specified, the histories of files matching given
29814
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1035
    pattern in the revision given by startrev are followed, including copies.
14343
9ed227f79e47 revset: add follow(filename) to follow a filename's history across copies
Matt Mackall <mpm@selenic.com>
parents: 14342
diff changeset
  1036
    """
16185
352053e6cd8e context: add followfirst arg to filectx and workingfilectx
Patrick Mezard <patrick@mezard.eu>
parents: 16181
diff changeset
  1037
    return _follow(repo, subset, x, 'follow')
14343
9ed227f79e47 revset: add follow(filename) to follow a filename's history across copies
Matt Mackall <mpm@selenic.com>
parents: 14342
diff changeset
  1038
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1039
@predicate('_followfirst', safe=True)
16174
0a73c4bd9f47 graphlog: implement --follow-first
Patrick Mezard <patrick@mezard.eu>
parents: 16161
diff changeset
  1040
def _followfirst(repo, subset, x):
29814
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1041
    # ``followfirst([pattern[, startrev]])``
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1042
    # Like ``follow([pattern[, startrev]])`` but follows only the first parent
cbf9984a7957 revset: support "follow(renamed.py, e22f4f3f06c3)" (issue5334)
Gábor Stefanik <gabor.stefanik@nng.com>
parents: 29780
diff changeset
  1043
    # of every revisions or files revisions.
16185
352053e6cd8e context: add followfirst arg to filectx and workingfilectx
Patrick Mezard <patrick@mezard.eu>
parents: 16181
diff changeset
  1044
    return _follow(repo, subset, x, '_followfirst', followfirst=True)
14343
9ed227f79e47 revset: add follow(filename) to follow a filename's history across copies
Matt Mackall <mpm@selenic.com>
parents: 14342
diff changeset
  1045
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1046
@predicate('all()', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1047
def getall(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1048
    """All changesets, the same as ``0:tip``.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1049
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1050
    # i18n: "all" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1051
    getargs(x, 0, 0, _("all takes no arguments"))
24202
2de9ee016425 revset: have all() filter out null revision
Yuya Nishihara <yuya@tcha.org>
parents: 24201
diff changeset
  1052
    return subset & spanset(repo)  # drop "null" if any
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1053
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1054
@predicate('grep(regex)')
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1055
def grep(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1056
    """Like ``keyword(string)`` but accepts a regex. Use ``grep(r'...')``
14357
cb4ff8ef466b merge with stable
Martin Geisler <mg@aragost.com>
parents: 14343 14356
diff changeset
  1057
    to ensure special escape characters are handled correctly. Unlike
cb4ff8ef466b merge with stable
Martin Geisler <mg@aragost.com>
parents: 14343 14356
diff changeset
  1058
    ``keyword(string)``, the match is case-sensitive.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1059
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1060
    try:
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1061
        # i18n: "grep" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1062
        gr = re.compile(getstring(x, _("grep requires a string")))
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25654
diff changeset
  1063
    except re.error as e:
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1064
        raise error.ParseError(_('invalid match pattern: %s') % e)
20453
6aa7dcae6bd8 revset: added lazyset implementation to grep revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20452
diff changeset
  1065
6aa7dcae6bd8 revset: added lazyset implementation to grep revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20452
diff changeset
  1066
    def matches(x):
6aa7dcae6bd8 revset: added lazyset implementation to grep revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20452
diff changeset
  1067
        c = repo[x]
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1068
        for e in c.files() + [c.user(), c.description()]:
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1069
            if gr.search(e):
20453
6aa7dcae6bd8 revset: added lazyset implementation to grep revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20452
diff changeset
  1070
                return True
6aa7dcae6bd8 revset: added lazyset implementation to grep revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20452
diff changeset
  1071
        return False
6aa7dcae6bd8 revset: added lazyset implementation to grep revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20452
diff changeset
  1072
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1073
    return subset.filter(matches, condrepr=('<grep %r>', gr.pattern))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1074
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1075
@predicate('_matchfiles', safe=True)
16161
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1076
def _matchfiles(repo, subset, x):
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1077
    # _matchfiles takes a revset list of prefixed arguments:
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1078
    #
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1079
    #   [p:foo, i:bar, x:baz]
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1080
    #
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1081
    # builds a match object from them and filters subset. Allowed
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1082
    # prefixes are 'p:' for regular patterns, 'i:' for include
16181
1fd352aa08fc graphlog: evaluate FILE/-I/-X filesets on the working dir
Patrick Mezard <patrick@mezard.eu>
parents: 16174
diff changeset
  1083
    # patterns and 'x:' for exclude patterns. Use 'r:' prefix to pass
1fd352aa08fc graphlog: evaluate FILE/-I/-X filesets on the working dir
Patrick Mezard <patrick@mezard.eu>
parents: 16174
diff changeset
  1084
    # a revision identifier, or the empty string to reference the
1fd352aa08fc graphlog: evaluate FILE/-I/-X filesets on the working dir
Patrick Mezard <patrick@mezard.eu>
parents: 16174
diff changeset
  1085
    # working directory, from which the match object is
16411
4c2edcd84175 graphlog: correctly handle calls in subdirectories
Patrick Mezard <patrick@mezard.eu>
parents: 16409
diff changeset
  1086
    # initialized. Use 'd:' to set the default matching mode, default
4c2edcd84175 graphlog: correctly handle calls in subdirectories
Patrick Mezard <patrick@mezard.eu>
parents: 16409
diff changeset
  1087
    # to 'glob'. At most one 'r:' and 'd:' argument can be passed.
16161
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1088
28271
5c454ab69558 revset: drop translation markers from error messages of internal _matchfiles
Yuya Nishihara <yuya@tcha.org>
parents: 28217
diff changeset
  1089
    l = getargs(x, 1, -1, "_matchfiles requires at least one argument")
16161
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1090
    pats, inc, exc = [], [], []
16411
4c2edcd84175 graphlog: correctly handle calls in subdirectories
Patrick Mezard <patrick@mezard.eu>
parents: 16409
diff changeset
  1091
    rev, default = None, None
16161
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1092
    for arg in l:
28271
5c454ab69558 revset: drop translation markers from error messages of internal _matchfiles
Yuya Nishihara <yuya@tcha.org>
parents: 28217
diff changeset
  1093
        s = getstring(arg, "_matchfiles requires string arguments")
16161
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1094
        prefix, value = s[:2], s[2:]
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1095
        if prefix == 'p:':
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1096
            pats.append(value)
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1097
        elif prefix == 'i:':
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1098
            inc.append(value)
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1099
        elif prefix == 'x:':
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1100
            exc.append(value)
16181
1fd352aa08fc graphlog: evaluate FILE/-I/-X filesets on the working dir
Patrick Mezard <patrick@mezard.eu>
parents: 16174
diff changeset
  1101
        elif prefix == 'r:':
1fd352aa08fc graphlog: evaluate FILE/-I/-X filesets on the working dir
Patrick Mezard <patrick@mezard.eu>
parents: 16174
diff changeset
  1102
            if rev is not None:
28271
5c454ab69558 revset: drop translation markers from error messages of internal _matchfiles
Yuya Nishihara <yuya@tcha.org>
parents: 28217
diff changeset
  1103
                raise error.ParseError('_matchfiles expected at most one '
5c454ab69558 revset: drop translation markers from error messages of internal _matchfiles
Yuya Nishihara <yuya@tcha.org>
parents: 28217
diff changeset
  1104
                                       'revision')
23950
caff3675cba5 log: evaluate filesets on working copy, not its parent
Martin von Zweigbergk <martinvonz@google.com>
parents: 23847
diff changeset
  1105
            if value != '': # empty means working directory; leave rev as None
caff3675cba5 log: evaluate filesets on working copy, not its parent
Martin von Zweigbergk <martinvonz@google.com>
parents: 23847
diff changeset
  1106
                rev = value
16411
4c2edcd84175 graphlog: correctly handle calls in subdirectories
Patrick Mezard <patrick@mezard.eu>
parents: 16409
diff changeset
  1107
        elif prefix == 'd:':
4c2edcd84175 graphlog: correctly handle calls in subdirectories
Patrick Mezard <patrick@mezard.eu>
parents: 16409
diff changeset
  1108
            if default is not None:
28271
5c454ab69558 revset: drop translation markers from error messages of internal _matchfiles
Yuya Nishihara <yuya@tcha.org>
parents: 28217
diff changeset
  1109
                raise error.ParseError('_matchfiles expected at most one '
5c454ab69558 revset: drop translation markers from error messages of internal _matchfiles
Yuya Nishihara <yuya@tcha.org>
parents: 28217
diff changeset
  1110
                                       'default mode')
16411
4c2edcd84175 graphlog: correctly handle calls in subdirectories
Patrick Mezard <patrick@mezard.eu>
parents: 16409
diff changeset
  1111
            default = value
16161
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1112
        else:
28271
5c454ab69558 revset: drop translation markers from error messages of internal _matchfiles
Yuya Nishihara <yuya@tcha.org>
parents: 28217
diff changeset
  1113
            raise error.ParseError('invalid _matchfiles prefix: %s' % prefix)
16411
4c2edcd84175 graphlog: correctly handle calls in subdirectories
Patrick Mezard <patrick@mezard.eu>
parents: 16409
diff changeset
  1114
    if not default:
4c2edcd84175 graphlog: correctly handle calls in subdirectories
Patrick Mezard <patrick@mezard.eu>
parents: 16409
diff changeset
  1115
        default = 'glob'
20458
8dabcc889e33 revset: added lazyset implementation to _matchfiles
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20457
diff changeset
  1116
23061
f2aeff8a87b6 revset: avoid recalculating filesets
Matt Mackall <mpm@selenic.com>
parents: 23019
diff changeset
  1117
    m = matchmod.match(repo.root, repo.getcwd(), pats, include=inc,
f2aeff8a87b6 revset: avoid recalculating filesets
Matt Mackall <mpm@selenic.com>
parents: 23019
diff changeset
  1118
                       exclude=exc, ctx=repo[rev], default=default)
f2aeff8a87b6 revset: avoid recalculating filesets
Matt Mackall <mpm@selenic.com>
parents: 23019
diff changeset
  1119
27028
f92053df8f0b revset: speed up '_matchfiles'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26778
diff changeset
  1120
    # This directly read the changelog data as creating changectx for all
f92053df8f0b revset: speed up '_matchfiles'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26778
diff changeset
  1121
    # revisions is quite expensive.
27440
ff305ab2e0d7 log: speed up hg log <file|folder>
Laurent Charignon <lcharignon@fb.com>
parents: 27293
diff changeset
  1122
    getfiles = repo.changelog.readfiles
27028
f92053df8f0b revset: speed up '_matchfiles'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26778
diff changeset
  1123
    wdirrev = node.wdirrev
20458
8dabcc889e33 revset: added lazyset implementation to _matchfiles
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20457
diff changeset
  1124
    def matches(x):
27028
f92053df8f0b revset: speed up '_matchfiles'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26778
diff changeset
  1125
        if x == wdirrev:
f92053df8f0b revset: speed up '_matchfiles'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26778
diff changeset
  1126
            files = repo[x].files()
f92053df8f0b revset: speed up '_matchfiles'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26778
diff changeset
  1127
        else:
27440
ff305ab2e0d7 log: speed up hg log <file|folder>
Laurent Charignon <lcharignon@fb.com>
parents: 27293
diff changeset
  1128
            files = getfiles(x)
27028
f92053df8f0b revset: speed up '_matchfiles'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26778
diff changeset
  1129
        for f in files:
16161
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1130
            if m(f):
20458
8dabcc889e33 revset: added lazyset implementation to _matchfiles
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20457
diff changeset
  1131
                return True
8dabcc889e33 revset: added lazyset implementation to _matchfiles
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20457
diff changeset
  1132
        return False
8dabcc889e33 revset: added lazyset implementation to _matchfiles
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20457
diff changeset
  1133
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1134
    return subset.filter(matches,
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1135
                         condrepr=('<matchfiles patterns=%r, include=%r '
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1136
                                   'exclude=%r, default=%r, rev=%r>',
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1137
                                   pats, inc, exc, default, rev))
16161
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1138
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1139
@predicate('file(pattern)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1140
def hasfile(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1141
    """Changesets affecting files matched by pattern.
17244
483aa765f6c4 revset: add explanation about difference between 'filelog()' and 'file()'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17186
diff changeset
  1142
17265
c30307eeec4b revset: polish explanation of the difference between file() and filelog()
Greg Ward <greg@gerg.ca>
parents: 17259
diff changeset
  1143
    For a faster but less accurate result, consider using ``filelog()``
c30307eeec4b revset: polish explanation of the difference between file() and filelog()
Greg Ward <greg@gerg.ca>
parents: 17259
diff changeset
  1144
    instead.
20289
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
  1145
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
  1146
    This predicate uses ``glob:`` as the default kind of pattern.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1147
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1148
    # i18n: "file" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1149
    pat = getstring(x, _("file requires a pattern"))
16161
5a627b49b4d9 graphlog: paths/-I/-X handling requires a new revset
Patrick Mezard <patrick@mezard.eu>
parents: 16096
diff changeset
  1150
    return _matchfiles(repo, subset, ('string', 'p:' + pat))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1151
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1152
@predicate('head()', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1153
def head(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1154
    """Changeset is a named branch head.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1155
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1156
    # i18n: "head" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1157
    getargs(x, 0, 0, _("head takes no arguments"))
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1158
    hs = set()
25620
5f87f2305ad0 revset: translate node directly with changelog in 'head'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25619
diff changeset
  1159
    cl = repo.changelog
29407
20fabe814f89 revsets: use itervalues() where only values are needed
Martin von Zweigbergk <martinvonz@google.com>
parents: 29406
diff changeset
  1160
    for ls in repo.branchmap().itervalues():
25620
5f87f2305ad0 revset: translate node directly with changelog in 'head'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25619
diff changeset
  1161
        hs.update(cl.rev(h) for h in ls)
29408
785cadec2091 revset: make head() honor order of subset
Martin von Zweigbergk <martinvonz@google.com>
parents: 29407
diff changeset
  1162
    return subset & baseset(hs)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1163
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1164
@predicate('heads(set)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1165
def heads(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1166
    """Members of set with no children in set.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1167
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1168
    s = getset(repo, subset, x)
20366
5ec6321f49a9 revset: added substraction to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20365
diff changeset
  1169
    ps = parents(repo, subset, x)
5ec6321f49a9 revset: added substraction to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20365
diff changeset
  1170
    return s - ps
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1171
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1172
@predicate('hidden()', safe=True)
17390
74b44f25b4b1 revset: add hidden() revset
Patrick Mezard <patrick@mezard.eu>
parents: 17291
diff changeset
  1173
def hidden(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1174
    """Hidden changesets.
17390
74b44f25b4b1 revset: add hidden() revset
Patrick Mezard <patrick@mezard.eu>
parents: 17291
diff changeset
  1175
    """
74b44f25b4b1 revset: add hidden() revset
Patrick Mezard <patrick@mezard.eu>
parents: 17291
diff changeset
  1176
    # i18n: "hidden" is a keyword
74b44f25b4b1 revset: add hidden() revset
Patrick Mezard <patrick@mezard.eu>
parents: 17291
diff changeset
  1177
    getargs(x, 0, 0, _("hidden takes no arguments"))
18382
f3b21beb9802 filtering: rename filters to their antonyms
Kevin Bullock <kbullock@ringworld.org>
parents: 18251
diff changeset
  1178
    hiddenrevs = repoview.filterrevs(repo, 'visible')
20367
2ac278aab2b4 revset: added intersection to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20366
diff changeset
  1179
    return subset & hiddenrevs
17390
74b44f25b4b1 revset: add hidden() revset
Patrick Mezard <patrick@mezard.eu>
parents: 17291
diff changeset
  1180
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1181
@predicate('keyword(string)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1182
def keyword(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1183
    """Search commit message, user name, and names of changed files for
14357
cb4ff8ef466b merge with stable
Martin Geisler <mg@aragost.com>
parents: 14343 14356
diff changeset
  1184
    string. The match is case-insensitive.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1185
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1186
    # i18n: "keyword" is a keyword
15726
9b822edecb4c i18n: use "encoding.lower()" to normalize specified string for revset
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15596
diff changeset
  1187
    kw = encoding.lower(getstring(x, _("keyword requires a string")))
20447
abb91b74f758 revset: added lazyset implementation to keyword revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20446
diff changeset
  1188
abb91b74f758 revset: added lazyset implementation to keyword revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20446
diff changeset
  1189
    def matches(r):
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1190
        c = repo[r]
25551
c1d163ce7394 revset: gratuitous formating fix in keyword
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25550
diff changeset
  1191
        return any(kw in encoding.lower(t)
c1d163ce7394 revset: gratuitous formating fix in keyword
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25550
diff changeset
  1192
                   for t in c.files() + [c.user(), c.description()])
20447
abb91b74f758 revset: added lazyset implementation to keyword revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20446
diff changeset
  1193
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1194
    return subset.filter(matches, condrepr=('<keyword %r>', kw))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1195
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1196
@predicate('limit(set[, n[, offset]])', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1197
def limit(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1198
    """First n members of set, defaulting to 1, starting from offset.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1199
    """
26638
7afaf2566e25 revset: add optional offset argument to limit() predicate
Yuya Nishihara <yuya@tcha.org>
parents: 26637
diff changeset
  1200
    args = getargsdict(x, 'limit', 'set n offset')
26637
179764469754 revset: port limit() to support keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 26636
diff changeset
  1201
    if 'set' not in args:
179764469754 revset: port limit() to support keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 26636
diff changeset
  1202
        # i18n: "limit" is a keyword
26638
7afaf2566e25 revset: add optional offset argument to limit() predicate
Yuya Nishihara <yuya@tcha.org>
parents: 26637
diff changeset
  1203
        raise error.ParseError(_("limit requires one to three arguments"))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1204
    try:
26638
7afaf2566e25 revset: add optional offset argument to limit() predicate
Yuya Nishihara <yuya@tcha.org>
parents: 26637
diff changeset
  1205
        lim, ofs = 1, 0
26637
179764469754 revset: port limit() to support keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 26636
diff changeset
  1206
        if 'n' in args:
15116
d8501bcbb221 revset: add default of 1 to limit and last functions
Matt Mackall <mpm@selenic.com>
parents: 14901
diff changeset
  1207
            # i18n: "limit" is a keyword
26637
179764469754 revset: port limit() to support keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 26636
diff changeset
  1208
            lim = int(getstring(args['n'], _("limit requires a number")))
26638
7afaf2566e25 revset: add optional offset argument to limit() predicate
Yuya Nishihara <yuya@tcha.org>
parents: 26637
diff changeset
  1209
        if 'offset' in args:
7afaf2566e25 revset: add optional offset argument to limit() predicate
Yuya Nishihara <yuya@tcha.org>
parents: 26637
diff changeset
  1210
            # i18n: "limit" is a keyword
7afaf2566e25 revset: add optional offset argument to limit() predicate
Yuya Nishihara <yuya@tcha.org>
parents: 26637
diff changeset
  1211
            ofs = int(getstring(args['offset'], _("limit requires a number")))
7afaf2566e25 revset: add optional offset argument to limit() predicate
Yuya Nishihara <yuya@tcha.org>
parents: 26637
diff changeset
  1212
        if ofs < 0:
7afaf2566e25 revset: add optional offset argument to limit() predicate
Yuya Nishihara <yuya@tcha.org>
parents: 26637
diff changeset
  1213
            raise error.ParseError(_("negative offset"))
14851
f96c354493d7 revsets: actually catch type error on tip^p1(tip) (issue2884)
Matt Mackall <mpm@selenic.com>
parents: 14842
diff changeset
  1214
    except (TypeError, ValueError):
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1215
        # i18n: "limit" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1216
        raise error.ParseError(_("limit expects a number"))
26637
179764469754 revset: port limit() to support keyword arguments
Yuya Nishihara <yuya@tcha.org>
parents: 26636
diff changeset
  1217
    os = getset(repo, fullreposet(repo), args['set'])
22804
5385314e0b14 revset-limit: remove user of baseset.append
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22802
diff changeset
  1218
    result = []
20446
d258486604f4 revset: changed limit revset implementation to work with lazy revsets
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20445
diff changeset
  1219
    it = iter(os)
26638
7afaf2566e25 revset: add optional offset argument to limit() predicate
Yuya Nishihara <yuya@tcha.org>
parents: 26637
diff changeset
  1220
    for x in xrange(ofs):
7afaf2566e25 revset: add optional offset argument to limit() predicate
Yuya Nishihara <yuya@tcha.org>
parents: 26637
diff changeset
  1221
        y = next(it, None)
7afaf2566e25 revset: add optional offset argument to limit() predicate
Yuya Nishihara <yuya@tcha.org>
parents: 26637
diff changeset
  1222
        if y is None:
7afaf2566e25 revset: add optional offset argument to limit() predicate
Yuya Nishihara <yuya@tcha.org>
parents: 26637
diff changeset
  1223
            break
20446
d258486604f4 revset: changed limit revset implementation to work with lazy revsets
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20445
diff changeset
  1224
    for x in xrange(lim):
25144
81a395447b34 revset: use 'next()' to detect end of iteration in 'limit'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25143
diff changeset
  1225
        y = next(it, None)
81a395447b34 revset: use 'next()' to detect end of iteration in 'limit'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25143
diff changeset
  1226
        if y is None:
20446
d258486604f4 revset: changed limit revset implementation to work with lazy revsets
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20445
diff changeset
  1227
            break
26636
ff6baf32b3ba revset: eliminate temporary reference to subset in limit() and last()
Yuya Nishihara <yuya@tcha.org>
parents: 26587
diff changeset
  1228
        elif y in subset:
25144
81a395447b34 revset: use 'next()' to detect end of iteration in 'limit'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25143
diff changeset
  1229
            result.append(y)
28426
3d39ac06af9a revset: add inspection data to limit() and last() functions
Yuya Nishihara <yuya@tcha.org>
parents: 28425
diff changeset
  1230
    return baseset(result, datarepr=('<limit n=%d, offset=%d, %r, %r>',
3d39ac06af9a revset: add inspection data to limit() and last() functions
Yuya Nishihara <yuya@tcha.org>
parents: 28425
diff changeset
  1231
                                     lim, ofs, subset, os))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1232
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1233
@predicate('last(set, [n])', safe=True)
14061
611d2f8a4ba2 revsets: add a last function
Matt Mackall <mpm@selenic.com>
parents: 14057
diff changeset
  1234
def last(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1235
    """Last n members of set, defaulting to 1.
14061
611d2f8a4ba2 revsets: add a last function
Matt Mackall <mpm@selenic.com>
parents: 14057
diff changeset
  1236
    """
611d2f8a4ba2 revsets: add a last function
Matt Mackall <mpm@selenic.com>
parents: 14057
diff changeset
  1237
    # i18n: "last" is a keyword
15116
d8501bcbb221 revset: add default of 1 to limit and last functions
Matt Mackall <mpm@selenic.com>
parents: 14901
diff changeset
  1238
    l = getargs(x, 1, 2, _("last requires one or two arguments"))
14061
611d2f8a4ba2 revsets: add a last function
Matt Mackall <mpm@selenic.com>
parents: 14057
diff changeset
  1239
    try:
15116
d8501bcbb221 revset: add default of 1 to limit and last functions
Matt Mackall <mpm@selenic.com>
parents: 14901
diff changeset
  1240
        lim = 1
d8501bcbb221 revset: add default of 1 to limit and last functions
Matt Mackall <mpm@selenic.com>
parents: 14901
diff changeset
  1241
        if len(l) == 2:
d8501bcbb221 revset: add default of 1 to limit and last functions
Matt Mackall <mpm@selenic.com>
parents: 14901
diff changeset
  1242
            # i18n: "last" is a keyword
d8501bcbb221 revset: add default of 1 to limit and last functions
Matt Mackall <mpm@selenic.com>
parents: 14901
diff changeset
  1243
            lim = int(getstring(l[1], _("last requires a number")))
14851
f96c354493d7 revsets: actually catch type error on tip^p1(tip) (issue2884)
Matt Mackall <mpm@selenic.com>
parents: 14842
diff changeset
  1244
    except (TypeError, ValueError):
14061
611d2f8a4ba2 revsets: add a last function
Matt Mackall <mpm@selenic.com>
parents: 14057
diff changeset
  1245
        # i18n: "last" is a keyword
611d2f8a4ba2 revsets: add a last function
Matt Mackall <mpm@selenic.com>
parents: 14057
diff changeset
  1246
        raise error.ParseError(_("last expects a number"))
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
  1247
    os = getset(repo, fullreposet(repo), l[0])
20534
4849f574aa24 revset: changed last implementation to use lazy classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20527
diff changeset
  1248
    os.reverse()
22805
e74245b79901 revset-last: remove user of baseset.append
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22804
diff changeset
  1249
    result = []
20534
4849f574aa24 revset: changed last implementation to use lazy classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20527
diff changeset
  1250
    it = iter(os)
4849f574aa24 revset: changed last implementation to use lazy classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20527
diff changeset
  1251
    for x in xrange(lim):
25145
3553163bb736 revset: use 'next()' to detect end of iteration in 'last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25144
diff changeset
  1252
        y = next(it, None)
3553163bb736 revset: use 'next()' to detect end of iteration in 'last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25144
diff changeset
  1253
        if y is None:
20534
4849f574aa24 revset: changed last implementation to use lazy classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20527
diff changeset
  1254
            break
26636
ff6baf32b3ba revset: eliminate temporary reference to subset in limit() and last()
Yuya Nishihara <yuya@tcha.org>
parents: 26587
diff changeset
  1255
        elif y in subset:
25145
3553163bb736 revset: use 'next()' to detect end of iteration in 'last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25144
diff changeset
  1256
            result.append(y)
28426
3d39ac06af9a revset: add inspection data to limit() and last() functions
Yuya Nishihara <yuya@tcha.org>
parents: 28425
diff changeset
  1257
    return baseset(result, datarepr=('<last n=%d, %r, %r>', lim, subset, os))
14061
611d2f8a4ba2 revsets: add a last function
Matt Mackall <mpm@selenic.com>
parents: 14057
diff changeset
  1258
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1259
@predicate('max(set)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1260
def maxrev(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1261
    """Changeset with highest revision number in set.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1262
    """
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
  1263
    os = getset(repo, fullreposet(repo), x)
26305
ade5c488d622 revset: remove existence check from min() and max()
Durham Goode <durham@fb.com>
parents: 26304
diff changeset
  1264
    try:
20754
f15ff553b762 revset: changed minrev and maxrev implementations to use ordered sets
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20753
diff changeset
  1265
        m = os.max()
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1266
        if m in subset:
28427
969a4615c4c4 revset: add inspection data to max() and min() functions
Yuya Nishihara <yuya@tcha.org>
parents: 28426
diff changeset
  1267
            return baseset([m], datarepr=('<max %r, %r>', subset, os))
26305
ade5c488d622 revset: remove existence check from min() and max()
Durham Goode <durham@fb.com>
parents: 26304
diff changeset
  1268
    except ValueError:
ade5c488d622 revset: remove existence check from min() and max()
Durham Goode <durham@fb.com>
parents: 26304
diff changeset
  1269
        # os.max() throws a ValueError when the collection is empty.
ade5c488d622 revset: remove existence check from min() and max()
Durham Goode <durham@fb.com>
parents: 26304
diff changeset
  1270
        # Same as python's max().
ade5c488d622 revset: remove existence check from min() and max()
Durham Goode <durham@fb.com>
parents: 26304
diff changeset
  1271
        pass
28427
969a4615c4c4 revset: add inspection data to max() and min() functions
Yuya Nishihara <yuya@tcha.org>
parents: 28426
diff changeset
  1272
    return baseset(datarepr=('<max %r, %r>', subset, os))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1273
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1274
@predicate('merge()', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1275
def merge(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1276
    """Changeset is a merge changeset.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1277
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1278
    # i18n: "merge" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1279
    getargs(x, 0, 0, _("merge takes no arguments"))
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1280
    cl = repo.changelog
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1281
    return subset.filter(lambda r: cl.parentrevs(r)[1] != -1,
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1282
                         condrepr='<merge>')
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1283
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1284
@predicate('branchpoint()', safe=True)
17753
69d5078d760d revsets: add branchpoint() function
Ivan Andrus <darthandrus@gmail.com>
parents: 17675
diff changeset
  1285
def branchpoint(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1286
    """Changesets with more than one child.
17753
69d5078d760d revsets: add branchpoint() function
Ivan Andrus <darthandrus@gmail.com>
parents: 17675
diff changeset
  1287
    """
69d5078d760d revsets: add branchpoint() function
Ivan Andrus <darthandrus@gmail.com>
parents: 17675
diff changeset
  1288
    # i18n: "branchpoint" is a keyword
69d5078d760d revsets: add branchpoint() function
Ivan Andrus <darthandrus@gmail.com>
parents: 17675
diff changeset
  1289
    getargs(x, 0, 0, _("branchpoint takes no arguments"))
69d5078d760d revsets: add branchpoint() function
Ivan Andrus <darthandrus@gmail.com>
parents: 17675
diff changeset
  1290
    cl = repo.changelog
69d5078d760d revsets: add branchpoint() function
Ivan Andrus <darthandrus@gmail.com>
parents: 17675
diff changeset
  1291
    if not subset:
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
  1292
        return baseset()
25549
f93ff3ab8d14 revset: mark spots that should use 'smartset.min()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25548
diff changeset
  1293
    # XXX this should be 'parentset.min()' assuming 'parentset' is a smartset
f93ff3ab8d14 revset: mark spots that should use 'smartset.min()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25548
diff changeset
  1294
    # (and if it is not, it should.)
17753
69d5078d760d revsets: add branchpoint() function
Ivan Andrus <darthandrus@gmail.com>
parents: 17675
diff changeset
  1295
    baserev = min(subset)
69d5078d760d revsets: add branchpoint() function
Ivan Andrus <darthandrus@gmail.com>
parents: 17675
diff changeset
  1296
    parentscount = [0]*(len(repo) - baserev)
17785
ac5c9c8046f7 clfilter: use changelog to iterate over the repo in branchpoint
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17753
diff changeset
  1297
    for r in cl.revs(start=baserev + 1):
17753
69d5078d760d revsets: add branchpoint() function
Ivan Andrus <darthandrus@gmail.com>
parents: 17675
diff changeset
  1298
        for p in cl.parentrevs(r):
69d5078d760d revsets: add branchpoint() function
Ivan Andrus <darthandrus@gmail.com>
parents: 17675
diff changeset
  1299
            if p >= baserev:
69d5078d760d revsets: add branchpoint() function
Ivan Andrus <darthandrus@gmail.com>
parents: 17675
diff changeset
  1300
                parentscount[p - baserev] += 1
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1301
    return subset.filter(lambda r: parentscount[r - baserev] > 1,
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1302
                         condrepr='<branchpoint>')
17753
69d5078d760d revsets: add branchpoint() function
Ivan Andrus <darthandrus@gmail.com>
parents: 17675
diff changeset
  1303
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1304
@predicate('min(set)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1305
def minrev(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1306
    """Changeset with lowest revision number in set.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1307
    """
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
  1308
    os = getset(repo, fullreposet(repo), x)
26305
ade5c488d622 revset: remove existence check from min() and max()
Durham Goode <durham@fb.com>
parents: 26304
diff changeset
  1309
    try:
20754
f15ff553b762 revset: changed minrev and maxrev implementations to use ordered sets
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20753
diff changeset
  1310
        m = os.min()
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1311
        if m in subset:
28427
969a4615c4c4 revset: add inspection data to max() and min() functions
Yuya Nishihara <yuya@tcha.org>
parents: 28426
diff changeset
  1312
            return baseset([m], datarepr=('<min %r, %r>', subset, os))
26305
ade5c488d622 revset: remove existence check from min() and max()
Durham Goode <durham@fb.com>
parents: 26304
diff changeset
  1313
    except ValueError:
ade5c488d622 revset: remove existence check from min() and max()
Durham Goode <durham@fb.com>
parents: 26304
diff changeset
  1314
        # os.min() throws a ValueError when the collection is empty.
ade5c488d622 revset: remove existence check from min() and max()
Durham Goode <durham@fb.com>
parents: 26304
diff changeset
  1315
        # Same as python's min().
ade5c488d622 revset: remove existence check from min() and max()
Durham Goode <durham@fb.com>
parents: 26304
diff changeset
  1316
        pass
28427
969a4615c4c4 revset: add inspection data to max() and min() functions
Yuya Nishihara <yuya@tcha.org>
parents: 28426
diff changeset
  1317
    return baseset(datarepr=('<min %r, %r>', subset, os))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1318
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1319
@predicate('modifies(pattern)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1320
def modifies(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1321
    """Changesets modifying files matched by pattern.
20289
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
  1322
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
  1323
    The pattern without explicit kind like ``glob:`` is expected to be
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
  1324
    relative to the current directory and match against a file or a
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
  1325
    directory.
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1326
    """
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1327
    # i18n: "modifies" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1328
    pat = getstring(x, _("modifies requires a pattern"))
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1329
    return checkstatus(repo, subset, pat, 0)
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1330
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1331
@predicate('named(namespace)')
23836
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1332
def named(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1333
    """The changesets in a given namespace.
23836
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1334
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1335
    If `namespace` starts with `re:`, the remainder of the string is treated as
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1336
    a regular expression. To match a namespace that actually starts with `re:`,
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1337
    use the prefix `literal:`.
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1338
    """
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1339
    # i18n: "named" is a keyword
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1340
    args = getargs(x, 1, 1, _('named requires a namespace argument'))
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1341
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1342
    ns = getstring(args[0],
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1343
                   # i18n: "named" is a keyword
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1344
                   _('the argument to named must be a string'))
26481
7d132557e44a util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents: 26307
diff changeset
  1345
    kind, pattern, matcher = util.stringmatcher(ns)
23836
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1346
    namespaces = set()
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1347
    if kind == 'literal':
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1348
        if pattern not in repo.names:
23978
eeb5d5ab14a6 revset: raise RepoLookupError to make present() predicate continue the query
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23956
diff changeset
  1349
            raise error.RepoLookupError(_("namespace '%s' does not exist")
eeb5d5ab14a6 revset: raise RepoLookupError to make present() predicate continue the query
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23956
diff changeset
  1350
                                        % ns)
23836
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1351
        namespaces.add(repo.names[pattern])
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1352
    else:
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1353
        for name, ns in repo.names.iteritems():
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1354
            if matcher(name):
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1355
                namespaces.add(ns)
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1356
        if not namespaces:
23978
eeb5d5ab14a6 revset: raise RepoLookupError to make present() predicate continue the query
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23956
diff changeset
  1357
            raise error.RepoLookupError(_("no namespace exists"
eeb5d5ab14a6 revset: raise RepoLookupError to make present() predicate continue the query
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23956
diff changeset
  1358
                                          " that match '%s'") % pattern)
23836
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1359
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1360
    names = set()
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1361
    for ns in namespaces:
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1362
        for name in ns.listnames(repo):
24151
38824c53c2f1 revset: mask specific names for named() predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24008
diff changeset
  1363
            if name not in ns.deprecated:
38824c53c2f1 revset: mask specific names for named() predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24008
diff changeset
  1364
                names.update(repo[n].rev() for n in ns.nodes(repo, name))
23836
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1365
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1366
    names -= set([node.nullrev])
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1367
    return subset & names
3fb61fcbc4e4 namespaces: add revset for 'named(namespace)'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23833
diff changeset
  1368
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1369
@predicate('id(string)', safe=True)
16417
b4b0c6931e11 revset: avoid demandimport bug
Matt Mackall <mpm@selenic.com>
parents: 16415
diff changeset
  1370
def node_(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1371
    """Revision non-ambiguously specified by the given hex string prefix.
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  1372
    """
12815
079a618ea89d revset: add translator comments to i18n strings
Martin Geisler <mg@lazybytes.net>
parents: 12786
diff changeset
  1373
    # i18n: "id" is a keyword
12736
7e14e67e6622 revset: use 'requires' instead of 'wants' in error message
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 12733
diff changeset
  1374
    l = getargs(x, 1, 1, _("id requires one argument"))
12815
079a618ea89d revset: add translator comments to i18n strings
Martin Geisler <mg@lazybytes.net>
parents: 12786
diff changeset
  1375
    # i18n: "id" is a keyword
12736
7e14e67e6622 revset: use 'requires' instead of 'wants' in error message
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 12733
diff changeset
  1376
    n = getstring(l[0], _("id requires a string"))
12716
c7e619e30ba3 revset: add id() and rev() to allow explicitly referring to changes by hash or rev
Augie Fackler <durin42@gmail.com>
parents: 12715
diff changeset
  1377
    if len(n) == 40:
24904
b5c227f3e461 revset: id() called with 40-byte strings should give the same results as for short strings
Alexander Drozdov <al.drozdov@gmail.com>
parents: 24777
diff changeset
  1378
        try:
b5c227f3e461 revset: id() called with 40-byte strings should give the same results as for short strings
Alexander Drozdov <al.drozdov@gmail.com>
parents: 24777
diff changeset
  1379
            rn = repo.changelog.rev(node.bin(n))
b5c227f3e461 revset: id() called with 40-byte strings should give the same results as for short strings
Alexander Drozdov <al.drozdov@gmail.com>
parents: 24777
diff changeset
  1380
        except (LookupError, TypeError):
b5c227f3e461 revset: id() called with 40-byte strings should give the same results as for short strings
Alexander Drozdov <al.drozdov@gmail.com>
parents: 24777
diff changeset
  1381
            rn = None
12716
c7e619e30ba3 revset: add id() and rev() to allow explicitly referring to changes by hash or rev
Augie Fackler <durin42@gmail.com>
parents: 12715
diff changeset
  1382
    else:
16735
47b8ec0eb7fb revset: fix traceback for bogus revisions in id(rev)
Matt Harbison <matt_harbison@yahoo.com>
parents: 16640
diff changeset
  1383
        rn = None
47b8ec0eb7fb revset: fix traceback for bogus revisions in id(rev)
Matt Harbison <matt_harbison@yahoo.com>
parents: 16640
diff changeset
  1384
        pm = repo.changelog._partialmatch(n)
47b8ec0eb7fb revset: fix traceback for bogus revisions in id(rev)
Matt Harbison <matt_harbison@yahoo.com>
parents: 16640
diff changeset
  1385
        if pm is not None:
47b8ec0eb7fb revset: fix traceback for bogus revisions in id(rev)
Matt Harbison <matt_harbison@yahoo.com>
parents: 16640
diff changeset
  1386
            rn = repo.changelog.rev(pm)
47b8ec0eb7fb revset: fix traceback for bogus revisions in id(rev)
Matt Harbison <matt_harbison@yahoo.com>
parents: 16640
diff changeset
  1387
23005
9bfe68357c01 revset-node: speedup by a few hundred fold
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23004
diff changeset
  1388
    if rn is None:
9bfe68357c01 revset-node: speedup by a few hundred fold
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23004
diff changeset
  1389
        return baseset()
9bfe68357c01 revset-node: speedup by a few hundred fold
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23004
diff changeset
  1390
    result = baseset([rn])
9bfe68357c01 revset-node: speedup by a few hundred fold
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23004
diff changeset
  1391
    return result & subset
12716
c7e619e30ba3 revset: add id() and rev() to allow explicitly referring to changes by hash or rev
Augie Fackler <durin42@gmail.com>
parents: 12715
diff changeset
  1392
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1393
@predicate('obsolete()', safe=True)
17170
63a4a3871607 revset: add an `obsolete` symbol
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17102
diff changeset
  1394
def obsolete(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1395
    """Mutable changeset with a newer version."""
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
  1396
    # i18n: "obsolete" is a keyword
17170
63a4a3871607 revset: add an `obsolete` symbol
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17102
diff changeset
  1397
    getargs(x, 0, 0, _("obsolete takes no arguments"))
17825
3cc06457f15e obsolete: rename `getobscache` into `getrevs`
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17804
diff changeset
  1398
    obsoletes = obsmod.getrevs(repo, 'obsolete')
20367
2ac278aab2b4 revset: added intersection to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20366
diff changeset
  1399
    return subset & obsoletes
17170
63a4a3871607 revset: add an `obsolete` symbol
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17102
diff changeset
  1400
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1401
@predicate('only(set, [set])', safe=True)
23466
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1402
def only(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1403
    """Changesets that are ancestors of the first set that are not ancestors
23466
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1404
    of any other head in the repo. If a second set is specified, the result
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1405
    is ancestors of the first set that are not ancestors of the second set
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1406
    (i.e. ::<set1> - ::<set2>).
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1407
    """
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1408
    cl = repo.changelog
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1409
    # i18n: "only" is a keyword
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1410
    args = getargs(x, 1, 2, _('only takes one or two arguments'))
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
  1411
    include = getset(repo, fullreposet(repo), args[0])
23466
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1412
    if len(args) == 1:
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1413
        if not include:
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1414
            return baseset()
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1415
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1416
        descendants = set(_revdescendants(repo, include, False))
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1417
        exclude = [rev for rev in cl.headrevs()
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1418
            if not rev in descendants and not rev in include]
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1419
    else:
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
  1420
        exclude = getset(repo, fullreposet(repo), args[1])
23466
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1421
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1422
    results = set(cl.findmissingrevs(common=exclude, heads=include))
25554
94441df6206c revset: mark spots that use 'set' instead of 'smartset'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25553
diff changeset
  1423
    # XXX we should turn this into a baseset instead of a set, smartset may do
94441df6206c revset: mark spots that use 'set' instead of 'smartset'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25553
diff changeset
  1424
    # some optimisations from the fact this is a baseset.
23466
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1425
    return subset & results
d5b1a452cc32 revset: move 'only' so that functions are sorted alphabetically
Yuya Nishihara <yuya@tcha.org>
parents: 23426
diff changeset
  1426
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1427
@predicate('origin([set])', safe=True)
17185
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1428
def origin(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1429
    """
17185
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1430
    Changesets that were specified as a source for the grafts, transplants or
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1431
    rebases that created the given revisions.  Omitting the optional set is the
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1432
    same as passing all().  If a changeset created by these operations is itself
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1433
    specified as a source for one of these operations, only the source changeset
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1434
    for the first operation is selected.
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1435
    """
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1436
    if x is not None:
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
  1437
        dests = getset(repo, fullreposet(repo), x)
17185
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1438
    else:
24201
77ef059b3317 revset: drop unnecessary calls of getall() with empty argument
Yuya Nishihara <yuya@tcha.org>
parents: 24163
diff changeset
  1439
        dests = fullreposet(repo)
17185
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1440
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1441
    def _firstsrc(rev):
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1442
        src = _getrevsource(repo, rev)
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1443
        if src is None:
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1444
            return None
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1445
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1446
        while True:
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1447
            prev = _getrevsource(repo, src)
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1448
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1449
            if prev is None:
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1450
                return src
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1451
            src = prev
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1452
22944
5aae3dea8044 revset: better naming of variables containing the value of a single argument
Mads Kiilerich <madski@unity3d.com>
parents: 22891
diff changeset
  1453
    o = set([_firstsrc(r) for r in dests])
22498
64673dc48931 revset: remove invalid value in the origin set
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22497
diff changeset
  1454
    o -= set([None])
25554
94441df6206c revset: mark spots that use 'set' instead of 'smartset'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25553
diff changeset
  1455
    # XXX we should turn this into a baseset instead of a set, smartset may do
94441df6206c revset: mark spots that use 'set' instead of 'smartset'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25553
diff changeset
  1456
    # some optimisations from the fact this is a baseset.
22536
8040a44aab1c revset: use `subset &` in `origin`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22535
diff changeset
  1457
    return subset & o
17185
2c7c4824969e revset: add origin() predicate
Matt Harbison <matt_harbison@yahoo.com>
parents: 17173
diff changeset
  1458
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1459
@predicate('outgoing([path])', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1460
def outgoing(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1461
    """Changesets not found in the specified destination repository, or the
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1462
    default push location.
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  1463
    """
24722
02a5618e2fbf revset: don't import discovery at module level
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24708
diff changeset
  1464
    # Avoid cycles.
25971
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
  1465
    from . import (
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
  1466
        discovery,
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
  1467
        hg,
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
  1468
    )
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1469
    # i18n: "outgoing" is a keyword
14717
c8ee2729e89f revset and fileset: fix typos in parser error messages
Mads Kiilerich <mads@kiilerich.com>
parents: 14715
diff changeset
  1470
    l = getargs(x, 0, 1, _("outgoing takes one or no arguments"))
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1471
    # i18n: "outgoing" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1472
    dest = l and getstring(l[0], _("outgoing requires a repository path")) or ''
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1473
    dest = repo.ui.expandpath(dest or 'default-push', dest or 'default')
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1474
    dest, branches = hg.parseurl(dest)
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1475
    revs, checkout = hg.addbranchrevs(repo, repo, branches, [])
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1476
    if revs:
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1477
        revs = [repo.lookup(rev) for rev in revs]
14556
517e1d88bf7e hg: change various repository() users to use peer() where appropriate
Matt Mackall <mpm@selenic.com>
parents: 14509
diff changeset
  1478
    other = hg.peer(repo, {}, dest)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1479
    repo.ui.pushbuffer()
15837
cd956049fc14 discovery: introduce outgoing object for result of findcommonoutgoing
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 15819
diff changeset
  1480
    outgoing = discovery.findcommonoutgoing(repo, other, onlyheads=revs)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1481
    repo.ui.popbuffer()
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1482
    cl = repo.changelog
15837
cd956049fc14 discovery: introduce outgoing object for result of findcommonoutgoing
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 15819
diff changeset
  1483
    o = set([cl.rev(r) for r in outgoing.missing])
22529
5c53d7888aef revset: use `subset &` in `outgoing`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22528
diff changeset
  1484
    return subset & o
12716
c7e619e30ba3 revset: add id() and rev() to allow explicitly referring to changes by hash or rev
Augie Fackler <durin42@gmail.com>
parents: 12715
diff changeset
  1485
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1486
@predicate('p1([set])', safe=True)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1487
def p1(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1488
    """First parent of changesets in set, or the working directory.
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  1489
    """
12928
a5f7f1e9340e revsets: let p1() and p2() return parents of working dir
Kevin Bullock <kbullock@ringworld.org>
parents: 12859
diff changeset
  1490
    if x is None:
13878
a8d13ee0ce68 misc: replace .parents()[0] with p1()
Matt Mackall <mpm@selenic.com>
parents: 13873
diff changeset
  1491
        p = repo[x].p1().rev()
22538
a428db9ab61d revset: use `subset &` in bare `p1()`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22537
diff changeset
  1492
        if p >= 0:
a428db9ab61d revset: use `subset &` in bare `p1()`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22537
diff changeset
  1493
            return subset & baseset([p])
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
  1494
        return baseset()
12928
a5f7f1e9340e revsets: let p1() and p2() return parents of working dir
Kevin Bullock <kbullock@ringworld.org>
parents: 12859
diff changeset
  1495
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1496
    ps = set()
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1497
    cl = repo.changelog
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
  1498
    for r in getset(repo, fullreposet(repo), x):
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1499
        ps.add(cl.parentrevs(r)[0])
22495
668b26d32bf6 revset: remove nullrev from set computed in p1() and p2()
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22494
diff changeset
  1500
    ps -= set([node.nullrev])
25554
94441df6206c revset: mark spots that use 'set' instead of 'smartset'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25553
diff changeset
  1501
    # XXX we should turn this into a baseset instead of a set, smartset may do
94441df6206c revset: mark spots that use 'set' instead of 'smartset'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25553
diff changeset
  1502
    # some optimisations from the fact this is a baseset.
20367
2ac278aab2b4 revset: added intersection to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20366
diff changeset
  1503
    return subset & ps
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1504
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1505
@predicate('p2([set])', safe=True)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1506
def p2(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1507
    """Second parent of changesets in set, or the working directory.
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  1508
    """
12928
a5f7f1e9340e revsets: let p1() and p2() return parents of working dir
Kevin Bullock <kbullock@ringworld.org>
parents: 12859
diff changeset
  1509
    if x is None:
a5f7f1e9340e revsets: let p1() and p2() return parents of working dir
Kevin Bullock <kbullock@ringworld.org>
parents: 12859
diff changeset
  1510
        ps = repo[x].parents()
a5f7f1e9340e revsets: let p1() and p2() return parents of working dir
Kevin Bullock <kbullock@ringworld.org>
parents: 12859
diff changeset
  1511
        try:
12935
98b79c892768 revset: fix p1, p2 and parents in dirstate case (a5f7f1e9340e)
Patrick Mezard <pmezard@gmail.com>
parents: 12929
diff changeset
  1512
            p = ps[1].rev()
22539
6f434ef54222 revset: use `subset &` in bare `p2()`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22538
diff changeset
  1513
            if p >= 0:
6f434ef54222 revset: use `subset &` in bare `p2()`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22538
diff changeset
  1514
                return subset & baseset([p])
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
  1515
            return baseset()
12928
a5f7f1e9340e revsets: let p1() and p2() return parents of working dir
Kevin Bullock <kbullock@ringworld.org>
parents: 12859
diff changeset
  1516
        except IndexError:
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
  1517
            return baseset()
12928
a5f7f1e9340e revsets: let p1() and p2() return parents of working dir
Kevin Bullock <kbullock@ringworld.org>
parents: 12859
diff changeset
  1518
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1519
    ps = set()
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1520
    cl = repo.changelog
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
  1521
    for r in getset(repo, fullreposet(repo), x):
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1522
        ps.add(cl.parentrevs(r)[1])
22495
668b26d32bf6 revset: remove nullrev from set computed in p1() and p2()
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22494
diff changeset
  1523
    ps -= set([node.nullrev])
25554
94441df6206c revset: mark spots that use 'set' instead of 'smartset'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25553
diff changeset
  1524
    # XXX we should turn this into a baseset instead of a set, smartset may do
94441df6206c revset: mark spots that use 'set' instead of 'smartset'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25553
diff changeset
  1525
    # some optimisations from the fact this is a baseset.
20367
2ac278aab2b4 revset: added intersection to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20366
diff changeset
  1526
    return subset & ps
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1527
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1528
@predicate('parents([set])', safe=True)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1529
def parents(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1530
    """
12929
515c2786e1cf revsets: let parents() return parents of working dir
Kevin Bullock <kbullock@ringworld.org>
parents: 12928
diff changeset
  1531
    The set of all parents for all changesets in set, or the working directory.
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  1532
    """
12929
515c2786e1cf revsets: let parents() return parents of working dir
Kevin Bullock <kbullock@ringworld.org>
parents: 12928
diff changeset
  1533
    if x is None:
22496
35af9361a049 revset: refactor parents() into a single return point
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22495
diff changeset
  1534
        ps = set(p.rev() for p in repo[x].parents())
35af9361a049 revset: refactor parents() into a single return point
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22495
diff changeset
  1535
    else:
35af9361a049 revset: refactor parents() into a single return point
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22495
diff changeset
  1536
        ps = set()
35af9361a049 revset: refactor parents() into a single return point
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22495
diff changeset
  1537
        cl = repo.changelog
25716
d50677c3bf44 revset: prefetch method in "parents"
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25706
diff changeset
  1538
        up = ps.update
d50677c3bf44 revset: prefetch method in "parents"
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25706
diff changeset
  1539
        parentrevs = cl.parentrevs
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
  1540
        for r in getset(repo, fullreposet(repo), x):
25765
5e1b0739611c revset: use integer representation of wdir() in revset
Yuya Nishihara <yuya@tcha.org>
parents: 25716
diff changeset
  1541
            if r == node.wdirrev:
25716
d50677c3bf44 revset: prefetch method in "parents"
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25706
diff changeset
  1542
                up(p.rev() for p in repo[r].parents())
25689
1cce81121472 revset: fix a crash in parents() when 'wdir()' is in the set
Matt Harbison <matt_harbison@yahoo.com>
parents: 25660
diff changeset
  1543
            else:
25716
d50677c3bf44 revset: prefetch method in "parents"
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25706
diff changeset
  1544
                up(parentrevs(r))
22497
8ea3f47bcaff revset: remove nullrev from set computed in parents()
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22496
diff changeset
  1545
    ps -= set([node.nullrev])
22712
093df3b77f27 revert: bring back usage of `subset & ps` in `parents`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22692
diff changeset
  1546
    return subset & ps
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1547
25621
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1548
def _phase(repo, subset, target):
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1549
    """helper to select all rev in phase <target>"""
25622
85294076adce revset: make use of natively-computed set for 'draft()' and 'secret()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25621
diff changeset
  1550
    repo._phasecache.loadphaserevs(repo) # ensure phase's sets are loaded
85294076adce revset: make use of natively-computed set for 'draft()' and 'secret()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25621
diff changeset
  1551
    if repo._phasecache._phasesets:
85294076adce revset: make use of natively-computed set for 'draft()' and 'secret()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25621
diff changeset
  1552
        s = repo._phasecache._phasesets[target] - repo.changelog.filteredrevs
85294076adce revset: make use of natively-computed set for 'draft()' and 'secret()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25621
diff changeset
  1553
        s = baseset(s)
85294076adce revset: make use of natively-computed set for 'draft()' and 'secret()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25621
diff changeset
  1554
        s.sort() # set are non ordered, so we enforce ascending
85294076adce revset: make use of natively-computed set for 'draft()' and 'secret()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25621
diff changeset
  1555
        return subset & s
85294076adce revset: make use of natively-computed set for 'draft()' and 'secret()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25621
diff changeset
  1556
    else:
85294076adce revset: make use of natively-computed set for 'draft()' and 'secret()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25621
diff changeset
  1557
        phase = repo._phasecache.phase
85294076adce revset: make use of natively-computed set for 'draft()' and 'secret()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25621
diff changeset
  1558
        condition = lambda r: phase(repo, r) == target
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1559
        return subset.filter(condition, condrepr=('<phase %r>', target),
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1560
                             cache=False)
25621
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1561
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1562
@predicate('draft()', safe=True)
25621
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1563
def draft(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1564
    """Changeset in draft phase."""
25621
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1565
    # i18n: "draft" is a keyword
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1566
    getargs(x, 0, 0, _("draft takes no arguments"))
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1567
    target = phases.draft
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1568
    return _phase(repo, subset, target)
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1569
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1570
@predicate('secret()', safe=True)
25621
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1571
def secret(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1572
    """Changeset in secret phase."""
25621
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1573
    # i18n: "secret" is a keyword
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1574
    getargs(x, 0, 0, _("secret takes no arguments"))
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1575
    target = phases.secret
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1576
    return _phase(repo, subset, target)
21a874693619 revset: refactor the non-public phase code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25620
diff changeset
  1577
14070
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1578
def parentspec(repo, subset, x, n):
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1579
    """``set^0``
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1580
    The set.
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1581
    ``set^1`` (or ``set^``), ``set^2``
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1582
    First or second parent, respectively, of all changesets in set.
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  1583
    """
12320
40c40c6f20b8 revset: handle re.compile() errors in grep()
Brodie Rao <brodie@bitheap.org>
parents: 11882
diff changeset
  1584
    try:
14070
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1585
        n = int(n[1])
14072
2e4d79dcc0a0 revset: add missing whitespace
Kevin Gessner <kevin@kevingessner.com>
parents: 14070
diff changeset
  1586
        if n not in (0, 1, 2):
14070
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1587
            raise ValueError
14851
f96c354493d7 revsets: actually catch type error on tip^p1(tip) (issue2884)
Matt Mackall <mpm@selenic.com>
parents: 14842
diff changeset
  1588
    except (TypeError, ValueError):
14070
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1589
        raise error.ParseError(_("^ expects a number 0, 1, or 2"))
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1590
    ps = set()
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1591
    cl = repo.changelog
23165
7e8737e6ab08 revset-parentspec: call 'getset' on a 'fullreposet'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23164
diff changeset
  1592
    for r in getset(repo, fullreposet(repo), x):
14070
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1593
        if n == 0:
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1594
            ps.add(r)
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1595
        elif n == 1:
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1596
            ps.add(cl.parentrevs(r)[0])
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1597
        elif n == 2:
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1598
            parents = cl.parentrevs(r)
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1599
            if len(parents) > 1:
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  1600
                ps.add(parents[1])
20367
2ac278aab2b4 revset: added intersection to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20366
diff changeset
  1601
    return subset & ps
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1602
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1603
@predicate('present(set)', safe=True)
11944
df52ff0980fe revset: predicate to avoid lookup errors
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11886
diff changeset
  1604
def present(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1605
    """An empty set, if any revision in set isn't found; otherwise,
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  1606
    all revisions in set.
16748
0a730d3c5aae doc: add detail explanation for 'present()' predicate of revsets
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16735
diff changeset
  1607
0a730d3c5aae doc: add detail explanation for 'present()' predicate of revsets
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16735
diff changeset
  1608
    If any of specified revisions is not present in the local repository,
0a730d3c5aae doc: add detail explanation for 'present()' predicate of revsets
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16735
diff changeset
  1609
    the query is normally aborted. But this predicate allows the query
0a730d3c5aae doc: add detail explanation for 'present()' predicate of revsets
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16735
diff changeset
  1610
    to continue even in such cases.
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  1611
    """
11944
df52ff0980fe revset: predicate to avoid lookup errors
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11886
diff changeset
  1612
    try:
df52ff0980fe revset: predicate to avoid lookup errors
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11886
diff changeset
  1613
        return getset(repo, subset, x)
df52ff0980fe revset: predicate to avoid lookup errors
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11886
diff changeset
  1614
    except error.RepoLookupError:
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
  1615
        return baseset()
11944
df52ff0980fe revset: predicate to avoid lookup errors
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11886
diff changeset
  1616
25224
d032f57936f5 revset: drop docstring from internal _notpublic() function
Yuya Nishihara <yuya@tcha.org>
parents: 25191
diff changeset
  1617
# for internal use
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1618
@predicate('_notpublic', safe=True)
25191
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  1619
def _notpublic(repo, subset, x):
25225
57fae767c52b revset: drop translation marker from error message of _notpublic()
Yuya Nishihara <yuya@tcha.org>
parents: 25224
diff changeset
  1620
    getargs(x, 0, 0, "_notpublic takes no arguments")
25612
97528adbf74b revset: ensure we have loaded phases data in '_notpublic()'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25567
diff changeset
  1621
    repo._phasecache.loadphaserevs(repo) # ensure phase's sets are loaded
25191
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  1622
    if repo._phasecache._phasesets:
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  1623
        s = set()
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  1624
        for u in repo._phasecache._phasesets[1:]:
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  1625
            s.update(u)
25619
833fa28cd949 revset: use a baseset in _notpublic()
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25612
diff changeset
  1626
        s = baseset(s - repo.changelog.filteredrevs)
833fa28cd949 revset: use a baseset in _notpublic()
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25612
diff changeset
  1627
        s.sort()
25191
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  1628
        return subset & s
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  1629
    else:
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  1630
        phase = repo._phasecache.phase
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  1631
        target = phases.public
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  1632
        condition = lambda r: phase(repo, r) != target
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1633
        return subset.filter(condition, condrepr=('<phase %r>', target),
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1634
                             cache=False)
25191
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  1635
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1636
@predicate('public()', safe=True)
15819
33ca11b010e2 phases: implements simple revset symbol
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 15791
diff changeset
  1637
def public(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1638
    """Changeset in public phase."""
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
  1639
    # i18n: "public" is a keyword
15819
33ca11b010e2 phases: implements simple revset symbol
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 15791
diff changeset
  1640
    getargs(x, 0, 0, _("public takes no arguments"))
23019
c8f32accd00a revset-phases: prefetch attributes in phasesrelated revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23018
diff changeset
  1641
    phase = repo._phasecache.phase
c8f32accd00a revset-phases: prefetch attributes in phasesrelated revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23018
diff changeset
  1642
    target = phases.public
c8f32accd00a revset-phases: prefetch attributes in phasesrelated revsets
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23018
diff changeset
  1643
    condition = lambda r: phase(repo, r) == target
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1644
    return subset.filter(condition, condrepr=('<phase %r>', target),
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1645
                         cache=False)
15819
33ca11b010e2 phases: implements simple revset symbol
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 15791
diff changeset
  1646
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1647
@predicate('remote([id [,path]])', safe=True)
15936
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1648
def remote(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1649
    """Local revision that corresponds to the given identifier in a
15936
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1650
    remote repository, if present. Here, the '.' identifier is a
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1651
    synonym for the current local branch.
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1652
    """
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1653
25971
e9cd028f2dff revset: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25929
diff changeset
  1654
    from . import hg # avoid start-up nasties
15936
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1655
    # i18n: "remote" is a keyword
27293
9e06e7fb037d grammar: favor zero, one, two over ... or no
timeless <timeless@mozdev.org>
parents: 27028
diff changeset
  1656
    l = getargs(x, 0, 2, _("remote takes zero, one, or two arguments"))
15936
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1657
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1658
    q = '.'
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1659
    if len(l) > 0:
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1660
    # i18n: "remote" is a keyword
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1661
        q = getstring(l[0], _("remote requires a string id"))
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1662
    if q == '.':
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1663
        q = repo['.'].branch()
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1664
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1665
    dest = ''
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1666
    if len(l) > 1:
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1667
        # i18n: "remote" is a keyword
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1668
        dest = getstring(l[1], _("remote requires a repository path"))
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1669
    dest = repo.ui.expandpath(dest or 'default')
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1670
    dest, branches = hg.parseurl(dest)
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1671
    revs, checkout = hg.addbranchrevs(repo, repo, branches, [])
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1672
    if revs:
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1673
        revs = [repo.lookup(rev) for rev in revs]
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1674
    other = hg.peer(repo, {}, dest)
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1675
    n = other.lookup(q)
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1676
    if n in repo:
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1677
        r = repo[n].rev()
16006
39e60576ac98 revset: fix 'remote()' failure when remote repo has more revs than local
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15966
diff changeset
  1678
        if r in subset:
20364
a6cf48b2880d revset: added baseset class (still empty) to improve revset performance
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20289
diff changeset
  1679
            return baseset([r])
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
  1680
    return baseset()
15936
878bc4a62a73 revset: add remote() predicate to lookup remote revisions
Matt Mackall <mpm@selenic.com>
parents: 15903
diff changeset
  1681
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1682
@predicate('removes(pattern)', safe=True)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1683
def removes(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1684
    """Changesets which remove files matching pattern.
20289
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
  1685
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
  1686
    The pattern without explicit kind like ``glob:`` is expected to be
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
  1687
    relative to the current directory and match against a file or a
96be25f1da45 revset: add explanation about the pattern without explicit kind
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20288
diff changeset
  1688
    directory.
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  1689
    """
12815
079a618ea89d revset: add translator comments to i18n strings
Martin Geisler <mg@lazybytes.net>
parents: 12786
diff changeset
  1690
    # i18n: "removes" is a keyword
12736
7e14e67e6622 revset: use 'requires' instead of 'wants' in error message
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 12733
diff changeset
  1691
    pat = getstring(x, _("removes requires a pattern"))
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1692
    return checkstatus(repo, subset, pat, 2)
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1693
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1694
@predicate('rev(number)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1695
def rev(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1696
    """Revision with the given numeric identifier.
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  1697
    """
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1698
    # i18n: "rev" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1699
    l = getargs(x, 1, 1, _("rev requires one argument"))
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1700
    try:
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1701
        # i18n: "rev" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1702
        l = int(getstring(l[0], _("rev requires a number")))
14851
f96c354493d7 revsets: actually catch type error on tip^p1(tip) (issue2884)
Matt Mackall <mpm@selenic.com>
parents: 14842
diff changeset
  1703
    except (TypeError, ValueError):
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1704
        # i18n: "rev" is a keyword
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1705
        raise error.ParseError(_("rev expects a number"))
24031
fe1abb5d92f6 revset: have rev() validate input by repo.changelog.__contains__()
Yuya Nishihara <yuya@tcha.org>
parents: 24012
diff changeset
  1706
    if l not in repo.changelog and l != node.nullrev:
23062
ba89f7b542c9 revset: have rev() drop out-of-range or filtered rev explicitly (issue4396)
Yuya Nishihara <yuya@tcha.org>
parents: 23061
diff changeset
  1707
        return baseset()
22537
bbf4f3dfd700 revset: use `subset &` in `rev`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22536
diff changeset
  1708
    return subset & baseset([l])
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1709
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1710
@predicate('matching(revision [, field])', safe=True)
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1711
def matching(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1712
    """Changesets in which a given set of fields match the set of fields in the
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1713
    selected revision or set.
16528
5d803620ca05 doc: flatten description of 'matching()' predicate to be formatted well
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16521
diff changeset
  1714
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1715
    To match more than one field pass the list of fields to match separated
16528
5d803620ca05 doc: flatten description of 'matching()' predicate to be formatted well
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16521
diff changeset
  1716
    by spaces (e.g. ``author description``).
5d803620ca05 doc: flatten description of 'matching()' predicate to be formatted well
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16521
diff changeset
  1717
5d803620ca05 doc: flatten description of 'matching()' predicate to be formatted well
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16521
diff changeset
  1718
    Valid fields are most regular revision fields and some special fields.
5d803620ca05 doc: flatten description of 'matching()' predicate to be formatted well
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16521
diff changeset
  1719
5d803620ca05 doc: flatten description of 'matching()' predicate to be formatted well
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16521
diff changeset
  1720
    Regular revision fields are ``description``, ``author``, ``branch``,
17102
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1721
    ``date``, ``files``, ``phase``, ``parents``, ``substate``, ``user``
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1722
    and ``diff``.
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1723
    Note that ``author`` and ``user`` are synonyms. ``diff`` refers to the
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1724
    contents of the revision. Two revisions matching their ``diff`` will
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1725
    also match their ``files``.
16528
5d803620ca05 doc: flatten description of 'matching()' predicate to be formatted well
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16521
diff changeset
  1726
5d803620ca05 doc: flatten description of 'matching()' predicate to be formatted well
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16521
diff changeset
  1727
    Special fields are ``summary`` and ``metadata``:
5d803620ca05 doc: flatten description of 'matching()' predicate to be formatted well
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16521
diff changeset
  1728
    ``summary`` matches the first line of the description.
16639
00290bd359fe revset: documentation typo "metatadata"
Jesse Glick <jesse.glick@oracle.com>
parents: 16528
diff changeset
  1729
    ``metadata`` is equivalent to matching ``description user date``
16528
5d803620ca05 doc: flatten description of 'matching()' predicate to be formatted well
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16521
diff changeset
  1730
    (i.e. it matches the main metadata fields).
5d803620ca05 doc: flatten description of 'matching()' predicate to be formatted well
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16521
diff changeset
  1731
5d803620ca05 doc: flatten description of 'matching()' predicate to be formatted well
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16521
diff changeset
  1732
    ``metadata`` is the default field which is used when no fields are
5d803620ca05 doc: flatten description of 'matching()' predicate to be formatted well
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 16521
diff changeset
  1733
    specified. You can match more than one field at a time.
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1734
    """
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
  1735
    # i18n: "matching" is a keyword
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1736
    l = getargs(x, 1, 2, _("matching takes 1 or 2 arguments"))
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1737
23166
30e0dcd7c5ff revset-matching: call 'getset' on a 'fullreposet'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23165
diff changeset
  1738
    revs = getset(repo, fullreposet(repo), l[0])
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1739
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1740
    fieldlist = ['metadata']
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1741
    if len(l) > 1:
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1742
            fieldlist = getstring(l[1],
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
  1743
                # i18n: "matching" is a keyword
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1744
                _("matching requires a string "
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1745
                "as its second argument")).split()
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1746
17102
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1747
    # Make sure that there are no repeated fields,
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1748
    # expand the 'special' 'metadata' field type
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1749
    # and check the 'files' whenever we check the 'diff'
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1750
    fields = []
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1751
    for field in fieldlist:
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1752
        if field == 'metadata':
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1753
            fields += ['user', 'description', 'date']
17102
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1754
        elif field == 'diff':
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1755
            # a revision matching the diff must also match the files
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1756
            # since matching the diff is very costly, make sure to
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1757
            # also match the files first
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1758
            fields += ['files', 'diff']
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1759
        else:
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1760
            if field == 'author':
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1761
                field = 'user'
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1762
            fields.append(field)
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1763
    fields = set(fields)
16444
432f198600c6 revset: make matching keyword not match summary when matching for description
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16417
diff changeset
  1764
    if 'summary' in fields and 'description' in fields:
432f198600c6 revset: make matching keyword not match summary when matching for description
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16417
diff changeset
  1765
        # If a revision matches its description it also matches its summary
432f198600c6 revset: make matching keyword not match summary when matching for description
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16417
diff changeset
  1766
        fields.discard('summary')
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1767
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1768
    # We may want to match more than one field
16446
984e0412e82b revset: speedup matching() by first matching fields that take less time to
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16445
diff changeset
  1769
    # Not all fields take the same amount of time to be matched
984e0412e82b revset: speedup matching() by first matching fields that take less time to
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16445
diff changeset
  1770
    # Sort the selected fields in order of increasing matching cost
16453
d2a865d4b963 revset: make matching() work on python 2.4
Patrick Mezard <patrick@mezard.eu>
parents: 16452
diff changeset
  1771
    fieldorder = ['phase', 'parents', 'user', 'date', 'branch', 'summary',
17102
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1772
        'files', 'description', 'substate', 'diff']
16446
984e0412e82b revset: speedup matching() by first matching fields that take less time to
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16445
diff changeset
  1773
    def fieldkeyfunc(f):
984e0412e82b revset: speedup matching() by first matching fields that take less time to
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16445
diff changeset
  1774
        try:
984e0412e82b revset: speedup matching() by first matching fields that take less time to
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16445
diff changeset
  1775
            return fieldorder.index(f)
984e0412e82b revset: speedup matching() by first matching fields that take less time to
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16445
diff changeset
  1776
        except ValueError:
984e0412e82b revset: speedup matching() by first matching fields that take less time to
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16445
diff changeset
  1777
            # assume an unknown field is very costly
984e0412e82b revset: speedup matching() by first matching fields that take less time to
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16445
diff changeset
  1778
            return len(fieldorder)
984e0412e82b revset: speedup matching() by first matching fields that take less time to
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16445
diff changeset
  1779
    fields = list(fields)
984e0412e82b revset: speedup matching() by first matching fields that take less time to
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16445
diff changeset
  1780
    fields.sort(key=fieldkeyfunc)
984e0412e82b revset: speedup matching() by first matching fields that take less time to
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16445
diff changeset
  1781
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1782
    # Each field will be matched with its own "getfield" function
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1783
    # which will be added to the getfieldfuncs array of functions
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1784
    getfieldfuncs = []
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1785
    _funcs = {
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1786
        'user': lambda r: repo[r].user(),
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1787
        'branch': lambda r: repo[r].branch(),
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1788
        'date': lambda r: repo[r].date(),
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1789
        'description': lambda r: repo[r].description(),
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1790
        'files': lambda r: repo[r].files(),
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1791
        'parents': lambda r: repo[r].parents(),
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1792
        'phase': lambda r: repo[r].phase(),
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1793
        'substate': lambda r: repo[r].substate,
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1794
        'summary': lambda r: repo[r].description().splitlines()[0],
17102
d9a046ae4d8e revset: add "diff" field to "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17100
diff changeset
  1795
        'diff': lambda r: list(repo[r].diff(git=True),)
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1796
    }
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1797
    for info in fields:
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1798
        getfield = _funcs.get(info, None)
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1799
        if getfield is None:
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1800
            raise error.ParseError(
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
  1801
                # i18n: "matching" is a keyword
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1802
                _("unexpected field name passed to matching: %s") % info)
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1803
        getfieldfuncs.append(getfield)
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1804
    # convert the getfield array of functions into a "getinfo" function
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1805
    # which returns an array of field values (or a single value if there
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1806
    # is only one field to match)
16445
453c8670566c revset: speedup matching() by stopping the match early if a field does not match
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16444
diff changeset
  1807
    getinfo = lambda r: [f(r) for f in getfieldfuncs]
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1808
20459
51890507c6b3 revset: added lazyset implementation to matching revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20458
diff changeset
  1809
    def matches(x):
51890507c6b3 revset: added lazyset implementation to matching revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20458
diff changeset
  1810
        for rev in revs:
51890507c6b3 revset: added lazyset implementation to matching revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20458
diff changeset
  1811
            target = getinfo(rev)
16445
453c8670566c revset: speedup matching() by stopping the match early if a field does not match
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16444
diff changeset
  1812
            match = True
453c8670566c revset: speedup matching() by stopping the match early if a field does not match
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16444
diff changeset
  1813
            for n, f in enumerate(getfieldfuncs):
20459
51890507c6b3 revset: added lazyset implementation to matching revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20458
diff changeset
  1814
                if target[n] != f(x):
16445
453c8670566c revset: speedup matching() by stopping the match early if a field does not match
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16444
diff changeset
  1815
                    match = False
453c8670566c revset: speedup matching() by stopping the match early if a field does not match
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16444
diff changeset
  1816
            if match:
20459
51890507c6b3 revset: added lazyset implementation to matching revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20458
diff changeset
  1817
                return True
51890507c6b3 revset: added lazyset implementation to matching revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20458
diff changeset
  1818
        return False
51890507c6b3 revset: added lazyset implementation to matching revset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20458
diff changeset
  1819
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1820
    return subset.filter(matches, condrepr=('<matching%r %r>', fields, revs))
16402
1fb2f1400ea8 revset: add "matching" keyword
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16218
diff changeset
  1821
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1822
@predicate('reverse(set)', safe=True)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1823
def reverse(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1824
    """Reverse order of set.
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  1825
    """
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1826
    l = getset(repo, subset, x)
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1827
    l.reverse()
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1828
    return l
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1829
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  1830
@predicate('roots(set)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  1831
def roots(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  1832
    """Changesets in set with no parent changeset in set.
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  1833
    """
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
  1834
    s = getset(repo, fullreposet(repo), x)
25647
46a96dd4d976 revset: improves time complexity of 'roots(xxx)'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25634
diff changeset
  1835
    parents = repo.changelog.parentrevs
46a96dd4d976 revset: improves time complexity of 'roots(xxx)'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25634
diff changeset
  1836
    def filter(r):
46a96dd4d976 revset: improves time complexity of 'roots(xxx)'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25634
diff changeset
  1837
        for p in parents(r):
46a96dd4d976 revset: improves time complexity of 'roots(xxx)'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25634
diff changeset
  1838
            if 0 <= p and p in s:
46a96dd4d976 revset: improves time complexity of 'roots(xxx)'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25634
diff changeset
  1839
                return False
46a96dd4d976 revset: improves time complexity of 'roots(xxx)'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25634
diff changeset
  1840
        return True
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  1841
    return subset & s.filter(filter, condrepr='<roots>')
11944
df52ff0980fe revset: predicate to avoid lookup errors
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 11886
diff changeset
  1842
29265
3f9e68864ccc revset: define table of sort() key functions
Yuya Nishihara <yuya@tcha.org>
parents: 29264
diff changeset
  1843
_sortkeyfuncs = {
3f9e68864ccc revset: define table of sort() key functions
Yuya Nishihara <yuya@tcha.org>
parents: 29264
diff changeset
  1844
    'rev': lambda c: c.rev(),
3f9e68864ccc revset: define table of sort() key functions
Yuya Nishihara <yuya@tcha.org>
parents: 29264
diff changeset
  1845
    'branch': lambda c: c.branch(),
3f9e68864ccc revset: define table of sort() key functions
Yuya Nishihara <yuya@tcha.org>
parents: 29264
diff changeset
  1846
    'desc': lambda c: c.description(),
3f9e68864ccc revset: define table of sort() key functions
Yuya Nishihara <yuya@tcha.org>
parents: 29264
diff changeset
  1847
    'user': lambda c: c.user(),
3f9e68864ccc revset: define table of sort() key functions
Yuya Nishihara <yuya@tcha.org>
parents: 29264
diff changeset
  1848
    'author': lambda c: c.user(),
3f9e68864ccc revset: define table of sort() key functions
Yuya Nishihara <yuya@tcha.org>
parents: 29264
diff changeset
  1849
    'date': lambda c: c.date()[0],
3f9e68864ccc revset: define table of sort() key functions
Yuya Nishihara <yuya@tcha.org>
parents: 29264
diff changeset
  1850
}
3f9e68864ccc revset: define table of sort() key functions
Yuya Nishihara <yuya@tcha.org>
parents: 29264
diff changeset
  1851
29365
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1852
def _getsortargs(x):
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1853
    """Parse sort options into (set, [(key, reverse)], opts)"""
29348
2188f170f5b6 revset: add new topographical sort
Martijn Pieters <mjpieters@fb.com>
parents: 29347
diff changeset
  1854
    args = getargsdict(x, 'sort', 'set keys topo.firstbranch')
29238
e150c1d5f262 revset: use getargsdict for sort()
Martijn Pieters <mjpieters@fb.com>
parents: 29216
diff changeset
  1855
    if 'set' not in args:
e150c1d5f262 revset: use getargsdict for sort()
Martijn Pieters <mjpieters@fb.com>
parents: 29216
diff changeset
  1856
        # i18n: "sort" is a keyword
e150c1d5f262 revset: use getargsdict for sort()
Martijn Pieters <mjpieters@fb.com>
parents: 29216
diff changeset
  1857
        raise error.ParseError(_('sort requires one or two arguments'))
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1858
    keys = "rev"
29238
e150c1d5f262 revset: use getargsdict for sort()
Martijn Pieters <mjpieters@fb.com>
parents: 29216
diff changeset
  1859
    if 'keys' in args:
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
  1860
        # i18n: "sort" is a keyword
29238
e150c1d5f262 revset: use getargsdict for sort()
Martijn Pieters <mjpieters@fb.com>
parents: 29216
diff changeset
  1861
        keys = getstring(args['keys'], _("sort spec must be a string"))
e150c1d5f262 revset: use getargsdict for sort()
Martijn Pieters <mjpieters@fb.com>
parents: 29216
diff changeset
  1862
29363
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1863
    keyflags = []
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1864
    for k in keys.split():
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1865
        fk = k
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1866
        reverse = (k[0] == '-')
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1867
        if reverse:
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1868
            k = k[1:]
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1869
        if k not in _sortkeyfuncs and k != 'topo':
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1870
            raise error.ParseError(_("unknown sort key %r") % fk)
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1871
        keyflags.append((k, reverse))
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1872
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1873
    if len(keyflags) > 1 and any(k == 'topo' for k, reverse in keyflags):
29348
2188f170f5b6 revset: add new topographical sort
Martijn Pieters <mjpieters@fb.com>
parents: 29347
diff changeset
  1874
        # i18n: "topo" is a keyword
29646
a8a5dd8986f0 revset: refactor to make xgettext put i18n comments into hg.pot file
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29441
diff changeset
  1875
        raise error.ParseError(_('topo sort order cannot be combined '
a8a5dd8986f0 revset: refactor to make xgettext put i18n comments into hg.pot file
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29441
diff changeset
  1876
                                 'with other sort keys'))
29348
2188f170f5b6 revset: add new topographical sort
Martijn Pieters <mjpieters@fb.com>
parents: 29347
diff changeset
  1877
29364
76a1a703e23d revset: build dict of extra sort options before evaluating set
Yuya Nishihara <yuya@tcha.org>
parents: 29363
diff changeset
  1878
    opts = {}
29348
2188f170f5b6 revset: add new topographical sort
Martijn Pieters <mjpieters@fb.com>
parents: 29347
diff changeset
  1879
    if 'topo.firstbranch' in args:
29363
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1880
        if any(k == 'topo' for k, reverse in keyflags):
29364
76a1a703e23d revset: build dict of extra sort options before evaluating set
Yuya Nishihara <yuya@tcha.org>
parents: 29363
diff changeset
  1881
            opts['topo.firstbranch'] = args['topo.firstbranch']
29348
2188f170f5b6 revset: add new topographical sort
Martijn Pieters <mjpieters@fb.com>
parents: 29347
diff changeset
  1882
        else:
2188f170f5b6 revset: add new topographical sort
Martijn Pieters <mjpieters@fb.com>
parents: 29347
diff changeset
  1883
            # i18n: "topo" and "topo.firstbranch" are keywords
29646
a8a5dd8986f0 revset: refactor to make xgettext put i18n comments into hg.pot file
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29441
diff changeset
  1884
            raise error.ParseError(_('topo.firstbranch can only be used '
a8a5dd8986f0 revset: refactor to make xgettext put i18n comments into hg.pot file
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29441
diff changeset
  1885
                                     'when using the topo sort key'))
29348
2188f170f5b6 revset: add new topographical sort
Martijn Pieters <mjpieters@fb.com>
parents: 29347
diff changeset
  1886
29365
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1887
    return args['set'], keyflags, opts
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1888
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1889
@predicate('sort(set[, [-]key... [, ...]])', safe=True)
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1890
def sort(repo, subset, x):
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1891
    """Sort set by keys. The default sort order is ascending, specify a key
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1892
    as ``-key`` to sort in descending order.
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1893
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1894
    The keys can be:
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1895
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1896
    - ``rev`` for the revision number,
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1897
    - ``branch`` for the branch name,
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1898
    - ``desc`` for the commit message (description),
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1899
    - ``user`` for user name (``author`` can be used as an alias),
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1900
    - ``date`` for the commit date
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1901
    - ``topo`` for a reverse topographical sort
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1902
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1903
    The ``topo`` sort order cannot be combined with other sort keys. This sort
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1904
    takes one optional argument, ``topo.firstbranch``, which takes a revset that
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1905
    specifies what topographical branches to prioritize in the sort.
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1906
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1907
    """
f652e84f23f2 revset: extract function that validates sort() arguments
Yuya Nishihara <yuya@tcha.org>
parents: 29364
diff changeset
  1908
    s, keyflags, opts = _getsortargs(x)
29364
76a1a703e23d revset: build dict of extra sort options before evaluating set
Yuya Nishihara <yuya@tcha.org>
parents: 29363
diff changeset
  1909
    revs = getset(repo, subset, s)
76a1a703e23d revset: build dict of extra sort options before evaluating set
Yuya Nishihara <yuya@tcha.org>
parents: 29363
diff changeset
  1910
29363
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1911
    if not keyflags:
20719
cce8fbedc82a revset: changed sort method to use native sort implementation of smartsets
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20718
diff changeset
  1912
        return revs
29363
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1913
    if len(keyflags) == 1 and keyflags[0][0] == "rev":
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1914
        revs.sort(reverse=keyflags[0][1])
20719
cce8fbedc82a revset: changed sort method to use native sort implementation of smartsets
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20718
diff changeset
  1915
        return revs
29363
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1916
    elif keyflags[0][0] == "topo":
29364
76a1a703e23d revset: build dict of extra sort options before evaluating set
Yuya Nishihara <yuya@tcha.org>
parents: 29363
diff changeset
  1917
        firstbranch = ()
76a1a703e23d revset: build dict of extra sort options before evaluating set
Yuya Nishihara <yuya@tcha.org>
parents: 29363
diff changeset
  1918
        if 'topo.firstbranch' in opts:
76a1a703e23d revset: build dict of extra sort options before evaluating set
Yuya Nishihara <yuya@tcha.org>
parents: 29363
diff changeset
  1919
            firstbranch = getset(repo, subset, opts['topo.firstbranch'])
29348
2188f170f5b6 revset: add new topographical sort
Martijn Pieters <mjpieters@fb.com>
parents: 29347
diff changeset
  1920
        revs = baseset(_toposort(revs, repo.changelog.parentrevs, firstbranch),
2188f170f5b6 revset: add new topographical sort
Martijn Pieters <mjpieters@fb.com>
parents: 29347
diff changeset
  1921
                       istopo=True)
29363
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1922
        if keyflags[0][1]:
29348
2188f170f5b6 revset: add new topographical sort
Martijn Pieters <mjpieters@fb.com>
parents: 29347
diff changeset
  1923
            revs.reverse()
2188f170f5b6 revset: add new topographical sort
Martijn Pieters <mjpieters@fb.com>
parents: 29347
diff changeset
  1924
        return revs
2188f170f5b6 revset: add new topographical sort
Martijn Pieters <mjpieters@fb.com>
parents: 29347
diff changeset
  1925
29001
923fa9e06ea0 revset: make sort() do dumb multi-pass sorting for multiple keys (issue5218)
Yuya Nishihara <yuya@tcha.org>
parents: 28910
diff changeset
  1926
    # sort() is guaranteed to be stable
923fa9e06ea0 revset: make sort() do dumb multi-pass sorting for multiple keys (issue5218)
Yuya Nishihara <yuya@tcha.org>
parents: 28910
diff changeset
  1927
    ctxs = [repo[r] for r in revs]
29363
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1928
    for k, reverse in reversed(keyflags):
2d18c61173f1 revset: build list of (key, reverse) pairs before sorting
Yuya Nishihara <yuya@tcha.org>
parents: 29362
diff changeset
  1929
        ctxs.sort(key=_sortkeyfuncs[k], reverse=reverse)
29001
923fa9e06ea0 revset: make sort() do dumb multi-pass sorting for multiple keys (issue5218)
Yuya Nishihara <yuya@tcha.org>
parents: 28910
diff changeset
  1930
    return baseset([c.rev() for c in ctxs])
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  1931
29348
2188f170f5b6 revset: add new topographical sort
Martijn Pieters <mjpieters@fb.com>
parents: 29347
diff changeset
  1932
def _toposort(revs, parentsfunc, firstbranch=()):
29347
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1933
    """Yield revisions from heads to roots one (topo) branch at a time.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1934
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1935
    This function aims to be used by a graph generator that wishes to minimize
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1936
    the number of parallel branches and their interleaving.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1937
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1938
    Example iteration order (numbers show the "true" order in a changelog):
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1939
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1940
      o  4
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1941
      |
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1942
      o  1
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1943
      |
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1944
      | o  3
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1945
      | |
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1946
      | o  2
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1947
      |/
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1948
      o  0
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1949
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1950
    Note that the ancestors of merges are understood by the current
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1951
    algorithm to be on the same branch. This means no reordering will
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1952
    occur behind a merge.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1953
    """
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1954
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1955
    ### Quick summary of the algorithm
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1956
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1957
    # This function is based around a "retention" principle. We keep revisions
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1958
    # in memory until we are ready to emit a whole branch that immediately
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1959
    # "merges" into an existing one. This reduces the number of parallel
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1960
    # branches with interleaved revisions.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1961
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1962
    # During iteration revs are split into two groups:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1963
    # A) revision already emitted
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1964
    # B) revision in "retention". They are stored as different subgroups.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1965
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1966
    # for each REV, we do the following logic:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1967
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1968
    #   1) if REV is a parent of (A), we will emit it. If there is a
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1969
    #   retention group ((B) above) that is blocked on REV being
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1970
    #   available, we emit all the revisions out of that retention
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1971
    #   group first.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1972
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1973
    #   2) else, we'll search for a subgroup in (B) awaiting for REV to be
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1974
    #   available, if such subgroup exist, we add REV to it and the subgroup is
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1975
    #   now awaiting for REV.parents() to be available.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1976
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1977
    #   3) finally if no such group existed in (B), we create a new subgroup.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1978
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1979
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1980
    # To bootstrap the algorithm, we emit the tipmost revision (which
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1981
    # puts it in group (A) from above).
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1982
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1983
    revs.sort(reverse=True)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1984
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1985
    # Set of parents of revision that have been emitted. They can be considered
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1986
    # unblocked as the graph generator is already aware of them so there is no
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1987
    # need to delay the revisions that reference them.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1988
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1989
    # If someone wants to prioritize a branch over the others, pre-filling this
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1990
    # set will force all other branches to wait until this branch is ready to be
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1991
    # emitted.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1992
    unblocked = set(firstbranch)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1993
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1994
    # list of groups waiting to be displayed, each group is defined by:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1995
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1996
    #   (revs:    lists of revs waiting to be displayed,
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1997
    #    blocked: set of that cannot be displayed before those in 'revs')
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1998
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  1999
    # The second value ('blocked') correspond to parents of any revision in the
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2000
    # group ('revs') that is not itself contained in the group. The main idea
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2001
    # of this algorithm is to delay as much as possible the emission of any
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2002
    # revision.  This means waiting for the moment we are about to display
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2003
    # these parents to display the revs in a group.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2004
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2005
    # This first implementation is smart until it encounters a merge: it will
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2006
    # emit revs as soon as any parent is about to be emitted and can grow an
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2007
    # arbitrary number of revs in 'blocked'. In practice this mean we properly
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2008
    # retains new branches but gives up on any special ordering for ancestors
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2009
    # of merges. The implementation can be improved to handle this better.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2010
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2011
    # The first subgroup is special. It corresponds to all the revision that
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2012
    # were already emitted. The 'revs' lists is expected to be empty and the
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2013
    # 'blocked' set contains the parents revisions of already emitted revision.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2014
    #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2015
    # You could pre-seed the <parents> set of groups[0] to a specific
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2016
    # changesets to select what the first emitted branch should be.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2017
    groups = [([], unblocked)]
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2018
    pendingheap = []
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2019
    pendingset = set()
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2020
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2021
    heapq.heapify(pendingheap)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2022
    heappop = heapq.heappop
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2023
    heappush = heapq.heappush
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2024
    for currentrev in revs:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2025
        # Heap works with smallest element, we want highest so we invert
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2026
        if currentrev not in pendingset:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2027
            heappush(pendingheap, -currentrev)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2028
            pendingset.add(currentrev)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2029
        # iterates on pending rev until after the current rev have been
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2030
        # processed.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2031
        rev = None
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2032
        while rev != currentrev:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2033
            rev = -heappop(pendingheap)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2034
            pendingset.remove(rev)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2035
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2036
            # Seek for a subgroup blocked, waiting for the current revision.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2037
            matching = [i for i, g in enumerate(groups) if rev in g[1]]
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2038
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2039
            if matching:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2040
                # The main idea is to gather together all sets that are blocked
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2041
                # on the same revision.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2042
                #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2043
                # Groups are merged when a common blocking ancestor is
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2044
                # observed. For example, given two groups:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2045
                #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2046
                # revs [5, 4] waiting for 1
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2047
                # revs [3, 2] waiting for 1
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2048
                #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2049
                # These two groups will be merged when we process
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2050
                # 1. In theory, we could have merged the groups when
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2051
                # we added 2 to the group it is now in (we could have
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2052
                # noticed the groups were both blocked on 1 then), but
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2053
                # the way it works now makes the algorithm simpler.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2054
                #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2055
                # We also always keep the oldest subgroup first. We can
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2056
                # probably improve the behavior by having the longest set
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2057
                # first. That way, graph algorithms could minimise the length
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2058
                # of parallel lines their drawing. This is currently not done.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2059
                targetidx = matching.pop(0)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2060
                trevs, tparents = groups[targetidx]
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2061
                for i in matching:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2062
                    gr = groups[i]
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2063
                    trevs.extend(gr[0])
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2064
                    tparents |= gr[1]
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2065
                # delete all merged subgroups (except the one we kept)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2066
                # (starting from the last subgroup for performance and
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2067
                # sanity reasons)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2068
                for i in reversed(matching):
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2069
                    del groups[i]
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2070
            else:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2071
                # This is a new head. We create a new subgroup for it.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2072
                targetidx = len(groups)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2073
                groups.append(([], set([rev])))
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2074
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2075
            gr = groups[targetidx]
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2076
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2077
            # We now add the current nodes to this subgroups. This is done
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2078
            # after the subgroup merging because all elements from a subgroup
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2079
            # that relied on this rev must precede it.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2080
            #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2081
            # we also update the <parents> set to include the parents of the
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2082
            # new nodes.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2083
            if rev == currentrev: # only display stuff in rev
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2084
                gr[0].append(rev)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2085
            gr[1].remove(rev)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2086
            parents = [p for p in parentsfunc(rev) if p > node.nullrev]
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2087
            gr[1].update(parents)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2088
            for p in parents:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2089
                if p not in pendingset:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2090
                    pendingset.add(p)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2091
                    heappush(pendingheap, -p)
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2092
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2093
            # Look for a subgroup to display
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2094
            #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2095
            # When unblocked is empty (if clause), we were not waiting for any
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2096
            # revisions during the first iteration (if no priority was given) or
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2097
            # if we emitted a whole disconnected set of the graph (reached a
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2098
            # root).  In that case we arbitrarily take the oldest known
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2099
            # subgroup. The heuristic could probably be better.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2100
            #
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2101
            # Otherwise (elif clause) if the subgroup is blocked on
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2102
            # a revision we just emitted, we can safely emit it as
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2103
            # well.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2104
            if not unblocked:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2105
                if len(groups) > 1:  # display other subset
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2106
                    targetidx = 1
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2107
                    gr = groups[1]
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2108
            elif not gr[1] & unblocked:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2109
                gr = None
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2110
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2111
            if gr is not None:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2112
                # update the set of awaited revisions with the one from the
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2113
                # subgroup
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2114
                unblocked |= gr[1]
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2115
                # output all revisions in the subgroup
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2116
                for r in gr[0]:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2117
                    yield r
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2118
                # delete the subgroup that you just output
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2119
                # unless it is groups[0] in which case you just empty it.
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2120
                if targetidx:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2121
                    del groups[targetidx]
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2122
                else:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2123
                    gr[0][:] = []
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2124
    # Check if we have some subgroup waiting for revisions we are not going to
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2125
    # iterate over
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2126
    for g in groups:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2127
        for r in g[0]:
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2128
            yield r
98535ad46fc0 revset: move groupbranchiter over from graphmod
Martijn Pieters <mjpieters@fb.com>
parents: 29346
diff changeset
  2129
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  2130
@predicate('subrepo([pattern])')
24446
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2131
def subrepo(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  2132
    """Changesets that add, modify or remove the given subrepo.  If no subrepo
24446
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2133
    pattern is named, any subrepo changes are returned.
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2134
    """
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2135
    # i18n: "subrepo" is a keyword
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2136
    args = getargs(x, 0, 1, _('subrepo takes at most one argument'))
28272
760f9d04842a revset: define "pat" variable unconditionally in subrepo()
Yuya Nishihara <yuya@tcha.org>
parents: 28271
diff changeset
  2137
    pat = None
24446
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2138
    if len(args) != 0:
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2139
        pat = getstring(args[0], _("subrepo requires a pattern"))
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2140
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2141
    m = matchmod.exact(repo.root, repo.root, ['.hgsubstate'])
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2142
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2143
    def submatches(names):
26481
7d132557e44a util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents: 26307
diff changeset
  2144
        k, p, m = util.stringmatcher(pat)
24446
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2145
        for name in names:
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2146
            if m(name):
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2147
                yield name
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2148
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2149
    def matches(x):
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2150
        c = repo[x]
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2151
        s = repo.status(c.p1().node(), c.node(), match=m)
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2152
28272
760f9d04842a revset: define "pat" variable unconditionally in subrepo()
Yuya Nishihara <yuya@tcha.org>
parents: 28271
diff changeset
  2153
        if pat is None:
24446
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2154
            return s.added or s.modified or s.removed
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2155
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2156
        if s.added:
25149
3f0744eeaeaf cleanup: use __builtins__.any instead of util.any
Augie Fackler <augie@google.com>
parents: 25146
diff changeset
  2157
            return any(submatches(c.substate.keys()))
24446
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2158
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2159
        if s.modified:
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2160
            subs = set(c.p1().substate.keys())
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2161
            subs.update(c.substate.keys())
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2162
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2163
            for path in submatches(subs):
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2164
                if c.p1().substate.get(path) != c.substate.get(path):
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2165
                    return True
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2166
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2167
        if s.removed:
25149
3f0744eeaeaf cleanup: use __builtins__.any instead of util.any
Augie Fackler <augie@google.com>
parents: 25146
diff changeset
  2168
            return any(submatches(c.p1().substate.keys()))
24446
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2169
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2170
        return False
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2171
28424
534f968d33e5 revset: add inspection data to all filter() calls
Yuya Nishihara <yuya@tcha.org>
parents: 28423
diff changeset
  2172
    return subset.filter(matches, condrepr=('<subrepo %r>', pat))
24446
582cfcc843c7 revset: add the 'subrepo' symbol
Matt Harbison <matt_harbison@yahoo.com>
parents: 24419
diff changeset
  2173
16823
b23bacb230c9 revset: add pattern matching to the 'user' revset expression
Simon King <simon@simonking.org.uk>
parents: 16822
diff changeset
  2174
def _substringmatcher(pattern):
26481
7d132557e44a util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents: 26307
diff changeset
  2175
    kind, pattern, matcher = util.stringmatcher(pattern)
16823
b23bacb230c9 revset: add pattern matching to the 'user' revset expression
Simon King <simon@simonking.org.uk>
parents: 16822
diff changeset
  2176
    if kind == 'literal':
b23bacb230c9 revset: add pattern matching to the 'user' revset expression
Simon King <simon@simonking.org.uk>
parents: 16822
diff changeset
  2177
        matcher = lambda s: pattern in s
b23bacb230c9 revset: add pattern matching to the 'user' revset expression
Simon King <simon@simonking.org.uk>
parents: 16822
diff changeset
  2178
    return kind, pattern, matcher
16819
5260a9e93113 revset: add helper function for matching strings to patterns
Simon King <simon@simonking.org.uk>
parents: 16803
diff changeset
  2179
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  2180
@predicate('tag([name])', safe=True)
12715
33820dccbea4 revset: rename tagged() to tag() and allow it to take an optional tag name
Augie Fackler <durin42@gmail.com>
parents: 12616
diff changeset
  2181
def tag(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  2182
    """The specified tag by name, or all tagged revisions if no name is given.
20824
c57c9cece645 revset: document the regular expression support for tag(name)
Matt Harbison <matt_harbison@yahoo.com>
parents: 20289
diff changeset
  2183
c57c9cece645 revset: document the regular expression support for tag(name)
Matt Harbison <matt_harbison@yahoo.com>
parents: 20289
diff changeset
  2184
    If `name` starts with `re:`, the remainder of the name is treated as
c57c9cece645 revset: document the regular expression support for tag(name)
Matt Harbison <matt_harbison@yahoo.com>
parents: 20289
diff changeset
  2185
    a regular expression. To match a tag that actually starts with `re:`,
c57c9cece645 revset: document the regular expression support for tag(name)
Matt Harbison <matt_harbison@yahoo.com>
parents: 20289
diff changeset
  2186
    use the prefix `literal:`.
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  2187
    """
12815
079a618ea89d revset: add translator comments to i18n strings
Martin Geisler <mg@lazybytes.net>
parents: 12786
diff changeset
  2188
    # i18n: "tag" is a keyword
12715
33820dccbea4 revset: rename tagged() to tag() and allow it to take an optional tag name
Augie Fackler <durin42@gmail.com>
parents: 12616
diff changeset
  2189
    args = getargs(x, 0, 1, _("tag takes one or no arguments"))
11280
a5eb0bf7e158 revset: add tagged predicate
Matt Mackall <mpm@selenic.com>
parents: 11279
diff changeset
  2190
    cl = repo.changelog
12715
33820dccbea4 revset: rename tagged() to tag() and allow it to take an optional tag name
Augie Fackler <durin42@gmail.com>
parents: 12616
diff changeset
  2191
    if args:
16820
20f55613fb2a revset: add pattern matching to 'tag' revset expression
Simon King <simon@simonking.org.uk>
parents: 16819
diff changeset
  2192
        pattern = getstring(args[0],
20f55613fb2a revset: add pattern matching to 'tag' revset expression
Simon King <simon@simonking.org.uk>
parents: 16819
diff changeset
  2193
                            # i18n: "tag" is a keyword
20f55613fb2a revset: add pattern matching to 'tag' revset expression
Simon King <simon@simonking.org.uk>
parents: 16819
diff changeset
  2194
                            _('the argument to tag must be a string'))
26481
7d132557e44a util: extract stringmatcher() from revset
Matt Harbison <matt_harbison@yahoo.com>
parents: 26307
diff changeset
  2195
        kind, pattern, matcher = util.stringmatcher(pattern)
16820
20f55613fb2a revset: add pattern matching to 'tag' revset expression
Simon King <simon@simonking.org.uk>
parents: 16819
diff changeset
  2196
        if kind == 'literal':
16825
b6ef1395d77f revset: avoid validating all tag nodes for tag(x)
Matt Mackall <mpm@selenic.com>
parents: 16824
diff changeset
  2197
            # avoid resolving all tags
b6ef1395d77f revset: avoid validating all tag nodes for tag(x)
Matt Mackall <mpm@selenic.com>
parents: 16824
diff changeset
  2198
            tn = repo._tagscache.tags.get(pattern, None)
b6ef1395d77f revset: avoid validating all tag nodes for tag(x)
Matt Mackall <mpm@selenic.com>
parents: 16824
diff changeset
  2199
            if tn is None:
23978
eeb5d5ab14a6 revset: raise RepoLookupError to make present() predicate continue the query
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23956
diff changeset
  2200
                raise error.RepoLookupError(_("tag '%s' does not exist")
eeb5d5ab14a6 revset: raise RepoLookupError to make present() predicate continue the query
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23956
diff changeset
  2201
                                            % pattern)
16825
b6ef1395d77f revset: avoid validating all tag nodes for tag(x)
Matt Mackall <mpm@selenic.com>
parents: 16824
diff changeset
  2202
            s = set([repo[tn].rev()])
16820
20f55613fb2a revset: add pattern matching to 'tag' revset expression
Simon King <simon@simonking.org.uk>
parents: 16819
diff changeset
  2203
        else:
20f55613fb2a revset: add pattern matching to 'tag' revset expression
Simon King <simon@simonking.org.uk>
parents: 16819
diff changeset
  2204
            s = set([cl.rev(n) for t, n in repo.tagslist() if matcher(t)])
12715
33820dccbea4 revset: rename tagged() to tag() and allow it to take an optional tag name
Augie Fackler <durin42@gmail.com>
parents: 12616
diff changeset
  2205
    else:
33820dccbea4 revset: rename tagged() to tag() and allow it to take an optional tag name
Augie Fackler <durin42@gmail.com>
parents: 12616
diff changeset
  2206
        s = set([cl.rev(n) for t, n in repo.tagslist() if t != 'tip'])
20367
2ac278aab2b4 revset: added intersection to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20366
diff changeset
  2207
    return subset & s
11280
a5eb0bf7e158 revset: add tagged predicate
Matt Mackall <mpm@selenic.com>
parents: 11279
diff changeset
  2208
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  2209
@predicate('tagged', safe=True)
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  2210
def tagged(repo, subset, x):
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  2211
    return tag(repo, subset, x)
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  2212
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  2213
@predicate('unstable()', safe=True)
17171
9c750c3e4fac obsolete: compute unstable changeset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17170
diff changeset
  2214
def unstable(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  2215
    """Non-obsolete changesets with obsolete ancestors.
17291
2d6bbf87f7b4 revset: minor doc fixes on obsolete related revsets
Patrick Mezard <patrick@mezard.eu>
parents: 17272
diff changeset
  2216
    """
17259
e96ad092fb18 i18n: add/relocate "i18n keyword" comments for i18n messages in revset.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17258
diff changeset
  2217
    # i18n: "unstable" is a keyword
17258
5822345e9e46 revset: use appropriate predicate name in error messages
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17244
diff changeset
  2218
    getargs(x, 0, 0, _("unstable takes no arguments"))
17825
3cc06457f15e obsolete: rename `getobscache` into `getrevs`
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17804
diff changeset
  2219
    unstables = obsmod.getrevs(repo, 'unstable')
20367
2ac278aab2b4 revset: added intersection to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20366
diff changeset
  2220
    return subset & unstables
17171
9c750c3e4fac obsolete: compute unstable changeset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17170
diff changeset
  2221
9c750c3e4fac obsolete: compute unstable changeset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17170
diff changeset
  2222
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  2223
@predicate('user(string)', safe=True)
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  2224
def user(repo, subset, x):
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27517
diff changeset
  2225
    """User name contains string. The match is case-insensitive.
16823
b23bacb230c9 revset: add pattern matching to the 'user' revset expression
Simon King <simon@simonking.org.uk>
parents: 16822
diff changeset
  2226
b23bacb230c9 revset: add pattern matching to the 'user' revset expression
Simon King <simon@simonking.org.uk>
parents: 16822
diff changeset
  2227
    If `string` starts with `re:`, the remainder of the string is treated as
b23bacb230c9 revset: add pattern matching to the 'user' revset expression
Simon King <simon@simonking.org.uk>
parents: 16822
diff changeset
  2228
    a regular expression. To match a user that actually contains `re:`, use
b23bacb230c9 revset: add pattern matching to the 'user' revset expression
Simon King <simon@simonking.org.uk>
parents: 16822
diff changeset
  2229
    the prefix `literal:`.
13359
87f248e78173 bookmarks: move revset support to core
Matt Mackall <mpm@selenic.com>
parents: 13031
diff changeset
  2230
    """
13915
8f81d6f4047f revset: rearrange code so functions are sorted alphabetically
Idan Kamara <idankk86@gmail.com>
parents: 13914
diff changeset
  2231
    return author(repo, subset, x)
13359
87f248e78173 bookmarks: move revset support to core
Matt Mackall <mpm@selenic.com>
parents: 13031
diff changeset
  2232
24777
077683371b7b revset: undocument wdir() until its command outputs get stable
Yuya Nishihara <yuya@tcha.org>
parents: 24722
diff changeset
  2233
# experimental
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  2234
@predicate('wdir', safe=True)
24419
0e41f110e69e revset: add wdir() function to specify workingctx revision by command
Yuya Nishihara <yuya@tcha.org>
parents: 24374
diff changeset
  2235
def wdir(repo, subset, x):
0e41f110e69e revset: add wdir() function to specify workingctx revision by command
Yuya Nishihara <yuya@tcha.org>
parents: 24374
diff changeset
  2236
    # i18n: "wdir" is a keyword
0e41f110e69e revset: add wdir() function to specify workingctx revision by command
Yuya Nishihara <yuya@tcha.org>
parents: 24374
diff changeset
  2237
    getargs(x, 0, 0, _("wdir takes no arguments"))
25765
5e1b0739611c revset: use integer representation of wdir() in revset
Yuya Nishihara <yuya@tcha.org>
parents: 25716
diff changeset
  2238
    if node.wdirrev in subset or isinstance(subset, fullreposet):
5e1b0739611c revset: use integer representation of wdir() in revset
Yuya Nishihara <yuya@tcha.org>
parents: 25716
diff changeset
  2239
        return baseset([node.wdirrev])
24419
0e41f110e69e revset: add wdir() function to specify workingctx revision by command
Yuya Nishihara <yuya@tcha.org>
parents: 24374
diff changeset
  2240
    return baseset()
0e41f110e69e revset: add wdir() function to specify workingctx revision by command
Yuya Nishihara <yuya@tcha.org>
parents: 24374
diff changeset
  2241
15898
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2242
# for internal use
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  2243
@predicate('_list', safe=True)
15898
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2244
def _list(repo, subset, x):
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2245
    s = getstring(x, "internal error")
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2246
    if not s:
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
  2247
        return baseset()
25341
9d6cc87bd507 revset: make internal _list() expression remove duplicated revisions
Yuya Nishihara <yuya@tcha.org>
parents: 25309
diff changeset
  2248
    # remove duplicates here. it's difficult for caller to deduplicate sets
9d6cc87bd507 revset: make internal _list() expression remove duplicated revisions
Yuya Nishihara <yuya@tcha.org>
parents: 25309
diff changeset
  2249
    # because different symbols can point to the same rev.
25344
ceaf04bb14ff revset: add fast path for _list() of integer revisions
Yuya Nishihara <yuya@tcha.org>
parents: 25343
diff changeset
  2250
    cl = repo.changelog
25341
9d6cc87bd507 revset: make internal _list() expression remove duplicated revisions
Yuya Nishihara <yuya@tcha.org>
parents: 25309
diff changeset
  2251
    ls = []
9d6cc87bd507 revset: make internal _list() expression remove duplicated revisions
Yuya Nishihara <yuya@tcha.org>
parents: 25309
diff changeset
  2252
    seen = set()
9d6cc87bd507 revset: make internal _list() expression remove duplicated revisions
Yuya Nishihara <yuya@tcha.org>
parents: 25309
diff changeset
  2253
    for t in s.split('\0'):
25344
ceaf04bb14ff revset: add fast path for _list() of integer revisions
Yuya Nishihara <yuya@tcha.org>
parents: 25343
diff changeset
  2254
        try:
ceaf04bb14ff revset: add fast path for _list() of integer revisions
Yuya Nishihara <yuya@tcha.org>
parents: 25343
diff changeset
  2255
            # fast path for integer revision
ceaf04bb14ff revset: add fast path for _list() of integer revisions
Yuya Nishihara <yuya@tcha.org>
parents: 25343
diff changeset
  2256
            r = int(t)
ceaf04bb14ff revset: add fast path for _list() of integer revisions
Yuya Nishihara <yuya@tcha.org>
parents: 25343
diff changeset
  2257
            if str(r) != t or r not in cl:
ceaf04bb14ff revset: add fast path for _list() of integer revisions
Yuya Nishihara <yuya@tcha.org>
parents: 25343
diff changeset
  2258
                raise ValueError
26143
42bb1812686f revset: fix resolving strings from a list
Durham Goode <durham@fb.com>
parents: 26102
diff changeset
  2259
            revs = [r]
25344
ceaf04bb14ff revset: add fast path for _list() of integer revisions
Yuya Nishihara <yuya@tcha.org>
parents: 25343
diff changeset
  2260
        except ValueError:
26143
42bb1812686f revset: fix resolving strings from a list
Durham Goode <durham@fb.com>
parents: 26102
diff changeset
  2261
            revs = stringset(repo, subset, t)
42bb1812686f revset: fix resolving strings from a list
Durham Goode <durham@fb.com>
parents: 26102
diff changeset
  2262
42bb1812686f revset: fix resolving strings from a list
Durham Goode <durham@fb.com>
parents: 26102
diff changeset
  2263
        for r in revs:
42bb1812686f revset: fix resolving strings from a list
Durham Goode <durham@fb.com>
parents: 26102
diff changeset
  2264
            if r in seen:
42bb1812686f revset: fix resolving strings from a list
Durham Goode <durham@fb.com>
parents: 26102
diff changeset
  2265
                continue
42bb1812686f revset: fix resolving strings from a list
Durham Goode <durham@fb.com>
parents: 26102
diff changeset
  2266
            if (r in subset
42bb1812686f revset: fix resolving strings from a list
Durham Goode <durham@fb.com>
parents: 26102
diff changeset
  2267
                or r == node.nullrev and isinstance(subset, fullreposet)):
42bb1812686f revset: fix resolving strings from a list
Durham Goode <durham@fb.com>
parents: 26102
diff changeset
  2268
                ls.append(r)
42bb1812686f revset: fix resolving strings from a list
Durham Goode <durham@fb.com>
parents: 26102
diff changeset
  2269
            seen.add(r)
25341
9d6cc87bd507 revset: make internal _list() expression remove duplicated revisions
Yuya Nishihara <yuya@tcha.org>
parents: 25309
diff changeset
  2270
    return baseset(ls)
15898
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2271
20566
98024950ade0 revset: added _intlist method to replace _list for %ld
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20552
diff changeset
  2272
# for internal use
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  2273
@predicate('_intlist', safe=True)
20566
98024950ade0 revset: added _intlist method to replace _list for %ld
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20552
diff changeset
  2274
def _intlist(repo, subset, x):
98024950ade0 revset: added _intlist method to replace _list for %ld
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20552
diff changeset
  2275
    s = getstring(x, "internal error")
98024950ade0 revset: added _intlist method to replace _list for %ld
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20552
diff changeset
  2276
    if not s:
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
  2277
        return baseset()
20566
98024950ade0 revset: added _intlist method to replace _list for %ld
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20552
diff changeset
  2278
    ls = [int(r) for r in s.split('\0')]
22876
d52ca940c665 revset-_intlist: remove usage of `set()`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22875
diff changeset
  2279
    s = subset
20566
98024950ade0 revset: added _intlist method to replace _list for %ld
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20552
diff changeset
  2280
    return baseset([r for r in ls if r in s])
98024950ade0 revset: added _intlist method to replace _list for %ld
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20552
diff changeset
  2281
20569
0d4be103c734 revset: added _hexlist method to replace _list for %ln
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20566
diff changeset
  2282
# for internal use
27587
c8dc480142a8 revset: use decorator to mark a predicate as safe
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27586
diff changeset
  2283
@predicate('_hexlist', safe=True)
20569
0d4be103c734 revset: added _hexlist method to replace _list for %ln
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20566
diff changeset
  2284
def _hexlist(repo, subset, x):
0d4be103c734 revset: added _hexlist method to replace _list for %ln
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20566
diff changeset
  2285
    s = getstring(x, "internal error")
0d4be103c734 revset: added _hexlist method to replace _list for %ln
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20566
diff changeset
  2286
    if not s:
22802
1fcd361efaf4 baseset: use default value instead of [] when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22801
diff changeset
  2287
        return baseset()
20569
0d4be103c734 revset: added _hexlist method to replace _list for %ln
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20566
diff changeset
  2288
    cl = repo.changelog
0d4be103c734 revset: added _hexlist method to replace _list for %ln
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20566
diff changeset
  2289
    ls = [cl.rev(node.bin(r)) for r in s.split('\0')]
22877
489d2f3688c9 revset-_hexlist: remove usage of `set()`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22876
diff changeset
  2290
    s = subset
20569
0d4be103c734 revset: added _hexlist method to replace _list for %ln
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20566
diff changeset
  2291
    return baseset([r for r in ls if r in s])
15898
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2292
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2293
methods = {
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2294
    "range": rangeset,
16860
e1aa1ed30030 revset: turn dagrange into a function
Bryan O'Sullivan <bryano@fb.com>
parents: 16859
diff changeset
  2295
    "dagrange": dagrange,
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2296
    "string": stringset,
24932
022282152632 revset: don't error out if tokens parse as existing symbols
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24923
diff changeset
  2297
    "symbol": stringset,
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2298
    "and": andset,
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2299
    "or": orset,
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2300
    "not": notset,
28217
d2ac8b57a75d revset: use smartset minus operator
Durham Goode <durham@fb.com>
parents: 28139
diff changeset
  2301
    "difference": differenceset,
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2302
    "list": listset,
25704
70a2082f855a revset: add parsing rule for key=value pair
Yuya Nishihara <yuya@tcha.org>
parents: 25689
diff changeset
  2303
    "keyvalue": keyvaluepair,
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2304
    "func": func,
14070
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  2305
    "ancestor": ancestorspec,
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  2306
    "parent": parentspec,
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  2307
    "parentpost": p1,
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2308
}
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2309
29117
7828cadd2873 revset: construct arguments of only() against matched tree
Yuya Nishihara <yuya@tcha.org>
parents: 29116
diff changeset
  2310
def _matchonly(revs, bases):
7828cadd2873 revset: construct arguments of only() against matched tree
Yuya Nishihara <yuya@tcha.org>
parents: 29116
diff changeset
  2311
    """
7828cadd2873 revset: construct arguments of only() against matched tree
Yuya Nishihara <yuya@tcha.org>
parents: 29116
diff changeset
  2312
    >>> f = lambda *args: _matchonly(*map(parse, args))
7828cadd2873 revset: construct arguments of only() against matched tree
Yuya Nishihara <yuya@tcha.org>
parents: 29116
diff changeset
  2313
    >>> f('ancestors(A)', 'not ancestors(B)')
7828cadd2873 revset: construct arguments of only() against matched tree
Yuya Nishihara <yuya@tcha.org>
parents: 29116
diff changeset
  2314
    ('list', ('symbol', 'A'), ('symbol', 'B'))
7828cadd2873 revset: construct arguments of only() against matched tree
Yuya Nishihara <yuya@tcha.org>
parents: 29116
diff changeset
  2315
    """
7828cadd2873 revset: construct arguments of only() against matched tree
Yuya Nishihara <yuya@tcha.org>
parents: 29116
diff changeset
  2316
    if (revs is not None
29116
0c9b05dae010 revset: unnest isonly() closure from optimize()
Yuya Nishihara <yuya@tcha.org>
parents: 29098
diff changeset
  2317
        and revs[0] == 'func'
29441
9e8d258708bb revset: check invalid function syntax "func-name"() explicitly
Yuya Nishihara <yuya@tcha.org>
parents: 29425
diff changeset
  2318
        and getsymbol(revs[1]) == 'ancestors'
29116
0c9b05dae010 revset: unnest isonly() closure from optimize()
Yuya Nishihara <yuya@tcha.org>
parents: 29098
diff changeset
  2319
        and bases is not None
0c9b05dae010 revset: unnest isonly() closure from optimize()
Yuya Nishihara <yuya@tcha.org>
parents: 29098
diff changeset
  2320
        and bases[0] == 'not'
0c9b05dae010 revset: unnest isonly() closure from optimize()
Yuya Nishihara <yuya@tcha.org>
parents: 29098
diff changeset
  2321
        and bases[1][0] == 'func'
29441
9e8d258708bb revset: check invalid function syntax "func-name"() explicitly
Yuya Nishihara <yuya@tcha.org>
parents: 29425
diff changeset
  2322
        and getsymbol(bases[1][1]) == 'ancestors'):
29117
7828cadd2873 revset: construct arguments of only() against matched tree
Yuya Nishihara <yuya@tcha.org>
parents: 29116
diff changeset
  2323
        return ('list', revs[2], bases[1][2])
29116
0c9b05dae010 revset: unnest isonly() closure from optimize()
Yuya Nishihara <yuya@tcha.org>
parents: 29098
diff changeset
  2324
29769
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2325
def _fixops(x):
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2326
    """Rewrite raw parsed tree to resolve ambiguous syntax which cannot be
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2327
    handled well by our simple top-down parser"""
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2328
    if not isinstance(x, tuple):
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2329
        return x
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2330
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2331
    op = x[0]
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2332
    if op == 'parent':
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2333
        # x^:y means (x^) : y, not x ^ (:y)
29770
9c51a5de76db revset: also parse x^: as (x^):
Yuya Nishihara <yuya@tcha.org>
parents: 29769
diff changeset
  2334
        # x^:  means (x^) :,   not x ^ (:)
29769
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2335
        post = ('parentpost', x[1])
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2336
        if x[2][0] == 'dagrangepre':
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2337
            return _fixops(('dagrange', post, x[2][1]))
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2338
        elif x[2][0] == 'rangepre':
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2339
            return _fixops(('range', post, x[2][1]))
29770
9c51a5de76db revset: also parse x^: as (x^):
Yuya Nishihara <yuya@tcha.org>
parents: 29769
diff changeset
  2340
        elif x[2][0] == 'rangeall':
9c51a5de76db revset: also parse x^: as (x^):
Yuya Nishihara <yuya@tcha.org>
parents: 29769
diff changeset
  2341
            return _fixops(('rangepost', post))
29769
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2342
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2343
    return (op,) + tuple(_fixops(y) for y in x[1:])
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2344
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2345
def _optimize(x, small):
13031
3da456d0c885 code style: prefer 'is' and 'is not' tests with singletons
Martin Geisler <mg@aragost.com>
parents: 12936
diff changeset
  2346
    if x is None:
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2347
        return 0, x
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2348
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2349
    smallbonus = 1
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2350
    if small:
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2351
        smallbonus = .5
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2352
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2353
    op = x[0]
11283
a6356b2695a3 revset: fix - handling in the optimizer
Matt Mackall <mpm@selenic.com>
parents: 11282
diff changeset
  2354
    if op == 'minus':
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2355
        return _optimize(('and', x[1], ('not', x[2])), small)
23765
537a2669a113 revset: use '%' as an operator for 'only'
Sean Farley <sean.michael.farley@gmail.com>
parents: 23742
diff changeset
  2356
    elif op == 'only':
29118
8c295c3b2ce2 revset: introduce temporary variables in optimize() where they look better
Yuya Nishihara <yuya@tcha.org>
parents: 29117
diff changeset
  2357
        t = ('func', ('symbol', 'only'), ('list', x[1], x[2]))
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2358
        return _optimize(t, small)
25094
8b99e9a8db05 revset: map postfix '%' to only() to optimize operand recursively (issue4670)
Yuya Nishihara <yuya@tcha.org>
parents: 24904
diff changeset
  2359
    elif op == 'onlypost':
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2360
        return _optimize(('func', ('symbol', 'only'), x[1]), small)
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2361
    elif op == 'dagrangepre':
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2362
        return _optimize(('func', ('symbol', 'ancestors'), x[1]), small)
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2363
    elif op == 'dagrangepost':
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2364
        return _optimize(('func', ('symbol', 'descendants'), x[1]), small)
25819
be29d26e2949 revset: parse nullary ":" operator as "0:tip"
Yuya Nishihara <yuya@tcha.org>
parents: 25815
diff changeset
  2365
    elif op == 'rangeall':
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2366
        return _optimize(('range', ('string', '0'), ('string', 'tip')), small)
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2367
    elif op == 'rangepre':
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2368
        return _optimize(('range', ('string', '0'), x[1]), small)
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2369
    elif op == 'rangepost':
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2370
        return _optimize(('range', x[1], ('string', 'tip')), small)
11467
6b836d5c8c9e revset: make negate work for sort specs
Matt Mackall <mpm@selenic.com>
parents: 11456
diff changeset
  2371
    elif op == 'negate':
29118
8c295c3b2ce2 revset: introduce temporary variables in optimize() where they look better
Yuya Nishihara <yuya@tcha.org>
parents: 29117
diff changeset
  2372
        s = getstring(x[1], _("can't negate that"))
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2373
        return _optimize(('string', '-' + s), small)
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2374
    elif op in 'string symbol negate':
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2375
        return smallbonus, x # single revisions are small
16859
eeb464ed7275 revset: drop unreachable code
Bryan O'Sullivan <bryano@fb.com>
parents: 16838
diff changeset
  2376
    elif op == 'and':
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2377
        wa, ta = _optimize(x[1], True)
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2378
        wb, tb = _optimize(x[2], True)
29116
0c9b05dae010 revset: unnest isonly() closure from optimize()
Yuya Nishihara <yuya@tcha.org>
parents: 29098
diff changeset
  2379
        w = min(wa, wb)
20499
2efd608473fb revset: optimize missing ancestor expressions
Siddharth Agarwal <sid0@fb.com>
parents: 20498
diff changeset
  2380
2efd608473fb revset: optimize missing ancestor expressions
Siddharth Agarwal <sid0@fb.com>
parents: 20498
diff changeset
  2381
        # (::x and not ::y)/(not ::y and ::x) have a fast path
29117
7828cadd2873 revset: construct arguments of only() against matched tree
Yuya Nishihara <yuya@tcha.org>
parents: 29116
diff changeset
  2382
        tm = _matchonly(ta, tb) or _matchonly(tb, ta)
7828cadd2873 revset: construct arguments of only() against matched tree
Yuya Nishihara <yuya@tcha.org>
parents: 29116
diff changeset
  2383
        if tm:
7828cadd2873 revset: construct arguments of only() against matched tree
Yuya Nishihara <yuya@tcha.org>
parents: 29116
diff changeset
  2384
            return w, ('func', ('symbol', 'only'), tm)
20499
2efd608473fb revset: optimize missing ancestor expressions
Siddharth Agarwal <sid0@fb.com>
parents: 20498
diff changeset
  2385
28217
d2ac8b57a75d revset: use smartset minus operator
Durham Goode <durham@fb.com>
parents: 28139
diff changeset
  2386
        if tb is not None and tb[0] == 'not':
d2ac8b57a75d revset: use smartset minus operator
Durham Goode <durham@fb.com>
parents: 28139
diff changeset
  2387
            return wa, ('difference', ta, tb[1])
d2ac8b57a75d revset: use smartset minus operator
Durham Goode <durham@fb.com>
parents: 28139
diff changeset
  2388
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2389
        if wa > wb:
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2390
            return w, (op, tb, ta)
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2391
        return w, (op, ta, tb)
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2392
    elif op == 'or':
25343
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2393
        # fast path for machine-generated expression, that is likely to have
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2394
        # lots of trivial revisions: 'a + b + c()' to '_list(a b) + c()'
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2395
        ws, ts, ss = [], [], []
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2396
        def flushss():
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2397
            if not ss:
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2398
                return
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2399
            if len(ss) == 1:
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2400
                w, t = ss[0]
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2401
            else:
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2402
                s = '\0'.join(t[1] for w, t in ss)
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2403
                y = ('func', ('symbol', '_list'), ('string', s))
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2404
                w, t = _optimize(y, False)
25343
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2405
            ws.append(w)
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2406
            ts.append(t)
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2407
            del ss[:]
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2408
        for y in x[1:]:
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2409
            w, t = _optimize(y, False)
25996
b12e00a05d57 revset: prevent crash caused by empty group expression while optimizing "or"
Yuya Nishihara <yuya@tcha.org>
parents: 25995
diff changeset
  2410
            if t is not None and (t[0] == 'string' or t[0] == 'symbol'):
25343
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2411
                ss.append((w, t))
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2412
                continue
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2413
            flushss()
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2414
            ws.append(w)
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2415
            ts.append(t)
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2416
        flushss()
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2417
        if len(ts) == 1:
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2418
            return ws[0], ts[0] # 'or' operation is fully optimized out
25307
4d1e56b29a91 revset: comment that we can't swap 'or' operands by weight
Yuya Nishihara <yuya@tcha.org>
parents: 25270
diff changeset
  2419
        # we can't reorder trees by weight because it would change the order.
4d1e56b29a91 revset: comment that we can't swap 'or' operands by weight
Yuya Nishihara <yuya@tcha.org>
parents: 25270
diff changeset
  2420
        # ("sort(a + b)" == "sort(b + a)", but "a + b" != "b + a")
25309
b333ca94403d revset: reduce nesting of chained 'or' operations (issue4624)
Yuya Nishihara <yuya@tcha.org>
parents: 25308
diff changeset
  2421
        #   ts = tuple(t for w, t in sorted(zip(ws, ts), key=lambda wt: wt[0]))
25343
7fbef7932af9 revset: optimize 'or' operation of trivial revisions to a list
Yuya Nishihara <yuya@tcha.org>
parents: 25342
diff changeset
  2422
        return max(ws), (op,) + tuple(ts)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2423
    elif op == 'not':
25191
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  2424
        # Optimize not public() to _notpublic() because we have a fast version
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  2425
        if x[1] == ('func', ('symbol', 'public'), None):
27637
b502138f5faa cleanup: remove superfluous space after space after equals (python)
timeless <timeless@mozdev.org>
parents: 27587
diff changeset
  2426
            newsym = ('func', ('symbol', '_notpublic'), None)
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2427
            o = _optimize(newsym, not small)
25191
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  2428
            return o[0], o[1]
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  2429
        else:
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2430
            o = _optimize(x[1], not small)
25191
08d1ef09ed37 revset: optimize not public revset
Laurent Charignon <lcharignon@fb.com>
parents: 25149
diff changeset
  2431
            return o[0], (op, o[1])
14070
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  2432
    elif op == 'parentpost':
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2433
        o = _optimize(x[1], small)
14070
305c97670d7a revset: add ^ and ~ operators from parentrevspec extension
Kevin Gessner <kevin@kevingessner.com>
parents: 14061
diff changeset
  2434
        return o[0], (op, o[1])
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2435
    elif op == 'group':
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2436
        return _optimize(x[1], small)
27987
b19d8d5d6b51 revset: flatten chained 'list' operations (aka function args) (issue5072)
Yuya Nishihara <yuya@tcha.org>
parents: 27945
diff changeset
  2437
    elif op in 'dagrange range parent ancestorspec':
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2438
        wa, ta = _optimize(x[1], small)
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2439
        wb, tb = _optimize(x[2], small)
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2440
        return wa + wb, (op, ta, tb)
27987
b19d8d5d6b51 revset: flatten chained 'list' operations (aka function args) (issue5072)
Yuya Nishihara <yuya@tcha.org>
parents: 27945
diff changeset
  2441
    elif op == 'list':
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2442
        ws, ts = zip(*(_optimize(y, small) for y in x[1:]))
27987
b19d8d5d6b51 revset: flatten chained 'list' operations (aka function args) (issue5072)
Yuya Nishihara <yuya@tcha.org>
parents: 27945
diff changeset
  2443
        return sum(ws), (op,) + ts
29766
5004ef47f437 revset: fix keyword arguments to go through optimization process
Yuya Nishihara <yuya@tcha.org>
parents: 29646
diff changeset
  2444
    elif op == 'keyvalue':
5004ef47f437 revset: fix keyword arguments to go through optimization process
Yuya Nishihara <yuya@tcha.org>
parents: 29646
diff changeset
  2445
        w, t = _optimize(x[2], small)
5004ef47f437 revset: fix keyword arguments to go through optimization process
Yuya Nishihara <yuya@tcha.org>
parents: 29646
diff changeset
  2446
        return w, (op, x[1], t)
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2447
    elif op == 'func':
29441
9e8d258708bb revset: check invalid function syntax "func-name"() explicitly
Yuya Nishihara <yuya@tcha.org>
parents: 29425
diff changeset
  2448
        f = getsymbol(x[1])
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2449
        wa, ta = _optimize(x[2], small)
14650
93731b3efd0d revset: add desc(string) to search in commit messages
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14649
diff changeset
  2450
        if f in ("author branch closed date desc file grep keyword "
93731b3efd0d revset: add desc(string) to search in commit messages
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14649
diff changeset
  2451
                 "outgoing user"):
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2452
            w = 10 # slow
12351
b913232d13c1 revsets: reduce cost of outgoing in the optimizer
Matt Mackall <mpm@selenic.com>
parents: 12321
diff changeset
  2453
        elif f in "modifies adds removes":
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2454
            w = 30 # slower
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2455
        elif f == "contains":
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2456
            w = 100 # very slow
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2457
        elif f == "ancestor":
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2458
            w = 1 * smallbonus
22451
186fd06283b4 revset: lower weight for _intlist function
Durham Goode <durham@fb.com>
parents: 22450
diff changeset
  2459
        elif f in "reverse limit first _intlist":
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2460
            w = 0
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2461
        elif f in "sort":
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2462
            w = 10 # assume most sorts look at changelog
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2463
        else:
11279
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2464
            w = 1
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2465
        return w + wa, (op, x[1], ta)
62ccf4cd6e7f revset: optimize the parse tree directly
Matt Mackall <mpm@selenic.com>
parents: 11278
diff changeset
  2466
    return 1, x
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2467
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2468
def optimize(tree):
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2469
    _weight, newtree = _optimize(tree, small=True)
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2470
    return newtree
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2471
23845
0a7fd54d4e60 revset: introduce "_parsealiasdecl" to parse alias declarations strictly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23844
diff changeset
  2472
# the set of valid characters for the initial letter of symbols in
0a7fd54d4e60 revset: introduce "_parsealiasdecl" to parse alias declarations strictly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23844
diff changeset
  2473
# alias declarations and definitions
0a7fd54d4e60 revset: introduce "_parsealiasdecl" to parse alias declarations strictly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23844
diff changeset
  2474
_aliassyminitletters = set(c for c in [chr(i) for i in xrange(256)]
0a7fd54d4e60 revset: introduce "_parsealiasdecl" to parse alias declarations strictly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23844
diff changeset
  2475
                           if c.isalnum() or c in '._@$' or ord(c) > 127)
0a7fd54d4e60 revset: introduce "_parsealiasdecl" to parse alias declarations strictly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23844
diff changeset
  2476
29073
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2477
def _parsewith(spec, lookup=None, syminitletters=None):
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2478
    """Generate a parse tree of given spec with given tokenizing options
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2479
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2480
    >>> _parsewith('foo($1)', syminitletters=_aliassyminitletters)
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2481
    ('func', ('symbol', 'foo'), ('symbol', '$1'))
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2482
    >>> _parsewith('$1')
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2483
    Traceback (most recent call last):
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2484
      ...
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2485
    ParseError: ("syntax error in revset '$1'", 0)
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2486
    >>> _parsewith('foo bar')
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2487
    Traceback (most recent call last):
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2488
      ...
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2489
    ParseError: ('invalid token', 4)
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2490
    """
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2491
    p = parser.parser(elements)
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2492
    tree, pos = p.parse(tokenize(spec, lookup=lookup,
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2493
                                 syminitletters=syminitletters))
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2494
    if pos != len(spec):
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2495
        raise error.ParseError(_('invalid token'), pos)
29769
abe4eecc3253 revset: resolve ambiguity of x^:y before alias expansion
Yuya Nishihara <yuya@tcha.org>
parents: 29767
diff changeset
  2496
    return _fixops(parser.simplifyinfixops(tree, ('list', 'or')))
29073
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2497
28870
475dad3432fd parser: add stub class that will host alias parsing and expansion
Yuya Nishihara <yuya@tcha.org>
parents: 28869
diff changeset
  2498
class _aliasrules(parser.basealiasrules):
475dad3432fd parser: add stub class that will host alias parsing and expansion
Yuya Nishihara <yuya@tcha.org>
parents: 28869
diff changeset
  2499
    """Parsing and expansion rule set of revset aliases"""
475dad3432fd parser: add stub class that will host alias parsing and expansion
Yuya Nishihara <yuya@tcha.org>
parents: 28869
diff changeset
  2500
    _section = _('revset alias')
29074
e7c679738503 revset: define _parsealias() in _aliasrules class
Yuya Nishihara <yuya@tcha.org>
parents: 29073
diff changeset
  2501
e7c679738503 revset: define _parsealias() in _aliasrules class
Yuya Nishihara <yuya@tcha.org>
parents: 29073
diff changeset
  2502
    @staticmethod
e7c679738503 revset: define _parsealias() in _aliasrules class
Yuya Nishihara <yuya@tcha.org>
parents: 29073
diff changeset
  2503
    def _parse(spec):
e7c679738503 revset: define _parsealias() in _aliasrules class
Yuya Nishihara <yuya@tcha.org>
parents: 29073
diff changeset
  2504
        """Parse alias declaration/definition ``spec``
e7c679738503 revset: define _parsealias() in _aliasrules class
Yuya Nishihara <yuya@tcha.org>
parents: 29073
diff changeset
  2505
e7c679738503 revset: define _parsealias() in _aliasrules class
Yuya Nishihara <yuya@tcha.org>
parents: 29073
diff changeset
  2506
        This allows symbol names to use also ``$`` as an initial letter
e7c679738503 revset: define _parsealias() in _aliasrules class
Yuya Nishihara <yuya@tcha.org>
parents: 29073
diff changeset
  2507
        (for backward compatibility), and callers of this function should
e7c679738503 revset: define _parsealias() in _aliasrules class
Yuya Nishihara <yuya@tcha.org>
parents: 29073
diff changeset
  2508
        examine whether ``$`` is used also for unexpected symbols or not.
e7c679738503 revset: define _parsealias() in _aliasrules class
Yuya Nishihara <yuya@tcha.org>
parents: 29073
diff changeset
  2509
        """
e7c679738503 revset: define _parsealias() in _aliasrules class
Yuya Nishihara <yuya@tcha.org>
parents: 29073
diff changeset
  2510
        return _parsewith(spec, syminitletters=_aliassyminitletters)
28910
1203159c8928 parser: factor out _trygetfunc() that extracts function name and arguments
Yuya Nishihara <yuya@tcha.org>
parents: 28898
diff changeset
  2511
1203159c8928 parser: factor out _trygetfunc() that extracts function name and arguments
Yuya Nishihara <yuya@tcha.org>
parents: 28898
diff changeset
  2512
    @staticmethod
1203159c8928 parser: factor out _trygetfunc() that extracts function name and arguments
Yuya Nishihara <yuya@tcha.org>
parents: 28898
diff changeset
  2513
    def _trygetfunc(tree):
1203159c8928 parser: factor out _trygetfunc() that extracts function name and arguments
Yuya Nishihara <yuya@tcha.org>
parents: 28898
diff changeset
  2514
        if tree[0] == 'func' and tree[1][0] == 'symbol':
1203159c8928 parser: factor out _trygetfunc() that extracts function name and arguments
Yuya Nishihara <yuya@tcha.org>
parents: 28898
diff changeset
  2515
            return tree[1][1], getlist(tree[2])
28870
475dad3432fd parser: add stub class that will host alias parsing and expansion
Yuya Nishihara <yuya@tcha.org>
parents: 28869
diff changeset
  2516
28898
8d398155bfda revset: rename findaliases() to expandaliases()
Yuya Nishihara <yuya@tcha.org>
parents: 28895
diff changeset
  2517
def expandaliases(ui, tree, showwarning=None):
28893
ee11167fe1da parser: extract helper that creates a dict of aliases
Yuya Nishihara <yuya@tcha.org>
parents: 28892
diff changeset
  2518
    aliases = _aliasrules.buildmap(ui.configitems('revsetalias'))
28895
4bf9ed7a260e parser: move functions that process alias expansion to rule-set class
Yuya Nishihara <yuya@tcha.org>
parents: 28894
diff changeset
  2519
    tree = _aliasrules.expand(aliases, tree)
23725
6a81f88758aa revset: delay showing parse error for the revset alias until it is referred
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23720
diff changeset
  2520
    if showwarning:
6a81f88758aa revset: delay showing parse error for the revset alias until it is referred
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23720
diff changeset
  2521
        # warn about problematic (but not referred) aliases
6a81f88758aa revset: delay showing parse error for the revset alias until it is referred
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23720
diff changeset
  2522
        for name, alias in sorted(aliases.iteritems()):
6a81f88758aa revset: delay showing parse error for the revset alias until it is referred
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23720
diff changeset
  2523
            if alias.error and not alias.warned:
23844
ddf2172e901d revset: store full detail into revsetalias.error for error source distinction
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23843
diff changeset
  2524
                showwarning(_('warning: %s\n') % (alias.error))
23725
6a81f88758aa revset: delay showing parse error for the revset alias until it is referred
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23720
diff changeset
  2525
                alias.warned = True
6a81f88758aa revset: delay showing parse error for the revset alias until it is referred
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23720
diff changeset
  2526
    return tree
14098
9f5a0acb0056 revset aliases
Alexander Solovyov <alexander@solovyov.net>
parents: 14073
diff changeset
  2527
23742
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2528
def foldconcat(tree):
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2529
    """Fold elements to be concatenated by `##`
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2530
    """
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2531
    if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2532
        return tree
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2533
    if tree[0] == '_concat':
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2534
        pending = [tree]
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2535
        l = []
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2536
        while pending:
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2537
            e = pending.pop()
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2538
            if e[0] == '_concat':
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2539
                pending.extend(reversed(e[1:]))
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2540
            elif e[0] in ('string', 'symbol'):
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2541
                l.append(e[1])
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2542
            else:
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2543
                msg = _("\"##\" can't concatenate \"%s\" element") % (e[0])
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2544
                raise error.ParseError(msg)
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2545
        return ('string', ''.join(l))
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2546
    else:
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2547
        return tuple(foldconcat(t) for t in tree)
14098
9f5a0acb0056 revset aliases
Alexander Solovyov <alexander@solovyov.net>
parents: 14073
diff changeset
  2548
20779
ffc2295c6b80 revset: pass a lookup function to the tokenizer
Matt Mackall <mpm@selenic.com>
parents: 20754
diff changeset
  2549
def parse(spec, lookup=None):
29073
81bac118f9e2 revset: factor out common parsing function
Yuya Nishihara <yuya@tcha.org>
parents: 29072
diff changeset
  2550
    return _parsewith(spec, lookup=lookup)
20779
ffc2295c6b80 revset: pass a lookup function to the tokenizer
Matt Mackall <mpm@selenic.com>
parents: 20754
diff changeset
  2551
24518
dd5e56f08d13 revset: add hook after tree parsing
Laurent Charignon <lcharignon@fb.com>
parents: 24459
diff changeset
  2552
def posttreebuilthook(tree, repo):
dd5e56f08d13 revset: add hook after tree parsing
Laurent Charignon <lcharignon@fb.com>
parents: 24459
diff changeset
  2553
    # hook for extensions to execute code on the optimized tree
dd5e56f08d13 revset: add hook after tree parsing
Laurent Charignon <lcharignon@fb.com>
parents: 24459
diff changeset
  2554
    pass
dd5e56f08d13 revset: add hook after tree parsing
Laurent Charignon <lcharignon@fb.com>
parents: 24459
diff changeset
  2555
20779
ffc2295c6b80 revset: pass a lookup function to the tokenizer
Matt Mackall <mpm@selenic.com>
parents: 20754
diff changeset
  2556
def match(ui, spec, repo=None):
29418
bcefb25acf52 revset: implement match() in terms of matchany()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29408
diff changeset
  2557
    """Create a matcher for a single revision spec."""
bcefb25acf52 revset: implement match() in terms of matchany()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29408
diff changeset
  2558
    return matchany(ui, [spec], repo=repo)
25926
996102be8b91 revset: split post-parsing stage from match()
Yuya Nishihara <yuya@tcha.org>
parents: 25902
diff changeset
  2559
25927
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2560
def matchany(ui, specs, repo=None):
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2561
    """Create a matcher that will include any revisions matching one of the
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2562
    given specs"""
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2563
    if not specs:
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2564
        def mfunc(repo, subset=None):
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2565
            return baseset()
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2566
        return mfunc
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2567
    if not all(specs):
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2568
        raise error.ParseError(_("empty query"))
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2569
    lookup = None
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2570
    if repo:
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2571
        lookup = repo.__contains__
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2572
    if len(specs) == 1:
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2573
        tree = parse(specs[0], lookup)
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2574
    else:
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2575
        tree = ('or',) + tuple(parse(s, lookup) for s in specs)
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2576
    return _makematcher(ui, tree, repo)
44da63623fca revset: add matchany() to construct OR expression from a list of specs
Yuya Nishihara <yuya@tcha.org>
parents: 25926
diff changeset
  2577
25926
996102be8b91 revset: split post-parsing stage from match()
Yuya Nishihara <yuya@tcha.org>
parents: 25902
diff changeset
  2578
def _makematcher(ui, tree, repo):
14900
fc3d6f300d7d revset: allow bypassing alias expansion
Matt Mackall <mpm@selenic.com>
parents: 14851
diff changeset
  2579
    if ui:
28898
8d398155bfda revset: rename findaliases() to expandaliases()
Yuya Nishihara <yuya@tcha.org>
parents: 28895
diff changeset
  2580
        tree = expandaliases(ui, tree, showwarning=ui.warn)
23742
3a4d8a6ce432 revset: introduce new operator "##" to concatenate strings/symbols at runtime
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23729
diff changeset
  2581
    tree = foldconcat(tree)
29119
a032ebea4e97 revset: factor out public optimize() function from recursion
Yuya Nishihara <yuya@tcha.org>
parents: 29118
diff changeset
  2582
    tree = optimize(tree)
24518
dd5e56f08d13 revset: add hook after tree parsing
Laurent Charignon <lcharignon@fb.com>
parents: 24459
diff changeset
  2583
    posttreebuilthook(tree, repo)
24114
fafd9a1284cf revset: make match function initiate query from full set by default
Yuya Nishihara <yuya@tcha.org>
parents: 24031
diff changeset
  2584
    def mfunc(repo, subset=None):
fafd9a1284cf revset: make match function initiate query from full set by default
Yuya Nishihara <yuya@tcha.org>
parents: 24031
diff changeset
  2585
        if subset is None:
24115
ff24af40728b revset: specify fullreposet without using spanset factory
Yuya Nishihara <yuya@tcha.org>
parents: 24114
diff changeset
  2586
            subset = fullreposet(repo)
22885
e02b16f496f2 match: check if an object is a baseset using `isascending` instead of `set`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22884
diff changeset
  2587
        if util.safehasattr(subset, 'isascending'):
22686
2717dcff7be1 revset: use a single return statement in matcher function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22670
diff changeset
  2588
            result = getset(repo, subset, tree)
2717dcff7be1 revset: use a single return statement in matcher function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22670
diff changeset
  2589
        else:
2717dcff7be1 revset: use a single return statement in matcher function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22670
diff changeset
  2590
            result = getset(repo, baseset(subset), tree)
2717dcff7be1 revset: use a single return statement in matcher function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22670
diff changeset
  2591
        return result
11275
c9ce8ecd6ca1 revset: introduce revset core
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
  2592
    return mfunc
12821
165079e564f0 revsets: generate predicate help dynamically
Patrick Mezard <pmezard@gmail.com>
parents: 12815
diff changeset
  2593
14901
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2594
def formatspec(expr, *args):
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2595
    '''
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2596
    This is a convenience function for using revsets internally, and
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2597
    escapes arguments appropriately. Aliases are intentionally ignored
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2598
    so that intended expression behavior isn't accidentally subverted.
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2599
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2600
    Supported arguments:
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2601
15266
8bea39ca9acb revset: add %r for embedded revset support to formatspec
Matt Mackall <mpm@selenic.com>
parents: 15153
diff changeset
  2602
    %r = revset expression, parenthesized
14901
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2603
    %d = int(arg), no quoting
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2604
    %s = string(arg), escaped and single-quoted
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2605
    %b = arg.branch(), escaped and single-quoted
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2606
    %n = hex(arg), single-quoted
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2607
    %% = a literal '%'
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2608
15266
8bea39ca9acb revset: add %r for embedded revset support to formatspec
Matt Mackall <mpm@selenic.com>
parents: 15153
diff changeset
  2609
    Prefixing the type with 'l' specifies a parenthesized list of that type.
15140
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2610
15268
bd5103819c2e revset: fix %r handling in formatspec
Matt Mackall <mpm@selenic.com>
parents: 15266
diff changeset
  2611
    >>> formatspec('%r:: and %lr', '10 or 11', ("this()", "that()"))
bd5103819c2e revset: fix %r handling in formatspec
Matt Mackall <mpm@selenic.com>
parents: 15266
diff changeset
  2612
    '(10 or 11):: and ((this()) or (that()))'
14901
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2613
    >>> formatspec('%d:: and not %d::', 10, 20)
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2614
    '10:: and not 20::'
15325
cdf1daa3b83f revset: deal with empty lists in formatspec
Matt Mackall <mpm@selenic.com>
parents: 15268
diff changeset
  2615
    >>> formatspec('%ld or %ld', [], [1])
15898
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2616
    "_list('') or 1"
14901
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2617
    >>> formatspec('keyword(%s)', 'foo\\xe9')
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2618
    "keyword('foo\\\\xe9')"
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2619
    >>> b = lambda: 'default'
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2620
    >>> b.branch = b
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2621
    >>> formatspec('branch(%b)', b)
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2622
    "branch('default')"
15140
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2623
    >>> formatspec('root(%ls)', ['a', 'b', 'c', 'd'])
15898
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2624
    "root(_list('a\\x00b\\x00c\\x00d'))"
14901
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2625
    '''
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2626
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2627
    def quote(s):
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2628
        return repr(str(s))
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2629
15140
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2630
    def argtype(c, arg):
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2631
        if c == 'd':
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2632
            return str(int(arg))
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2633
        elif c == 's':
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2634
            return quote(arg)
15266
8bea39ca9acb revset: add %r for embedded revset support to formatspec
Matt Mackall <mpm@selenic.com>
parents: 15153
diff changeset
  2635
        elif c == 'r':
8bea39ca9acb revset: add %r for embedded revset support to formatspec
Matt Mackall <mpm@selenic.com>
parents: 15153
diff changeset
  2636
            parse(arg) # make sure syntax errors are confined
8bea39ca9acb revset: add %r for embedded revset support to formatspec
Matt Mackall <mpm@selenic.com>
parents: 15153
diff changeset
  2637
            return '(%s)' % arg
15140
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2638
        elif c == 'n':
16417
b4b0c6931e11 revset: avoid demandimport bug
Matt Mackall <mpm@selenic.com>
parents: 16415
diff changeset
  2639
            return quote(node.hex(arg))
15140
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2640
        elif c == 'b':
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2641
            return quote(arg.branch())
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2642
15595
a585d78e7b2f revset: balance %l or-expressions (issue3129)
Matt Mackall <mpm@selenic.com>
parents: 15532
diff changeset
  2643
    def listexp(s, t):
a585d78e7b2f revset: balance %l or-expressions (issue3129)
Matt Mackall <mpm@selenic.com>
parents: 15532
diff changeset
  2644
        l = len(s)
a585d78e7b2f revset: balance %l or-expressions (issue3129)
Matt Mackall <mpm@selenic.com>
parents: 15532
diff changeset
  2645
        if l == 0:
15898
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2646
            return "_list('')"
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2647
        elif l == 1:
15595
a585d78e7b2f revset: balance %l or-expressions (issue3129)
Matt Mackall <mpm@selenic.com>
parents: 15532
diff changeset
  2648
            return argtype(t, s[0])
15898
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2649
        elif t == 'd':
20566
98024950ade0 revset: added _intlist method to replace _list for %ld
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20552
diff changeset
  2650
            return "_intlist('%s')" % "\0".join(str(int(a)) for a in s)
15898
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2651
        elif t == 's':
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2652
            return "_list('%s')" % "\0".join(s)
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2653
        elif t == 'n':
20569
0d4be103c734 revset: added _hexlist method to replace _list for %ln
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20566
diff changeset
  2654
            return "_hexlist('%s')" % "\0".join(node.hex(a) for a in s)
15898
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2655
        elif t == 'b':
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2656
            return "_list('%s')" % "\0".join(a.branch() for a in s)
6902e13ddd03 revset: optimize building large lists in formatrevspec
Matt Mackall <mpm@selenic.com>
parents: 15837
diff changeset
  2657
15791
a814f8fcc65a Use explicit integer division
Martin Geisler <mg@aragost.com>
parents: 15726
diff changeset
  2658
        m = l // 2
15595
a585d78e7b2f revset: balance %l or-expressions (issue3129)
Matt Mackall <mpm@selenic.com>
parents: 15532
diff changeset
  2659
        return '(%s or %s)' % (listexp(s[:m], t), listexp(s[m:], t))
a585d78e7b2f revset: balance %l or-expressions (issue3129)
Matt Mackall <mpm@selenic.com>
parents: 15532
diff changeset
  2660
14901
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2661
    ret = ''
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2662
    pos = 0
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2663
    arg = 0
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2664
    while pos < len(expr):
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2665
        c = expr[pos]
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2666
        if c == '%':
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2667
            pos += 1
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2668
            d = expr[pos]
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2669
            if d == '%':
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2670
                ret += d
15268
bd5103819c2e revset: fix %r handling in formatspec
Matt Mackall <mpm@selenic.com>
parents: 15266
diff changeset
  2671
            elif d in 'dsnbr':
15140
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2672
                ret += argtype(d, args[arg])
14901
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2673
                arg += 1
15140
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2674
            elif d == 'l':
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2675
                # a list of some type
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2676
                pos += 1
353a1ba928f6 revset: add 'l' flag to formatspec for args
Matt Mackall <mpm@selenic.com>
parents: 15138
diff changeset
  2677
                d = expr[pos]
15596
2555f441a32f merge with stable
Matt Mackall <mpm@selenic.com>
parents: 15595
diff changeset
  2678
                ret += listexp(list(args[arg]), d)
14901
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2679
                arg += 1
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2680
            else:
29389
98e8313dcd9e i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents: 29365
diff changeset
  2681
                raise error.Abort(_('unexpected revspec format character %s')
98e8313dcd9e i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents: 29365
diff changeset
  2682
                                  % d)
14901
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2683
        else:
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2684
            ret += c
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2685
        pos += 1
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2686
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2687
    return ret
a773119f30ba revset: add formatspec convenience query builder
Matt Mackall <mpm@selenic.com>
parents: 14900
diff changeset
  2688
16218
81a1a00f5738 debugrevspec: pretty print output
Patrick Mezard <patrick@mezard.eu>
parents: 16185
diff changeset
  2689
def prettyformat(tree):
25253
3f1a9b44b8c2 parser: move prettyformat() function from revset module
Yuya Nishihara <yuya@tcha.org>
parents: 25251
diff changeset
  2690
    return parser.prettyformat(tree, ('string', 'symbol'))
16218
81a1a00f5738 debugrevspec: pretty print output
Patrick Mezard <patrick@mezard.eu>
parents: 16185
diff changeset
  2691
19719
2f9d5c5256ea revset: add helper function to get revset parse tree depth
Alexander Plavin <alexander@plav.in>
parents: 19706
diff changeset
  2692
def depth(tree):
2f9d5c5256ea revset: add helper function to get revset parse tree depth
Alexander Plavin <alexander@plav.in>
parents: 19706
diff changeset
  2693
    if isinstance(tree, tuple):
2f9d5c5256ea revset: add helper function to get revset parse tree depth
Alexander Plavin <alexander@plav.in>
parents: 19706
diff changeset
  2694
        return max(map(depth, tree)) + 1
2f9d5c5256ea revset: add helper function to get revset parse tree depth
Alexander Plavin <alexander@plav.in>
parents: 19706
diff changeset
  2695
    else:
2f9d5c5256ea revset: add helper function to get revset parse tree depth
Alexander Plavin <alexander@plav.in>
parents: 19706
diff changeset
  2696
        return 0
2f9d5c5256ea revset: add helper function to get revset parse tree depth
Alexander Plavin <alexander@plav.in>
parents: 19706
diff changeset
  2697
19720
f0b992a9be9c revset: add helper function to get functions used in a revset parse tree
Alexander Plavin <alexander@plav.in>
parents: 19719
diff changeset
  2698
def funcsused(tree):
f0b992a9be9c revset: add helper function to get functions used in a revset parse tree
Alexander Plavin <alexander@plav.in>
parents: 19719
diff changeset
  2699
    if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
f0b992a9be9c revset: add helper function to get functions used in a revset parse tree
Alexander Plavin <alexander@plav.in>
parents: 19719
diff changeset
  2700
        return set()
f0b992a9be9c revset: add helper function to get functions used in a revset parse tree
Alexander Plavin <alexander@plav.in>
parents: 19719
diff changeset
  2701
    else:
f0b992a9be9c revset: add helper function to get functions used in a revset parse tree
Alexander Plavin <alexander@plav.in>
parents: 19719
diff changeset
  2702
        funcs = set()
f0b992a9be9c revset: add helper function to get functions used in a revset parse tree
Alexander Plavin <alexander@plav.in>
parents: 19719
diff changeset
  2703
        for s in tree[1:]:
f0b992a9be9c revset: add helper function to get functions used in a revset parse tree
Alexander Plavin <alexander@plav.in>
parents: 19719
diff changeset
  2704
            funcs |= funcsused(s)
f0b992a9be9c revset: add helper function to get functions used in a revset parse tree
Alexander Plavin <alexander@plav.in>
parents: 19719
diff changeset
  2705
        if tree[0] == 'func':
f0b992a9be9c revset: add helper function to get functions used in a revset parse tree
Alexander Plavin <alexander@plav.in>
parents: 19719
diff changeset
  2706
            funcs.add(tree[1][1])
f0b992a9be9c revset: add helper function to get functions used in a revset parse tree
Alexander Plavin <alexander@plav.in>
parents: 19719
diff changeset
  2707
        return funcs
f0b992a9be9c revset: add helper function to get functions used in a revset parse tree
Alexander Plavin <alexander@plav.in>
parents: 19719
diff changeset
  2708
28423
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2709
def _formatsetrepr(r):
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2710
    """Format an optional printable representation of a set
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2711
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2712
    ========  =================================
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2713
    type(r)   example
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2714
    ========  =================================
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2715
    tuple     ('<not %r>', other)
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2716
    str       '<branch closed>'
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2717
    callable  lambda: '<branch %r>' % sorted(b)
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2718
    object    other
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2719
    ========  =================================
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2720
    """
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2721
    if r is None:
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2722
        return ''
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2723
    elif isinstance(r, tuple):
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2724
        return r[0] % r[1:]
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2725
    elif isinstance(r, str):
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2726
        return r
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2727
    elif callable(r):
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2728
        return r()
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2729
    else:
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2730
        return repr(r)
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2731
22692
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2732
class abstractsmartset(object):
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2733
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2734
    def __nonzero__(self):
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2735
        """True if the smartset is not empty"""
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2736
        raise NotImplementedError()
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2737
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2738
    def __contains__(self, rev):
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2739
        """provide fast membership testing"""
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2740
        raise NotImplementedError()
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2741
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2742
    def __iter__(self):
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2743
        """iterate the set in the order it is supposed to be iterated"""
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2744
        raise NotImplementedError()
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2745
22716
6877ba8d85ff abstractsmartset: document the `fastasc` and `fastdesc` attributes/methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22715
diff changeset
  2746
    # Attributes containing a function to perform a fast iteration in a given
6877ba8d85ff abstractsmartset: document the `fastasc` and `fastdesc` attributes/methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22715
diff changeset
  2747
    # direction. A smartset can have none, one, or both defined.
6877ba8d85ff abstractsmartset: document the `fastasc` and `fastdesc` attributes/methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22715
diff changeset
  2748
    #
6877ba8d85ff abstractsmartset: document the `fastasc` and `fastdesc` attributes/methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22715
diff changeset
  2749
    # Default value is None instead of a function returning None to avoid
6877ba8d85ff abstractsmartset: document the `fastasc` and `fastdesc` attributes/methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22715
diff changeset
  2750
    # initializing an iterator just for testing if a fast method exists.
6877ba8d85ff abstractsmartset: document the `fastasc` and `fastdesc` attributes/methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22715
diff changeset
  2751
    fastasc = None
6877ba8d85ff abstractsmartset: document the `fastasc` and `fastdesc` attributes/methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22715
diff changeset
  2752
    fastdesc = None
6877ba8d85ff abstractsmartset: document the `fastasc` and `fastdesc` attributes/methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22715
diff changeset
  2753
22692
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2754
    def isascending(self):
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2755
        """True if the set will iterate in ascending order"""
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2756
        raise NotImplementedError()
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2757
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2758
    def isdescending(self):
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2759
        """True if the set will iterate in descending order"""
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2760
        raise NotImplementedError()
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2761
29346
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2762
    def istopo(self):
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2763
        """True if the set will iterate in topographical order"""
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2764
        raise NotImplementedError()
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2765
26099
ab66c1dee405 revset: cache smartset's min/max
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26095
diff changeset
  2766
    @util.cachefunc
22692
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2767
    def min(self):
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2768
        """return the minimum element in the set"""
22722
e8832cf1abf6 abstractsmartset: add a default implementation for min and max
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22721
diff changeset
  2769
        if self.fastasc is not None:
e8832cf1abf6 abstractsmartset: add a default implementation for min and max
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22721
diff changeset
  2770
            for r in self.fastasc():
e8832cf1abf6 abstractsmartset: add a default implementation for min and max
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22721
diff changeset
  2771
                return r
e8832cf1abf6 abstractsmartset: add a default implementation for min and max
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22721
diff changeset
  2772
            raise ValueError('arg is an empty sequence')
e8832cf1abf6 abstractsmartset: add a default implementation for min and max
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22721
diff changeset
  2773
        return min(self)
22692
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2774
26099
ab66c1dee405 revset: cache smartset's min/max
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26095
diff changeset
  2775
    @util.cachefunc
22692
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2776
    def max(self):
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2777
        """return the maximum element in the set"""
22722
e8832cf1abf6 abstractsmartset: add a default implementation for min and max
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22721
diff changeset
  2778
        if self.fastdesc is not None:
e8832cf1abf6 abstractsmartset: add a default implementation for min and max
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22721
diff changeset
  2779
            for r in self.fastdesc():
e8832cf1abf6 abstractsmartset: add a default implementation for min and max
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22721
diff changeset
  2780
                return r
e8832cf1abf6 abstractsmartset: add a default implementation for min and max
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22721
diff changeset
  2781
            raise ValueError('arg is an empty sequence')
e8832cf1abf6 abstractsmartset: add a default implementation for min and max
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22721
diff changeset
  2782
        return max(self)
22692
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2783
22808
228b0aafb1ce smartset: add first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22805
diff changeset
  2784
    def first(self):
228b0aafb1ce smartset: add first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22805
diff changeset
  2785
        """return the first element in the set (user iteration perspective)
228b0aafb1ce smartset: add first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22805
diff changeset
  2786
228b0aafb1ce smartset: add first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22805
diff changeset
  2787
        Return None if the set is empty"""
228b0aafb1ce smartset: add first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22805
diff changeset
  2788
        raise NotImplementedError()
228b0aafb1ce smartset: add first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22805
diff changeset
  2789
228b0aafb1ce smartset: add first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22805
diff changeset
  2790
    def last(self):
228b0aafb1ce smartset: add first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22805
diff changeset
  2791
        """return the last element in the set (user iteration perspective)
228b0aafb1ce smartset: add first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22805
diff changeset
  2792
228b0aafb1ce smartset: add first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22805
diff changeset
  2793
        Return None if the set is empty"""
228b0aafb1ce smartset: add first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22805
diff changeset
  2794
        raise NotImplementedError()
228b0aafb1ce smartset: add first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22805
diff changeset
  2795
22995
2587631c5f8a revset: make __len__ part of the offical API
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22944
diff changeset
  2796
    def __len__(self):
2587631c5f8a revset: make __len__ part of the offical API
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22944
diff changeset
  2797
        """return the length of the smartsets
2587631c5f8a revset: make __len__ part of the offical API
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22944
diff changeset
  2798
2587631c5f8a revset: make __len__ part of the offical API
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22944
diff changeset
  2799
        This can be expensive on smartset that could be lazy otherwise."""
2587631c5f8a revset: make __len__ part of the offical API
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22944
diff changeset
  2800
        raise NotImplementedError()
2587631c5f8a revset: make __len__ part of the offical API
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22944
diff changeset
  2801
22692
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2802
    def reverse(self):
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2803
        """reverse the expected iteration order"""
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2804
        raise NotImplementedError()
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2805
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2806
    def sort(self, reverse=True):
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2807
        """get the set to iterate in an ascending or descending order"""
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2808
        raise NotImplementedError()
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2809
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2810
    def __and__(self, other):
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2811
        """Returns a new object with the intersection of the two collections.
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2812
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2813
        This is part of the mandatory API for smartset."""
24459
7d369fae098e revset: optimize "x & fullreposet" case
Yuya Nishihara <yuya@tcha.org>
parents: 24458
diff changeset
  2814
        if isinstance(other, fullreposet):
7d369fae098e revset: optimize "x & fullreposet" case
Yuya Nishihara <yuya@tcha.org>
parents: 24458
diff changeset
  2815
            return self
28423
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2816
        return self.filter(other.__contains__, condrepr=other, cache=False)
22692
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2817
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2818
    def __add__(self, other):
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2819
        """Returns a new object with the union of the two collections.
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2820
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2821
        This is part of the mandatory API for smartset."""
22861
546fa6576815 revset: restore order of `or` operation as in Mercurial 2.9
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22860
diff changeset
  2822
        return addset(self, other)
22692
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2823
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2824
    def __sub__(self, other):
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2825
        """Returns a new object with the substraction of the two collections.
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2826
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2827
        This is part of the mandatory API for smartset."""
22730
aeacc2055f0d abstractsmartset: add default implementation for __sub__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22729
diff changeset
  2828
        c = other.__contains__
28423
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2829
        return self.filter(lambda r: not c(r), condrepr=('<not %r>', other),
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2830
                           cache=False)
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2831
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2832
    def filter(self, condition, condrepr=None, cache=True):
22692
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2833
        """Returns this smartset filtered by condition as a new smartset.
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2834
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2835
        `condition` is a callable which takes a revision number and returns a
28423
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2836
        boolean. Optional `condrepr` provides a printable representation of
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2837
        the given `condition`.
22692
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2838
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2839
        This is part of the mandatory API for smartset."""
22864
96b6b3d78697 revset: cache most conditions used in `filter`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22863
diff changeset
  2840
        # builtin cannot be cached. but do not needs to
96b6b3d78697 revset: cache most conditions used in `filter`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22863
diff changeset
  2841
        if cache and util.safehasattr(condition, 'func_code'):
96b6b3d78697 revset: cache most conditions used in `filter`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22863
diff changeset
  2842
            condition = util.cachefunc(condition)
28423
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2843
        return filteredset(self, condition, condrepr)
22692
78c916f24dd9 revset: introduce an abstractsmartset class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22691
diff changeset
  2844
22825
0e8bb81b58b9 baseset: stop inheriting from built-in list class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22817
diff changeset
  2845
class baseset(abstractsmartset):
20416
e72bcc245ecb revset: added docstring to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20393
diff changeset
  2846
    """Basic data structure that represents a revset and contains the basic
e72bcc245ecb revset: added docstring to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20393
diff changeset
  2847
    operation that it should be able to perform.
20727
1e59f760d850 revset: added comments to all methods needed to duck-type from baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20726
diff changeset
  2848
1e59f760d850 revset: added comments to all methods needed to duck-type from baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20726
diff changeset
  2849
    Every method in this class should be implemented by any smartset class.
20416
e72bcc245ecb revset: added docstring to baseset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20393
diff changeset
  2850
    """
29346
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2851
    def __init__(self, data=(), datarepr=None, istopo=False):
28425
02d7faaf455c revset: stub to add extra data to baseset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28424
diff changeset
  2852
        """
02d7faaf455c revset: stub to add extra data to baseset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28424
diff changeset
  2853
        datarepr: a tuple of (format, obj, ...), a function or an object that
02d7faaf455c revset: stub to add extra data to baseset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28424
diff changeset
  2854
                  provides a printable representation of the given data.
02d7faaf455c revset: stub to add extra data to baseset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28424
diff changeset
  2855
        """
28786
69c6e9623bdc revset: force ascending order for baseset initialized from a set
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28785
diff changeset
  2856
        self._ascending = None
29346
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2857
        self._istopo = istopo
22825
0e8bb81b58b9 baseset: stop inheriting from built-in list class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22817
diff changeset
  2858
        if not isinstance(data, list):
26060
4ee2af2194d4 baseset: keep the input set around
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26053
diff changeset
  2859
            if isinstance(data, set):
4ee2af2194d4 baseset: keep the input set around
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26053
diff changeset
  2860
                self._set = data
28786
69c6e9623bdc revset: force ascending order for baseset initialized from a set
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28785
diff changeset
  2861
                # set has no order we pick one for stability purpose
69c6e9623bdc revset: force ascending order for baseset initialized from a set
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28785
diff changeset
  2862
                self._ascending = True
22825
0e8bb81b58b9 baseset: stop inheriting from built-in list class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22817
diff changeset
  2863
            data = list(data)
0e8bb81b58b9 baseset: stop inheriting from built-in list class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22817
diff changeset
  2864
        self._list = data
28425
02d7faaf455c revset: stub to add extra data to baseset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28424
diff changeset
  2865
        self._datarepr = datarepr
20365
bc770ee6a351 revset: implemented set caching for revset evaluation
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20364
diff changeset
  2866
22826
4ffb327e4719 baseset: implement a fastasc and fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22825
diff changeset
  2867
    @util.propertycache
22879
efe5062145c1 baseset: make `_set` a property cache
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22877
diff changeset
  2868
    def _set(self):
efe5062145c1 baseset: make `_set` a property cache
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22877
diff changeset
  2869
        return set(self._list)
efe5062145c1 baseset: make `_set` a property cache
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22877
diff changeset
  2870
efe5062145c1 baseset: make `_set` a property cache
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22877
diff changeset
  2871
    @util.propertycache
22826
4ffb327e4719 baseset: implement a fastasc and fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22825
diff changeset
  2872
    def _asclist(self):
4ffb327e4719 baseset: implement a fastasc and fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22825
diff changeset
  2873
        asclist = self._list[:]
4ffb327e4719 baseset: implement a fastasc and fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22825
diff changeset
  2874
        asclist.sort()
4ffb327e4719 baseset: implement a fastasc and fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22825
diff changeset
  2875
        return asclist
4ffb327e4719 baseset: implement a fastasc and fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22825
diff changeset
  2876
22827
c1107cb21df2 baseset: prepare lazy ordering in __iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22826
diff changeset
  2877
    def __iter__(self):
c1107cb21df2 baseset: prepare lazy ordering in __iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22826
diff changeset
  2878
        if self._ascending is None:
c1107cb21df2 baseset: prepare lazy ordering in __iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22826
diff changeset
  2879
            return iter(self._list)
c1107cb21df2 baseset: prepare lazy ordering in __iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22826
diff changeset
  2880
        elif self._ascending:
c1107cb21df2 baseset: prepare lazy ordering in __iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22826
diff changeset
  2881
            return iter(self._asclist)
c1107cb21df2 baseset: prepare lazy ordering in __iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22826
diff changeset
  2882
        else:
c1107cb21df2 baseset: prepare lazy ordering in __iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22826
diff changeset
  2883
            return reversed(self._asclist)
c1107cb21df2 baseset: prepare lazy ordering in __iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22826
diff changeset
  2884
22826
4ffb327e4719 baseset: implement a fastasc and fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22825
diff changeset
  2885
    def fastasc(self):
4ffb327e4719 baseset: implement a fastasc and fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22825
diff changeset
  2886
        return iter(self._asclist)
4ffb327e4719 baseset: implement a fastasc and fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22825
diff changeset
  2887
4ffb327e4719 baseset: implement a fastasc and fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22825
diff changeset
  2888
    def fastdesc(self):
4ffb327e4719 baseset: implement a fastasc and fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22825
diff changeset
  2889
        return reversed(self._asclist)
4ffb327e4719 baseset: implement a fastasc and fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22825
diff changeset
  2890
22503
300e07582e9b revset: add an optimised baseset.__contains__ (issue4371)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22105
diff changeset
  2891
    @util.propertycache
300e07582e9b revset: add an optimised baseset.__contains__ (issue4371)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22105
diff changeset
  2892
    def __contains__(self):
22880
5b635b44af14 baseset: access `_set` directly for containment check
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22879
diff changeset
  2893
        return self._set.__contains__
22503
300e07582e9b revset: add an optimised baseset.__contains__ (issue4371)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22105
diff changeset
  2894
22691
d8a08b68f754 revset: add a `__nonzero__` to baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22690
diff changeset
  2895
    def __nonzero__(self):
22825
0e8bb81b58b9 baseset: stop inheriting from built-in list class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22817
diff changeset
  2896
        return bool(self._list)
0e8bb81b58b9 baseset: stop inheriting from built-in list class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22817
diff changeset
  2897
0e8bb81b58b9 baseset: stop inheriting from built-in list class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22817
diff changeset
  2898
    def sort(self, reverse=False):
22829
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2899
        self._ascending = not bool(reverse)
29346
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2900
        self._istopo = False
22825
0e8bb81b58b9 baseset: stop inheriting from built-in list class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22817
diff changeset
  2901
0e8bb81b58b9 baseset: stop inheriting from built-in list class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22817
diff changeset
  2902
    def reverse(self):
22829
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2903
        if self._ascending is None:
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2904
            self._list.reverse()
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2905
        else:
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2906
            self._ascending = not self._ascending
29346
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2907
        self._istopo = False
22825
0e8bb81b58b9 baseset: stop inheriting from built-in list class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22817
diff changeset
  2908
0e8bb81b58b9 baseset: stop inheriting from built-in list class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22817
diff changeset
  2909
    def __len__(self):
0e8bb81b58b9 baseset: stop inheriting from built-in list class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22817
diff changeset
  2910
        return len(self._list)
22691
d8a08b68f754 revset: add a `__nonzero__` to baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22690
diff changeset
  2911
20725
cf628b50afbb revset: added isascending and isdescending methods to smartset classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20724
diff changeset
  2912
    def isascending(self):
20727
1e59f760d850 revset: added comments to all methods needed to duck-type from baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20726
diff changeset
  2913
        """Returns True if the collection is ascending order, False if not.
1e59f760d850 revset: added comments to all methods needed to duck-type from baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20726
diff changeset
  2914
1e59f760d850 revset: added comments to all methods needed to duck-type from baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20726
diff changeset
  2915
        This is part of the mandatory API for smartset."""
22863
a1a02b516cca baseset: empty or one-element sets are ascending and descending
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22862
diff changeset
  2916
        if len(self) <= 1:
a1a02b516cca baseset: empty or one-element sets are ascending and descending
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22862
diff changeset
  2917
            return True
22828
966860f7a1a8 baseset: fix isascending and isdescending
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22827
diff changeset
  2918
        return self._ascending is not None and self._ascending
20725
cf628b50afbb revset: added isascending and isdescending methods to smartset classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20724
diff changeset
  2919
cf628b50afbb revset: added isascending and isdescending methods to smartset classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20724
diff changeset
  2920
    def isdescending(self):
20727
1e59f760d850 revset: added comments to all methods needed to duck-type from baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20726
diff changeset
  2921
        """Returns True if the collection is descending order, False if not.
1e59f760d850 revset: added comments to all methods needed to duck-type from baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20726
diff changeset
  2922
1e59f760d850 revset: added comments to all methods needed to duck-type from baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20726
diff changeset
  2923
        This is part of the mandatory API for smartset."""
22863
a1a02b516cca baseset: empty or one-element sets are ascending and descending
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22862
diff changeset
  2924
        if len(self) <= 1:
a1a02b516cca baseset: empty or one-element sets are ascending and descending
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22862
diff changeset
  2925
            return True
22828
966860f7a1a8 baseset: fix isascending and isdescending
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22827
diff changeset
  2926
        return self._ascending is not None and not self._ascending
20725
cf628b50afbb revset: added isascending and isdescending methods to smartset classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20724
diff changeset
  2927
29346
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2928
    def istopo(self):
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2929
        """Is the collection is in topographical order or not.
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2930
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2931
        This is part of the mandatory API for smartset."""
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2932
        if len(self) <= 1:
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2933
            return True
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2934
        return self._istopo
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  2935
22812
fcd12b310148 baseset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22811
diff changeset
  2936
    def first(self):
fcd12b310148 baseset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22811
diff changeset
  2937
        if self:
22829
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2938
            if self._ascending is None:
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2939
                return self._list[0]
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2940
            elif self._ascending:
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2941
                return self._asclist[0]
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2942
            else:
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2943
                return self._asclist[-1]
22812
fcd12b310148 baseset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22811
diff changeset
  2944
        return None
fcd12b310148 baseset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22811
diff changeset
  2945
fcd12b310148 baseset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22811
diff changeset
  2946
    def last(self):
fcd12b310148 baseset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22811
diff changeset
  2947
        if self:
22829
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2948
            if self._ascending is None:
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2949
                return self._list[-1]
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2950
            elif self._ascending:
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2951
                return self._asclist[-1]
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2952
            else:
36e09c25f870 baseset: explicitly track order of the baseset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22828
diff changeset
  2953
                return self._asclist[0]
22812
fcd12b310148 baseset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22811
diff changeset
  2954
        return None
fcd12b310148 baseset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22811
diff changeset
  2955
24457
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  2956
    def __repr__(self):
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  2957
        d = {None: '', False: '-', True: '+'}[self._ascending]
28425
02d7faaf455c revset: stub to add extra data to baseset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28424
diff changeset
  2958
        s = _formatsetrepr(self._datarepr)
02d7faaf455c revset: stub to add extra data to baseset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28424
diff changeset
  2959
        if not s:
28785
87b89dca669d revset: stabilize repr of baseset initialized with a set
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28720
diff changeset
  2960
            l = self._list
87b89dca669d revset: stabilize repr of baseset initialized with a set
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28720
diff changeset
  2961
            # if _list has been built from a set, it might have a different
87b89dca669d revset: stabilize repr of baseset initialized with a set
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28720
diff changeset
  2962
            # order from one python implementation to another.
87b89dca669d revset: stabilize repr of baseset initialized with a set
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28720
diff changeset
  2963
            # We fallback to the sorted version for a stable output.
87b89dca669d revset: stabilize repr of baseset initialized with a set
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28720
diff changeset
  2964
            if self._ascending is not None:
87b89dca669d revset: stabilize repr of baseset initialized with a set
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28720
diff changeset
  2965
                l = self._asclist
87b89dca669d revset: stabilize repr of baseset initialized with a set
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 28720
diff changeset
  2966
            s = repr(l)
28425
02d7faaf455c revset: stub to add extra data to baseset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28424
diff changeset
  2967
        return '<%s%s %s>' % (type(self).__name__, d, s)
24457
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  2968
22726
169aa5e74b52 lazyset: rename the class to filteredset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22725
diff changeset
  2969
class filteredset(abstractsmartset):
20427
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2970
    """Duck type for baseset class which iterates lazily over the revisions in
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2971
    the subset and contains a function which tests for membership in the
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2972
    revset
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2973
    """
28423
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2974
    def __init__(self, subset, condition=lambda x: True, condrepr=None):
20738
33943add5d65 revset: add some documentation for lazyset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20737
diff changeset
  2975
        """
33943add5d65 revset: add some documentation for lazyset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20737
diff changeset
  2976
        condition: a function that decide whether a revision in the subset
33943add5d65 revset: add some documentation for lazyset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20737
diff changeset
  2977
                   belongs to the revset or not.
28423
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2978
        condrepr: a tuple of (format, obj, ...), a function or an object that
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2979
                  provides a printable representation of the given condition.
20738
33943add5d65 revset: add some documentation for lazyset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20737
diff changeset
  2980
        """
20427
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2981
        self._subset = subset
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2982
        self._condition = condition
28423
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  2983
        self._condrepr = condrepr
20427
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2984
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2985
    def __contains__(self, x):
26212
0d8df1f510c6 revset: uncache filteredset.__contains__
Yuya Nishihara <yuya@tcha.org>
parents: 26143
diff changeset
  2986
        return x in self._subset and self._condition(x)
20427
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2987
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2988
    def __iter__(self):
22719
21fda9dcd4e8 lazyset: split the iteration logic from the condition filtering logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22718
diff changeset
  2989
        return self._iterfilter(self._subset)
21fda9dcd4e8 lazyset: split the iteration logic from the condition filtering logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22718
diff changeset
  2990
21fda9dcd4e8 lazyset: split the iteration logic from the condition filtering logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22718
diff changeset
  2991
    def _iterfilter(self, it):
20427
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2992
        cond = self._condition
22719
21fda9dcd4e8 lazyset: split the iteration logic from the condition filtering logic
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22718
diff changeset
  2993
        for x in it:
20427
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2994
            if cond(x):
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2995
                yield x
4a9191ca848e revset: added lazyset class with basic operations
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20424
diff changeset
  2996
22720
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  2997
    @property
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  2998
    def fastasc(self):
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  2999
        it = self._subset.fastasc
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  3000
        if it is None:
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  3001
            return None
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  3002
        return lambda: self._iterfilter(it())
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  3003
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  3004
    @property
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  3005
    def fastdesc(self):
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  3006
        it = self._subset.fastdesc
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  3007
        if it is None:
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  3008
            return None
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  3009
        return lambda: self._iterfilter(it())
4388f99c5512 lazyset: inherit the fastasc and fastdesc method from subset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22719
diff changeset
  3010
20552
0e99a66eb7bc revset: added __nonzero__ method to lazyset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20540
diff changeset
  3011
    def __nonzero__(self):
29304
5e32852fa4bd revset: make filteredset.__nonzero__ respect the order of the filteredset
Kostia Balytskyi <ikostia@fb.com>
parents: 29265
diff changeset
  3012
        fast = None
5e32852fa4bd revset: make filteredset.__nonzero__ respect the order of the filteredset
Kostia Balytskyi <ikostia@fb.com>
parents: 29265
diff changeset
  3013
        candidates = [self.fastasc if self.isascending() else None,
5e32852fa4bd revset: make filteredset.__nonzero__ respect the order of the filteredset
Kostia Balytskyi <ikostia@fb.com>
parents: 29265
diff changeset
  3014
                      self.fastdesc if self.isdescending() else None,
5e32852fa4bd revset: make filteredset.__nonzero__ respect the order of the filteredset
Kostia Balytskyi <ikostia@fb.com>
parents: 29265
diff changeset
  3015
                      self.fastasc,
5e32852fa4bd revset: make filteredset.__nonzero__ respect the order of the filteredset
Kostia Balytskyi <ikostia@fb.com>
parents: 29265
diff changeset
  3016
                      self.fastdesc]
5e32852fa4bd revset: make filteredset.__nonzero__ respect the order of the filteredset
Kostia Balytskyi <ikostia@fb.com>
parents: 29265
diff changeset
  3017
        for candidate in candidates:
5e32852fa4bd revset: make filteredset.__nonzero__ respect the order of the filteredset
Kostia Balytskyi <ikostia@fb.com>
parents: 29265
diff changeset
  3018
            if candidate is not None:
5e32852fa4bd revset: make filteredset.__nonzero__ respect the order of the filteredset
Kostia Balytskyi <ikostia@fb.com>
parents: 29265
diff changeset
  3019
                fast = candidate
5e32852fa4bd revset: make filteredset.__nonzero__ respect the order of the filteredset
Kostia Balytskyi <ikostia@fb.com>
parents: 29265
diff changeset
  3020
                break
5e32852fa4bd revset: make filteredset.__nonzero__ respect the order of the filteredset
Kostia Balytskyi <ikostia@fb.com>
parents: 29265
diff changeset
  3021
26307
428a8747f4ee revset: avoid implicit None testing in revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26306
diff changeset
  3022
        if fast is not None:
26306
d157e1f18e3f revset: speed up existence checks for ordered filtered sets
Durham Goode <durham@fb.com>
parents: 26305
diff changeset
  3023
            it = fast()
26307
428a8747f4ee revset: avoid implicit None testing in revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26306
diff changeset
  3024
        else:
428a8747f4ee revset: avoid implicit None testing in revset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26306
diff changeset
  3025
            it = self
26306
d157e1f18e3f revset: speed up existence checks for ordered filtered sets
Durham Goode <durham@fb.com>
parents: 26305
diff changeset
  3026
d157e1f18e3f revset: speed up existence checks for ordered filtered sets
Durham Goode <durham@fb.com>
parents: 26305
diff changeset
  3027
        for r in it:
20552
0e99a66eb7bc revset: added __nonzero__ method to lazyset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20540
diff changeset
  3028
            return True
0e99a66eb7bc revset: added __nonzero__ method to lazyset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20540
diff changeset
  3029
        return False
0e99a66eb7bc revset: added __nonzero__ method to lazyset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20540
diff changeset
  3030
20429
f5b560c60bcd revset: added operations to duck type baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20428
diff changeset
  3031
    def __len__(self):
f5b560c60bcd revset: added operations to duck type baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20428
diff changeset
  3032
        # Basic implementation to be changed in future patches.
28718
f103f985ac00 revset: prevent infinite recursion on pypy
Maciej Fijalkowski <fijall@gmail.com>
parents: 28717
diff changeset
  3033
        # until this gets improved, we use generator expression
f103f985ac00 revset: prevent infinite recursion on pypy
Maciej Fijalkowski <fijall@gmail.com>
parents: 28717
diff changeset
  3034
        # here, since list compr is free to call __len__ again
f103f985ac00 revset: prevent infinite recursion on pypy
Maciej Fijalkowski <fijall@gmail.com>
parents: 28717
diff changeset
  3035
        # causing infinite recursion
f103f985ac00 revset: prevent infinite recursion on pypy
Maciej Fijalkowski <fijall@gmail.com>
parents: 28717
diff changeset
  3036
        l = baseset(r for r in self)
20429
f5b560c60bcd revset: added operations to duck type baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20428
diff changeset
  3037
        return len(l)
f5b560c60bcd revset: added operations to duck type baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20428
diff changeset
  3038
f5b560c60bcd revset: added operations to duck type baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20428
diff changeset
  3039
    def sort(self, reverse=False):
22862
9e5576f822cc filteredset: drop explicit order management
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22861
diff changeset
  3040
        self._subset.sort(reverse=reverse)
20429
f5b560c60bcd revset: added operations to duck type baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20428
diff changeset
  3041
f5b560c60bcd revset: added operations to duck type baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20428
diff changeset
  3042
    def reverse(self):
f5b560c60bcd revset: added operations to duck type baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20428
diff changeset
  3043
        self._subset.reverse()
f5b560c60bcd revset: added operations to duck type baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20428
diff changeset
  3044
20725
cf628b50afbb revset: added isascending and isdescending methods to smartset classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20724
diff changeset
  3045
    def isascending(self):
22862
9e5576f822cc filteredset: drop explicit order management
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22861
diff changeset
  3046
        return self._subset.isascending()
20725
cf628b50afbb revset: added isascending and isdescending methods to smartset classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20724
diff changeset
  3047
cf628b50afbb revset: added isascending and isdescending methods to smartset classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20724
diff changeset
  3048
    def isdescending(self):
22862
9e5576f822cc filteredset: drop explicit order management
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22861
diff changeset
  3049
        return self._subset.isdescending()
20725
cf628b50afbb revset: added isascending and isdescending methods to smartset classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20724
diff changeset
  3050
29346
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3051
    def istopo(self):
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3052
        return self._subset.istopo()
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3053
22813
5a96df266b2b filteredset: implement `first` and `last`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22812
diff changeset
  3054
    def first(self):
5a96df266b2b filteredset: implement `first` and `last`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22812
diff changeset
  3055
        for x in self:
5a96df266b2b filteredset: implement `first` and `last`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22812
diff changeset
  3056
            return x
5a96df266b2b filteredset: implement `first` and `last`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22812
diff changeset
  3057
        return None
5a96df266b2b filteredset: implement `first` and `last`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22812
diff changeset
  3058
5a96df266b2b filteredset: implement `first` and `last`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22812
diff changeset
  3059
    def last(self):
5a96df266b2b filteredset: implement `first` and `last`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22812
diff changeset
  3060
        it = None
25648
9b9877d2b229 revset: rework 'filteredset.last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25647
diff changeset
  3061
        if self.isascending():
22862
9e5576f822cc filteredset: drop explicit order management
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22861
diff changeset
  3062
            it = self.fastdesc
25648
9b9877d2b229 revset: rework 'filteredset.last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25647
diff changeset
  3063
        elif self.isdescending():
9b9877d2b229 revset: rework 'filteredset.last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25647
diff changeset
  3064
            it = self.fastasc
9b9877d2b229 revset: rework 'filteredset.last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25647
diff changeset
  3065
        if it is not None:
9b9877d2b229 revset: rework 'filteredset.last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25647
diff changeset
  3066
            for x in it():
9b9877d2b229 revset: rework 'filteredset.last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25647
diff changeset
  3067
                return x
9b9877d2b229 revset: rework 'filteredset.last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25647
diff changeset
  3068
            return None #empty case
9b9877d2b229 revset: rework 'filteredset.last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25647
diff changeset
  3069
        else:
9b9877d2b229 revset: rework 'filteredset.last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25647
diff changeset
  3070
            x = None
9b9877d2b229 revset: rework 'filteredset.last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25647
diff changeset
  3071
            for x in self:
9b9877d2b229 revset: rework 'filteredset.last'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25647
diff changeset
  3072
                pass
22813
5a96df266b2b filteredset: implement `first` and `last`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22812
diff changeset
  3073
            return x
5a96df266b2b filteredset: implement `first` and `last`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22812
diff changeset
  3074
24457
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3075
    def __repr__(self):
28423
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  3076
        xs = [repr(self._subset)]
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  3077
        s = _formatsetrepr(self._condrepr)
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  3078
        if s:
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  3079
            xs.append(s)
0d79d91ba7e3 revset: add extra data to filteredset for better inspection
Yuya Nishihara <yuya@tcha.org>
parents: 28395
diff changeset
  3080
        return '<%s %s>' % (type(self).__name__, ', '.join(xs))
24457
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3081
25131
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3082
def _iterordered(ascending, iter1, iter2):
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3083
    """produce an ordered iteration from two iterators with the same order
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3084
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3085
    The ascending is used to indicated the iteration direction.
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3086
    """
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3087
    choice = max
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3088
    if ascending:
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3089
        choice = min
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3090
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3091
    val1 = None
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3092
    val2 = None
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3093
    try:
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3094
        # Consume both iterators in an ordered way until one is empty
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3095
        while True:
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3096
            if val1 is None:
29216
ead25aa27a43 py3: convert to next() function
timeless <timeless@mozdev.org>
parents: 29215
diff changeset
  3097
                val1 = next(iter1)
25131
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3098
            if val2 is None:
29216
ead25aa27a43 py3: convert to next() function
timeless <timeless@mozdev.org>
parents: 29215
diff changeset
  3099
                val2 = next(iter2)
29215
f5983805574e revset: rename variable to avoid shadowing with builtin next() function
timeless <timeless@mozdev.org>
parents: 29147
diff changeset
  3100
            n = choice(val1, val2)
f5983805574e revset: rename variable to avoid shadowing with builtin next() function
timeless <timeless@mozdev.org>
parents: 29147
diff changeset
  3101
            yield n
f5983805574e revset: rename variable to avoid shadowing with builtin next() function
timeless <timeless@mozdev.org>
parents: 29147
diff changeset
  3102
            if val1 == n:
25131
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3103
                val1 = None
29215
f5983805574e revset: rename variable to avoid shadowing with builtin next() function
timeless <timeless@mozdev.org>
parents: 29147
diff changeset
  3104
            if val2 == n:
25131
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3105
                val2 = None
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3106
    except StopIteration:
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3107
        # Flush any remaining values and consume the other one
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3108
        it = iter2
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3109
        if val1 is not None:
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3110
            yield val1
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3111
            it = iter1
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3112
        elif val2 is not None:
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3113
            # might have been equality and both are empty
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3114
            yield val2
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3115
        for val in it:
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3116
            yield val
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3117
22793
ff6689b47e48 addset: drop the leading underscore from the class name
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22792
diff changeset
  3118
class addset(abstractsmartset):
20708
17c89e5a5627 revset: made addset a private class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20707
diff changeset
  3119
    """Represent the addition of two sets
17c89e5a5627 revset: made addset a private class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20707
diff changeset
  3120
17c89e5a5627 revset: made addset a private class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20707
diff changeset
  3121
    Wrapper structure for lazily adding two structures without losing much
20694
621c94378d0d revset: added addset class with its basic methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20693
diff changeset
  3122
    performance on the __contains__ method
20708
17c89e5a5627 revset: made addset a private class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20707
diff changeset
  3123
20712
c152e538b85b revset: added ascending attribute to addset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20711
diff changeset
  3124
    If the ascending attribute is set, that means the two structures are
c152e538b85b revset: added ascending attribute to addset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20711
diff changeset
  3125
    ordered in either an ascending or descending way. Therefore, we can add
21024
7731a2281cf0 spelling: fixes from spell checker
Mads Kiilerich <madski@unity3d.com>
parents: 20991
diff changeset
  3126
    them maintaining the order by iterating over both at the same time
25024
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3127
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3128
    >>> xs = baseset([0, 3, 2])
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3129
    >>> ys = baseset([5, 2, 4])
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3130
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3131
    >>> rs = addset(xs, ys)
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3132
    >>> bool(rs), 0 in rs, 1 in rs, 5 in rs, rs.first(), rs.last()
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3133
    (True, True, False, True, 0, 4)
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3134
    >>> rs = addset(xs, baseset([]))
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3135
    >>> bool(rs), 0 in rs, 1 in rs, rs.first(), rs.last()
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3136
    (True, True, False, 0, 2)
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3137
    >>> rs = addset(baseset([]), baseset([]))
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3138
    >>> bool(rs), 0 in rs, rs.first(), rs.last()
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3139
    (False, False, None, None)
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3140
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3141
    iterate unsorted:
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3142
    >>> rs = addset(xs, ys)
28717
c5f212c8ad78 pypy: fix doctests for pypy optimizations
Maciej Fijalkowski <fijall@gmail.com>
parents: 28708
diff changeset
  3143
    >>> # (use generator because pypy could call len())
c5f212c8ad78 pypy: fix doctests for pypy optimizations
Maciej Fijalkowski <fijall@gmail.com>
parents: 28708
diff changeset
  3144
    >>> list(x for x in rs)  # without _genlist
25024
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3145
    [0, 3, 2, 5, 4]
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3146
    >>> assert not rs._genlist
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3147
    >>> len(rs)
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3148
    5
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3149
    >>> [x for x in rs]  # with _genlist
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3150
    [0, 3, 2, 5, 4]
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3151
    >>> assert rs._genlist
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3152
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3153
    iterate ascending:
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3154
    >>> rs = addset(xs, ys, ascending=True)
28717
c5f212c8ad78 pypy: fix doctests for pypy optimizations
Maciej Fijalkowski <fijall@gmail.com>
parents: 28708
diff changeset
  3155
    >>> # (use generator because pypy could call len())
c5f212c8ad78 pypy: fix doctests for pypy optimizations
Maciej Fijalkowski <fijall@gmail.com>
parents: 28708
diff changeset
  3156
    >>> list(x for x in rs), list(x for x in rs.fastasc())  # without _asclist
25024
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3157
    ([0, 2, 3, 4, 5], [0, 2, 3, 4, 5])
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3158
    >>> assert not rs._asclist
25115
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3159
    >>> len(rs)
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3160
    5
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3161
    >>> [x for x in rs], [x for x in rs.fastasc()]
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3162
    ([0, 2, 3, 4, 5], [0, 2, 3, 4, 5])
25024
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3163
    >>> assert rs._asclist
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3164
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3165
    iterate descending:
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3166
    >>> rs = addset(xs, ys, ascending=False)
28717
c5f212c8ad78 pypy: fix doctests for pypy optimizations
Maciej Fijalkowski <fijall@gmail.com>
parents: 28708
diff changeset
  3167
    >>> # (use generator because pypy could call len())
c5f212c8ad78 pypy: fix doctests for pypy optimizations
Maciej Fijalkowski <fijall@gmail.com>
parents: 28708
diff changeset
  3168
    >>> list(x for x in rs), list(x for x in rs.fastdesc())  # without _asclist
25024
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3169
    ([5, 4, 3, 2, 0], [5, 4, 3, 2, 0])
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3170
    >>> assert not rs._asclist
25115
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3171
    >>> len(rs)
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3172
    5
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3173
    >>> [x for x in rs], [x for x in rs.fastdesc()]
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3174
    ([5, 4, 3, 2, 0], [5, 4, 3, 2, 0])
25024
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3175
    >>> assert rs._asclist
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3176
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3177
    iterate ascending without fastasc:
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3178
    >>> rs = addset(xs, generatorset(ys), ascending=True)
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3179
    >>> assert rs.fastasc is None
25115
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3180
    >>> [x for x in rs]
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3181
    [0, 2, 3, 4, 5]
25024
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3182
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3183
    iterate descending without fastdesc:
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3184
    >>> rs = addset(generatorset(xs), ys, ascending=False)
263bbed1833c revset: test current behavior of addset class
Yuya Nishihara <yuya@tcha.org>
parents: 25023
diff changeset
  3185
    >>> assert rs.fastdesc is None
25115
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3186
    >>> [x for x in rs]
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3187
    [5, 4, 3, 2, 0]
20694
621c94378d0d revset: added addset class with its basic methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20693
diff changeset
  3188
    """
20712
c152e538b85b revset: added ascending attribute to addset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20711
diff changeset
  3189
    def __init__(self, revs1, revs2, ascending=None):
20694
621c94378d0d revset: added addset class with its basic methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20693
diff changeset
  3190
        self._r1 = revs1
621c94378d0d revset: added addset class with its basic methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20693
diff changeset
  3191
        self._r2 = revs2
621c94378d0d revset: added addset class with its basic methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20693
diff changeset
  3192
        self._iter = None
20712
c152e538b85b revset: added ascending attribute to addset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20711
diff changeset
  3193
        self._ascending = ascending
20720
5f8400efca0b revset: added cached generated list to addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20719
diff changeset
  3194
        self._genlist = None
22859
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3195
        self._asclist = None
20720
5f8400efca0b revset: added cached generated list to addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20719
diff changeset
  3196
20845
bc95143446e8 _addset: add a __len__ method
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20833
diff changeset
  3197
    def __len__(self):
bc95143446e8 _addset: add a __len__ method
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20833
diff changeset
  3198
        return len(self._list)
bc95143446e8 _addset: add a __len__ method
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20833
diff changeset
  3199
22743
eccf9907b844 addset: add a __nonzero__ method
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22742
diff changeset
  3200
    def __nonzero__(self):
23100
ac494b087feb revset: fix O(2^n) perf regression in addset
Durham Goode <durham@fb.com>
parents: 23078
diff changeset
  3201
        return bool(self._r1) or bool(self._r2)
22743
eccf9907b844 addset: add a __nonzero__ method
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22742
diff changeset
  3202
20720
5f8400efca0b revset: added cached generated list to addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20719
diff changeset
  3203
    @util.propertycache
5f8400efca0b revset: added cached generated list to addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20719
diff changeset
  3204
    def _list(self):
5f8400efca0b revset: added cached generated list to addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20719
diff changeset
  3205
        if not self._genlist:
25115
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3206
            self._genlist = baseset(iter(self))
20720
5f8400efca0b revset: added cached generated list to addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20719
diff changeset
  3207
        return self._genlist
20694
621c94378d0d revset: added addset class with its basic methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20693
diff changeset
  3208
25115
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3209
    def __iter__(self):
20722
6894223ebc38 revset: changed _iterator() method on addset to work with a given order
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20721
diff changeset
  3210
        """Iterate over both collections without repeating elements
6894223ebc38 revset: changed _iterator() method on addset to work with a given order
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20721
diff changeset
  3211
6894223ebc38 revset: changed _iterator() method on addset to work with a given order
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20721
diff changeset
  3212
        If the ascending attribute is not set, iterate over the first one and
6894223ebc38 revset: changed _iterator() method on addset to work with a given order
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20721
diff changeset
  3213
        then over the second one checking for membership on the first one so we
6894223ebc38 revset: changed _iterator() method on addset to work with a given order
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20721
diff changeset
  3214
        dont yield any duplicates.
6894223ebc38 revset: changed _iterator() method on addset to work with a given order
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20721
diff changeset
  3215
6894223ebc38 revset: changed _iterator() method on addset to work with a given order
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20721
diff changeset
  3216
        If the ascending attribute is set, iterate over both collections at the
6894223ebc38 revset: changed _iterator() method on addset to work with a given order
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20721
diff changeset
  3217
        same time, yielding only one value at a time in the given order.
6894223ebc38 revset: changed _iterator() method on addset to work with a given order
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20721
diff changeset
  3218
        """
22799
bafa371d7af3 addset: drop caching through generatorset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22798
diff changeset
  3219
        if self._ascending is None:
25115
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3220
            if self._genlist:
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3221
                return iter(self._genlist)
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3222
            def arbitraryordergen():
22799
bafa371d7af3 addset: drop caching through generatorset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22798
diff changeset
  3223
                for r in self._r1:
bafa371d7af3 addset: drop caching through generatorset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22798
diff changeset
  3224
                    yield r
22881
057ebf7714e1 addset: drop `.set()` usage during iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22880
diff changeset
  3225
                inr1 = self._r1.__contains__
22799
bafa371d7af3 addset: drop caching through generatorset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22798
diff changeset
  3226
                for r in self._r2:
22881
057ebf7714e1 addset: drop `.set()` usage during iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22880
diff changeset
  3227
                    if not inr1(r):
20694
621c94378d0d revset: added addset class with its basic methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20693
diff changeset
  3228
                        yield r
25115
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3229
            return arbitraryordergen()
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3230
        # try to use our own fast iterator if it exists
22859
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3231
        self._trysetasclist()
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3232
        if self._ascending:
25130
a94ef7f517e0 revset: use fastasc/fastdesc switch consistently in addset.__iter__
Yuya Nishihara <yuya@tcha.org>
parents: 25129
diff changeset
  3233
            attr = 'fastasc'
22859
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3234
        else:
25130
a94ef7f517e0 revset: use fastasc/fastdesc switch consistently in addset.__iter__
Yuya Nishihara <yuya@tcha.org>
parents: 25129
diff changeset
  3235
            attr = 'fastdesc'
a94ef7f517e0 revset: use fastasc/fastdesc switch consistently in addset.__iter__
Yuya Nishihara <yuya@tcha.org>
parents: 25129
diff changeset
  3236
        it = getattr(self, attr)
25115
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3237
        if it is not None:
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3238
            return it()
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3239
        # maybe half of the component supports fast
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3240
        # get iterator for _r1
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3241
        iter1 = getattr(self._r1, attr)
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3242
        if iter1 is None:
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3243
            # let's avoid side effect (not sure it matters)
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3244
            iter1 = iter(sorted(self._r1, reverse=not self._ascending))
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3245
        else:
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3246
            iter1 = iter1()
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3247
        # get iterator for _r2
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3248
        iter2 = getattr(self._r2, attr)
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3249
        if iter2 is None:
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3250
            # let's avoid side effect (not sure it matters)
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3251
            iter2 = iter(sorted(self._r2, reverse=not self._ascending))
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3252
        else:
5548f558db3d revset: fix iteration over ordered addset composed of non-ordered operands
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25105
diff changeset
  3253
            iter2 = iter2()
25131
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3254
        return _iterordered(self._ascending, iter1, iter2)
22859
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3255
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3256
    def _trysetasclist(self):
23139
e53f6b72a0e4 spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 23127
diff changeset
  3257
        """populate the _asclist attribute if possible and necessary"""
22859
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3258
        if self._genlist is not None and self._asclist is None:
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3259
            self._asclist = sorted(self._genlist)
20694
621c94378d0d revset: added addset class with its basic methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20693
diff changeset
  3260
22742
6bbc26adcc6a addset: offer a fastasc and fastdesc methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22741
diff changeset
  3261
    @property
6bbc26adcc6a addset: offer a fastasc and fastdesc methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22741
diff changeset
  3262
    def fastasc(self):
22859
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3263
        self._trysetasclist()
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3264
        if self._asclist is not None:
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3265
            return self._asclist.__iter__
22742
6bbc26adcc6a addset: offer a fastasc and fastdesc methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22741
diff changeset
  3266
        iter1 = self._r1.fastasc
6bbc26adcc6a addset: offer a fastasc and fastdesc methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22741
diff changeset
  3267
        iter2 = self._r2.fastasc
6bbc26adcc6a addset: offer a fastasc and fastdesc methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22741
diff changeset
  3268
        if None in (iter1, iter2):
6bbc26adcc6a addset: offer a fastasc and fastdesc methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22741
diff changeset
  3269
            return None
25131
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3270
        return lambda: _iterordered(True, iter1(), iter2())
22742
6bbc26adcc6a addset: offer a fastasc and fastdesc methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22741
diff changeset
  3271
6bbc26adcc6a addset: offer a fastasc and fastdesc methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22741
diff changeset
  3272
    @property
6bbc26adcc6a addset: offer a fastasc and fastdesc methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22741
diff changeset
  3273
    def fastdesc(self):
22859
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3274
        self._trysetasclist()
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3275
        if self._asclist is not None:
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3276
            return self._asclist.__reversed__
22742
6bbc26adcc6a addset: offer a fastasc and fastdesc methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22741
diff changeset
  3277
        iter1 = self._r1.fastdesc
6bbc26adcc6a addset: offer a fastasc and fastdesc methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22741
diff changeset
  3278
        iter2 = self._r2.fastdesc
6bbc26adcc6a addset: offer a fastasc and fastdesc methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22741
diff changeset
  3279
        if None in (iter1, iter2):
6bbc26adcc6a addset: offer a fastasc and fastdesc methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22741
diff changeset
  3280
            return None
25131
adfe4d9680bf revset: extract addset._iterordered to free function
Yuya Nishihara <yuya@tcha.org>
parents: 25130
diff changeset
  3281
        return lambda: _iterordered(False, iter1(), iter2())
22741
ef2c1ea8fb2c addset: split simple and ordered iteration
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22740
diff changeset
  3282
20694
621c94378d0d revset: added addset class with its basic methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20693
diff changeset
  3283
    def __contains__(self, x):
621c94378d0d revset: added addset class with its basic methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20693
diff changeset
  3284
        return x in self._r1 or x in self._r2
621c94378d0d revset: added addset class with its basic methods
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20693
diff changeset
  3285
20724
e9a64b3f2925 revset: added sort method in addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20723
diff changeset
  3286
    def sort(self, reverse=False):
e9a64b3f2925 revset: added sort method in addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20723
diff changeset
  3287
        """Sort the added set
e9a64b3f2925 revset: added sort method in addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20723
diff changeset
  3288
e9a64b3f2925 revset: added sort method in addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20723
diff changeset
  3289
        For this we use the cached list with all the generated values and if we
e9a64b3f2925 revset: added sort method in addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20723
diff changeset
  3290
        know they are ascending or descending we can sort them in a smart way.
e9a64b3f2925 revset: added sort method in addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20723
diff changeset
  3291
        """
22859
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3292
        self._ascending = not reverse
20724
e9a64b3f2925 revset: added sort method in addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20723
diff changeset
  3293
20733
adf4ec7e6f60 revset: added isascending and isdescending methods to _addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20732
diff changeset
  3294
    def isascending(self):
adf4ec7e6f60 revset: added isascending and isdescending methods to _addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20732
diff changeset
  3295
        return self._ascending is not None and self._ascending
adf4ec7e6f60 revset: added isascending and isdescending methods to _addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20732
diff changeset
  3296
adf4ec7e6f60 revset: added isascending and isdescending methods to _addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20732
diff changeset
  3297
    def isdescending(self):
adf4ec7e6f60 revset: added isascending and isdescending methods to _addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20732
diff changeset
  3298
        return self._ascending is not None and not self._ascending
adf4ec7e6f60 revset: added isascending and isdescending methods to _addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20732
diff changeset
  3299
29346
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3300
    def istopo(self):
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3301
        # not worth the trouble asserting if the two sets combined are still
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3302
        # in topographical order. Use the sort() predicate to explicitly sort
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3303
        # again instead.
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3304
        return False
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3305
20723
fb9852c46a42 revset: added reverse method to addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20722
diff changeset
  3306
    def reverse(self):
22859
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3307
        if self._ascending is None:
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3308
            self._list.reverse()
513c0ba61db8 addset: do lazy sorting
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22857
diff changeset
  3309
        else:
20723
fb9852c46a42 revset: added reverse method to addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20722
diff changeset
  3310
            self._ascending = not self._ascending
fb9852c46a42 revset: added reverse method to addset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20722
diff changeset
  3311
22810
7f97cb12782f addset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22809
diff changeset
  3312
    def first(self):
23127
7361d8244efb addset: fix `first` and `last` on sorted addset (issue4426)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23115
diff changeset
  3313
        for x in self:
7361d8244efb addset: fix `first` and `last` on sorted addset (issue4426)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23115
diff changeset
  3314
            return x
22810
7f97cb12782f addset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22809
diff changeset
  3315
        return None
7f97cb12782f addset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22809
diff changeset
  3316
7f97cb12782f addset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22809
diff changeset
  3317
    def last(self):
23127
7361d8244efb addset: fix `first` and `last` on sorted addset (issue4426)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23115
diff changeset
  3318
        self.reverse()
7361d8244efb addset: fix `first` and `last` on sorted addset (issue4426)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23115
diff changeset
  3319
        val = self.first()
7361d8244efb addset: fix `first` and `last` on sorted addset (issue4426)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23115
diff changeset
  3320
        self.reverse()
7361d8244efb addset: fix `first` and `last` on sorted addset (issue4426)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23115
diff changeset
  3321
        return val
22810
7f97cb12782f addset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22809
diff changeset
  3322
24457
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3323
    def __repr__(self):
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3324
        d = {None: '', False: '-', True: '+'}[self._ascending]
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3325
        return '<%s%s %r, %r>' % (type(self).__name__, d, self._r1, self._r2)
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3326
22795
c21342159fad generatorset: drop the leading underscore in the class name
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22794
diff changeset
  3327
class generatorset(abstractsmartset):
20705
9cc2249a9461 revset: made generatorset a private class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20703
diff changeset
  3328
    """Wrap a generator for lazy iteration
9cc2249a9461 revset: made generatorset a private class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20703
diff changeset
  3329
9cc2249a9461 revset: made generatorset a private class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20703
diff changeset
  3330
    Wrapper structure for generators that provides lazy membership and can
20540
fa16c710a3d8 revset: added cached generated list on generatorset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20538
diff changeset
  3331
    be iterated more than once.
fa16c710a3d8 revset: added cached generated list on generatorset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20538
diff changeset
  3332
    When asked for membership it generates values until either it finds the
fa16c710a3d8 revset: added cached generated list on generatorset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20538
diff changeset
  3333
    requested one or has gone through all the elements in the generator
fa16c710a3d8 revset: added cached generated list on generatorset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20538
diff changeset
  3334
    """
22755
f9a825b593be generatorset: make it possible to use gen as fastasc or fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22754
diff changeset
  3335
    def __init__(self, gen, iterasc=None):
20739
1b4f2399f3c4 revset: add documentation and comment for _generatorset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20738
diff changeset
  3336
        """
1b4f2399f3c4 revset: add documentation and comment for _generatorset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20738
diff changeset
  3337
        gen: a generator producing the values for the generatorset.
1b4f2399f3c4 revset: add documentation and comment for _generatorset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20738
diff changeset
  3338
        """
20536
532b114a6e02 revset: added generatorset class with cached __contains__ method
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20534
diff changeset
  3339
        self._gen = gen
22798
586d7058ea48 generatorset: get list-based fast iterations after the generator is consumed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22797
diff changeset
  3340
        self._asclist = None
20536
532b114a6e02 revset: added generatorset class with cached __contains__ method
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20534
diff changeset
  3341
        self._cache = {}
22796
43f664bf2e1e generatorset: stop using a base as the _genlist
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22795
diff changeset
  3342
        self._genlist = []
20703
e07b1fd30805 revset: added sort methods to generatorsets
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20695
diff changeset
  3343
        self._finished = False
22800
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3344
        self._ascending = True
22755
f9a825b593be generatorset: make it possible to use gen as fastasc or fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22754
diff changeset
  3345
        if iterasc is not None:
f9a825b593be generatorset: make it possible to use gen as fastasc or fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22754
diff changeset
  3346
            if iterasc:
22797
61ecabeeadb3 generatorset: move iteration code into _iterator
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22796
diff changeset
  3347
                self.fastasc = self._iterator
22757
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3348
                self.__contains__ = self._asccontains
22755
f9a825b593be generatorset: make it possible to use gen as fastasc or fastdesc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22754
diff changeset
  3349
            else:
22797
61ecabeeadb3 generatorset: move iteration code into _iterator
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22796
diff changeset
  3350
                self.fastdesc = self._iterator
22757
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3351
                self.__contains__ = self._desccontains
20540
fa16c710a3d8 revset: added cached generated list on generatorset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20538
diff changeset
  3352
22739
4ed47a1b2d24 generatorset: implement __nonzero__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22738
diff changeset
  3353
    def __nonzero__(self):
24936
2aa94b6fe51c revset: make generatorset.__nonzero__ lazy
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24932
diff changeset
  3354
        # Do not use 'for r in self' because it will enforce the iteration
2aa94b6fe51c revset: make generatorset.__nonzero__ lazy
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24932
diff changeset
  3355
        # order (default ascending), possibly unrolling a whole descending
2aa94b6fe51c revset: make generatorset.__nonzero__ lazy
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24932
diff changeset
  3356
        # iterator.
2aa94b6fe51c revset: make generatorset.__nonzero__ lazy
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24932
diff changeset
  3357
        if self._genlist:
2aa94b6fe51c revset: make generatorset.__nonzero__ lazy
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24932
diff changeset
  3358
            return True
2aa94b6fe51c revset: make generatorset.__nonzero__ lazy
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24932
diff changeset
  3359
        for r in self._consumegen():
22739
4ed47a1b2d24 generatorset: implement __nonzero__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22738
diff changeset
  3360
            return True
4ed47a1b2d24 generatorset: implement __nonzero__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22738
diff changeset
  3361
        return False
4ed47a1b2d24 generatorset: implement __nonzero__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22738
diff changeset
  3362
20536
532b114a6e02 revset: added generatorset class with cached __contains__ method
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20534
diff changeset
  3363
    def __contains__(self, x):
532b114a6e02 revset: added generatorset class with cached __contains__ method
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20534
diff changeset
  3364
        if x in self._cache:
532b114a6e02 revset: added generatorset class with cached __contains__ method
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20534
diff changeset
  3365
            return self._cache[x]
532b114a6e02 revset: added generatorset class with cached __contains__ method
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20534
diff changeset
  3366
20828
3210b7930899 revset: improve performance of _generatorset.__contains__ (issue 4201)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20780
diff changeset
  3367
        # Use new values only, as existing values would be cached.
3210b7930899 revset: improve performance of _generatorset.__contains__ (issue 4201)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20780
diff changeset
  3368
        for l in self._consumegen():
20634
f2bb7ba59456 revset: changed generatorset code to remove unnecesary function call
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20613
diff changeset
  3369
            if l == x:
f2bb7ba59456 revset: changed generatorset code to remove unnecesary function call
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20613
diff changeset
  3370
                return True
20536
532b114a6e02 revset: added generatorset class with cached __contains__ method
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20534
diff changeset
  3371
532b114a6e02 revset: added generatorset class with cached __contains__ method
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20534
diff changeset
  3372
        self._cache[x] = False
532b114a6e02 revset: added generatorset class with cached __contains__ method
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20534
diff changeset
  3373
        return False
532b114a6e02 revset: added generatorset class with cached __contains__ method
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20534
diff changeset
  3374
22757
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3375
    def _asccontains(self, x):
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3376
        """version of contains optimised for ascending generator"""
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3377
        if x in self._cache:
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3378
            return self._cache[x]
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3379
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3380
        # Use new values only, as existing values would be cached.
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3381
        for l in self._consumegen():
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3382
            if l == x:
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3383
                return True
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3384
            if l > x:
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3385
                break
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3386
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3387
        self._cache[x] = False
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3388
        return False
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3389
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3390
    def _desccontains(self, x):
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3391
        """version of contains optimised for descending generator"""
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3392
        if x in self._cache:
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3393
            return self._cache[x]
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3394
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3395
        # Use new values only, as existing values would be cached.
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3396
        for l in self._consumegen():
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3397
            if l == x:
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3398
                return True
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3399
            if l < x:
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3400
                break
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3401
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3402
        self._cache[x] = False
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3403
        return False
d34969a4b1a8 generatorset: move membership testing on ordered gen to the main class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22756
diff changeset
  3404
20536
532b114a6e02 revset: added generatorset class with cached __contains__ method
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20534
diff changeset
  3405
    def __iter__(self):
22800
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3406
        if self._ascending:
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3407
            it = self.fastasc
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3408
        else:
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3409
            it = self.fastdesc
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3410
        if it is not None:
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3411
            return it()
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3412
        # we need to consume the iterator
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3413
        for x in self._consumegen():
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3414
            pass
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3415
        # recall the same code
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3416
        return iter(self)
22797
61ecabeeadb3 generatorset: move iteration code into _iterator
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22796
diff changeset
  3417
61ecabeeadb3 generatorset: move iteration code into _iterator
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22796
diff changeset
  3418
    def _iterator(self):
20833
47d43e2323c5 revset: fix generatorset race condition
Durham Goode <durham@fb.com>
parents: 20829
diff changeset
  3419
        if self._finished:
22670
44dce874de97 revset: rely on built in iterator when possible in _generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22669
diff changeset
  3420
            return iter(self._genlist)
20833
47d43e2323c5 revset: fix generatorset race condition
Durham Goode <durham@fb.com>
parents: 20829
diff changeset
  3421
22494
14f6cebfcb8a revset: document the choice made in __generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22487
diff changeset
  3422
        # We have to use this complex iteration strategy to allow multiple
14f6cebfcb8a revset: document the choice made in __generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22487
diff changeset
  3423
        # iterations at the same time. We need to be able to catch revision
23139
e53f6b72a0e4 spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 23127
diff changeset
  3424
        # removed from _consumegen and added to genlist in another instance.
22494
14f6cebfcb8a revset: document the choice made in __generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22487
diff changeset
  3425
        #
14f6cebfcb8a revset: document the choice made in __generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22487
diff changeset
  3426
        # Getting rid of it would provide an about 15% speed up on this
14f6cebfcb8a revset: document the choice made in __generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22487
diff changeset
  3427
        # iteration.
20833
47d43e2323c5 revset: fix generatorset race condition
Durham Goode <durham@fb.com>
parents: 20829
diff changeset
  3428
        genlist = self._genlist
22669
00c8abe64cf3 revset: prefetch an attribute in _generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22539
diff changeset
  3429
        nextrev = self._consumegen().next
00c8abe64cf3 revset: prefetch an attribute in _generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22539
diff changeset
  3430
        _len = len # cache global lookup
22670
44dce874de97 revset: rely on built in iterator when possible in _generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22669
diff changeset
  3431
        def gen():
44dce874de97 revset: rely on built in iterator when possible in _generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22669
diff changeset
  3432
            i = 0
44dce874de97 revset: rely on built in iterator when possible in _generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22669
diff changeset
  3433
            while True:
44dce874de97 revset: rely on built in iterator when possible in _generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22669
diff changeset
  3434
                if i < _len(genlist):
44dce874de97 revset: rely on built in iterator when possible in _generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22669
diff changeset
  3435
                    yield genlist[i]
44dce874de97 revset: rely on built in iterator when possible in _generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22669
diff changeset
  3436
                else:
44dce874de97 revset: rely on built in iterator when possible in _generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22669
diff changeset
  3437
                    yield nextrev()
44dce874de97 revset: rely on built in iterator when possible in _generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22669
diff changeset
  3438
                i += 1
44dce874de97 revset: rely on built in iterator when possible in _generatorset.__iter__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22669
diff changeset
  3439
        return gen()
20828
3210b7930899 revset: improve performance of _generatorset.__contains__ (issue 4201)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20780
diff changeset
  3440
3210b7930899 revset: improve performance of _generatorset.__contains__ (issue 4201)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20780
diff changeset
  3441
    def _consumegen(self):
22528
b6dc3b79bb25 revset: avoid in loop lookup in _generatorset._consumegen
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22527
diff changeset
  3442
        cache = self._cache
b6dc3b79bb25 revset: avoid in loop lookup in _generatorset._consumegen
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22527
diff changeset
  3443
        genlist = self._genlist.append
20634
f2bb7ba59456 revset: changed generatorset code to remove unnecesary function call
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20613
diff changeset
  3444
        for item in self._gen:
22528
b6dc3b79bb25 revset: avoid in loop lookup in _generatorset._consumegen
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22527
diff changeset
  3445
            cache[item] = True
b6dc3b79bb25 revset: avoid in loop lookup in _generatorset._consumegen
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22527
diff changeset
  3446
            genlist(item)
20634
f2bb7ba59456 revset: changed generatorset code to remove unnecesary function call
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20613
diff changeset
  3447
            yield item
22798
586d7058ea48 generatorset: get list-based fast iterations after the generator is consumed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22797
diff changeset
  3448
        if not self._finished:
586d7058ea48 generatorset: get list-based fast iterations after the generator is consumed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22797
diff changeset
  3449
            self._finished = True
586d7058ea48 generatorset: get list-based fast iterations after the generator is consumed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22797
diff changeset
  3450
            asc = self._genlist[:]
586d7058ea48 generatorset: get list-based fast iterations after the generator is consumed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22797
diff changeset
  3451
            asc.sort()
586d7058ea48 generatorset: get list-based fast iterations after the generator is consumed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22797
diff changeset
  3452
            self._asclist = asc
586d7058ea48 generatorset: get list-based fast iterations after the generator is consumed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22797
diff changeset
  3453
            self.fastasc = asc.__iter__
586d7058ea48 generatorset: get list-based fast iterations after the generator is consumed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22797
diff changeset
  3454
            self.fastdesc = asc.__reversed__
20703
e07b1fd30805 revset: added sort methods to generatorsets
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20695
diff changeset
  3455
22996
a43d929d1fa1 generatorset: implement __len__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22995
diff changeset
  3456
    def __len__(self):
a43d929d1fa1 generatorset: implement __len__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22995
diff changeset
  3457
        for x in self._consumegen():
a43d929d1fa1 generatorset: implement __len__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22995
diff changeset
  3458
            pass
a43d929d1fa1 generatorset: implement __len__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22995
diff changeset
  3459
        return len(self._genlist)
a43d929d1fa1 generatorset: implement __len__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22995
diff changeset
  3460
20703
e07b1fd30805 revset: added sort methods to generatorsets
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20695
diff changeset
  3461
    def sort(self, reverse=False):
22800
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3462
        self._ascending = not reverse
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3463
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3464
    def reverse(self):
29a8c20e14a8 generatorset: explicitly track iteration order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22799
diff changeset
  3465
        self._ascending = not self._ascending
20703
e07b1fd30805 revset: added sort methods to generatorsets
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20695
diff changeset
  3466
22801
6af9e9b6af39 generatorset: implement isascending and isdescending
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22800
diff changeset
  3467
    def isascending(self):
6af9e9b6af39 generatorset: implement isascending and isdescending
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22800
diff changeset
  3468
        return self._ascending
6af9e9b6af39 generatorset: implement isascending and isdescending
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22800
diff changeset
  3469
6af9e9b6af39 generatorset: implement isascending and isdescending
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22800
diff changeset
  3470
    def isdescending(self):
6af9e9b6af39 generatorset: implement isascending and isdescending
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22800
diff changeset
  3471
        return not self._ascending
6af9e9b6af39 generatorset: implement isascending and isdescending
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22800
diff changeset
  3472
29346
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3473
    def istopo(self):
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3474
        # not worth the trouble asserting if the two sets combined are still
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3475
        # in topographical order. Use the sort() predicate to explicitly sort
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3476
        # again instead.
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3477
        return False
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3478
22811
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3479
    def first(self):
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3480
        if self._ascending:
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3481
            it = self.fastasc
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3482
        else:
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3483
            it = self.fastdesc
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3484
        if it is None:
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3485
            # we need to consume all and try again
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3486
            for x in self._consumegen():
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3487
                pass
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3488
            return self.first()
25146
f542a2c89b60 generatorset: use 'next()' to simplify the code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25145
diff changeset
  3489
        return next(it(), None)
22811
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3490
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3491
    def last(self):
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3492
        if self._ascending:
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3493
            it = self.fastdesc
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3494
        else:
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3495
            it = self.fastasc
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3496
        if it is None:
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3497
            # we need to consume all and try again
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3498
            for x in self._consumegen():
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3499
                pass
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3500
            return self.first()
25146
f542a2c89b60 generatorset: use 'next()' to simplify the code
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25145
diff changeset
  3501
        return next(it(), None)
22811
c1fd827e1ae0 generatorset: implement first and last methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22810
diff changeset
  3502
24457
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3503
    def __repr__(self):
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3504
        d = {False: '-', True: '+'}[self._ascending]
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3505
        return '<%s%s>' % (type(self).__name__, d)
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3506
24116
8b90a60181d1 revset: drop factory that promotes spanset to fullreposet
Yuya Nishihara <yuya@tcha.org>
parents: 24115
diff changeset
  3507
class spanset(abstractsmartset):
20482
a979078bd788 revset: added spanset class to represent revision ranges
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20481
diff changeset
  3508
    """Duck type for baseset class which represents a range of revisions and
a979078bd788 revset: added spanset class to represent revision ranges
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20481
diff changeset
  3509
    can work lazily and without having all the range in memory
20737
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3510
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3511
    Note that spanset(x, y) behave almost like xrange(x, y) except for two
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3512
    notable points:
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3513
    - when x < y it will be automatically descending,
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3514
    - revision filtered with this repoview will be skipped.
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3515
20482
a979078bd788 revset: added spanset class to represent revision ranges
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20481
diff changeset
  3516
    """
20525
aa73a6327df4 revset: changed spanset to take a repo argument
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20521
diff changeset
  3517
    def __init__(self, repo, start=0, end=None):
20737
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3518
        """
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3519
        start: first revision included the set
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3520
               (default to 0)
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3521
        end:   first revision excluded (last+1)
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3522
               (default to len(repo)
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3523
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3524
        Spanset will be descending if `end` < `start`.
b141080e70c5 revset: added documentation and comment for spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20734
diff changeset
  3525
        """
22717
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3526
        if end is None:
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3527
            end = len(repo)
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3528
        self._ascending = start <= end
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3529
        if not self._ascending:
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3530
            start, end = end + 1, start +1
20482
a979078bd788 revset: added spanset class to represent revision ranges
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20481
diff changeset
  3531
        self._start = start
22717
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3532
        self._end = end
20525
aa73a6327df4 revset: changed spanset to take a repo argument
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20521
diff changeset
  3533
        self._hiddenrevs = repo.changelog.filteredrevs
20521
1850a7f5fb66 revset: changed spanset implementation to take hidden revisions into account
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20512
diff changeset
  3534
22717
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3535
    def sort(self, reverse=False):
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3536
        self._ascending = not reverse
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3537
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3538
    def reverse(self):
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3539
        self._ascending = not self._ascending
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3540
29346
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3541
    def istopo(self):
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3542
        # not worth the trouble asserting if the two sets combined are still
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3543
        # in topographical order. Use the sort() predicate to explicitly sort
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3544
        # again instead.
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3545
        return False
38e0c83c7ee4 revset: record if a set is in topographical order
Martijn Pieters <mjpieters@fb.com>
parents: 29304
diff changeset
  3546
22717
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3547
    def _iterfilter(self, iterrange):
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3548
        s = self._hiddenrevs
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3549
        for r in iterrange:
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3550
            if r not in s:
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3551
                yield r
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3552
20482
a979078bd788 revset: added spanset class to represent revision ranges
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20481
diff changeset
  3553
    def __iter__(self):
22717
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3554
        if self._ascending:
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3555
            return self.fastasc()
20482
a979078bd788 revset: added spanset class to represent revision ranges
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20481
diff changeset
  3556
        else:
22717
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3557
            return self.fastdesc()
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3558
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3559
    def fastasc(self):
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3560
        iterrange = xrange(self._start, self._end)
20521
1850a7f5fb66 revset: changed spanset implementation to take hidden revisions into account
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20512
diff changeset
  3561
        if self._hiddenrevs:
22717
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3562
            return self._iterfilter(iterrange)
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3563
        return iter(iterrange)
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3564
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3565
    def fastdesc(self):
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3566
        iterrange = xrange(self._end - 1, self._start - 1, -1)
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3567
        if self._hiddenrevs:
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3568
            return self._iterfilter(iterrange)
b89f7e3a414d spanset: enforce the order lazily to gain `fastasc` and `fastdesc` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22716
diff changeset
  3569
        return iter(iterrange)
20482
a979078bd788 revset: added spanset class to represent revision ranges
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20481
diff changeset
  3570
21201
c8b9c6147108 revset: fix revision filtering in spanset.contains (regression)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21199
diff changeset
  3571
    def __contains__(self, rev):
22526
1e6d2b6b37ea revset: do less lookup during spanset.__contains__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22510
diff changeset
  3572
        hidden = self._hiddenrevs
22718
7b123e5639f4 spanset: do a single range check in __contains__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22717
diff changeset
  3573
        return ((self._start <= rev < self._end)
22526
1e6d2b6b37ea revset: do less lookup during spanset.__contains__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22510
diff changeset
  3574
                and not (hidden and rev in hidden))
20482
a979078bd788 revset: added spanset class to represent revision ranges
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20481
diff changeset
  3575
20716
fa1ac5faa7c4 revset: added __nonzero__ method to spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20714
diff changeset
  3576
    def __nonzero__(self):
fa1ac5faa7c4 revset: added __nonzero__ method to spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20714
diff changeset
  3577
        for r in self:
fa1ac5faa7c4 revset: added __nonzero__ method to spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20714
diff changeset
  3578
            return True
fa1ac5faa7c4 revset: added __nonzero__ method to spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20714
diff changeset
  3579
        return False
fa1ac5faa7c4 revset: added __nonzero__ method to spanset class
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20714
diff changeset
  3580
20484
0f1ef9e9e904 revset: added operations to spanset to duck type baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20483
diff changeset
  3581
    def __len__(self):
20521
1850a7f5fb66 revset: changed spanset implementation to take hidden revisions into account
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20512
diff changeset
  3582
        if not self._hiddenrevs:
1850a7f5fb66 revset: changed spanset implementation to take hidden revisions into account
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20512
diff changeset
  3583
            return abs(self._end - self._start)
1850a7f5fb66 revset: changed spanset implementation to take hidden revisions into account
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20512
diff changeset
  3584
        else:
1850a7f5fb66 revset: changed spanset implementation to take hidden revisions into account
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20512
diff changeset
  3585
            count = 0
21205
e2031c8ca4f8 revset: also inline spanset._contained in __len__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21204
diff changeset
  3586
            start = self._start
e2031c8ca4f8 revset: also inline spanset._contained in __len__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21204
diff changeset
  3587
            end = self._end
20521
1850a7f5fb66 revset: changed spanset implementation to take hidden revisions into account
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20512
diff changeset
  3588
            for rev in self._hiddenrevs:
21284
3e53a64345c1 revset: cosmetic changes in spanset range comparison
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21283
diff changeset
  3589
                if (end < rev <= start) or (start <= rev < end):
20521
1850a7f5fb66 revset: changed spanset implementation to take hidden revisions into account
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20512
diff changeset
  3590
                    count += 1
1850a7f5fb66 revset: changed spanset implementation to take hidden revisions into account
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20512
diff changeset
  3591
            return abs(self._end - self._start) - count
20484
0f1ef9e9e904 revset: added operations to spanset to duck type baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20483
diff changeset
  3592
20725
cf628b50afbb revset: added isascending and isdescending methods to smartset classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20724
diff changeset
  3593
    def isascending(self):
23826
c90d195320c5 revset: fix spanset.isascending() to honor sort() or reverse() request
Yuya Nishihara <yuya@tcha.org>
parents: 23724
diff changeset
  3594
        return self._ascending
20725
cf628b50afbb revset: added isascending and isdescending methods to smartset classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20724
diff changeset
  3595
cf628b50afbb revset: added isascending and isdescending methods to smartset classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20724
diff changeset
  3596
    def isdescending(self):
23826
c90d195320c5 revset: fix spanset.isascending() to honor sort() or reverse() request
Yuya Nishihara <yuya@tcha.org>
parents: 23724
diff changeset
  3597
        return not self._ascending
20725
cf628b50afbb revset: added isascending and isdescending methods to smartset classes
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20724
diff changeset
  3598
22809
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3599
    def first(self):
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3600
        if self._ascending:
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3601
            it = self.fastasc
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3602
        else:
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3603
            it = self.fastdesc
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3604
        for x in it():
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3605
            return x
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3606
        return None
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3607
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3608
    def last(self):
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3609
        if self._ascending:
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3610
            it = self.fastdesc
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3611
        else:
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3612
            it = self.fastasc
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3613
        for x in it():
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3614
            return x
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3615
        return None
88dad916c008 spanset: implement `first` and `last` methods
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22808
diff changeset
  3616
24457
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3617
    def __repr__(self):
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3618
        d = {False: '-', True: '+'}[self._ascending]
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3619
        return '<%s%s %d:%d>' % (type(self).__name__, d,
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3620
                                 self._start, self._end - 1)
c5022f3579b9 revset: add __repr__ to all smartset classes
Yuya Nishihara <yuya@tcha.org>
parents: 24446
diff changeset
  3621
24116
8b90a60181d1 revset: drop factory that promotes spanset to fullreposet
Yuya Nishihara <yuya@tcha.org>
parents: 24115
diff changeset
  3622
class fullreposet(spanset):
22508
2434c68d82a8 revert: add a fullreposet class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22504
diff changeset
  3623
    """a set containing all revisions in the repo
2434c68d82a8 revert: add a fullreposet class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22504
diff changeset
  3624
24204
d2de20e1451f revset: extend fullreposet to make "null" revision magically appears in set
Yuya Nishihara <yuya@tcha.org>
parents: 24203
diff changeset
  3625
    This class exists to host special optimization and magic to handle virtual
d2de20e1451f revset: extend fullreposet to make "null" revision magically appears in set
Yuya Nishihara <yuya@tcha.org>
parents: 24203
diff changeset
  3626
    revisions such as "null".
22508
2434c68d82a8 revert: add a fullreposet class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22504
diff changeset
  3627
    """
2434c68d82a8 revert: add a fullreposet class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22504
diff changeset
  3628
2434c68d82a8 revert: add a fullreposet class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22504
diff changeset
  3629
    def __init__(self, repo):
2434c68d82a8 revert: add a fullreposet class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22504
diff changeset
  3630
        super(fullreposet, self).__init__(repo)
2434c68d82a8 revert: add a fullreposet class
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22504
diff changeset
  3631
22510
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3632
    def __and__(self, other):
23139
e53f6b72a0e4 spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 23127
diff changeset
  3633
        """As self contains the whole repo, all of the other set should also be
e53f6b72a0e4 spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 23127
diff changeset
  3634
        in self. Therefore `self & other = other`.
22510
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3635
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3636
        This boldly assumes the other contains valid revs only.
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3637
        """
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3638
        # other not a smartset, make is so
22883
e7b8cf352614 fullreposet: detect smartset using "isascending" instead of "set"
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22882
diff changeset
  3639
        if not util.safehasattr(other, 'isascending'):
22510
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3640
            # filter out hidden revision
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3641
            # (this boldly assumes all smartset are pure)
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3642
            #
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3643
            # `other` was used with "&", let's assume this is a set like
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3644
            # object.
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3645
            other = baseset(other - self._hiddenrevs)
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3646
25547
99a1f73af85b revset: point out wrong behavior in fullreposet
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25344
diff changeset
  3647
        # XXX As fullreposet is also used as bootstrap, this is wrong.
99a1f73af85b revset: point out wrong behavior in fullreposet
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25344
diff changeset
  3648
        #
99a1f73af85b revset: point out wrong behavior in fullreposet
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25344
diff changeset
  3649
        # With a giveme312() revset returning [3,1,2], this makes
99a1f73af85b revset: point out wrong behavior in fullreposet
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25344
diff changeset
  3650
        #   'hg log -r "giveme312()"' -> 1, 2, 3 (wrong)
99a1f73af85b revset: point out wrong behavior in fullreposet
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25344
diff changeset
  3651
        # We cannot just drop it because other usage still need to sort it:
99a1f73af85b revset: point out wrong behavior in fullreposet
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25344
diff changeset
  3652
        #   'hg log -r "all() and giveme312()"' -> 1, 2, 3 (right)
99a1f73af85b revset: point out wrong behavior in fullreposet
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25344
diff changeset
  3653
        #
99a1f73af85b revset: point out wrong behavior in fullreposet
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25344
diff changeset
  3654
        # There is also some faulty revset implementations that rely on it
99a1f73af85b revset: point out wrong behavior in fullreposet
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25344
diff changeset
  3655
        # (eg: children as of its state in e8075329c5fb)
99a1f73af85b revset: point out wrong behavior in fullreposet
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25344
diff changeset
  3656
        #
99a1f73af85b revset: point out wrong behavior in fullreposet
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25344
diff changeset
  3657
        # When we fix the two points above we can move this into the if clause
23827
4982fade9733 revset: simplify fullreposet.__and__ to call sort() with boolean flag
Yuya Nishihara <yuya@tcha.org>
parents: 23822
diff changeset
  3658
        other.sort(reverse=self.isdescending())
22510
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3659
        return other
911f5a6579d1 revset: fast implementation for fullreposet.__and__
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22509
diff changeset
  3660
24458
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3661
def prettyformatset(revs):
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3662
    lines = []
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3663
    rs = repr(revs)
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3664
    p = 0
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3665
    while p < len(rs):
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3666
        q = rs.find('<', p + 1)
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3667
        if q < 0:
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3668
            q = len(rs)
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3669
        l = rs.count('<', 0, p) - rs.count('>', 0, p)
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3670
        assert l >= 0
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3671
        lines.append((l, rs[p:q].rstrip()))
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3672
        p = q
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3673
    return '\n'.join('  ' * l + s for l, s in lines)
7d87f672d069 debugrevspec: show nesting structure of smartsets if verbose
Yuya Nishihara <yuya@tcha.org>
parents: 24457
diff changeset
  3674
28393
ac11ba7c2e56 registrar: define revsetpredicate to decorate revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28272
diff changeset
  3675
def loadpredicate(ui, extname, registrarobj):
ac11ba7c2e56 registrar: define revsetpredicate to decorate revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28272
diff changeset
  3676
    """Load revset predicates from specified registrarobj
ac11ba7c2e56 registrar: define revsetpredicate to decorate revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28272
diff changeset
  3677
    """
ac11ba7c2e56 registrar: define revsetpredicate to decorate revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28272
diff changeset
  3678
    for name, func in registrarobj._table.iteritems():
ac11ba7c2e56 registrar: define revsetpredicate to decorate revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28272
diff changeset
  3679
        symbols[name] = func
ac11ba7c2e56 registrar: define revsetpredicate to decorate revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28272
diff changeset
  3680
        if func._safe:
ac11ba7c2e56 registrar: define revsetpredicate to decorate revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28272
diff changeset
  3681
            safesymbols.add(name)
ac11ba7c2e56 registrar: define revsetpredicate to decorate revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28272
diff changeset
  3682
28395
0383f7a5e86c revset: replace predicate by revsetpredicate of registrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28393
diff changeset
  3683
# load built-in predicates explicitly to setup safesymbols
0383f7a5e86c revset: replace predicate by revsetpredicate of registrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28393
diff changeset
  3684
loadpredicate(None, None, predicate)
0383f7a5e86c revset: replace predicate by revsetpredicate of registrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 28393
diff changeset
  3685
12823
80deae3bc5ea hggettext: handle i18nfunctions declaration for docstrings translations
Patrick Mezard <pmezard@gmail.com>
parents: 12821
diff changeset
  3686
# tell hggettext to extract docstrings from these functions:
80deae3bc5ea hggettext: handle i18nfunctions declaration for docstrings translations
Patrick Mezard <pmezard@gmail.com>
parents: 12821
diff changeset
  3687
i18nfunctions = symbols.values()