Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
Suggest the git configured default branch during init
Sekhat Temporus committed 11 months ago
commit dad5630030473de840e65bca37bae6fe61f70b59
parent 41f9048d96596473600e6c77f041d29cd3de1d6f
1 file changed +15 -7
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))?;