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 { } else {
PathBuf::from(&assertion.command) 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() { if !self.cwd.exists() {
log::error!(target: "test", "{path}: Directory {} does not exist..", self.cwd.display()); 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. 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;
use radicle::cob::patch::PatchId; use radicle::cob::patch::PatchId;
use radicle::storage::git::transport;
use radicle::{prelude::*, Node}; use radicle::{prelude::*, Node};
use crate::commands::rad_sync as sync; 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 profile = ctx.profile()?;
let repository = profile.storage.repository(id)?; let repository = profile.storage.repository(id)?;
transport::local::register(profile.storage.clone());
if options.fetch { if options.fetch {
sync::fetch_all(repository.id(), &mut Node::new(profile.socket()))?; sync::fetch_all(repository.id(), &mut Node::new(profile.socket()))?;
} }

View File

@ -1,6 +1,4 @@
use std::path::Path; use anyhow::{anyhow, Context};
use anyhow::anyhow;
use radicle::cob::patch::{Clock, MergeTarget, Patch, PatchId, Patches}; use radicle::cob::patch::{Clock, MergeTarget, Patch, PatchId, Patches};
use radicle::git; 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`. /// The branch must be in storage for others to merge the `Patch`.
pub fn push_to_storage( pub fn push_to_storage(
working: &git::raw::Repository,
storage: &Repository, storage: &Repository,
head_branch: &git::raw::Branch, head_branch: &git::raw::Branch,
options: &Options, options: &Options,
) -> anyhow::Result<()> { ) -> anyhow::Result<git::RefString> {
let head_oid = branch_oid(head_branch)?; 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 storage.commit(head_oid).is_err() {
if !options.push { if !options.push {
term::blank(); term::blank();
@ -152,21 +154,13 @@ pub fn push_to_storage(
.into()); .into());
} }
let output = match head_branch.upstream() { let (mut remote, _) = radicle::rad::remote(working)?;
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);
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. /// Find patches with a merge base equal to the one provided.

View File

@ -88,9 +88,19 @@ pub fn run(
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let mut patches = patch::Patches::open(storage)?; let mut patches = patch::Patches::open(storage)?;
let head_branch = try_branch(workdir.head()?)?; 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)?; 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. // 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 // 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 // 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. // `HEAD`; This is what we are proposing as a patch.
let head_branch = try_branch(workdir.head()?)?; 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 (_, target_oid) = get_merge_target(storage, &head_branch)?;
let mut patches = patch::Patches::open(storage)?; 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 { ": " }; let separator = if err.contains('\n') { ":\n" } else { ": " };
println!( println!(
"{ERROR_PREFIX} {}{}{error}", "{ERROR_PREFIX} {}{}{error:#}",
Paint::red(header).bold(), Paint::red(header).bold(),
Paint::red(separator), Paint::red(separator),
); );

View File

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