pub fn parse_git_url(url: &str) -> Result<(String, String)>Expand description
Parses a Git URL into owner and repository name components.
This function extracts the repository owner (user/organization) and repository name from various Git URL formats. It handles the most common Git URL patterns used across different hosting platforms and local repositories.
§Arguments
url- The Git repository URL to parse
§Return Value
Returns a tuple (owner, repository_name) where:
owneris the user, organization, or “local” for local repositoriesrepository_nameis the repository name (with.gitsuffix removed)
§Supported URL Formats
§HTTPS URLs
https://github.com/rust-lang/cargo.git→("rust-lang", "cargo")https://gitlab.com/group/project.git→ `(“group”, “project”)https://bitbucket.org/user/repo.git→ `(“user”, “repo”)
§SSH URLs
git@github.com:rust-lang/cargo.git→("rust-lang", "cargo")git@gitlab.com:group/project.git→("group", "project")
§Local URLs
file:///path/to/repo.git→("local", "repo")/absolute/path/to/repo→("local", "repo")./relative/path/repo.git→("local", "repo")
§Examples
use ccpm::git::parse_git_url;
// Parse GitHub URL
let (owner, repo) = parse_git_url("https://github.com/rust-lang/cargo.git")?;
assert_eq!(owner, "rust-lang");
assert_eq!(repo, "cargo");
// Parse SSH URL
let (owner, repo) = parse_git_url("git@github.com:user/project.git")?;
assert_eq!(owner, "user");
assert_eq!(repo, "project");
// Parse local repository
let (owner, repo) = parse_git_url("/home/user/my-repo")?;
assert_eq!(owner, "local");
assert_eq!(repo, "my-repo");§Use Cases
- Cache directory naming: Generate consistent cache paths
- Repository identification: Create unique identifiers for repositories
- Metadata extraction: Extract repository information for display
- Path generation: Create filesystem-safe directory names
§Cache Integration Example
use ccpm::git::parse_git_url;
use std::path::PathBuf;
let url = "https://github.com/rust-lang/cargo.git";
let (owner, repo) = parse_git_url(url)?;
// Create cache directory path
let cache_path = PathBuf::from("/home/user/.ccpm/cache")
.join(&owner)
.join(&repo);
println!("Cache location: {}", cache_path.display());
// Output: Cache location: /home/user/.ccpm/cache/rust-lang/cargo§Authentication Handling
The parser handles URLs with embedded authentication but extracts only the repository components:
use ccpm::git::parse_git_url;
// Authentication is ignored in parsing
let (owner, repo) = parse_git_url("https://token:value@github.com/user/repo.git")?;
assert_eq!(owner, "user");
assert_eq!(repo, "repo");§Errors
Returns an error if:
- The URL format is not recognized
- The URL doesn’t contain sufficient path components
- The URL structure doesn’t match expected patterns
§Platform Considerations
The parser handles platform-specific path formats:
- Windows: Supports backslash separators in local paths
- Unix: Handles standard forward slash separators
- All platforms: Normalizes path separators internally