feat: support short git URLs

Something like git@github.com:dadada/dadada.git
This commit is contained in:
Tim Schubert 2025-08-24 14:41:17 +02:00
parent d83955f001
commit 9161f5799d
No known key found for this signature in database
2 changed files with 37 additions and 6 deletions

View file

@ -55,7 +55,7 @@ fn main() -> Result<()> {
match cli.command { match cli.command {
Commands::Resolve { root, repo } => { Commands::Resolve { root, repo } => {
let root = SrcRoot::new(resolve_root(root)?); let root = SrcRoot::new(resolve_root(root)?);
println!("{}", root.resolve_repo(repo)?.as_ref().display()); println!("{}", root.resolve_repo(repo.as_str())?.as_ref().display());
} }
Commands::Setup { root } => { Commands::Setup { root } => {
let root = resolve_root(root)?; let root = resolve_root(root)?;

View file

@ -75,12 +75,12 @@ impl SrcRoot {
/// # Errors /// # Errors
/// File system errors /// File system errors
pub fn resolve_repo(&self, arg: String) -> Result<impl AsRef<Path>> { pub fn resolve_repo(&self, arg: &str) -> Result<impl AsRef<Path>> {
if let Ok(url) = Url::parse(&arg) { if let Some(found) = self.search_repo(arg)? {
self.ensure_repo_checkout(&url) Ok(found)
} else { } else {
let slug: OsString = arg.into(); let url = normalize_url(arg)?;
self.search_repo(slug)?.context("Repo not found") self.ensure_repo_checkout(&url)
} }
} }
@ -89,3 +89,34 @@ impl SrcRoot {
Self { root_path: path } Self { root_path: path }
} }
} }
fn normalize_url(url: &str) -> Result<Url> {
match Url::parse(url) {
Ok(url) => Ok(url),
Err(e @ url::ParseError::RelativeUrlWithoutBase) => {
let normalized_url = &format!("ssh://{url}");
normalize_ssh(e, normalized_url)
}
Err(e @ url::ParseError::InvalidPort) => normalize_ssh(e, url),
Err(e) => Err(e.into()),
}
}
fn normalize_ssh(e: url::ParseError, url: &str) -> Result<Url> {
match Url::parse(url) {
Ok(o) => Ok(o),
Err(url::ParseError::InvalidPort) => {
let mut split = url.splitn(3, ':');
let mut normalized_url = String::with_capacity(url.len());
normalized_url.push_str(split.next().context(e)?);
normalized_url.push(':');
normalized_url.push_str(split.next().context(e)?);
normalized_url.push('/');
let tail = split.next().context(e)?;
normalized_url.push_str(tail);
let normalized_url = Url::parse(&normalized_url)?;
Ok(normalized_url)
}
Err(e) => Err(e.into()),
}
}