Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: branch: make "set" do upsert as before #3932

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Dropped support for deprecated `jj branch delete`/`forget` `--glob` option.

* `jj branch set` now creates new branch if it doesn't exist. Use `jj branch
move` to ensure that the target branch already exists.
[#3584](https://github.com/martinvonz/jj/issues/3584)

### Deprecations

* Replacing `-l` shorthand for `--limit` with `-n` in `jj log`, `jj op log` and `jj obslog`.
Expand Down
21 changes: 16 additions & 5 deletions cli/src/commands/branch/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::cli_util::{CommandHelper, RevisionArg};
use crate::command_error::{user_error_with_hint, CommandError};
use crate::ui::Ui;

/// Update an existing branch to point to a certain commit
/// Create or update a branch to point to a certain commit
#[derive(clap::Args, Clone, Debug)]
pub struct BranchSetArgs {
/// The branch's target revision
Expand All @@ -47,13 +47,11 @@ 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();
for name in branch_names {
let old_target = repo.view().get_local_branch(name);
if old_target.is_absent() {
return Err(user_error_with_hint(
format!("No such branch: {name}"),
"Use `jj branch create` to create it.",
));
new_branch_names.push(name);
}
if !args.allow_backwards && !is_fast_forward(repo, old_target, target_commit.id()) {
return Err(user_error_with_hint(
Expand Down Expand Up @@ -84,5 +82,18 @@ pub fn cmd_branch_set(
target_commit.id().hex()
),
)?;

if !new_branch_names.is_empty() {
writeln!(
ui.status(),
"Created branches: {}",
new_branch_names.join(", "),
)?;
// TODO: delete this hint in jj 0.25+
writeln!(
yuja marked this conversation as resolved.
Show resolved Hide resolved
ui.hint_default(),
"Consider using `jj branch move` if your intention was to move existing branches."
)?;
}
Ok(())
}
4 changes: 2 additions & 2 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ For information about branches, see https://github.com/martinvonz/jj/blob/main/d
* `list` — List branches and their targets
* `move` — Move existing branches to target revision
* `rename` — Rename `old` branch name to `new` branch name
* `set` — Update an existing branch to point to a certain commit
* `set` — Create or update a branch to point to a certain commit
* `track` — Start tracking given remote branches
* `untrack` — Stop tracking given remote branches

Expand Down Expand Up @@ -370,7 +370,7 @@ The new branch name points at the same commit as the old branch name.

## `jj branch set`

Update an existing branch to point to a certain commit
Create or update a branch to point to a certain commit

**Usage:** `jj branch set [OPTIONS] <NAMES>...`

Expand Down
12 changes: 5 additions & 7 deletions cli/tests/test_branch_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,16 @@ fn test_branch_move() {
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");

let stderr = test_env.jj_cmd_failure(&repo_path, &["branch", "set", "foo"]);
insta::assert_snapshot!(stderr, @r###"
Error: No such branch: foo
Hint: Use `jj branch create` to create it.
"###);
let stderr = test_env.jj_cmd_failure(&repo_path, &["branch", "move", "foo"]);
insta::assert_snapshot!(stderr, @r###"
Error: No such branch: foo
"###);

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

test_env.jj_cmd_ok(&repo_path, &["new"]);
let stderr = test_env.jj_cmd_failure(&repo_path, &["branch", "create", "foo"]);
Expand Down