hgext/largefiles/storefactory.py
author Raphaël Gomès <rgomes@octobus.net>
Tue, 21 Nov 2023 16:20:58 +0100
branchstable
changeset 51132 d45afa07564c
parent 50929 18c8c18993f0
child 51863 f4733654f144
permissions -rw-r--r--
relnotes: add 6.6 There are no differences between the rc and the actual release.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     1
# This software may be used and distributed according to the terms of the
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     2
# GNU General Public License version 2 or any later version.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     3
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     4
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     5
import re
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     6
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     7
from mercurial.i18n import _
29305
814076f4ace3 largefiles: move basestore._openstore into new module to remove cycle
liscju <piotr.listkiewicz@gmail.com>
parents: 29067
diff changeset
     8
from mercurial import (
814076f4ace3 largefiles: move basestore._openstore into new module to remove cycle
liscju <piotr.listkiewicz@gmail.com>
parents: 29067
diff changeset
     9
    error,
814076f4ace3 largefiles: move basestore._openstore into new module to remove cycle
liscju <piotr.listkiewicz@gmail.com>
parents: 29067
diff changeset
    10
    hg,
814076f4ace3 largefiles: move basestore._openstore into new module to remove cycle
liscju <piotr.listkiewicz@gmail.com>
parents: 29067
diff changeset
    11
    util,
814076f4ace3 largefiles: move basestore._openstore into new module to remove cycle
liscju <piotr.listkiewicz@gmail.com>
parents: 29067
diff changeset
    12
)
46907
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43089
diff changeset
    13
from mercurial.utils import (
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43089
diff changeset
    14
    urlutil,
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43089
diff changeset
    15
)
19918
ae65192fd6b4 largefiles: refactor basestore, extract _gethash method
Mads Kiilerich <madski@unity3d.com>
parents: 19008
diff changeset
    16
29305
814076f4ace3 largefiles: move basestore._openstore into new module to remove cycle
liscju <piotr.listkiewicz@gmail.com>
parents: 29067
diff changeset
    17
from . import (
814076f4ace3 largefiles: move basestore._openstore into new module to remove cycle
liscju <piotr.listkiewicz@gmail.com>
parents: 29067
diff changeset
    18
    lfutil,
814076f4ace3 largefiles: move basestore._openstore into new module to remove cycle
liscju <piotr.listkiewicz@gmail.com>
parents: 29067
diff changeset
    19
    localstore,
814076f4ace3 largefiles: move basestore._openstore into new module to remove cycle
liscju <piotr.listkiewicz@gmail.com>
parents: 29067
diff changeset
    20
    wirestore,
814076f4ace3 largefiles: move basestore._openstore into new module to remove cycle
liscju <piotr.listkiewicz@gmail.com>
parents: 29067
diff changeset
    21
)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    22
46947
3f29765e0d95 lfs: use the new APIs
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46908
diff changeset
    23
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    24
# During clone this function is passed the src's ui object
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    25
# but it needs the dest's ui object so it can read out of
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    26
# the config file. Use repo.ui instead.
35564
cf841f2b5a72 largefiles: add support for 'largefiles://' url scheme
Boris Feld <boris.feld@octobus.net>
parents: 29355
diff changeset
    27
def openstore(repo=None, remote=None, put=False, ui=None):
cf841f2b5a72 largefiles: add support for 'largefiles://' url scheme
Boris Feld <boris.feld@octobus.net>
parents: 29355
diff changeset
    28
    if ui is None:
cf841f2b5a72 largefiles: add support for 'largefiles://' url scheme
Boris Feld <boris.feld@octobus.net>
parents: 29355
diff changeset
    29
        ui = repo.ui
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    30
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    31
    if not remote:
15943
f9efb325ea32 largefiles: fix caching largefiles from an aliased repo (issue3212)
Na'Tosha Bard <natosha@unity3d.com>
parents: 15319
diff changeset
    32
        lfpullsource = getattr(repo, 'lfpullsource', None)
46947
3f29765e0d95 lfs: use the new APIs
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46908
diff changeset
    33
        if put:
3f29765e0d95 lfs: use the new APIs
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46908
diff changeset
    34
            path = urlutil.get_unique_push_path(
3f29765e0d95 lfs: use the new APIs
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46908
diff changeset
    35
                b'lfpullsource', repo, ui, lfpullsource
3f29765e0d95 lfs: use the new APIs
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46908
diff changeset
    36
            )
15943
f9efb325ea32 largefiles: fix caching largefiles from an aliased repo (issue3212)
Na'Tosha Bard <natosha@unity3d.com>
parents: 15319
diff changeset
    37
        else:
49733
8d0a220caee5 path: pass `path` to `peer` in largefiles
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48875
diff changeset
    38
            path = urlutil.get_unique_pull_path_obj(
8d0a220caee5 path: pass `path` to `peer` in largefiles
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48875
diff changeset
    39
                b'lfpullsource', ui, lfpullsource
46947
3f29765e0d95 lfs: use the new APIs
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46908
diff changeset
    40
            )
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15169
diff changeset
    41
46947
3f29765e0d95 lfs: use the new APIs
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46908
diff changeset
    42
        # XXX we should not explicitly pass b'default', as this will result in
3f29765e0d95 lfs: use the new APIs
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46908
diff changeset
    43
        # b'default' being returned if no `paths.default` was defined. We
3f29765e0d95 lfs: use the new APIs
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46908
diff changeset
    44
        # should explicitely handle the lack of value instead.
35564
cf841f2b5a72 largefiles: add support for 'largefiles://' url scheme
Boris Feld <boris.feld@octobus.net>
parents: 29355
diff changeset
    45
        if repo is None:
49733
8d0a220caee5 path: pass `path` to `peer` in largefiles
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48875
diff changeset
    46
            path = urlutil.get_unique_pull_path_obj(
8d0a220caee5 path: pass `path` to `peer` in largefiles
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48875
diff changeset
    47
                b'lfs',
8d0a220caee5 path: pass `path` to `peer` in largefiles
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48875
diff changeset
    48
                ui,
8d0a220caee5 path: pass `path` to `peer` in largefiles
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48875
diff changeset
    49
                b'default',
46947
3f29765e0d95 lfs: use the new APIs
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 46908
diff changeset
    50
            )
35564
cf841f2b5a72 largefiles: add support for 'largefiles://' url scheme
Boris Feld <boris.feld@octobus.net>
parents: 29355
diff changeset
    51
            remote = hg.peer(repo or ui, {}, path)
49733
8d0a220caee5 path: pass `path` to `peer` in largefiles
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48875
diff changeset
    52
        elif path.loc == b'default-push' or path.loc == b'default':
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    53
            remote = repo
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    54
        else:
35564
cf841f2b5a72 largefiles: add support for 'largefiles://' url scheme
Boris Feld <boris.feld@octobus.net>
parents: 29355
diff changeset
    55
            remote = hg.peer(repo or ui, {}, path)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    56
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    57
    # The path could be a scheme so use Mercurial's normal functionality
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    58
    # to resolve the scheme to a repository and use its path
50928
d718eddf01d9 safehasattr: drop usage in favor of hasattr
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 50891
diff changeset
    59
    path = hasattr(remote, 'url') and remote.url() or remote.path
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    60
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    61
    match = _scheme_re.match(path)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41365
diff changeset
    62
    if not match:  # regular filesystem path
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    63
        scheme = b'file'
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    64
    else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    65
        scheme = match.group(1)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    66
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    67
    try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    68
        storeproviders = _storeprovider[scheme]
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    69
    except KeyError:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    70
        raise error.Abort(_(b'unsupported URL scheme %r') % scheme)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    71
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16154
diff changeset
    72
    for classobj in storeproviders:
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    73
        try:
16247
d87d9d8a8e03 largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents: 16154
diff changeset
    74
            return classobj(ui, repo, remote)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    75
        except lfutil.storeprotonotcapable:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    76
            pass
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    77
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41365
diff changeset
    78
    raise error.Abort(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    79
        _(b'%s does not appear to be a largefile store')
46907
ffd3e823a7e5 urlutil: extract `url` related code from `util` into the new module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43089
diff changeset
    80
        % urlutil.hidepassword(path)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41365
diff changeset
    81
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41365
diff changeset
    82
29305
814076f4ace3 largefiles: move basestore._openstore into new module to remove cycle
liscju <piotr.listkiewicz@gmail.com>
parents: 29067
diff changeset
    83
814076f4ace3 largefiles: move basestore._openstore into new module to remove cycle
liscju <piotr.listkiewicz@gmail.com>
parents: 29067
diff changeset
    84
_storeprovider = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    85
    b'file': [localstore.localstore],
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    86
    b'http': [wirestore.wirestore],
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    87
    b'https': [wirestore.wirestore],
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    88
    b'ssh': [wirestore.wirestore],
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41365
diff changeset
    89
}
29305
814076f4ace3 largefiles: move basestore._openstore into new module to remove cycle
liscju <piotr.listkiewicz@gmail.com>
parents: 29067
diff changeset
    90
36310
ea62c2df882d largefiles: make scheme regex a bytes regex
Augie Fackler <augie@google.com>
parents: 35564
diff changeset
    91
_scheme_re = re.compile(br'^([a-zA-Z0-9+-.]+)://')
35564
cf841f2b5a72 largefiles: add support for 'largefiles://' url scheme
Boris Feld <boris.feld@octobus.net>
parents: 29355
diff changeset
    92
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41365
diff changeset
    93
35564
cf841f2b5a72 largefiles: add support for 'largefiles://' url scheme
Boris Feld <boris.feld@octobus.net>
parents: 29355
diff changeset
    94
def getlfile(ui, hash):
cf841f2b5a72 largefiles: add support for 'largefiles://' url scheme
Boris Feld <boris.feld@octobus.net>
parents: 29355
diff changeset
    95
    return util.chunkbuffer(openstore(ui=ui)._get(hash))