Debian Codegraph @

hgext/largefiles/storefactory.py

raw
1 # This software may be used and distributed according to the terms of the
2 # GNU General Public License version 2 or any later version.
3
4 from __future__ import annotations
5
6 import re
7
8 from mercurial.i18n import _
9 from mercurial import (
10 error,
11 util,
12 )
13 from mercurial.repo import (
14 factory as repo_factory,
15 )
16 from mercurial.utils import (
17 urlutil,
18 )
19
20 from . import (
21 lfutil,
22 localstore,
23 wirestore,
24 )
25
26
27 # During clone this function is passed the src's ui object
28 # but it needs the dest's ui object so it can read out of
29 # the config file. Use repo.ui instead.
30 def openstore(repo=None, remote=None, put=False, ui=None):
31 if ui is None:
32 ui = repo.ui
33
34 if not remote:
35 lfpullsource = getattr(repo, 'lfpullsource', None)
36 if put:
37 path = urlutil.get_unique_push_path(
38 b'lfpullsource', repo, ui, lfpullsource
39 )
40 else:
41 path = urlutil.get_unique_pull_path_obj(
42 b'lfpullsource', ui, lfpullsource
43 )
44
45 # XXX we should not explicitly pass b'default', as this will result in
46 # b'default' being returned if no `paths.default` was defined. We
47 # should explicitely handle the lack of value instead.
48 if repo is None:
49 path = urlutil.get_unique_pull_path_obj(
50 b'lfs',
51 ui,
52 b'default',
53 )
54 remote = repo_factory.peer(repo or ui, {}, path)
55 elif path.loc == b'default-push' or path.loc == b'default':
56 remote = repo
57 else:
58 remote = repo_factory.peer(repo or ui, {}, path)
59
60 # The path could be a scheme so use Mercurial's normal functionality
61 # to resolve the scheme to a repository and use its path
62 path = hasattr(remote, 'url') and remote.url() or remote.path
63
64 match = _scheme_re.match(path)
65 if not match: # regular filesystem path
66 scheme = b'file'
67 else:
68 scheme = match.group(1)
69
70 try:
71 storeproviders = _storeprovider[scheme]
72 except KeyError:
73 raise error.Abort(_(b'unsupported URL scheme %r') % scheme)
74
75 for classobj in storeproviders:
76 try:
77 return classobj(ui, repo, remote)
78 except lfutil.storeprotonotcapable:
79 pass
80
81 raise error.Abort(
82 _(b'%s does not appear to be a largefile store')
83 % urlutil.hidepassword(path)
84 )
85
86
87 _storeprovider = {
88 b'file': [localstore.localstore],
89 b'http': [wirestore.wirestore],
90 b'https': [wirestore.wirestore],
91 b'ssh': [wirestore.wirestore],
92 }
93
94 _scheme_re = re.compile(br'^([a-zA-Z0-9+-.]+)://')
95
96
97 def getlfile(ui, hash):
98 return util.chunkbuffer(openstore(ui=ui)._get(hash))