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 17, 2024
1 parent c34f35f commit 2b78698
Show file tree
Hide file tree
Showing 3 changed files with 80 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
51 changes: 50 additions & 1 deletion cli/src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;

Expand Down Expand Up @@ -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::<String>("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(())
}

Expand Down
28 changes: 28 additions & 0 deletions cli/tests/test_branch_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,34 @@ 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();
Expand Down

0 comments on commit 2b78698

Please sign in to comment.