Skip to content

Commit

Permalink
cli: add option to list only conflicted branches
Browse files Browse the repository at this point in the history
As requested in #1471, I added a new flag for `jj branch list` to only show branches that are conflicted.

Adds a unit test to check for listing only conflicted branches and regenerates the cli output to incorporate the new flag.

Closes #1471

reformat
  • Loading branch information
Kintaro committed Mar 23, 2024
1 parent 3034dbb commit 39e6dc1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* `jj git push` now prints messages from the remote.

* `jj branch list` now supports a `--conflicted/-c` option to show only conflicted branches.

### Fixed bugs

## [0.15.1] - 2024-03-06
Expand Down
7 changes: 6 additions & 1 deletion cli/src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ pub struct BranchListArgs {
#[arg(long, short, conflicts_with_all = ["all"])]
tracked: bool,

/// Show conflicted branches only.
#[arg(long, short, conflicts_with_all = ["all"])]
conflicted: bool,

/// Show branches whose local name matches
///
/// By default, the specified name matches exactly. Use `glob:` prefix to
Expand Down Expand Up @@ -673,10 +677,11 @@ fn cmd_branch_list(
let mut formatter = ui.stdout_formatter();
let formatter = formatter.as_mut();

let branches_to_list = view.branches().filter(|&(name, _)| {
let branches_to_list = view.branches().filter(|(name, target)| {
branch_names_to_list
.as_ref()
.map_or(true, |branch_names| branch_names.contains(name))
&& (!args.conflicted || target.local_target.has_conflict())
});
for (name, branch_target) in branches_to_list {
let (mut tracking_remote_refs, untracked_remote_refs) = branch_target
Expand Down
4 changes: 4 additions & 0 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ For information about branches, see https://github.com/martinvonz/jj/blob/main/d
Possible values: `true`, `false`
* `-c`, `--conflicted` — Show conflicted branches only
Possible values: `true`, `false`
* `-r`, `--revisions <REVISIONS>` — Show branches whose local targets are in the given revisions
Expand Down
39 changes: 39 additions & 0 deletions cli/tests/test_branch_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,45 @@ fn test_branch_list_tracked() {
"###);
}

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

// Track existing branch. Local branch should result in conflict.
test_env.jj_cmd_ok(&repo_path, &["new", "root()", "-m", "a"]);
test_env.jj_cmd_ok(&repo_path, &["new", "root()", "-m", "b"]);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "bar"]);
test_env.jj_cmd_ok(
&repo_path,
&["branch", "create", "foo", "-r", "description(a)"],
);
test_env.jj_cmd_ok(
&repo_path,
&[
"branch",
"create",
"foo",
"-r",
"description(b)",
"--at-op=@-",
],
);
test_env.jj_cmd_ok(&repo_path, &["status"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
bar: kkmpptxz 06a973bc (empty) b
foo (conflicted):
+ rlvkpnrz d8d5f980 (empty) a
+ kkmpptxz 06a973bc (empty) b
"###);
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["branch", "list", "--conflicted"]), @r###"
foo (conflicted):
+ rlvkpnrz d8d5f980 (empty) a
+ kkmpptxz 06a973bc (empty) b
"###);
}

fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
let template = r#"branches ++ " " ++ commit_id.short()"#;
test_env.jj_cmd_success(cwd, &["log", "-T", template])
Expand Down

0 comments on commit 39e6dc1

Please sign in to comment.