Git URL Parse
giturlparse is a Python module designed for parsing and rewriting Git repository URLs. It extracts components like host, owner, repository name, and platform from various Git URL formats (HTTPS, SSH, Git protocol). The current version is 0.14.0, with a release cadence that is active but not rigidly frequent, focusing on feature additions and bug fixes as needed.
Common errors
-
ModuleNotFoundError: No module named 'giturlparse'
cause The 'giturlparse' package is not installed in your Python environment or is not accessible via your Python path.fixInstall the package using pip: `pip install giturlparse` -
giturlparse.ParserError: Unable to parse URL: <your_invalid_url>
cause The input string provided to the `parse` function is not a recognized or valid Git repository URL format.fixEnsure the URL string is a correctly formatted Git URL (e.g., HTTPS, SSH, Git protocol) that `giturlparse` can process. Catch `ParserError` to handle invalid inputs gracefully. -
AttributeError: 'NoneType' object has no attribute '...' (e.g., 'some_method')
cause You are attempting to access a method or attribute on a parsed URL component (like `url2http`) that returned `None` because the corresponding URL type or attribute was not present or couldn't be generated from the parsed Git URL.fixAlways check if the attribute or method's result is not `None` before attempting to use it, especially for optional components or rewrite functions. For example, `if p.url2http: print(p.url2http)`.
Warnings
- breaking Python 2 support was dropped in version 0.10.0. Installations targeting Python 2 must use versions prior to 0.10.0.
- gotcha The parser logic, especially for non-GitHub platforms (like Bitbucket, GitLab) and URLs containing authentication (username:access_token), has evolved and been refined across multiple versions. Older versions may fail to parse or incorrectly parse newer or specific valid Git URL patterns.
- breaking The `rewrite` method for `GitUrlParsed` objects was introduced in version 0.10.0. Older versions of `giturlparse` (prior to 0.10.0) do not have this method, leading to an `AttributeError` when attempting to use it.
- breaking The `rewrite` method was removed from `GitUrlParsed` objects in version 0.14.0. Code attempting to use `parsed_url.rewrite(...)` will raise an `AttributeError`.
Install
-
pip install giturlparse
Imports
- parse
from giturlparse import parse
- GitUrlParsed
from giturlparse import GitUrlParsed
Quickstart
from giturlparse import parse
# Example 1: GitHub SSH URL
url_ssh = parse('git@github.com:nephila/giturlparse.git')
print(f"SSH URL: {url_ssh.url}")
print(f"Owner: {url_ssh.owner}, Name: {url_ssh.name}, Platform: {url_ssh.platform}")
# Example 2: GitLab HTTPS URL with username:token
url_https = parse('https://user:token@gitlab.com/group/repo.git')
print(f"HTTPS URL: {url_https.url}")
print(f"Owner: {url_https.owner}, Name: {url_https.name}, Resource: {url_https.resource}")
# Example 3: Rewriting a URL (feature from v0.14.0)
parsed_url = parse('git@github.com:oldowner/oldrepo.git')
rewritten_url = parsed_url.rewrite(owner='newowner', name='newrepo')
print(f"Rewritten URL (SSH): {rewritten_url.format('ssh')}")
print(f"Rewritten URL (HTTPS): {rewritten_url.format('https')}")