comparison mercurial/hg.py @ 46907:ffd3e823a7e5

urlutil: extract `url` related code from `util` into the new module The new module is well fitting for this new code. And this will be useful to make the gathered code collaborate more later. Differential Revision: https://phab.mercurial-scm.org/D10374
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Mon, 12 Apr 2021 03:01:04 +0200
parents d4ba4d51f85f
children 4452cb788404
comparison
equal deleted inserted replaced
46906:33524c46a092 46907:ffd3e823a7e5
53 vfs as vfsmod, 53 vfs as vfsmod,
54 ) 54 )
55 from .utils import ( 55 from .utils import (
56 hashutil, 56 hashutil,
57 stringutil, 57 stringutil,
58 urlutil,
58 ) 59 )
59 60
60 61
61 release = lock.release 62 release = lock.release
62 63
63 # shared features 64 # shared features
64 sharedbookmarks = b'bookmarks' 65 sharedbookmarks = b'bookmarks'
65 66
66 67
67 def _local(path): 68 def _local(path):
68 path = util.expandpath(util.urllocalpath(path)) 69 path = util.expandpath(urlutil.urllocalpath(path))
69 70
70 try: 71 try:
71 # we use os.stat() directly here instead of os.path.isfile() 72 # we use os.stat() directly here instead of os.path.isfile()
72 # because the latter started returning `False` on invalid path 73 # because the latter started returning `False` on invalid path
73 # exceptions starting in 3.8 and we care about handling 74 # exceptions starting in 3.8 and we care about handling
130 131
131 132
132 def parseurl(path, branches=None): 133 def parseurl(path, branches=None):
133 '''parse url#branch, returning (url, (branch, branches))''' 134 '''parse url#branch, returning (url, (branch, branches))'''
134 135
135 u = util.url(path) 136 u = urlutil.url(path)
136 branch = None 137 branch = None
137 if u.fragment: 138 if u.fragment:
138 branch = u.fragment 139 branch = u.fragment
139 u.fragment = None 140 u.fragment = None
140 return bytes(u), (branch, branches or []) 141 return bytes(u), (branch, branches or [])
150 b'static-http': statichttprepo, 151 b'static-http': statichttprepo,
151 } 152 }
152 153
153 154
154 def _peerlookup(path): 155 def _peerlookup(path):
155 u = util.url(path) 156 u = urlutil.url(path)
156 scheme = u.scheme or b'file' 157 scheme = u.scheme or b'file'
157 thing = schemes.get(scheme) or schemes[b'file'] 158 thing = schemes.get(scheme) or schemes[b'file']
158 try: 159 try:
159 return thing(path) 160 return thing(path)
160 except TypeError: 161 except TypeError:
175 return repo.local() 176 return repo.local()
176 177
177 178
178 def openpath(ui, path, sendaccept=True): 179 def openpath(ui, path, sendaccept=True):
179 '''open path with open if local, url.open if remote''' 180 '''open path with open if local, url.open if remote'''
180 pathurl = util.url(path, parsequery=False, parsefragment=False) 181 pathurl = urlutil.url(path, parsequery=False, parsefragment=False)
181 if pathurl.islocal(): 182 if pathurl.islocal():
182 return util.posixfile(pathurl.localpath(), b'rb') 183 return util.posixfile(pathurl.localpath(), b'rb')
183 else: 184 else:
184 return url.open(ui, path, sendaccept=sendaccept) 185 return url.open(ui, path, sendaccept=sendaccept)
185 186
263 >>> defaultdest(b'http://example.org/') 264 >>> defaultdest(b'http://example.org/')
264 '' 265 ''
265 >>> defaultdest(b'http://example.org/foo/') 266 >>> defaultdest(b'http://example.org/foo/')
266 'foo' 267 'foo'
267 """ 268 """
268 path = util.url(source).path 269 path = urlutil.url(source).path
269 if not path: 270 if not path:
270 return b'' 271 return b''
271 return os.path.basename(os.path.normpath(path)) 272 return os.path.basename(os.path.normpath(path))
272 273
273 274
569 stream=stream, 570 stream=stream,
570 ) 571 )
571 572
572 # Resolve the value to put in [paths] section for the source. 573 # Resolve the value to put in [paths] section for the source.
573 if islocal(source): 574 if islocal(source):
574 defaultpath = os.path.abspath(util.urllocalpath(source)) 575 defaultpath = os.path.abspath(urlutil.urllocalpath(source))
575 else: 576 else:
576 defaultpath = source 577 defaultpath = source
577 578
578 sharerepo = repository(ui, path=sharepath) 579 sharerepo = repository(ui, path=sharepath)
579 destrepo = share( 580 destrepo = share(
691 if dest: 692 if dest:
692 ui.status(_(b"destination directory: %s\n") % dest) 693 ui.status(_(b"destination directory: %s\n") % dest)
693 else: 694 else:
694 dest = ui.expandpath(dest) 695 dest = ui.expandpath(dest)
695 696
696 dest = util.urllocalpath(dest) 697 dest = urlutil.urllocalpath(dest)
697 source = util.urllocalpath(source) 698 source = urlutil.urllocalpath(source)
698 699
699 if not dest: 700 if not dest:
700 raise error.InputError(_(b"empty destination path is not valid")) 701 raise error.InputError(_(b"empty destination path is not valid"))
701 702
702 destvfs = vfsmod.vfs(dest, expandpath=True) 703 destvfs = vfsmod.vfs(dest, expandpath=True)
823 824
824 srcrepo = srcpeer.local() 825 srcrepo = srcpeer.local()
825 826
826 abspath = origsource 827 abspath = origsource
827 if islocal(origsource): 828 if islocal(origsource):
828 abspath = os.path.abspath(util.urllocalpath(origsource)) 829 abspath = os.path.abspath(urlutil.urllocalpath(origsource))
829 830
830 if islocal(dest): 831 if islocal(dest):
831 cleandir = dest 832 cleandir = dest
832 833
833 copy = False 834 copy = False
937 if narrow: 938 if narrow:
938 with local.wlock(), local.lock(): 939 with local.wlock(), local.lock():
939 local.setnarrowpats(storeincludepats, storeexcludepats) 940 local.setnarrowpats(storeincludepats, storeexcludepats)
940 narrowspec.copytoworkingcopy(local) 941 narrowspec.copytoworkingcopy(local)
941 942
942 u = util.url(abspath) 943 u = urlutil.url(abspath)
943 defaulturl = bytes(u) 944 defaulturl = bytes(u)
944 local.ui.setconfig(b'paths', b'default', defaulturl, b'clone') 945 local.ui.setconfig(b'paths', b'default', defaulturl, b'clone')
945 if not stream: 946 if not stream:
946 if pull: 947 if pull:
947 stream = False 948 stream = False
984 cleandir = None 985 cleandir = None
985 986
986 destrepo = destpeer.local() 987 destrepo = destpeer.local()
987 if destrepo: 988 if destrepo:
988 template = uimod.samplehgrcs[b'cloned'] 989 template = uimod.samplehgrcs[b'cloned']
989 u = util.url(abspath) 990 u = urlutil.url(abspath)
990 u.passwd = None 991 u.passwd = None
991 defaulturl = bytes(u) 992 defaulturl = bytes(u)
992 destrepo.vfs.write(b'hgrc', util.tonativeeol(template % defaulturl)) 993 destrepo.vfs.write(b'hgrc', util.tonativeeol(template % defaulturl))
993 destrepo.ui.setconfig(b'paths', b'default', defaulturl, b'clone') 994 destrepo.ui.setconfig(b'paths', b'default', defaulturl, b'clone')
994 995
1267 """ 1268 """
1268 source, branches = parseurl(ui.expandpath(source), opts.get(b'branch')) 1269 source, branches = parseurl(ui.expandpath(source), opts.get(b'branch'))
1269 other = peer(repo, opts, source) 1270 other = peer(repo, opts, source)
1270 cleanupfn = other.close 1271 cleanupfn = other.close
1271 try: 1272 try:
1272 ui.status(_(b'comparing with %s\n') % util.hidepassword(source)) 1273 ui.status(_(b'comparing with %s\n') % urlutil.hidepassword(source))
1273 revs, checkout = addbranchrevs(repo, other, branches, opts.get(b'rev')) 1274 revs, checkout = addbranchrevs(repo, other, branches, opts.get(b'rev'))
1274 1275
1275 if revs: 1276 if revs:
1276 revs = [other.lookup(rev) for rev in revs] 1277 revs = [other.lookup(rev) for rev in revs]
1277 other, chlist, cleanupfn = bundlerepo.getremotechanges( 1278 other, chlist, cleanupfn = bundlerepo.getremotechanges(
1328 hint=_(b"see 'hg help config.paths'"), 1329 hint=_(b"see 'hg help config.paths'"),
1329 ) 1330 )
1330 dest = path.pushloc or path.loc 1331 dest = path.pushloc or path.loc
1331 branches = path.branch, opts.get(b'branch') or [] 1332 branches = path.branch, opts.get(b'branch') or []
1332 1333
1333 ui.status(_(b'comparing with %s\n') % util.hidepassword(dest)) 1334 ui.status(_(b'comparing with %s\n') % urlutil.hidepassword(dest))
1334 revs, checkout = addbranchrevs(repo, repo, branches, opts.get(b'rev')) 1335 revs, checkout = addbranchrevs(repo, repo, branches, opts.get(b'rev'))
1335 if revs: 1336 if revs:
1336 revs = [repo[rev].node() for rev in scmutil.revrange(repo, revs)] 1337 revs = [repo[rev].node() for rev in scmutil.revrange(repo, revs)]
1337 1338
1338 other = peer(repo, opts, dest) 1339 other = peer(repo, opts, dest)