Skip to content

Commit

Permalink
cli: change jj branches to jj branch list
Browse files Browse the repository at this point in the history
  • Loading branch information
arxanas committed May 30, 2022
1 parent 71615c5 commit 06c0275
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 52 deletions.
71 changes: 34 additions & 37 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1630,13 +1630,16 @@ enum BranchSubcommand {
revision: Option<String>,

/// The branches to create.
#[clap(required = true)]
names: Vec<String>,
},

/// Delete an existing branch and propagate the deletion to remotes on the next push.
/// Delete an existing branch and propagate the deletion to remotes on the
/// next push.
#[clap(visible_alias("d"))]
Delete {
/// The branches to delete.
#[clap(required = true)]
names: Vec<String>,
},

Expand All @@ -1645,6 +1648,7 @@ enum BranchSubcommand {
#[clap(visible_alias("f"))]
Forget {
/// The branches to delete.
#[clap(required = true)]
names: Vec<String>,
},

Expand All @@ -1670,6 +1674,7 @@ enum BranchSubcommand {
allow_backwards: bool,

/// The branches to update.
#[clap(required = true)]
names: Vec<String>,
},
}
Expand Down Expand Up @@ -4037,39 +4042,31 @@ fn cmd_branch(
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let view = workspace_command.repo().view();
fn assert_branch_names_exist<'a>(
fn validate_branch_names_exist<'a>(
view: &'a View,
names: &'a [String],
) -> Result<Vec<&'a str>, CommandError> {
let result: Vec<_> = names
.iter()
.map(|branch_name| match view.get_local_branch(branch_name) {
Some(_) => Ok(branch_name.as_str()),
None => Err(CommandError::UserError(format!(
) -> Result<(), CommandError> {
for branch_name in names {
if view.get_local_branch(branch_name).is_none() {
return Err(CommandError::UserError(format!(
"No such branch: {}",
branch_name
))),
})
.try_collect()?;
Ok(result)
)));
}
}
Ok(())
}

fn make_branch_term(ui: &mut Ui, branch_names: &[impl AsRef<str>]) -> io::Result<String> {
if branch_names.is_empty() {
ui.write_warn("warning: No branches provided.\n")?;
fn make_branch_term(branch_names: &[impl AsRef<str>]) -> String {
match branch_names {
[branch_name] => format!("branch {}", branch_name.as_ref()),
branch_names => {
format!(
"branches {}",
branch_names.iter().map(AsRef::as_ref).join(", ")
)
}
}
let branch_term = if branch_names.len() == 1 {
"branch"
} else {
"branches"
};
Ok(format!(
"{branch_term} {}",
branch_names
.iter()
.map(|branch_name| branch_name.as_ref())
.join(", ")
))
}

match subcommand {
Expand All @@ -4096,7 +4093,7 @@ fn cmd_branch(
workspace_command.resolve_single_rev(ui, revision.as_deref().unwrap_or("@"))?;
let mut tx = workspace_command.start_transaction(&format!(
"create {} pointing to commit {}",
make_branch_term(ui, &branch_names)?,
make_branch_term(&branch_names),
target_commit.id().hex()
));
for branch_name in branch_names {
Expand Down Expand Up @@ -4138,7 +4135,7 @@ fn cmd_branch(
}
let mut tx = workspace_command.start_transaction(&format!(
"point {} to commit {}",
make_branch_term(ui, branch_names)?,
make_branch_term(branch_names),
target_commit.id().hex()
));
for branch_name in branch_names {
Expand All @@ -4151,20 +4148,20 @@ fn cmd_branch(
}

BranchSubcommand::Delete { names } => {
let branch_names = assert_branch_names_exist(view, names)?;
let mut tx = workspace_command
.start_transaction(&format!("delete {}", make_branch_term(ui, &branch_names)?,));
for branch_name in branch_names {
validate_branch_names_exist(view, names)?;
let mut tx =
workspace_command.start_transaction(&format!("delete {}", make_branch_term(names)));
for branch_name in names {
tx.mut_repo().remove_local_branch(branch_name);
}
workspace_command.finish_transaction(ui, tx)?;
}

BranchSubcommand::Forget { names } => {
let branch_names = assert_branch_names_exist(view, names)?;
let mut tx = workspace_command
.start_transaction(&format!("forget {}", make_branch_term(ui, &branch_names)?));
for branch_name in branch_names {
validate_branch_names_exist(view, names)?;
let mut tx =
workspace_command.start_transaction(&format!("forget {}", make_branch_term(names)));
for branch_name in names {
tx.mut_repo().remove_branch(branch_name);
}
workspace_command.finish_transaction(ui, tx)?;
Expand Down
15 changes: 0 additions & 15 deletions tests/test_branch_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,6 @@ fn test_branch_multiple_names() {
"###);
}

#[test]
fn test_branch_hint_no_branches() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");

let assert = test_env
.jj_cmd(&repo_path, &["branch", "delete"])
.assert()
.success();
let stderr = get_stderr_string(&assert);
insta::assert_snapshot!(stderr, @"warning: No branches provided.
");
}

fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
test_env.jj_cmd_success(cwd, &["log", "-T", r#"branches " " commit_id.short()"#])
}

0 comments on commit 06c0275

Please sign in to comment.