cli: Call `rad sync` on `git push rad`

Instead of `Node::announce_refs`, we call `rad sync` to have the
terminal block while waiting for nodes to fetch the changes.

We also change the use of `git push` in the patch command to not
trigger syncing in that case.
This commit is contained in:
Alexis Sellier 2023-04-24 17:26:45 +02:00
parent fed4e84dd7
commit 7d72b16087
No known key found for this signature in database
8 changed files with 47 additions and 26 deletions

View File

@ -221,7 +221,7 @@ impl TestFormula {
} else {
PathBuf::from(&assertion.command)
};
log::debug!(target: "test", "{path}: Running `{}` in `{}`..", cmd.display(), self.cwd.display());
log::debug!(target: "test", "{path}: Running `{}` with {:?} in `{}`..", cmd.display(), assertion.args, self.cwd.display());
if !self.cwd.exists() {
log::error!(target: "test", "{path}: Directory {} does not exist..", self.cwd.display());

View File

@ -72,6 +72,13 @@ index 0000000..e69de29
```
We can also see that it set an upstream for our patch branch:
```
$ git branch -vv
* flux-capacitor-power 3e674d1 [rad/flux-capacitor-power] Define power requirements
master f2de534 [rad/master] Second commit
```
Wait, let's add a README too! Just for fun.
```

View File

@ -21,6 +21,7 @@ use anyhow::anyhow;
use radicle::cob::patch;
use radicle::cob::patch::PatchId;
use radicle::storage::git::transport;
use radicle::{prelude::*, Node};
use crate::commands::rad_sync as sync;
@ -258,6 +259,8 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let profile = ctx.profile()?;
let repository = profile.storage.repository(id)?;
transport::local::register(profile.storage.clone());
if options.fetch {
sync::fetch_all(repository.id(), &mut Node::new(profile.socket()))?;
}

View File

@ -1,6 +1,4 @@
use std::path::Path;
use anyhow::anyhow;
use anyhow::{anyhow, Context};
use radicle::cob::patch::{Clock, MergeTarget, Patch, PatchId, Patches};
use radicle::git;
@ -136,11 +134,15 @@ pub fn try_branch(reference: git::raw::Reference<'_>) -> anyhow::Result<git::raw
///
/// The branch must be in storage for others to merge the `Patch`.
pub fn push_to_storage(
working: &git::raw::Repository,
storage: &Repository,
head_branch: &git::raw::Branch,
options: &Options,
) -> anyhow::Result<()> {
) -> anyhow::Result<git::RefString> {
let head_oid = branch_oid(head_branch)?;
let branch = branch_name(head_branch)?.try_into()?;
let branch = radicle::git::refs::workdir::branch(branch);
if storage.commit(head_oid).is_err() {
if !options.push {
term::blank();
@ -152,21 +154,13 @@ pub fn push_to_storage(
.into());
}
let output = match head_branch.upstream() {
Ok(_) => git::run::<_, _, &str, &str>(Path::new("."), ["push", "rad"], [])?,
Err(_) => git::run::<_, _, &str, &str>(
Path::new("."),
["push", "--set-upstream", "rad", branch_name(head_branch)?],
[],
)?,
};
if options.verbose {
term::blob(output);
let (mut remote, _) = radicle::rad::remote(working)?;
return Ok(());
}
remote
.push::<&str>(&[&branch], None)
.context("failed to push to storage")?;
}
Ok(())
Ok(branch)
}
/// Find patches with a merge base equal to the one provided.

View File

@ -88,9 +88,19 @@ pub fn run(
) -> anyhow::Result<()> {
let mut patches = patch::Patches::open(storage)?;
let head_branch = try_branch(workdir.head()?)?;
push_to_storage(storage, &head_branch, &options)?;
let head_branch_name = push_to_storage(workdir, storage, &head_branch, &options)?;
let (target_ref, target_oid) = get_merge_target(storage, &head_branch)?;
if head_branch.upstream().is_err() {
radicle::git::set_upstream(
workdir,
&radicle::rad::REMOTE_NAME,
branch_name(&head_branch)?,
&head_branch_name,
)?;
}
// TODO: Handle case where `rad/master` isn't up to date with the target.
// In that case we should warn the user that their master branch is not up
// to date, and error out, unless the user specifies manually the merge

View File

@ -71,7 +71,7 @@ pub fn run(
// `HEAD`; This is what we are proposing as a patch.
let head_branch = try_branch(workdir.head()?)?;
push_to_storage(storage, &head_branch, options)?;
push_to_storage(workdir, storage, &head_branch, options)?;
let (_, target_oid) = get_merge_target(storage, &head_branch)?;
let mut patches = patch::Patches::open(storage)?;

View File

@ -129,7 +129,7 @@ pub fn fail(header: &str, error: &anyhow::Error) {
let separator = if err.contains('\n') { ":\n" } else { ": " };
println!(
"{ERROR_PREFIX} {}{}{error}",
"{ERROR_PREFIX} {}{}{error:#}",
Paint::red(header).bold(),
Paint::red(separator),
);

View File

@ -1,4 +1,5 @@
#![allow(clippy::collapsible_if)]
use std::os::fd::{AsRawFd, FromRawFd};
use std::path::PathBuf;
use std::{env, io, process};
@ -125,11 +126,17 @@ pub fn run(profile: radicle::Profile) -> Result<(), Box<dyn std::error::Error +
// Connect to local node and announce refs to the network.
// If our node is not running, we simply skip this step, as the
// refs will be announced eventually, when the node restarts.
let mut node = radicle::Node::new(profile.socket());
if node.is_running() {
eprint!("Announcing refs... ");
node.announce_refs(url.repo)?;
eprintln!("ok");
if radicle::Node::new(profile.socket()).is_running() {
let stderr = io::stderr().as_raw_fd();
process::Command::new("rad")
.arg("sync")
.arg(proj.id.to_string())
.arg("--verbose")
.stdout(unsafe { process::Stdio::from_raw_fd(stderr) })
.stderr(process::Stdio::inherit())
.spawn()?
.wait()?;
}
}
}