diff --git a/radicle-cli/examples/rad-init-no-git.md b/radicle-cli/examples/rad-init-no-git.md new file mode 100644 index 00000000..03c488c8 --- /dev/null +++ b/radicle-cli/examples/rad-init-no-git.md @@ -0,0 +1,7 @@ +If you try to `init` from a directory that doesn't contain a Git repository, +it will fail: + +``` (fail) +$ rad init +✗ Initialization failed: a Git repository was not found at the current path +``` diff --git a/radicle-cli/src/commands/init.rs b/radicle-cli/src/commands/init.rs index 45a3e78d..1b2ef6c2 100644 --- a/radicle-cli/src/commands/init.rs +++ b/radicle-cli/src/commands/init.rs @@ -180,6 +180,13 @@ pub fn init(options: Options, profile: &profile::Profile) -> anyhow::Result<()> let path = options.path.unwrap_or_else(|| cwd.clone()); let path = path.as_path().canonicalize()?; let interactive = options.interactive; + let repo = match git::Repository::open(&path) { + Ok(r) => r, + Err(e) if radicle::git::ext::is_not_found_err(&e) => { + anyhow::bail!("a Git repository was not found at the current path") + } + Err(e) => return Err(e.into()), + }; term::headline(format!( "Initializing{}radicle 👾 project in {}", @@ -195,7 +202,6 @@ pub fn init(options: Options, profile: &profile::Profile) -> anyhow::Result<()> } )); - let repo = git::Repository::open(&path)?; if let Ok((remote, _)) = git::rad_remote(&repo) { if let Some(remote) = remote.url() { bail!("repository is already initialized with remote {remote}"); diff --git a/radicle-cli/tests/commands.rs b/radicle-cli/tests/commands.rs index 3437f6db..27f20361 100644 --- a/radicle-cli/tests/commands.rs +++ b/radicle-cli/tests/commands.rs @@ -127,6 +127,21 @@ fn rad_init() { .unwrap(); } +#[test] +fn rad_init_no_git() { + let mut environment = Environment::new(); + let profile = environment.profile("alice"); + let working = tempfile::tempdir().unwrap(); + + test( + "examples/rad-init-no-git.md", + working.path(), + Some(&profile.home), + [], + ) + .unwrap(); +} + #[test] fn rad_inspect() { let mut environment = Environment::new();