cli: Improve `rad init` error when not in Git repo

This commit is contained in:
cloudhead 2023-08-31 10:57:37 +02:00
parent a84555955d
commit 46b0dfb11c
No known key found for this signature in database
3 changed files with 29 additions and 1 deletions

View File

@ -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
```

View File

@ -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}");

View File

@ -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();