From 9a0d46abe28de15fc22a9f079272ed1f434780a8 Mon Sep 17 00:00:00 2001 From: "finswimmer77@gmail.com" Date: Fri, 17 Jan 2020 07:17:24 +0100 Subject: [PATCH 1/5] fix (git): match for `\w` instead of `.` for getting user --- poetry/vcs/git.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/poetry/vcs/git.py b/poetry/vcs/git.py index 780a687f40b..47b5c65f615 100644 --- a/poetry/vcs/git.py +++ b/poetry/vcs/git.py @@ -22,7 +22,7 @@ re.compile( r"^(git\+)?" r"(?Phttps?|git|ssh|rsync|file)://" - r"(?:(?P.+)@)*" + r"(?:(?P\w+)@)*" r"(?P[a-z0-9_.-]*)" r"(:?P[\d]+)?" r"(?P[:/]((?P[\w\-]+)/(?P([\w\-/]+)/)?)?" @@ -31,7 +31,7 @@ r"$" ), re.compile( - r"^(?:(?P.+)@)*" + r"^(?:(?P\w+)@)*" r"(?P[a-z0-9_.-]*)[:]*" r"(?P[\d]+)?" r"(?P/?(?P.+)/(?P([\w\-/]+)/)?(?P.+).git)" From e14edd6ae9168764ae32ca4c165019278d15f5f7 Mon Sep 17 00:00:00 2001 From: "finswimmer77@gmail.com" Date: Mon, 20 Jan 2020 15:21:55 +0100 Subject: [PATCH 2/5] change (vcs.git): hold pattern of the regex parts in a dictionary to be consistent over all regexs --- poetry/vcs/git.py | 94 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 29 deletions(-) diff --git a/poetry/vcs/git.py b/poetry/vcs/git.py index 47b5c65f615..3f73508693c 100644 --- a/poetry/vcs/git.py +++ b/poetry/vcs/git.py @@ -7,46 +7,82 @@ from poetry.utils._compat import decode +pattern_formats = { + "protocol": r"\w+", + "user": r"[a-zA-Z0-9_.-]+", + "resource": r"[a-z0-9_.-]+", + "port": r"\d+", + "path": r"[\w\-/]+", + "name": r"[\w\-]+", + "rev": r"[^@#]+", +} + PATTERNS = [ re.compile( r"(git\+)?" - r"((?P\w+)://)" - r"((?P\w+)@)?" - r"(?P[\w.\-]+)" - r"(:(?P\d+))?" - r"(?P(/(?P\w+)/)" - r"((?P([\w\-/]+)/)?(?P[\w\-]+)(\.git|/)?)?)" - r"([@#](?P[^@#]+))?" - r"$" + r"((?P{protocol})://)" + r"(?:(?P{user})@)*" + r"(?P{resource})" + r"(:(?P{port}))?" + r"(?P(/{path}/)" + r"(?P{name})(\.git|/)?)" + r"([@#](?P{rev}))?" + r"$".format( + protocol=pattern_formats["protocol"], + user=pattern_formats["user"], + resource=pattern_formats["resource"], + port=pattern_formats["port"], + path=pattern_formats["path"], + name=pattern_formats["name"], + rev=pattern_formats["rev"], + ) ), re.compile( r"^(git\+)?" r"(?Phttps?|git|ssh|rsync|file)://" - r"(?:(?P\w+)@)*" - r"(?P[a-z0-9_.-]*)" - r"(:?P[\d]+)?" - r"(?P[:/]((?P[\w\-]+)/(?P([\w\-/]+)/)?)?" - r"((?P[\w\-.]+?)(\.git|/)?)?)" - r"([@#](?P[^@#]+))?" - r"$" + r"(?:(?P{user})@)*" + r"(?P{resource})" + r"(:?P{port})?" + r"(?P[:/]({path})?" + r"((?P{name}?)(\.git|/)?)?)" + r"([@#](?P{rev}))?" + r"$".format( + user=pattern_formats["user"], + resource=pattern_formats["resource"], + port=pattern_formats["port"], + path=pattern_formats["path"], + name=pattern_formats["name"], + rev=pattern_formats["rev"], + ) ), re.compile( - r"^(?:(?P\w+)@)*" - r"(?P[a-z0-9_.-]*)[:]*" - r"(?P[\d]+)?" - r"(?P/?(?P.+)/(?P([\w\-/]+)/)?(?P.+).git)" - r"([@#](?P[^@#]+))?" - r"$" + r"^(?:(?P{user})@)*" + r"(?P{resource})[:]*" + r"(?P{port})?" + r"(?P({path})?(?P.+).git)" + r"([@#](?P{rev}))?" + r"$".format( + user=pattern_formats["user"], + resource=pattern_formats["resource"], + port=pattern_formats["port"], + path=pattern_formats["path"], + rev=pattern_formats["rev"], + ) ), re.compile( - r"((?P\w+)@)?" - r"(?P[\w.\-]+)" - r"[:/]{1,2}" - r"(?P((?P\w+)/)?" - r"(?P([\w\-/]+)/)?" - r"((?P[\w\-]+)(\.git|/)?)?)" - r"([@#](?P[^@#]+))?" - r"$" + r"((?P{user})@)?" + r"(?P{resource})" + r"[:/]{{1,2}}" + r"(?P({path}))?" + r"((?P{name})(\.git|/)?)" + r"([@#](?P{rev}))?" + r"$".format( + user=pattern_formats["user"], + resource=pattern_formats["resource"], + path=pattern_formats["path"], + name=pattern_formats["name"], + rev=pattern_formats["rev"], + ) ), ] From 1c7f1d8e7805a0ce83aa29499d800bf706b778f0 Mon Sep 17 00:00:00 2001 From: "finswimmer77@gmail.com" Date: Tue, 21 Jan 2020 06:28:04 +0100 Subject: [PATCH 3/5] new (vcs.git): test for `parse_url` and some fixes for the regex pattern --- poetry/vcs/git.py | 46 ++++++------ tests/vcs/test_git.py | 162 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 186 insertions(+), 22 deletions(-) diff --git a/poetry/vcs/git.py b/poetry/vcs/git.py index 3f73508693c..baa957ed8a6 100644 --- a/poetry/vcs/git.py +++ b/poetry/vcs/git.py @@ -10,25 +10,24 @@ pattern_formats = { "protocol": r"\w+", "user": r"[a-zA-Z0-9_.-]+", - "resource": r"[a-z0-9_.-]+", + "resource": r"[a-zA-Z0-9_.-]+", "port": r"\d+", - "path": r"[\w\-/]+", + "path": r"[\w\-/\\]+", "name": r"[\w\-]+", "rev": r"[^@#]+", } PATTERNS = [ re.compile( - r"(git\+)?" - r"((?P{protocol})://)" - r"(?:(?P{user})@)*" - r"(?P{resource})" + r"^(git\+)?" + r"(?Phttps?|git|ssh|rsync|file)://" + r"(?:(?P{user})@)?" + r"(?P{resource})?" r"(:(?P{port}))?" - r"(?P(/{path}/)" - r"(?P{name})(\.git|/)?)" + r"(?P[:/\\]({path}[/\\])?" + r"((?P{name}?)(\.git|[/\\])?)?)" r"([@#](?P{rev}))?" r"$".format( - protocol=pattern_formats["protocol"], user=pattern_formats["user"], resource=pattern_formats["resource"], port=pattern_formats["port"], @@ -38,15 +37,16 @@ ) ), re.compile( - r"^(git\+)?" - r"(?Phttps?|git|ssh|rsync|file)://" - r"(?:(?P{user})@)*" - r"(?P{resource})" - r"(:?P{port})?" - r"(?P[:/]({path})?" - r"((?P{name}?)(\.git|/)?)?)" + r"(git\+)?" + r"((?P{protocol})://)" + r"(?:(?P{user})@)?" + r"(?P{resource}:?)" + r"(:(?P{port}))?" + r"(?P({path})" + r"(?P{name})(\.git|/)?)" r"([@#](?P{rev}))?" r"$".format( + protocol=pattern_formats["protocol"], user=pattern_formats["user"], resource=pattern_formats["resource"], port=pattern_formats["port"], @@ -56,16 +56,18 @@ ) ), re.compile( - r"^(?:(?P{user})@)*" - r"(?P{resource})[:]*" - r"(?P{port})?" - r"(?P({path})?(?P.+).git)" + r"^(?:(?P{user})@)?" + r"(?P{resource})" + r"(:(?P{port}))?" + r"(?P([:/]{path}/)" + r"(?P{name})(\.git|/)?)" r"([@#](?P{rev}))?" r"$".format( user=pattern_formats["user"], resource=pattern_formats["resource"], port=pattern_formats["port"], path=pattern_formats["path"], + name=pattern_formats["name"], rev=pattern_formats["rev"], ) ), @@ -73,8 +75,8 @@ r"((?P{user})@)?" r"(?P{resource})" r"[:/]{{1,2}}" - r"(?P({path}))?" - r"((?P{name})(\.git|/)?)" + r"(?P({path})" + r"(?P{name})(\.git|/)?)" r"([@#](?P{rev}))?" r"$".format( user=pattern_formats["user"], diff --git a/tests/vcs/test_git.py b/tests/vcs/test_git.py index 667294ee614..ec9ebdb75b6 100644 --- a/tests/vcs/test_git.py +++ b/tests/vcs/test_git.py @@ -2,6 +2,7 @@ from poetry.vcs.git import Git from poetry.vcs.git import GitUrl +from poetry.vcs.git import ParsedUrl @pytest.mark.parametrize( @@ -74,3 +75,164 @@ ) def test_normalize_url(url, normalized): assert normalized == Git.normalize_url(url) + + +@pytest.mark.parametrize( + "url, parsed", + [ + ( + "git+ssh://user@hostname:project.git#commit", + ParsedUrl( + "ssh", "hostname", ":project.git", "user", None, "project", "commit" + ), + ), + ( + "git+http://user@hostname/project/blah.git@commit", + ParsedUrl( + "http", "hostname", "/project/blah.git", "user", None, "blah", "commit" + ), + ), + ( + "git+https://user@hostname/project/blah.git", + ParsedUrl( + "https", "hostname", "/project/blah.git", "user", None, "blah", None + ), + ), + ( + "git+https://user@hostname:project/blah.git", + ParsedUrl( + "https", "hostname", ":project/blah.git", "user", None, "blah", None + ), + ), + ( + "git+ssh://git@github.com:sdispater/poetry.git#v1.0.27", + ParsedUrl( + "ssh", + "github.com", + ":sdispater/poetry.git", + "git", + None, + "poetry", + "v1.0.27", + ), + ), + ( + "git+ssh://git@github.com:/sdispater/poetry.git", + ParsedUrl( + "ssh", + "github.com", + ":/sdispater/poetry.git", + "git", + None, + "poetry", + None, + ), + ), + ( + "git+ssh://git@github.com:org/repo", + ParsedUrl("ssh", "github.com", ":org/repo", "git", None, "repo", None), + ), + ( + "git+ssh://git@github.com/org/repo", + ParsedUrl("ssh", "github.com", "/org/repo", "git", None, "repo", None), + ), + ( + "git+ssh://foo:22/some/path", + ParsedUrl("ssh", "foo", "/some/path", None, "22", "path", None), + ), + ( + "git@github.com:org/repo", + ParsedUrl(None, "github.com", ":org/repo", "git", None, "repo", None), + ), + ( + "git+https://github.com/sdispater/pendulum", + ParsedUrl( + "https", + "github.com", + "/sdispater/pendulum", + None, + None, + "pendulum", + None, + ), + ), + ( + "git+https://github.com/sdispater/pendulum#7a018f2d075b03a73409e8356f9b29c9ad4ea2c5", + ParsedUrl( + "https", + "github.com", + "/sdispater/pendulum", + None, + None, + "pendulum", + "7a018f2d075b03a73409e8356f9b29c9ad4ea2c5", + ), + ), + ( + "git+ssh://git@git.example.com:b/b.git#v1.0.0", + ParsedUrl("ssh", "git.example.com", ":b/b.git", "git", None, "b", "v1.0.0"), + ), + ( + "git+ssh://git@github.com:sdispater/pendulum.git#foo/bar", + ParsedUrl( + "ssh", + "github.com", + ":sdispater/pendulum.git", + "git", + None, + "pendulum", + "foo/bar", + ), + ), + ( + "git+file:///foo/bar.git", + ParsedUrl("file", None, "/foo/bar.git", None, None, "bar", None), + ), + ( + "git+file://C:\\Users\\hello\\testing.git#zkat/windows-files", + ParsedUrl( + "file", + "C", + ":\\Users\\hello\\testing.git", + None, + None, + "testing", + "zkat/windows-files", + ), + ), + ( + "git+https://git.example.com/sdispater/project/my_repo.git", + ParsedUrl( + "https", + "git.example.com", + "/sdispater/project/my_repo.git", + None, + None, + "my_repo", + None, + ), + ), + ( + "git+ssh://git@git.example.com:sdispater/project/my_repo.git", + ParsedUrl( + "ssh", + "git.example.com", + ":sdispater/project/my_repo.git", + "git", + None, + "my_repo", + None, + ), + ), + ], +) +def test_parse_url(url, parsed): + result = ParsedUrl.parse(url) + assert parsed.name == result.name + assert parsed.pathname == result.pathname + assert parsed.port == result.port + assert parsed.protocol == result.protocol + assert parsed.resource == result.resource + assert parsed.rev == result.rev + assert parsed.url == result.url + assert parsed.user == result.user From c5f3ba4161c9f5fd99ad0be309bd424a7f708ca0 Mon Sep 17 00:00:00 2001 From: "finswimmer77@gmail.com" Date: Tue, 21 Jan 2020 21:18:44 +0100 Subject: [PATCH 4/5] new (vcs.git): test for `parse_url` with string that should fail --- tests/vcs/test_git.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/vcs/test_git.py b/tests/vcs/test_git.py index ec9ebdb75b6..82dee584fbf 100644 --- a/tests/vcs/test_git.py +++ b/tests/vcs/test_git.py @@ -236,3 +236,10 @@ def test_parse_url(url, parsed): assert parsed.rev == result.rev assert parsed.url == result.url assert parsed.user == result.user + + +def test_parse_url_should_fail(): + url = "https://" + "@" * 64 + "!" + + with pytest.raises(ValueError): + result = ParsedUrl.parse(url) From 35291b9a07912cb35da000341ee25f7ee06c1cf9 Mon Sep 17 00:00:00 2001 From: "finswimmer77@gmail.com" Date: Tue, 21 Jan 2020 21:21:55 +0100 Subject: [PATCH 5/5] fix (test.vcs.git): make flake8 happy --- tests/vcs/test_git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/vcs/test_git.py b/tests/vcs/test_git.py index 82dee584fbf..f0157db364e 100644 --- a/tests/vcs/test_git.py +++ b/tests/vcs/test_git.py @@ -242,4 +242,4 @@ def test_parse_url_should_fail(): url = "https://" + "@" * 64 + "!" with pytest.raises(ValueError): - result = ParsedUrl.parse(url) + ParsedUrl.parse(url)