From 449b23b698d7d4b13909667a49a0698eb495eeaa Mon Sep 17 00:00:00 2001 From: Vitalii Dmitriev Date: Mon, 18 Dec 2023 11:25:16 +0200 Subject: [PATCH] manifest_xml: fix url normalization for inits and remotes Before the change, repo normalizes the urls with a following format only: git@github.com:foo/bar It doesn't cover the following case: Results to: error: Cannot fetch somerepo from ssh://git@github.com/org/git@github.com:org/somerepo Current change fixes it by normalizing this format: git@github.com:foo Test: ./run_tests tests/test_manifest_xml.py Change-Id: I1ad0f5df0d52c0b7229ba4c9a4db4eecb5c1a003 Signed-off-by: Vitalii Dmitriev Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/398337 Commit-Queue: Vitalii Dmitriev Tested-by: Vitalii Dmitriev Reviewed-by: Mike Frysinger --- manifest_xml.py | 4 ++-- tests/test_manifest_xml.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/manifest_xml.py b/manifest_xml.py index 7e533a02d..4f7521225 100644 --- a/manifest_xml.py +++ b/manifest_xml.py @@ -133,8 +133,8 @@ def normalize_url(url: str) -> str: url = url.rstrip("/") parsed_url = urllib.parse.urlparse(url) - # This matches patterns like "git@github.com:foo/bar". - scp_like_url_re = r"^[^/:]+@[^/:]+:[^/]+/" + # This matches patterns like "git@github.com:foo". + scp_like_url_re = r"^[^/:]+@[^/:]+:[^/]+" # If our URL is missing a schema and matches git's # SCP-like syntax we should convert it to a proper diff --git a/tests/test_manifest_xml.py b/tests/test_manifest_xml.py index 6423bb967..3f03272ad 100644 --- a/tests/test_manifest_xml.py +++ b/tests/test_manifest_xml.py @@ -1139,6 +1139,9 @@ def test_has_trailing_slash(self): "http://foo.com/bar/baz", manifest_xml.normalize_url(url) ) + url = "http://foo.com/bar/" + self.assertEqual("http://foo.com/bar", manifest_xml.normalize_url(url)) + def test_has_leading_slash(self): """SCP-like syntax except a / comes before the : which git disallows.""" url = "/git@foo.com:bar/baf" @@ -1157,9 +1160,15 @@ def test_has_no_scheme(self): url = "foo.com/baf/bat" self.assertEqual(url, manifest_xml.normalize_url(url)) + url = "foo.com/baf" + self.assertEqual(url, manifest_xml.normalize_url(url)) + url = "git@foo.com/baf/bat" self.assertEqual(url, manifest_xml.normalize_url(url)) + url = "git@foo.com/baf" + self.assertEqual(url, manifest_xml.normalize_url(url)) + url = "/file/path/here" self.assertEqual(url, manifest_xml.normalize_url(url)) @@ -1168,3 +1177,30 @@ def test_has_no_scheme_matches_scp_like_syntax(self): self.assertEqual( "ssh://git@foo.com/bar/baf", manifest_xml.normalize_url(url) ) + + url = "git@foo.com:bar/" + self.assertEqual( + "ssh://git@foo.com/bar", manifest_xml.normalize_url(url) + ) + + def test_remote_url_resolution(self): + remote = manifest_xml._XmlRemote( + name="foo", + fetch="git@github.com:org2/", + manifestUrl="git@github.com:org2/custom_manifest.git", + ) + self.assertEqual("ssh://git@github.com/org2", remote.resolvedFetchUrl) + + remote = manifest_xml._XmlRemote( + name="foo", + fetch="ssh://git@github.com/org2/", + manifestUrl="git@github.com:org2/custom_manifest.git", + ) + self.assertEqual("ssh://git@github.com/org2", remote.resolvedFetchUrl) + + remote = manifest_xml._XmlRemote( + name="foo", + fetch="git@github.com:org2/", + manifestUrl="ssh://git@github.com/org2/custom_manifest.git", + ) + self.assertEqual("ssh://git@github.com/org2", remote.resolvedFetchUrl)