From befc76e0304f1891ce5fe7921acf2c486d5a5054 Mon Sep 17 00:00:00 2001 From: cloudhead Date: Fri, 1 Sep 2023 21:35:53 +0200 Subject: [PATCH] cli: Move `init` errors up so it fails sooner --- radicle-cli/examples/rad-init-no-git.md | 15 ++++++++++++ radicle-cli/examples/rad-init.md | 7 ++++++ radicle-cli/src/commands/init.rs | 31 +++++++++++-------------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/radicle-cli/examples/rad-init-no-git.md b/radicle-cli/examples/rad-init-no-git.md index 6d68311e..3b9b8bb0 100644 --- a/radicle-cli/examples/rad-init-no-git.md +++ b/radicle-cli/examples/rad-init-no-git.md @@ -5,3 +5,18 @@ it will fail: $ rad init ✗ Error: a Git repository was not found at the current path ``` + +Ok so let's initialize one. + +``` +$ git init -q +``` + +Now we try again. + +``` (fail) +$ rad init +✗ Error: repository head must point to a commit +``` + +Looks like we need a commit. diff --git a/radicle-cli/examples/rad-init.md b/radicle-cli/examples/rad-init.md index cd0f6613..5ce1833e 100644 --- a/radicle-cli/examples/rad-init.md +++ b/radicle-cli/examples/rad-init.md @@ -22,6 +22,13 @@ You can start your node with `rad node start`. To push changes, run `git push`. ``` +If we try to initialize it again, we get an error: + +``` (fail) +$ rad init +✗ Error: repository is already initialized with remote rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji +``` + Projects can be listed with the `ls` command: ``` diff --git a/radicle-cli/src/commands/init.rs b/radicle-cli/src/commands/init.rs index b268b4fd..04c5fb94 100644 --- a/radicle-cli/src/commands/init.rs +++ b/radicle-cli/src/commands/init.rs @@ -188,6 +188,18 @@ pub fn init(options: Options, profile: &profile::Profile) -> anyhow::Result<()> Err(e) => return Err(e.into()), }; + if let Ok((remote, _)) = git::rad_remote(&repo) { + if let Some(remote) = remote.url() { + bail!("repository is already initialized with remote {remote}"); + } + } + + let head: String = repo + .head() + .ok() + .and_then(|head| head.shorthand().map(|h| h.to_owned())) + .ok_or_else(|| anyhow!("repository head must point to a commit"))?; + term::headline(format!( "Initializing{}radicle 👾 project in {}", if let Some(visibility) = &options.visibility { @@ -206,21 +218,6 @@ pub fn init(options: Options, profile: &profile::Profile) -> anyhow::Result<()> } )); - // TODO: Move up. - if let Ok((remote, _)) = git::rad_remote(&repo) { - if let Some(remote) = remote.url() { - bail!("repository is already initialized with remote {remote}"); - } - } - - // TODO: Move up. - let signer = term::signer(profile)?; - let head: String = repo - .head() - .ok() - .and_then(|head| head.shorthand().map(|h| h.to_owned())) - .ok_or_else(|| anyhow!("error: repository head must point to a commit"))?; - let name = options.name.unwrap_or_else(|| { let default = path.file_name().map(|f| f.to_string_lossy().to_string()); term::input( @@ -258,6 +255,7 @@ pub fn init(options: Options, profile: &profile::Profile) -> anyhow::Result<()> Visibility::from_str(selected)? }; + let signer = term::signer(profile)?; let mut node = radicle::Node::new(profile.socket()); let mut spinner = term::spinner("Initializing..."); let mut push_cmd = String::from("git push"); @@ -333,9 +331,6 @@ pub fn init(options: Options, profile: &profile::Profile) -> anyhow::Result<()> Err(err) => { spinner.failed(); anyhow::bail!(err); - - // TODO: Handle error: "this repository is already initialized with remote {}" - // TODO: Handle error: "the `{}` branch was either not found, or has no commits" } }