feat: matcha new --template-url via shallow git clone

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-11 13:06:19 -05:00
1 parent c382602ca7
commit 8d31faa27e
2 files changed
+51 -5

No files matched your search

+36 -2
View File
@@ -15,9 +15,43 @@ pub fn run(name: &str, template_url: Option<&str>) -> std::io::Result<()> {
std::fs::create_dir_all(dest)?;
template::write_starter(dest, name)?;
}
Some(_) => return Err(Error::new(ErrorKind::Unsupported,
"--template-url not implemented yet")),
Some(url) => clone_template(url, dest)?,
}
println!("✓ created {name}\n cd {name} && matcha run");
Ok(())
}
fn resolve_url(spec: &str) -> String {
if let Some(rest) = spec.strip_prefix("gh:") {
format!("https://github.com/{rest}")
} else {
spec.to_string()
}
}
fn clone_template(url: &str, dest: &Path) -> std::io::Result<()> {
let status = std::process::Command::new("git")
.args(["clone", "--depth", "1", &resolve_url(url)])
.arg(dest)
.status()?;
if !status.success() {
return Err(Error::other("git clone failed"));
}
std::fs::remove_dir_all(dest.join(".git")).ok();
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gh_shorthand_expands() {
assert_eq!(resolve_url("gh:green-tea/tmpl"),
"https://github.com/green-tea/tmpl");
}
#[test]
fn full_url_passes_through() {
assert_eq!(resolve_url("https://example.com/x.git"),
"https://example.com/x.git");
}
}