hgext/directaccess.py
author Pierre-Yves David <pierre-yves.david@fb.com>
Fri, 11 Mar 2016 09:48:12 +0000
changeset 1619 5ef77a1d975c
parent 1552 ebebff25b891
child 1714 4565b046b850
permissions -rw-r--r--
hgext: turn 'hgext' into a namespace package Actually since Python 2.3, there is some way to turn top level package into "namespace package" so that multiple subpackage installed in different part of the path can still be imported transparently. This feature was previously thought (at least by myself) to be only provided by some setuptool black magic. Turning hgext into such namespace package allows third extensions to install themselves inside the "hgext" namespace package to avoid polluting the global python module namespace. They will now be able to do so without making it a pain to use a Mercurial "installed" in a different way/location than these extensions.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     1
""" This extension provides direct access
1552
ebebff25b891 check-code: make directaccess.py comply with check-code
Laurent Charignon <lcharignon@fb.com>
parents: 1537
diff changeset
     2
It is the ability to refer and access hidden sha in commands provided that you
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     3
know their value.
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     4
For example hg log -r xxx where xxx is a commit has should work whether xxx is
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     5
hidden or not as we assume that the user knows what he is doing when referring
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     6
to xxx.
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     7
"""
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     8
from mercurial import extensions
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
     9
from mercurial import cmdutil
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    10
from mercurial import repoview
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    11
from mercurial import branchmap
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    12
from mercurial import revset
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    13
from mercurial import error
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    14
from mercurial import commands
1367
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
    15
from mercurial import hg
1489
2f8a4d496585 directaccess: don't try to partialmatch things that aren't hashes
Siddharth Agarwal <sid0@fb.com>
parents: 1372
diff changeset
    16
from mercurial import util
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    17
from mercurial.i18n import _
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    18
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    19
cmdtable = {}
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    20
command = cmdutil.command(cmdtable)
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    21
1367
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
    22
# By default, all the commands have directaccess with warnings
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
    23
# List of commands that have no directaccess and directaccess with no warning
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    24
directaccesslevel = [
1371
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    25
    # Format:
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    26
    # ('nowarning', 'evolve', 'prune'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    27
    # means: no directaccess warning, for the command in evolve named prune
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    28
    #
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    29
    # ('error', None, 'serve'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    30
    # means: no directaccess for the command in core named serve
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    31
    #
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    32
    # The list is ordered alphabetically by command names, starting with all
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    33
    # the commands in core then all the commands in the extensions
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    34
    #
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    35
    # The general guideline is:
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    36
    # - remove directaccess warnings for read only commands
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    37
    # - no direct access for commands with consequences outside of the repo
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    38
    # - leave directaccess warnings for all the other commands
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    39
    #
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    40
    ('nowarning', None, 'annotate'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    41
    ('nowarning', None, 'archive'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    42
    ('nowarning', None, 'bisect'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    43
    ('nowarning', None, 'bookmarks'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    44
    ('nowarning', None, 'bundle'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    45
    ('nowarning', None, 'cat'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    46
    ('nowarning', None, 'diff'),
1367
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
    47
    ('nowarning', None, 'export'),
1371
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    48
    ('nowarning', None, 'identify'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    49
    ('nowarning', None, 'incoming'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    50
    ('nowarning', None, 'log'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    51
    ('nowarning', None, 'manifest'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    52
    ('error', None, 'outgoing'), # confusing if push errors and not outgoing
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    53
    ('error', None, 'push'), # destructive
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    54
    ('nowarning', None, 'revert'),
1368
c02cdb97ebfa directaccess: disable directaccess for push and serve
Laurent Charignon <lcharignon@fb.com>
parents: 1367
diff changeset
    55
    ('error', None, 'serve'),
1371
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    56
    ('nowarning', None, 'tags'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    57
    ('nowarning', None, 'unbundle'),
30d42079f4a2 directaccess: add some commands to the directaccess list
Laurent Charignon <lcharignon@fb.com>
parents: 1368
diff changeset
    58
    ('nowarning', None, 'update'),
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    59
]
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    60
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    61
def reposetup(ui, repo):
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    62
    repo._explicitaccess = set()
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    63
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    64
def _computehidden(repo):
1363
2eaa2943f9f3 directaccess: use cached filteredrevs
Laurent Charignon <lcharignon@fb.com>
parents: 1360
diff changeset
    65
    hidden = repoview.filterrevs(repo, 'visible')
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    66
    cl = repo.changelog
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    67
    dynamic = hidden & repo._explicitaccess
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    68
    if dynamic:
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    69
        blocked = cl.ancestors(dynamic, inclusive=True)
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    70
        hidden = frozenset(r for r in hidden if r not in blocked)
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    71
    return hidden
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    72
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    73
def setupdirectaccess():
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    74
    """ Add two new filtername that behave like visible to provide direct access
1552
ebebff25b891 check-code: make directaccess.py comply with check-code
Laurent Charignon <lcharignon@fb.com>
parents: 1537
diff changeset
    75
    and direct access with warning. Wraps the commands to setup direct access
ebebff25b891 check-code: make directaccess.py comply with check-code
Laurent Charignon <lcharignon@fb.com>
parents: 1537
diff changeset
    76
    """
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    77
    repoview.filtertable.update({'visible-directaccess-nowarn': _computehidden})
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    78
    repoview.filtertable.update({'visible-directaccess-warn': _computehidden})
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    79
    branchmap.subsettable['visible-directaccess-nowarn'] = 'visible'
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    80
    branchmap.subsettable['visible-directaccess-warn'] = 'visible'
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    81
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    82
    for warn, ext, cmd in directaccesslevel:
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    83
        try:
1347
b00c2fe51ac8 directaccess: don't crash when evolve is not loaded
Laurent Charignon <lcharignon@fb.com>
parents: 1339
diff changeset
    84
            cmdtable = extensions.find(ext).cmdtable if ext else commands.table
1367
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
    85
            wrapper = wrapwitherror if warn == 'error' else wrapwithoutwarning
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    86
            extensions.wrapcommand(cmdtable, cmd, wrapper)
1347
b00c2fe51ac8 directaccess: don't crash when evolve is not loaded
Laurent Charignon <lcharignon@fb.com>
parents: 1339
diff changeset
    87
        except (error.UnknownCommand, KeyError):
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    88
            pass
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    89
1367
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
    90
def wrapwitherror(orig, ui, repo, *args, **kwargs):
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
    91
    if repo and repo.filtername == 'visible-directaccess-warn':
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
    92
        repo = repo.filtered('visible')
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    93
    return orig(ui, repo, *args, **kwargs)
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    94
1367
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
    95
def wrapwithoutwarning(orig, ui, repo, *args, **kwargs):
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
    96
    if repo and repo.filtername == 'visible-directaccess-warn':
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
    97
        repo = repo.filtered("visible-directaccess-nowarn")
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    98
    return orig(ui, repo, *args, **kwargs)
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
    99
1360
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   100
def uisetup(ui):
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   101
    """ Change ordering of extensions to ensure that directaccess extsetup comes
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   102
    after the one of the extensions in the loadsafter list """
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   103
    loadsafter = ui.configlist('directaccess','loadsafter')
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   104
    order = list(extensions._order)
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   105
    directaccesidx = order.index('directaccess')
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   106
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   107
    # The min idx for directaccess to load after all the extensions in loadafter
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   108
    minidxdirectaccess = directaccesidx
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   109
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   110
    for ext in loadsafter:
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   111
        try:
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   112
            minidxdirectaccess = max(minidxdirectaccess, order.index(ext))
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   113
        except ValueError:
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   114
            pass # extension not loaded
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   115
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   116
    if minidxdirectaccess > directaccesidx:
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   117
        order.insert(minidxdirectaccess + 1, 'directaccess')
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   118
        order.remove('directaccess')
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   119
        extensions._order = order
5c13945b32fc directaccess: add mechanism to load directaccess after some other extensions
Laurent Charignon <lcharignon@fb.com>
parents: 1347
diff changeset
   120
1367
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
   121
def _repository(orig, *args, **kwargs):
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
   122
    """Make visible-directaccess-warn the default filter for new repos"""
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
   123
    repo = orig(*args, **kwargs)
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
   124
    return repo.filtered("visible-directaccess-warn")
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
   125
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   126
def extsetup(ui):
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   127
    extensions.wrapfunction(revset, 'posttreebuilthook', _posttreebuilthook)
1367
0c134ca37567 directaccess: change rule from opt-in to opt-out
Laurent Charignon <lcharignon@fb.com>
parents: 1363
diff changeset
   128
    extensions.wrapfunction(hg, 'repository', _repository)
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   129
    setupdirectaccess()
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   130
1489
2f8a4d496585 directaccess: don't try to partialmatch things that aren't hashes
Siddharth Agarwal <sid0@fb.com>
parents: 1372
diff changeset
   131
hashre = util.re.compile('[0-9a-fA-F]{1,40}')
2f8a4d496585 directaccess: don't try to partialmatch things that aren't hashes
Siddharth Agarwal <sid0@fb.com>
parents: 1372
diff changeset
   132
1492
f869033391b9 directaccesss: also look for symbol into list
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1489
diff changeset
   133
_listtuple = ('symbol', '_list')
f869033391b9 directaccesss: also look for symbol into list
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1489
diff changeset
   134
1537
50e683d9835e directaccess: fix case of shortened hash containing only digits
Laurent Charignon <lcharignon@fb.com>
parents: 1508
diff changeset
   135
def gethashsymbols(tree, maxrev):
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   136
    # Returns the list of symbols of the tree that look like hashes
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   137
    # for example for the revset 3::abe3ff it will return ('abe3ff')
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   138
    if not tree:
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   139
        return []
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   140
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   141
    if len(tree) == 2 and tree[0] == "symbol":
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   142
        try:
1537
50e683d9835e directaccess: fix case of shortened hash containing only digits
Laurent Charignon <lcharignon@fb.com>
parents: 1508
diff changeset
   143
            n = int(tree[1])
50e683d9835e directaccess: fix case of shortened hash containing only digits
Laurent Charignon <lcharignon@fb.com>
parents: 1508
diff changeset
   144
            # This isn't necessarily a rev number, could be a hash prefix
50e683d9835e directaccess: fix case of shortened hash containing only digits
Laurent Charignon <lcharignon@fb.com>
parents: 1508
diff changeset
   145
            if n > maxrev:
50e683d9835e directaccess: fix case of shortened hash containing only digits
Laurent Charignon <lcharignon@fb.com>
parents: 1508
diff changeset
   146
                return [tree[1]]
50e683d9835e directaccess: fix case of shortened hash containing only digits
Laurent Charignon <lcharignon@fb.com>
parents: 1508
diff changeset
   147
            else:
50e683d9835e directaccess: fix case of shortened hash containing only digits
Laurent Charignon <lcharignon@fb.com>
parents: 1508
diff changeset
   148
                return []
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   149
        except ValueError as e:
1489
2f8a4d496585 directaccess: don't try to partialmatch things that aren't hashes
Siddharth Agarwal <sid0@fb.com>
parents: 1372
diff changeset
   150
            if hashre.match(tree[1]):
2f8a4d496585 directaccess: don't try to partialmatch things that aren't hashes
Siddharth Agarwal <sid0@fb.com>
parents: 1372
diff changeset
   151
                return [tree[1]]
2f8a4d496585 directaccess: don't try to partialmatch things that aren't hashes
Siddharth Agarwal <sid0@fb.com>
parents: 1372
diff changeset
   152
            return []
1492
f869033391b9 directaccesss: also look for symbol into list
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1489
diff changeset
   153
    elif tree[0] == "func" and tree[1] == _listtuple:
f869033391b9 directaccesss: also look for symbol into list
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1489
diff changeset
   154
        # the optimiser will group sequence of hash request
f869033391b9 directaccesss: also look for symbol into list
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1489
diff changeset
   155
        result = []
f869033391b9 directaccesss: also look for symbol into list
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1489
diff changeset
   156
        for entry in tree[2][1].split('\0'):
f869033391b9 directaccesss: also look for symbol into list
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1489
diff changeset
   157
            if hashre.match(entry):
f869033391b9 directaccesss: also look for symbol into list
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1489
diff changeset
   158
                result.append(entry)
f869033391b9 directaccesss: also look for symbol into list
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1489
diff changeset
   159
        return result
1508
8dfb88ca0c08 directaccess: inspect trees of len() > 3
Durham Goode <durham@fb.com>
parents: 1492
diff changeset
   160
    elif len(tree) >= 3:
8dfb88ca0c08 directaccess: inspect trees of len() > 3
Durham Goode <durham@fb.com>
parents: 1492
diff changeset
   161
        results = []
8dfb88ca0c08 directaccess: inspect trees of len() > 3
Durham Goode <durham@fb.com>
parents: 1492
diff changeset
   162
        for subtree in tree[1:]:
1537
50e683d9835e directaccess: fix case of shortened hash containing only digits
Laurent Charignon <lcharignon@fb.com>
parents: 1508
diff changeset
   163
            results += gethashsymbols(subtree, maxrev)
1508
8dfb88ca0c08 directaccess: inspect trees of len() > 3
Durham Goode <durham@fb.com>
parents: 1492
diff changeset
   164
        return results
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   165
    else:
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   166
        return []
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   167
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   168
def _posttreebuilthook(orig, tree, repo):
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   169
    # This is use to enabled direct hash access
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   170
    # We extract the symbols that look like hashes and add them to the
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   171
    # explicitaccess set
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   172
    orig(tree, repo)
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   173
    filternm = ""
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   174
    if repo is not None:
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   175
        filternm = repo.filtername
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   176
    if filternm is not None and filternm.startswith('visible-directaccess'):
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   177
        prelength = len(repo._explicitaccess)
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   178
        accessbefore = set(repo._explicitaccess)
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   179
        cl = repo.unfiltered().changelog
1537
50e683d9835e directaccess: fix case of shortened hash containing only digits
Laurent Charignon <lcharignon@fb.com>
parents: 1508
diff changeset
   180
        repo.symbols = gethashsymbols(tree, len(cl))
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   181
        for node in repo.symbols:
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   182
            try:
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   183
                node = cl._partialmatch(node)
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   184
            except error.LookupError:
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   185
                node = None
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   186
            if node is not None:
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   187
                rev = cl.rev(node)
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   188
                if rev not in repo.changelog:
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   189
                    repo._explicitaccess.add(rev)
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   190
        if prelength != len(repo._explicitaccess):
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   191
            if repo.filtername != 'visible-directaccess-nowarn':
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   192
                unhiddencommits = repo._explicitaccess - accessbefore
1552
ebebff25b891 check-code: make directaccess.py comply with check-code
Laurent Charignon <lcharignon@fb.com>
parents: 1537
diff changeset
   193
                repo.ui.warn(_("Warning: accessing hidden changesets %s "
ebebff25b891 check-code: make directaccess.py comply with check-code
Laurent Charignon <lcharignon@fb.com>
parents: 1537
diff changeset
   194
                                "for write operation\n") %
ebebff25b891 check-code: make directaccess.py comply with check-code
Laurent Charignon <lcharignon@fb.com>
parents: 1537
diff changeset
   195
                                (",".join([str(repo.unfiltered()[l])
1339
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   196
                                    for l in unhiddencommits])))
0e2eb196923a inhibit: create direct access extension
Laurent Charignon <lcharignon@fb.com>
parents:
diff changeset
   197
            repo.invalidatevolatilesets()