From 980f8fcc0a044fa2b69903831fedcfdcfcc9d5da Mon Sep 17 00:00:00 2001 From: Quentin Boyer Date: Tue, 30 Apr 2024 17:51:12 +0200 Subject: [PATCH] Allow to delete series --- src/main.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/main.rs b/src/main.rs index f32d1e6..8e9898a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,6 +39,17 @@ enum Command { List(List), /// Send a patch series by mail Send(Send), + /// Delete a series + Delete(Delete), +} + +#[derive(Args, Debug)] +struct Delete { + /// Only delete the local branch of the series + #[arg(short, long)] + local_only: bool, + /// Branch to delete (defaults to the current branch) + branch: Option, } #[derive(Args, Debug)] @@ -539,6 +550,34 @@ fn main() -> Result<()> { return Err(miette!("Could not send emails")); } + Ok(()) + } + Command::Delete(delete) => { + let current_branch = git_cd(&["branch", "--show-current"])?; + + let branch = delete + .branch + .as_ref() + .try_m_unwrap_or_else(|| Ok(¤t_branch))?; + + let has_remote = git_cd(&["rev-parse", "@{u}"]).is_ok(); + if has_remote && !delete.local_only { + println!("Removing branch from remote repository"); + git_cd(&["push", "-d", "origin", branch])?; + } + + if branch == ¤t_branch { + println!("Branch {branch} currently checked out, switching to master"); + git_cd(&[ + "switch", + config.interdiff_base.as_deref().unwrap_or("master"), + ])?; + } + + git_cd(&["branch", "-d", branch])?; + let branch_dir = patch_dir.join(&branch); + std::fs::remove_dir_all(branch_dir).into_diagnostic()?; + Ok(()) } }