Patchwork D10375: urlutil: extract `parseurl` from `hg` into the new module

login
register
mail settings
Submitter phabricator
Date April 12, 2021, 3:32 p.m.
Message ID <differential-rev-PHID-DREV-j64fuqleioxxsog3c6tf-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48691/
State Superseded
Headers show

Comments

phabricator - April 12, 2021, 3:32 p.m.
marmoute created this revision.
Herald added a reviewer: durin42.
Herald added a reviewer: durin42.
Herald added a reviewer: martinvonz.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  The new module is well fitting for this new code. And this will be useful to
  make the gathered code collaborate more later.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D10375

AFFECTED FILES
  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

CHANGE DETAILS




To: marmoute, durin42, martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/tests/test-hg-parseurl.py b/tests/test-hg-parseurl.py
--- a/tests/test-hg-parseurl.py
+++ b/tests/test-hg-parseurl.py
@@ -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', [])),
         )
 
diff --git a/mercurial/utils/urlutil.py b/mercurial/utils/urlutil.py
--- a/mercurial/utils/urlutil.py
+++ b/mercurial/utils/urlutil.py
@@ -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.
 
diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -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)
diff --git a/mercurial/hg.py b/mercurial/hg.py
--- a/mercurial/hg.py
+++ b/mercurial/hg.py
@@ -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:
diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -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:
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -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)
@@ -3840,7 +3840,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()
@@ -4310,7 +4310,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)
@@ -5389,7 +5389,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))
@@ -7218,7 +7218,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)
@@ -7241,7 +7241,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:
diff --git a/hgext/patchbomb.py b/hgext/patchbomb.py
--- a/hgext/patchbomb.py
+++ b/hgext/patchbomb.py
@@ -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]
diff --git a/hgext/narrow/narrowcommands.py b/hgext/narrow/narrowcommands.py
--- a/hgext/narrow/narrowcommands.py
+++ b/hgext/narrow/narrowcommands.py
@@ -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)
 
diff --git a/hgext/largefiles/storefactory.py b/hgext/largefiles/storefactory.py
--- a/hgext/largefiles/storefactory.py
+++ b/hgext/largefiles/storefactory.py
@@ -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
diff --git a/hgext/infinitepush/__init__.py b/hgext/infinitepush/__init__.py
--- a/hgext/infinitepush/__init__.py
+++ b/hgext/infinitepush/__init__.py
@@ -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()
diff --git a/hgext/histedit.py b/hgext/histedit.py
--- a/hgext/histedit.py
+++ b/hgext/histedit.py
@@ -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)