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.
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.
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')}")