feat: support short git URLs
Something like git@github.com:dadada/dadada.git
This commit is contained in:
parent
d83955f001
commit
9161f5799d
2 changed files with 37 additions and 6 deletions
|
|
@ -55,7 +55,7 @@ fn main() -> Result<()> {
|
|||
match cli.command {
|
||||
Commands::Resolve { root, repo } => {
|
||||
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 } => {
|
||||
let root = resolve_root(root)?;
|
||||
|
|
|
|||
41
src/root.rs
41
src/root.rs
|
|
@ -75,12 +75,12 @@ impl SrcRoot {
|
|||
|
||||
/// # Errors
|
||||
/// File system errors
|
||||
pub fn resolve_repo(&self, arg: String) -> Result<impl AsRef<Path>> {
|
||||
if let Ok(url) = Url::parse(&arg) {
|
||||
self.ensure_repo_checkout(&url)
|
||||
pub fn resolve_repo(&self, arg: &str) -> Result<impl AsRef<Path>> {
|
||||
if let Some(found) = self.search_repo(arg)? {
|
||||
Ok(found)
|
||||
} else {
|
||||
let slug: OsString = arg.into();
|
||||
self.search_repo(slug)?.context("Repo not found")
|
||||
let url = normalize_url(arg)?;
|
||||
self.ensure_repo_checkout(&url)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -89,3 +89,34 @@ impl SrcRoot {
|
|||
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()),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue