cli: Add canonical remote to init and checkout

Add a remote that points to the canonical upstream.
This commit is contained in:
Alexis Sellier
2023-04-24 15:42:00 +02:00
parent d6cebf613f
commit 6346931ce2
14 changed files with 98 additions and 99 deletions
+11 -18
View File
@@ -3,33 +3,26 @@ use radicle_term::{Element, Table};
use crate::git;
use crate::terminal as term;
#[inline]
fn format_direction(d: &git::Direction) -> String {
match d {
git::Direction::Fetch => "fetch".to_owned(),
git::Direction::Push => "push".to_owned(),
}
}
pub fn run(repo: &git::Repository) -> anyhow::Result<()> {
let mut table = Table::default();
let remotes = git::rad_remotes(repo)?;
for r in remotes {
for spec in r.refspecs() {
let dir = spec.direction();
let url = r.url.clone();
let name = r.name.clone();
let nid_row = url.namespace.map_or(
term::format::dim("This is the canonical upstream".to_string()),
|namespace| term::format::highlight(namespace.to_string()),
for (dir, url) in [("fetch", Some(r.url)), ("push", r.pushurl)] {
let Some(url) = url else {
continue;
};
let nid = url.namespace.map_or(
term::format::dim("the canonical upstream".to_string()).italic(),
|namespace| term::format::tertiary(namespace.to_string()),
);
table.push([
term::format::badge_positive(format_direction(&dir)),
term::format::highlight(name.to_owned()),
nid_row,
term::format::bold(r.name.clone()),
nid,
term::format::parens(term::format::secondary(dir.to_owned())),
]);
}
}
table.print();
Ok(())
}
+9 -6
View File
@@ -76,13 +76,10 @@ pub enum RemoteError {
pub struct Remote<'a> {
pub(crate) name: String,
pub(crate) url: radicle::git::Url,
inner: git2::Remote<'a>,
}
pub(crate) pushurl: Option<radicle::git::Url>,
impl Remote<'_> {
pub fn refspecs(&self) -> git2::Refspecs {
self.inner.refspecs()
}
#[allow(dead_code)]
inner: git2::Remote<'a>,
}
impl<'a> TryFrom<git2::Remote<'a>> for Remote<'a> {
@@ -92,10 +89,16 @@ impl<'a> TryFrom<git2::Remote<'a>> for Remote<'a> {
let url = value.url().map_or(Err(RemoteError::MissingUrl), |url| {
Ok(radicle::git::Url::from_str(url)?)
})?;
let pushurl = value
.pushurl()
.map(radicle::git::Url::from_str)
.transpose()?;
let name = value.name().ok_or(RemoteError::MissingName)?;
Ok(Self {
name: name.to_owned(),
url,
pushurl,
inner: value,
})
}
+1 -1
View File
@@ -22,7 +22,7 @@ impl<'a> SetupRemote<'a> {
/// Run the setup for the given peer.
pub fn run(&self, node: NodeId) -> anyhow::Result<Option<(Remote, RefString)>> {
let url = radicle::git::Url::from(self.project).with_namespace(node);
let mut remote = radicle::git::configure_remote(self.repo, &node.to_string(), &url)?;
let mut remote = radicle::git::configure_remote(self.repo, &node.to_string(), &url, &url)?;
// Fetch the refs into the working copy.
if self.fetch {