From 0a48ac63cbeab30bb39d14744bf073d138918f5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Daron?= Date: Tue, 14 May 2024 18:20:37 +0200 Subject: [PATCH] cli: make jj branch track show conflicts --- CHANGELOG.md | 2 ++ cli/src/commands/branch.rs | 51 +++++++++++++++++++++++++++++++- cli/tests/test_branch_command.rs | 29 ++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d10496145c..7eaf1c33b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cli/src/commands/branch.rs b/cli/src/commands/branch.rs index e790a4cd4b..84974057fa 100644 --- a/cli/src/commands/branch.rs +++ b/cli/src/commands/branch.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use std::fmt; use std::io::Write as _; @@ -569,6 +569,55 @@ fn cmd_branch_track( names.len() )?; } + + //show conflicted branches if there are some + + if let Some(mut formatter) = ui.status_formatter() { + let template = { + let language = workspace_command.commit_template_language()?; + let text = command + .settings() + .config() + .get::("templates.branch_list")?; + workspace_command + .parse_template(&language, &text, CommitTemplateLanguage::wrap_ref_name)? + .labeled("branch_list") + }; + + let mut remote_per_branch: HashMap<&str, Vec<&str>> = HashMap::new(); + for n in names.iter() { + remote_per_branch + .entry(&n.branch) + .or_default() + .push(&n.remote); + } + let branches_to_list = + workspace_command + .repo() + .view() + .branches() + .filter(|(name, target)| { + remote_per_branch.contains_key(name) && 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 { + if remote_per_branch[name].contains(&remote_name) { + let ref_name = + RefName::remote(name, remote_name, remote_ref.clone(), local_target); + template.format(&ref_name, formatter.as_mut())?; + } + } + } + } Ok(()) } diff --git a/cli/tests/test_branch_command.rs b/cli/tests/test_branch_command.rs index 087cfbead3..5914f4f6d1 100644 --- a/cli/tests/test_branch_command.rs +++ b/cli/tests/test_branch_command.rs @@ -827,6 +827,35 @@ fn test_branch_track_untrack() { "###); } +#[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();