Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
Suggest the git configured default branch during init
Merged stemporus opened 1 year ago
2 files changed +16 -8 85ddcace 37ea8176
modified radicle-cli/examples/rad-init-no-git.md
@@ -16,7 +16,7 @@ Now we try again.

``` (fail)
$ rad init
-
✗ Error: repository head must point to a commit
+
✗ Error: repository must have it's default branch or it's current head pointing to a commit
```

Looks like we need a commit.
modified radicle-cli/src/commands/init.rs
@@ -12,6 +12,7 @@ use serde_json as json;

use radicle::crypto::ssh;
use radicle::explorer::ExplorerUrl;
+
use radicle::git::raw::BranchType;
use radicle::git::RefString;
use radicle::identity::project::ProjectName;
use radicle::identity::{Doc, RepoId, Visibility};
@@ -216,11 +217,18 @@ pub fn init(
        .unwrap_or_else(|| repo.path())
        .canonicalize()?;
    let interactive = options.interactive;
-
    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"))?;
+

+
    let default_branch = {
+
        let config = repo.config().and_then(|mut c| c.snapshot()).ok();
+

+
        config.as_ref()
+
            .and_then(|config| config.get_str("init.defaultbranch").ok())
+
            .and_then(|branch| repo.find_branch(branch, BranchType::Local).ok())
+
            .map(|branch| branch.into_reference())
+
            .or_else(|| repo.head().ok())
+
            .and_then(|branch| branch.shorthand().map(ToOwned::to_owned))
+
            .ok_or_else(|| anyhow!("repository must have it's default branch or it's current head pointing to a commit"))?
+
    };

    term::headline(format!(
        "Initializing{}radicle 👾 repository in {}..",
@@ -252,10 +260,10 @@ pub fn init(
        Some(branch) => branch,
        None if interactive.yes() => term::input(
            "Default branch",
-
            Some(head),
+
            Some(default_branch),
            Some("Please specify an existing branch"),
        )?,
-
        None => head,
+
        None => default_branch,
    };
    let branch = RefString::try_from(branch.clone())
        .map_err(|e| anyhow!("invalid branch name {:?}: {}", branch, e))?;