Git URL Parse
git-url-parse is a Python library designed for robustly parsing Git repository URLs, extracting components such as the host, owner, repository name, and protocol. It supports various Git URL formats including HTTP/S, SSH, and Git protocol. The current version is 1.2.2. While the project repository shows active maintenance with commits, official PyPI releases occur infrequently.
Common errors
-
ModuleNotFoundError: No module named 'giturlparse'
cause The package was installed as `git-url-parse`, but an incorrect module name (e.g., `giturlparse` or `git-url-parse`) was used in the import statement.fixThe correct import statement is `from git_url_parse import GitUrlParse`. -
AttributeError: module 'git_url_parse' has no attribute 'parse'
cause The `parse` method is part of the `GitUrlParse` class, not a top-level function in the `git_url_parse` module. You are trying to call it without instantiating the class.fixFirst, import the class: `from git_url_parse import GitUrlParse`. Then, create an instance and call the method: `parsed_url = GitUrlParse().parse(url)`.
Warnings
- breaking The v1.0.0 release introduced a significant breaking change, transitioning the API to a class-based approach. Older code that relied on direct function calls from the module root will no longer work.
- gotcha The PyPI package name uses hyphens (`git-url-parse`), but the Python module name uses underscores (`git_url_parse`). Attempting to import with hyphens will result in a ModuleNotFoundError.
- gotcha The main parsing method, `parse()`, must be called on an instance of the `GitUrlParse` class. Directly calling `git_url_parse.parse()` (as a module-level function) will raise an `AttributeError`.
- gotcha While the GitHub repository for git-url-parse is actively maintained with recent commits, the official PyPI package (v1.2.2) has not been updated since September 2022. This means recent bug fixes or minor features in the `main` branch may not be available via `pip install`.
Install
-
pip install git-url-parse
Imports
- GitUrlParse
import git_url_parse
from git_url_parse import GitUrlParse
Quickstart
from git_url_parse import GitUrlParse
# Example 1: HTTPS URL
url = "https://github.com/retr0h/git-url-parse.git"
parsed_url = GitUrlParse().parse(url)
print(f"Host: {parsed_url.host}")
print(f"Owner: {parsed_url.owner}")
print(f"Repo: {parsed_url.repo}")
print(f"Protocol: {parsed_url.protocol}")
print(f"Name: {parsed_url.name}")
print(f"Token: {parsed_url.token}") # Empty for public URLs