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:
owner
is the user, organization, or “local” for local repositoriesrepository_name
is the repository name (with.git
suffix 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 agpm_cli::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 agpm_cli::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/.agpm/cache")
.join(&owner)
.join(&repo);
println!("Cache location: {}", cache_path.display());
// Output: Cache location: /home/user/.agpm/cache/rust-lang/cargo
§Authentication Handling
The parser handles URLs with embedded authentication but extracts only the repository components:
use agpm_cli::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