Skip to main content

radicle_cli/commands/remote/
add.rs

1use std::str::FromStr;
2
3use radicle::Profile;
4use radicle::git;
5use radicle::git::fmt::RefString;
6use radicle::prelude::*;
7
8use crate::commands::checkout;
9use crate::commands::follow;
10use crate::commands::sync;
11use crate::node::SyncSettings;
12use crate::project::SetupRemote;
13
14pub fn run(
15    rid: RepoId,
16    nid: &PublicKey,
17    name: Option<RefString>,
18    tracking: Option<BranchName>,
19    profile: &Profile,
20    repo: &git::raw::Repository,
21    fetch: bool,
22    sync: bool,
23) -> anyhow::Result<()> {
24    if sync {
25        let mut node = radicle::Node::new(profile.socket_from_env());
26
27        if !profile.policies()?.is_following(nid)? {
28            let alias = name.as_ref().and_then(|n| Alias::from_str(n.as_str()).ok());
29
30            follow::follow(*nid, alias, &mut node, profile)?;
31            sync::fetch(
32                rid,
33                SyncSettings::default().with_profile(profile),
34                &mut node,
35                profile,
36            )?;
37        }
38    }
39    let aliases = profile.aliases();
40    let setup = SetupRemote {
41        rid,
42        tracking,
43        fetch,
44        repo,
45    };
46    checkout::setup_remote(&setup, nid, name, &aliases)?;
47
48    Ok(())
49}