1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-08-22 14:38:53 +02:00

Add gitea_api.Git.urlparse() for parsing git urls

This commit is contained in:
2025-03-31 22:58:09 +02:00
parent 61d12837d5
commit 292030dbb4
2 changed files with 68 additions and 0 deletions

View File

@@ -9,6 +9,38 @@ from typing import Tuple
class Git:
@staticmethod
def urlparse(url) -> urllib.parse.ParseResult:
"""
Parse git url.
Supported formats:
- https://example.com/owner/repo.git
- https://example.com:1234/owner/repo.git
- example.com/owner/repo.git
- user@example.com:owner/repo.git
- user@example.com:1234:owner/repo.git"
"""
# try ssh clone url first
pattern = r"(?P<netloc>[^@:]+@[^@:]+(:[0-9]+)?):(?P<path>.+)"
match = re.match(pattern, url)
if match:
scheme = ""
netloc = match.groupdict()["netloc"]
path = match.groupdict()["path"]
params = ''
query = ''
fragment = ''
result = urllib.parse.ParseResult(scheme, netloc, path, params, query, fragment)
return result
result = urllib.parse.urlparse(url)
if not result.netloc:
# empty netloc is most likely an error, prepend and then discard scheme to trick urlparse()
result = urllib.parse.urlparse("https://" + url)
result = urllib.parse.ParseResult("", *list(result)[1:])
return result
def __init__(self, workdir):
self.abspath = os.path.abspath(workdir)