mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-29 14:00:31 +08:00
cli: alter use subcommands for patch
Make `rad patch` accept subcommands for specifying its operation in place of the '--command' notation. For instance `rad patch update` versus `rad patch --update`. Also make the default operation list active patches, replacing patch creation. Patch creation is now done via `rad patch open`. Make these changes in a way consistent with other commands like `rad delegate`. Add '--no-confirm' to patch to support this subcommand's documentation test. Signed-off-by: Slack Coder <slackcoder@server.ky>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use std::fmt;
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::{anyhow, Context};
|
||||
@@ -10,9 +11,10 @@ use radicle::storage::git::Repository;
|
||||
|
||||
use crate::terminal as term;
|
||||
use crate::terminal::args::Error;
|
||||
use crate::terminal::patch;
|
||||
|
||||
use super::common;
|
||||
use super::{Options, Update};
|
||||
use super::{OptPatch, Options};
|
||||
|
||||
const PATCH_MSG: &str = r#"
|
||||
<!--
|
||||
@@ -33,11 +35,18 @@ blank is also okay.
|
||||
-->
|
||||
"#;
|
||||
|
||||
#[inline]
|
||||
fn confirm<D: fmt::Display>(prompt: D, options: &Options) -> bool {
|
||||
!options.confirm || term::confirm(prompt)
|
||||
}
|
||||
|
||||
/// Run patch creation.
|
||||
pub fn run(
|
||||
storage: &Repository,
|
||||
profile: &Profile,
|
||||
workdir: &git::raw::Repository,
|
||||
patch_id: OptPatch,
|
||||
message: patch::Comment,
|
||||
options: Options,
|
||||
) -> anyhow::Result<()> {
|
||||
let project = storage.project_of(&profile.public_key).context(format!(
|
||||
@@ -118,9 +127,9 @@ pub fn run(
|
||||
let base_oid = workdir.merge_base((*target_oid).into(), head_oid)?;
|
||||
let commits = common::patch_commits(workdir, &base_oid, &head_oid)?;
|
||||
|
||||
let patch = match &options.update {
|
||||
Update::No => None,
|
||||
Update::Any => {
|
||||
let patch = match &patch_id {
|
||||
OptPatch::None => None,
|
||||
OptPatch::Any => {
|
||||
let mut spinner = term::spinner("Finding patches to update...");
|
||||
let mut result = common::find_unmerged_with_base(
|
||||
head_oid,
|
||||
@@ -152,7 +161,7 @@ pub fn run(
|
||||
anyhow::bail!("No patches found that share a base, please create a new patch or specify the patch id manually");
|
||||
}
|
||||
}
|
||||
Update::Patch(id) => {
|
||||
OptPatch::Patch(id) => {
|
||||
if let Ok(patch) = patches.get_mut(id) {
|
||||
Some((*id, patch))
|
||||
} else {
|
||||
@@ -162,10 +171,12 @@ pub fn run(
|
||||
};
|
||||
|
||||
if let Some((id, patch)) = patch {
|
||||
if term::confirm("Update?") {
|
||||
if confirm("Update?", &options) {
|
||||
term::blank();
|
||||
|
||||
return update(patch, id, &base_oid, &head_oid, workdir, options, &signer);
|
||||
return update(
|
||||
patch, id, &base_oid, &head_oid, workdir, options, message, &signer,
|
||||
);
|
||||
} else {
|
||||
anyhow::bail!("Patch update aborted by user");
|
||||
}
|
||||
@@ -194,14 +205,14 @@ pub fn run(
|
||||
term::patch::list_commits(&commits)?;
|
||||
term::blank();
|
||||
|
||||
if !term::confirm("Continue?") {
|
||||
if !confirm("Continue?", &options) {
|
||||
anyhow::bail!("patch proposal aborted by user");
|
||||
}
|
||||
|
||||
let message = head_commit
|
||||
let commit_message = head_commit
|
||||
.message()
|
||||
.ok_or(anyhow!("commit summary is not valid UTF-8; aborting"))?;
|
||||
let message = options.message.get(&format!("{}{}", message, PATCH_MSG));
|
||||
let message = message.get(&format!("{}{}", commit_message, PATCH_MSG));
|
||||
let (title, description) = message.split_once("\n\n").unwrap_or((&message, ""));
|
||||
let (title, description) = (title.trim(), description.trim());
|
||||
let description = description.replace(PATCH_MSG.trim(), ""); // Delete help message.
|
||||
@@ -229,7 +240,7 @@ pub fn run(
|
||||
)));
|
||||
term::blank();
|
||||
|
||||
if !term::confirm("Create patch?") {
|
||||
if !confirm("Create patch?", &options) {
|
||||
anyhow::bail!("patch proposal aborted by user");
|
||||
}
|
||||
|
||||
@@ -261,6 +272,7 @@ fn update<G: Signer>(
|
||||
head: &Oid,
|
||||
workdir: &git::raw::Repository,
|
||||
options: Options,
|
||||
message: patch::Comment,
|
||||
signer: &G,
|
||||
) -> anyhow::Result<()> {
|
||||
// TODO(cloudhead): Handle error.
|
||||
@@ -280,13 +292,13 @@ fn update<G: Signer>(
|
||||
term::format::dim(format!("R{}", current_version + 1)),
|
||||
term::format::secondary(term::format::oid(*head)),
|
||||
);
|
||||
let message = options.message.get(REVISION_MSG);
|
||||
let message = message.get(REVISION_MSG);
|
||||
|
||||
// Difference between the two revisions.
|
||||
term::patch::print_commits_ahead_behind(workdir, *head, *current_revision.oid)?;
|
||||
term::blank();
|
||||
|
||||
if !term::confirm("Continue?") {
|
||||
if !confirm("Continue?", &options) {
|
||||
anyhow::bail!("patch update aborted by user");
|
||||
}
|
||||
patch.update(message, *base, *head, signer)?;
|
||||
|
||||
Reference in New Issue
Block a user