urlutil: extract `parseurl` from `hg` into the new module
authorPierre-Yves David <pierre-yves.david@octobus.net>
Mon, 12 Apr 2021 06:34:54 +0200
changeset 46908 4452cb788404
parent 46907 ffd3e823a7e5
child 46909 067840864f37
urlutil: extract `parseurl` from `hg` 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/D10375
hgext/histedit.py
hgext/infinitepush/__init__.py
hgext/largefiles/storefactory.py
hgext/narrow/narrowcommands.py
hgext/patchbomb.py
mercurial/commands.py
mercurial/debugcommands.py
mercurial/hg.py
mercurial/revset.py
mercurial/utils/urlutil.py
tests/test-hg-parseurl.py
--- a/hgext/histedit.py	Mon Apr 12 03:01:04 2021 +0200
+++ b/hgext/histedit.py	Mon Apr 12 06:34:54 2021 +0200
@@ -1042,7 +1042,7 @@
     if opts is None:
         opts = {}
     dest = ui.expandpath(remote or b'default-push', remote or b'default')
-    dest, branches = hg.parseurl(dest, None)[:2]
+    dest, branches = urlutil.parseurl(dest, None)[:2]
     ui.status(_(b'comparing with %s\n') % urlutil.hidepassword(dest))
 
     revs, checkout = hg.addbranchrevs(repo, repo, branches, None)
--- a/hgext/infinitepush/__init__.py	Mon Apr 12 03:01:04 2021 +0200
+++ b/hgext/infinitepush/__init__.py	Mon Apr 12 06:34:54 2021 +0200
@@ -116,6 +116,7 @@
 from mercurial.utils import (
     procutil,
     stringutil,
+    urlutil,
 )
 
 from mercurial import (
@@ -683,7 +684,9 @@
 def _pull(orig, ui, repo, source=b"default", **opts):
     opts = pycompat.byteskwargs(opts)
     # Copy paste from `pull` command
-    source, branches = hg.parseurl(ui.expandpath(source), opts.get(b'branch'))
+    source, branches = urlutil.parseurl(
+        ui.expandpath(source), opts.get(b'branch')
+    )
 
     scratchbookmarks = {}
     unfi = repo.unfiltered()
--- a/hgext/largefiles/storefactory.py	Mon Apr 12 03:01:04 2021 +0200
+++ b/hgext/largefiles/storefactory.py	Mon Apr 12 06:34:54 2021 +0200
@@ -43,12 +43,12 @@
         # meaning the current directory.
         if repo is None:
             path = ui.expandpath(b'default')
-            path, _branches = hg.parseurl(path)
+            path, _branches = urlutil.parseurl(path)
             remote = hg.peer(repo or ui, {}, path)
         elif path == b'default-push' or path == b'default':
             remote = repo
         else:
-            path, _branches = hg.parseurl(path)
+            path, _branches = urlutil.parseurl(path)
             remote = hg.peer(repo or ui, {}, path)
 
     # The path could be a scheme so use Mercurial's normal functionality
--- a/hgext/narrow/narrowcommands.py	Mon Apr 12 03:01:04 2021 +0200
+++ b/hgext/narrow/narrowcommands.py	Mon Apr 12 06:34:54 2021 +0200
@@ -594,7 +594,7 @@
         # be used for finding local-only changes for narrowing. They will
         # also define the set of revisions to update for widening.
         remotepath = ui.expandpath(remotepath or b'default')
-        url, branches = hg.parseurl(remotepath)
+        url, branches = urlutil.parseurl(remotepath)
         ui.status(_(b'comparing with %s\n') % urlutil.hidepassword(url))
         remote = hg.peer(repo, opts, url)
 
--- a/hgext/patchbomb.py	Mon Apr 12 03:01:04 2021 +0200
+++ b/hgext/patchbomb.py	Mon Apr 12 06:34:54 2021 +0200
@@ -531,7 +531,7 @@
     '''Return the revisions present locally but not in dest'''
     ui = repo.ui
     url = ui.expandpath(dest or b'default-push', dest or b'default')
-    url = hg.parseurl(url)[0]
+    url = urlutil.parseurl(url)[0]
     ui.status(_(b'comparing with %s\n') % urlutil.hidepassword(url))
 
     revs = [r for r in revs if r >= 0]
--- a/mercurial/commands.py	Mon Apr 12 03:01:04 2021 +0200
+++ b/mercurial/commands.py	Mon Apr 12 06:34:54 2021 +0200
@@ -1614,7 +1614,7 @@
         outgoing = discovery.outgoing(repo, common, heads)
     else:
         dest = ui.expandpath(dest or b'default-push', dest or b'default')
-        dest, branches = hg.parseurl(dest, opts.get(b'branch'))
+        dest, branches = urlutil.parseurl(dest, opts.get(b'branch'))
         other = hg.peer(repo, opts, dest)
         revs = [repo[r].hex() for r in revs]
         revs, checkout = hg.addbranchrevs(repo, repo, branches, revs)
@@ -3841,7 +3841,7 @@
     peer = None
     try:
         if source:
-            source, branches = hg.parseurl(ui.expandpath(source))
+            source, branches = urlutil.parseurl(ui.expandpath(source))
             # only pass ui when no repo
             peer = hg.peer(repo or ui, opts, source)
             repo = peer.local()
@@ -4311,7 +4311,7 @@
     cmdutil.check_incompatible_arguments(opts, b'subrepos', [b'bundle'])
 
     if opts.get(b'bookmarks'):
-        source, branches = hg.parseurl(
+        source, branches = urlutil.parseurl(
             ui.expandpath(source), opts.get(b'branch')
         )
         other = hg.peer(repo, opts, source)
@@ -5390,7 +5390,7 @@
     if not sources:
         sources = [b'default']
     for source in sources:
-        source, branches = hg.parseurl(
+        source, branches = urlutil.parseurl(
             ui.expandpath(source), opts.get(b'branch')
         )
         ui.status(_(b'pulling from %s\n') % urlutil.hidepassword(source))
@@ -7225,7 +7225,7 @@
             return
 
     def getincoming():
-        source, branches = hg.parseurl(ui.expandpath(b'default'))
+        source, branches = urlutil.parseurl(ui.expandpath(b'default'))
         sbranch = branches[0]
         try:
             other = hg.peer(repo, {}, source)
@@ -7248,7 +7248,9 @@
         source = sbranch = sother = commoninc = incoming = None
 
     def getoutgoing():
-        dest, branches = hg.parseurl(ui.expandpath(b'default-push', b'default'))
+        dest, branches = urlutil.parseurl(
+            ui.expandpath(b'default-push', b'default')
+        )
         dbranch = branches[0]
         revs, checkout = hg.addbranchrevs(repo, repo, branches, None)
         if source != dest:
--- a/mercurial/debugcommands.py	Mon Apr 12 03:01:04 2021 +0200
+++ b/mercurial/debugcommands.py	Mon Apr 12 06:34:54 2021 +0200
@@ -1060,7 +1060,7 @@
 
     if not remote_revs:
 
-        remoteurl, branches = hg.parseurl(ui.expandpath(remoteurl))
+        remoteurl, branches = urlutil.parseurl(ui.expandpath(remoteurl))
         remote = hg.peer(repo, opts, remoteurl)
         ui.status(_(b'comparing with %s\n') % urlutil.hidepassword(remoteurl))
     else:
@@ -3652,7 +3652,7 @@
             )
         source = b"default"
 
-    source, branches = hg.parseurl(ui.expandpath(source))
+    source, branches = urlutil.parseurl(ui.expandpath(source))
     url = urlutil.url(source)
 
     defaultport = {b'https': 443, b'ssh': 22}
@@ -3762,7 +3762,7 @@
     for backup in backups:
         # Much of this is copied from the hg incoming logic
         source = ui.expandpath(os.path.relpath(backup, encoding.getcwd()))
-        source, branches = hg.parseurl(source, opts.get(b"branch"))
+        source, branches = urlutil.parseurl(source, opts.get(b"branch"))
         try:
             other = hg.peer(repo, opts, source)
         except error.LookupError as ex:
--- a/mercurial/hg.py	Mon Apr 12 03:01:04 2021 +0200
+++ b/mercurial/hg.py	Mon Apr 12 06:34:54 2021 +0200
@@ -132,13 +132,9 @@
 
 def parseurl(path, branches=None):
     '''parse url#branch, returning (url, (branch, branches))'''
-
-    u = urlutil.url(path)
-    branch = None
-    if u.fragment:
-        branch = u.fragment
-        u.fragment = None
-    return bytes(u), (branch, branches or [])
+    msg = b'parseurl(...) moved to mercurial.utils.urlutil'
+    util.nouideprecwarn(msg, b'6.0', stacklevel=2)
+    return urlutil.parseurl(path, branches=branches)
 
 
 schemes = {
@@ -285,7 +281,7 @@
 
     # the sharedpath always ends in the .hg; we want the path to the repo
     source = repo.vfs.split(repo.sharedpath)[0]
-    srcurl, branches = parseurl(source)
+    srcurl, branches = urlutil.parseurl(source)
     srcrepo = repository(repo.ui, srcurl)
     repo.srcrepo = srcrepo
     return srcrepo
@@ -312,7 +308,7 @@
 
     if isinstance(source, bytes):
         origsource = ui.expandpath(source)
-        source, branches = parseurl(origsource)
+        source, branches = urlutil.parseurl(origsource)
         srcrepo = repository(ui, source)
         rev, checkout = addbranchrevs(srcrepo, srcrepo, branches, None)
     else:
@@ -676,7 +672,7 @@
 
     if isinstance(source, bytes):
         origsource = ui.expandpath(source)
-        source, branches = parseurl(origsource, branch)
+        source, branches = urlutil.parseurl(origsource, branch)
         srcpeer = peer(ui, peeropts, source)
     else:
         srcpeer = source.peer()  # in case we were called with a localrepo
@@ -1266,7 +1262,9 @@
         (remoterepo, incomingchangesetlist, displayer) parameters,
     and is supposed to contain only code that can't be unified.
     """
-    source, branches = parseurl(ui.expandpath(source), opts.get(b'branch'))
+    source, branches = urlutil.parseurl(
+        ui.expandpath(source), opts.get(b'branch')
+    )
     other = peer(repo, opts, source)
     cleanupfn = other.close
     try:
--- a/mercurial/revset.py	Mon Apr 12 03:01:04 2021 +0200
+++ b/mercurial/revset.py	Mon Apr 12 06:34:54 2021 +0200
@@ -41,6 +41,7 @@
 from .utils import (
     dateutil,
     stringutil,
+    urlutil,
 )
 
 # helpers for processing parsed tree
@@ -2122,7 +2123,7 @@
         # i18n: "remote" is a keyword
         dest = getstring(l[1], _(b"remote requires a repository path"))
     dest = repo.ui.expandpath(dest or b'default')
-    dest, branches = hg.parseurl(dest)
+    dest, branches = urlutil.parseurl(dest)
 
     other = hg.peer(repo, {}, dest)
     n = other.lookup(q)
--- a/mercurial/utils/urlutil.py	Mon Apr 12 03:01:04 2021 +0200
+++ b/mercurial/utils/urlutil.py	Mon Apr 12 06:34:54 2021 +0200
@@ -445,6 +445,16 @@
     return bytes(u)
 
 
+def parseurl(path, branches=None):
+    '''parse url#branch, returning (url, (branch, branches))'''
+    u = url(path)
+    branch = None
+    if u.fragment:
+        branch = u.fragment
+        u.fragment = None
+    return bytes(u), (branch, branches or [])
+
+
 class paths(dict):
     """Represents a collection of paths and their configs.
 
--- a/tests/test-hg-parseurl.py	Mon Apr 12 03:01:04 2021 +0200
+++ b/tests/test-hg-parseurl.py	Mon Apr 12 06:34:54 2021 +0200
@@ -2,44 +2,48 @@
 
 import unittest
 
-from mercurial import hg
+from mercurial.utils import urlutil
 
 
 class ParseRequestTests(unittest.TestCase):
     def testparse(self):
 
         self.assertEqual(
-            hg.parseurl(b'http://example.com/no/anchor'),
+            urlutil.parseurl(b'http://example.com/no/anchor'),
             (b'http://example.com/no/anchor', (None, [])),
         )
         self.assertEqual(
-            hg.parseurl(b'http://example.com/an/anchor#foo'),
+            urlutil.parseurl(b'http://example.com/an/anchor#foo'),
             (b'http://example.com/an/anchor', (b'foo', [])),
         )
         self.assertEqual(
-            hg.parseurl(b'http://example.com/no/anchor/branches', [b'foo']),
+            urlutil.parseurl(
+                b'http://example.com/no/anchor/branches', [b'foo']
+            ),
             (b'http://example.com/no/anchor/branches', (None, [b'foo'])),
         )
         self.assertEqual(
-            hg.parseurl(b'http://example.com/an/anchor/branches#bar', [b'foo']),
+            urlutil.parseurl(
+                b'http://example.com/an/anchor/branches#bar', [b'foo']
+            ),
             (b'http://example.com/an/anchor/branches', (b'bar', [b'foo'])),
         )
         self.assertEqual(
-            hg.parseurl(
+            urlutil.parseurl(
                 b'http://example.com/an/anchor/branches-None#foo', None
             ),
             (b'http://example.com/an/anchor/branches-None', (b'foo', [])),
         )
         self.assertEqual(
-            hg.parseurl(b'http://example.com/'),
+            urlutil.parseurl(b'http://example.com/'),
             (b'http://example.com/', (None, [])),
         )
         self.assertEqual(
-            hg.parseurl(b'http://example.com'),
+            urlutil.parseurl(b'http://example.com'),
             (b'http://example.com/', (None, [])),
         )
         self.assertEqual(
-            hg.parseurl(b'http://example.com#foo'),
+            urlutil.parseurl(b'http://example.com#foo'),
             (b'http://example.com/', (b'foo', [])),
         )