From 7d72b160874af0c029241f7b97df65987b41cd62 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 24 Apr 2023 17:26:45 +0200 Subject: [PATCH] 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. --- radicle-cli-test/src/lib.rs | 2 +- radicle-cli/examples/rad-patch.md | 7 ++++++ radicle-cli/src/commands/patch.rs | 3 +++ radicle-cli/src/commands/patch/common.rs | 28 ++++++++++-------------- radicle-cli/src/commands/patch/create.rs | 12 +++++++++- radicle-cli/src/commands/patch/update.rs | 2 +- radicle-cli/src/terminal.rs | 2 +- radicle-remote-helper/src/lib.rs | 17 +++++++++----- 8 files changed, 47 insertions(+), 26 deletions(-) diff --git a/radicle-cli-test/src/lib.rs b/radicle-cli-test/src/lib.rs index 1c36f6e2..8108cf89 100644 --- a/radicle-cli-test/src/lib.rs +++ b/radicle-cli-test/src/lib.rs @@ -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()); diff --git a/radicle-cli/examples/rad-patch.md b/radicle-cli/examples/rad-patch.md index 235e431a..5f0a66b5 100644 --- a/radicle-cli/examples/rad-patch.md +++ b/radicle-cli/examples/rad-patch.md @@ -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. ``` diff --git a/radicle-cli/src/commands/patch.rs b/radicle-cli/src/commands/patch.rs index dbcb3ff3..65060ff9 100644 --- a/radicle-cli/src/commands/patch.rs +++ b/radicle-cli/src/commands/patch.rs @@ -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()))?; } diff --git a/radicle-cli/src/commands/patch/common.rs b/radicle-cli/src/commands/patch/common.rs index d588dc74..4d7c2af9 100644 --- a/radicle-cli/src/commands/patch/common.rs +++ b/radicle-cli/src/commands/patch/common.rs @@ -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 anyhow::Result<()> { +) -> anyhow::Result { 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. diff --git a/radicle-cli/src/commands/patch/create.rs b/radicle-cli/src/commands/patch/create.rs index e00283e5..8d804118 100644 --- a/radicle-cli/src/commands/patch/create.rs +++ b/radicle-cli/src/commands/patch/create.rs @@ -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 diff --git a/radicle-cli/src/commands/patch/update.rs b/radicle-cli/src/commands/patch/update.rs index a5a6de94..df009e4d 100644 --- a/radicle-cli/src/commands/patch/update.rs +++ b/radicle-cli/src/commands/patch/update.rs @@ -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)?; diff --git a/radicle-cli/src/terminal.rs b/radicle-cli/src/terminal.rs index c6e82815..428c0dd0 100644 --- a/radicle-cli/src/terminal.rs +++ b/radicle-cli/src/terminal.rs @@ -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), ); diff --git a/radicle-remote-helper/src/lib.rs b/radicle-remote-helper/src/lib.rs index 15beb66d..8f283a21 100644 --- a/radicle-remote-helper/src/lib.rs +++ b/radicle-remote-helper/src/lib.rs @@ -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