cli: extract types for BranchSubcommands variants

This matches how the other commands are defined. It will also help
split up the large `cmd_branch()` function.
This commit is contained in:
Martin von Zweigbergk 2023-02-03 07:42:33 -08:00 committed by Martin von Zweigbergk
parent 086398dd55
commit 0d15a02281

View file

@ -21,9 +21,21 @@ use crate::ui::Ui;
/// https://github.com/martinvonz/jj/blob/main/docs/branches.md. /// https://github.com/martinvonz/jj/blob/main/docs/branches.md.
#[derive(clap::Subcommand, Clone, Debug)] #[derive(clap::Subcommand, Clone, Debug)]
pub enum BranchSubcommand { pub enum BranchSubcommand {
/// Create a new branch.
#[command(visible_alias("c"))] #[command(visible_alias("c"))]
Create { Create(BranchCreateArgs),
#[command(visible_alias("d"))]
Delete(BranchDeleteArgs),
#[command(visible_alias("f"))]
Forget(BranchForgetArgs),
#[command(visible_alias("l"))]
List(BranchListArgs),
#[command(visible_alias("s"))]
Set(BranchSetArgs),
}
/// Create a new branch.
#[derive(clap::Args, Clone, Debug)]
pub struct BranchCreateArgs {
/// The branch's target revision. /// The branch's target revision.
#[arg(long, short)] #[arg(long, short)]
revision: Option<RevisionArg>, revision: Option<RevisionArg>,
@ -31,32 +43,16 @@ pub enum BranchSubcommand {
/// The branches to create. /// The branches to create.
#[arg(required = true, value_parser=NonEmptyStringValueParser::new())] #[arg(required = true, value_parser=NonEmptyStringValueParser::new())]
names: Vec<String>, names: Vec<String>,
}, }
/// Delete an existing branch and propagate the deletion to remotes on the /// Delete an existing branch and propagate the deletion to remotes on the
/// next push. /// next push.
#[command(visible_alias("d"))] #[derive(clap::Args, Clone, Debug)]
Delete { pub struct BranchDeleteArgs {
/// The branches to delete. /// The branches to delete.
#[arg(required = true)] #[arg(required = true)]
names: Vec<String>, names: Vec<String>,
}, }
/// Forget everything about a branch, including its local and remote
/// targets.
///
/// A forgotten branch will not impact remotes on future pushes. It will be
/// recreated on future pulls if it still exists in the remote.
#[command(visible_alias("f"))]
Forget {
/// The branches to forget.
#[arg(required_unless_present_any(&["glob"]))]
names: Vec<String>,
/// A glob pattern indicating branches to forget.
#[arg(long)]
glob: Vec<String>,
},
/// List branches and their targets /// List branches and their targets
/// ///
@ -65,24 +61,39 @@ pub enum BranchSubcommand {
/// target revisions are preceded by a "-" and new target revisions are /// target revisions are preceded by a "-" and new target revisions are
/// preceded by a "+". For information about branches, see /// preceded by a "+". For information about branches, see
/// https://github.com/martinvonz/jj/blob/main/docs/branches.md. /// https://github.com/martinvonz/jj/blob/main/docs/branches.md.
#[command(visible_alias("l"))] #[derive(clap::Args, Clone, Debug)]
List, pub struct BranchListArgs;
/// Forget everything about a branch, including its local and remote
/// targets.
///
/// A forgotten branch will not impact remotes on future pushes. It will be
/// recreated on future pulls if it still exists in the remote.
#[derive(clap::Args, Clone, Debug)]
pub struct BranchForgetArgs {
/// The branches to forget.
#[arg(required_unless_present_any(& ["glob"]))]
pub names: Vec<String>,
/// A glob pattern indicating branches to forget.
#[arg(long)]
pub glob: Vec<String>,
}
/// Update a given branch to point to a certain commit. /// Update a given branch to point to a certain commit.
#[command(visible_alias("s"))] #[derive(clap::Args, Clone, Debug)]
Set { pub struct BranchSetArgs {
/// The branch's target revision. /// The branch's target revision.
#[arg(long, short)] #[arg(long, short)]
revision: Option<RevisionArg>, pub revision: Option<RevisionArg>,
/// Allow moving the branch backwards or sideways. /// Allow moving the branch backwards or sideways.
#[arg(long, short = 'B')] #[arg(long, short = 'B')]
allow_backwards: bool, pub allow_backwards: bool,
/// The branches to update. /// The branches to update.
#[arg(required = true)] #[arg(required = true)]
names: Vec<String>, pub names: Vec<String>,
},
} }
pub fn cmd_branch( pub fn cmd_branch(
@ -120,7 +131,7 @@ pub fn cmd_branch(
} }
match subcommand { match subcommand {
BranchSubcommand::Create { revision, names } => { BranchSubcommand::Create(BranchCreateArgs { revision, names }) => {
let branch_names: Vec<&str> = names let branch_names: Vec<&str> = names
.iter() .iter()
.map(|branch_name| match view.get_local_branch(branch_name) { .map(|branch_name| match view.get_local_branch(branch_name) {
@ -156,11 +167,11 @@ pub fn cmd_branch(
tx.finish(ui)?; tx.finish(ui)?;
} }
BranchSubcommand::Set { BranchSubcommand::Set(BranchSetArgs {
revision, revision,
allow_backwards, allow_backwards,
names: branch_names, names: branch_names,
} => { }) => {
if branch_names.len() > 1 { if branch_names.len() > 1 {
writeln!( writeln!(
ui.warning(), ui.warning(),
@ -199,7 +210,7 @@ pub fn cmd_branch(
tx.finish(ui)?; tx.finish(ui)?;
} }
BranchSubcommand::Delete { names } => { BranchSubcommand::Delete(BranchDeleteArgs { names }) => {
validate_branch_names_exist(view, names)?; validate_branch_names_exist(view, names)?;
let mut tx = let mut tx =
workspace_command.start_transaction(&format!("delete {}", make_branch_term(names))); workspace_command.start_transaction(&format!("delete {}", make_branch_term(names)));
@ -209,7 +220,7 @@ pub fn cmd_branch(
tx.finish(ui)?; tx.finish(ui)?;
} }
BranchSubcommand::Forget { names, glob } => { BranchSubcommand::Forget(BranchForgetArgs { names, glob }) => {
validate_branch_names_exist(view, names)?; validate_branch_names_exist(view, names)?;
let globbed_names = find_globs(view, glob)?; let globbed_names = find_globs(view, glob)?;
let names: BTreeSet<String> = names.iter().cloned().chain(globbed_names).collect(); let names: BTreeSet<String> = names.iter().cloned().chain(globbed_names).collect();
@ -221,7 +232,7 @@ pub fn cmd_branch(
tx.finish(ui)?; tx.finish(ui)?;
} }
BranchSubcommand::List => { BranchSubcommand::List(BranchListArgs) => {
list_branches(ui, command, &workspace_command)?; list_branches(ui, command, &workspace_command)?;
} }
} }