Skip to content

Commit

Permalink
cli: show commit summary at end of "branch set"
Browse files Browse the repository at this point in the history
For the same reason as the previous commit.

Created and moved stats are printed separately because it's unusual to do both
within one "branch set" invocation.
  • Loading branch information
yuja committed Jul 6, 2024
1 parent a5095c1 commit 5c649e7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
40 changes: 24 additions & 16 deletions cli/src/commands/branch/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ pub fn cmd_branch_set(
workspace_command.resolve_single_rev(args.revision.as_ref().unwrap_or(&RevisionArg::AT))?;
let repo = workspace_command.repo().as_ref();
let branch_names = &args.names;
let mut new_branch_names: Vec<&str> = Vec::new();
let mut new_branch_count = 0;
let mut moved_branch_count = 0;
for name in branch_names {
let old_target = repo.view().get_local_branch(name);
// If a branch is absent locally but is still tracking remote branches,
// we are resurrecting the local branch, not "creating" a new branch.
if old_target.is_absent() && !has_tracked_remote_branches(repo.view(), name) {
new_branch_names.push(name);
new_branch_count += 1;
} else if old_target.as_normal() != Some(target_commit.id()) {
moved_branch_count += 1;
}
if !args.allow_backwards && !is_fast_forward(repo, old_target, target_commit.id()) {
return Err(user_error_with_hint(
Expand All @@ -63,26 +66,31 @@ pub fn cmd_branch_set(
}
}

if branch_names.len() > 1 {
writeln!(
ui.warning_default(),
"Updating multiple branches: {}",
branch_names.join(", "),
)?;
}

let mut tx = workspace_command.start_transaction();
for branch_name in branch_names {
tx.mut_repo()
.set_local_branch_target(branch_name, RefTarget::normal(target_commit.id().clone()));
}

if !new_branch_names.is_empty() {
writeln!(
ui.status(),
"Created branches: {}",
new_branch_names.join(", "),
)?;
if let Some(mut formatter) = ui.status_formatter() {
if new_branch_count > 0 {
write!(
formatter,
"Created {new_branch_count} branches pointing to "
)?;
tx.write_commit_summary(formatter.as_mut(), &target_commit)?;
writeln!(formatter)?;
}
if moved_branch_count > 0 {
write!(formatter, "Moved {moved_branch_count} branches to ")?;
tx.write_commit_summary(formatter.as_mut(), &target_commit)?;
writeln!(formatter)?;
}
}
if branch_names.len() > 1 && args.revision.is_none() {
writeln!(ui.hint_default(), "Use -r to specify the target revision.")?;
}
if new_branch_count > 0 {
// TODO: delete this hint in jj 0.25+
writeln!(
ui.hint_default(),
Expand Down
36 changes: 30 additions & 6 deletions cli/tests/test_branch_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fn test_branch_multiple_names() {
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "set", "foo", "bar"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
Warning: Updating multiple branches: foo, bar
Moved 2 branches to zsuskuln 8bb159bc bar foo | (empty) (no description set)
Hint: Use -r to specify the target revision.
"###);
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
@ bar foo 8bb159bc30a9
Expand All @@ -63,6 +64,21 @@ fn test_branch_multiple_names() {
insta::assert_snapshot!(stderr, @r###"
Created 2 branches pointing to qpvuntsm 230dd059 bar foo | (empty) (no description set)
"###);

// Create and move with explicit -r
let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "set", "-r@", "bar", "baz"]);
insta::assert_snapshot!(stderr, @r###"
Created 1 branches pointing to zsuskuln 8bb159bc bar baz | (empty) (no description set)
Moved 1 branches to zsuskuln 8bb159bc bar baz | (empty) (no description set)
Hint: Consider using `jj branch move` if your intention was to move existing branches.
"###);

// Noop changes should not be included in the stats
let (_stdout, stderr) =
test_env.jj_cmd_ok(&repo_path, &["branch", "set", "-r@", "foo", "bar", "baz"]);
insta::assert_snapshot!(stderr, @r###"
Moved 1 branches to zsuskuln 8bb159bc bar baz foo | (empty) (no description set)
"###);
}

#[test]
Expand Down Expand Up @@ -128,7 +144,7 @@ fn test_branch_move() {

let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "set", "foo"]);
insta::assert_snapshot!(stderr, @r###"
Created branches: foo
Created 1 branches pointing to qpvuntsm 230dd059 foo | (empty) (no description set)
Hint: Consider using `jj branch move` if your intention was to move existing branches.
"###);

Expand All @@ -140,7 +156,9 @@ fn test_branch_move() {
"###);

let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "set", "foo"]);
insta::assert_snapshot!(stderr, @"");
insta::assert_snapshot!(stderr, @r###"
Moved 1 branches to mzvwutvl 167f90e7 foo | (empty) (no description set)
"###);

let stderr = test_env.jj_cmd_failure(&repo_path, &["branch", "set", "-r@-", "foo"]);
insta::assert_snapshot!(stderr, @r###"
Expand All @@ -152,7 +170,9 @@ fn test_branch_move() {
&repo_path,
&["branch", "set", "-r@-", "--allow-backwards", "foo"],
);
insta::assert_snapshot!(stderr, @"");
insta::assert_snapshot!(stderr, @r###"
Moved 1 branches to qpvuntsm 230dd059 foo | (empty) (no description set)
"###);

let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "move", "foo"]);
insta::assert_snapshot!(stderr, @r###"
Expand Down Expand Up @@ -191,7 +211,9 @@ fn test_branch_move() {

// Restoring local target shouldn't invalidate tracking state
let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "set", "foo"]);
insta::assert_snapshot!(stderr, @"");
insta::assert_snapshot!(stderr, @r###"
Moved 1 branches to mzvwutvl 66d48752 foo* | (empty) (no description set)
"###);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
foo: mzvwutvl 66d48752 (empty) (no description set)
@origin (behind by 1 commits): qpvuntsm 1eb845f3 (empty) commit
Expand Down Expand Up @@ -361,7 +383,9 @@ fn test_branch_move_conflicting() {
// descendant of B0, though.
let (_stdout, stderr) =
test_env.jj_cmd_ok(&repo_path, &["branch", "set", "-rdescription(A1)", "foo"]);
insta::assert_snapshot!(stderr, @"");
insta::assert_snapshot!(stderr, @r###"
Moved 1 branches to mzvwutvl 9328d344 foo | (empty) A1
"###);
insta::assert_snapshot!(get_log(), @r###"
@ A1 foo
◉ A0
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/test_git_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn test_git_push_current_branch() {
"###);

// Try pushing backwards
test_env.jj_cmd_success(
test_env.jj_cmd_ok(
&workspace_root,
&[
"branch",
Expand Down
2 changes: 2 additions & 0 deletions cli/tests/test_immutable_commits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ fn test_new_wc_commit_when_wc_immutable() {
test_env.jj_cmd_ok(test_env.env_root(), &["new", "-m=a"]);
let (_, stderr) = test_env.jj_cmd_ok(test_env.env_root(), &["branch", "set", "main"]);
insta::assert_snapshot!(stderr, @r###"
Moved 1 branches to kkmpptxz a164195b main | (empty) a
Warning: The working-copy commit in workspace 'default' became immutable, so a new commit has been created on top of it.
Working copy now at: zsuskuln ef5fa85b (empty) (no description set)
Parent commit : kkmpptxz a164195b main | (empty) a
Expand Down Expand Up @@ -144,6 +145,7 @@ fn test_new_wc_commit_when_wc_immutable_multi_workspace() {
test_env.jj_cmd_ok(workspace1_envroot.as_path(), &["edit", "default@"]);
let (_, stderr) = test_env.jj_cmd_ok(test_env.env_root(), &["branch", "set", "main"]);
insta::assert_snapshot!(stderr, @r###"
Moved 1 branches to kkmpptxz 40cbbd52 main | a
Warning: The working-copy commit in workspace 'default' became immutable, so a new commit has been created on top of it.
Warning: The working-copy commit in workspace 'workspace1' became immutable, so a new commit has been created on top of it.
Working copy now at: royxmykx 5bcb7da6 (empty) (no description set)
Expand Down

0 comments on commit 5c649e7

Please sign in to comment.