Skip to content

Commit

Permalink
cli: make jj branch track show conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
tdaron committed May 15, 2024
1 parent b9039c3 commit 5aceb55
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* `ui.color = "debug"` prints active labels alongside the regular colored output.

* `jj branch track` now show conflicts if there are some.

### Fixed bugs

* When the working copy commit becomes immutable, a new one is automatically created on top of it
Expand Down
48 changes: 47 additions & 1 deletion cli/src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::io::Write as _;

use clap::builder::NonEmptyStringValueParser;
use itertools::Itertools;
use jj_lib::git;
use jj_lib::git::{self, REMOTE_NAME_FOR_LOCAL_GIT_REPO};
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::{RefTarget, RemoteRef};
use jj_lib::repo::Repo;
Expand Down Expand Up @@ -569,6 +569,52 @@ fn cmd_branch_track(
names.len()
)?;
}

//show conflicted branches if there are some
let template = {
let language = workspace_command.commit_template_language()?;
let text = command
.settings()
.config()
.get::<String>("templates.branch_list")?;
workspace_command
.parse_template(&language, &text, CommitTemplateLanguage::wrap_ref_name)?
.labeled("branch_list")
};

ui.request_pager();
if let Some(mut formatter) = ui.status_formatter() {
let branches_to_list =
workspace_command
.repo()
.view()
.branches()
.filter(|(name, target)| {
names
.iter()
.map(|n| n.branch.clone())
.contains(&name.to_string())
&& (target.local_target.has_conflict())
});
for (name, branch_target) in branches_to_list {
let local_target = branch_target.local_target;
let ref_name = RefName::local(
name,
local_target.clone(),
branch_target.remote_refs.iter().map(|x| x.1),
);
template.format(&ref_name, formatter.as_mut())?;

for (remote_name, remote_ref) in branch_target.remote_refs {
//shall we show @git remote ?
if remote_name == REMOTE_NAME_FOR_LOCAL_GIT_REPO {
continue;
}
let ref_name = RefName::remote(name, remote_name, remote_ref.clone(), local_target);
template.format(&ref_name, formatter.as_mut())?;
}
}
}
Ok(())
}

Expand Down
27 changes: 27 additions & 0 deletions cli/tests/test_branch_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,34 @@ fn test_branch_track_untrack() {
◉ 000000000000
"###);
}
#[test]
fn test_branch_track_conflict() {
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");

let git_repo_path = test_env.env_root().join("git-repo");
git2::Repository::init_bare(git_repo_path).unwrap();
test_env.jj_cmd_ok(
&repo_path,
&["git", "remote", "add", "origin", "../git-repo"],
);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "main"]);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "a"]);
test_env.jj_cmd_ok(&repo_path, &["git", "push", "-b", "main"]);
test_env.jj_cmd_ok(&repo_path, &["branch", "untrack", "main@origin"]);
test_env.jj_cmd_ok(
&repo_path,
&["describe", "-m", "b", "-r", "main", "--ignore-immutable"],
);
let (_, stderr) = test_env.jj_cmd_ok(&repo_path, &["branch", "track", "main@origin"]);
insta::assert_snapshot!(stderr, @r###"
main (conflicted):
+ qpvuntsm b4a6b8c5 (empty) b
+ qpvuntsm hidden 4bfd80cd (empty) a
@origin (behind by 1 commits): qpvuntsm hidden 4bfd80cd (empty) a
"###);
}
#[test]
fn test_branch_track_untrack_patterns() {
let test_env = TestEnvironment::default();
Expand Down

0 comments on commit 5aceb55

Please sign in to comment.