Skip to content

Commit

Permalink
revset: exclude @git branches from remote_branches()
Browse files Browse the repository at this point in the history
As discussed in Discord, it's less useful if remote_branches() included
Git-tracking branches. Users wouldn't consider the backing Git repo as
a remote.

We could allow explicit 'remote_branches(remote=exact:"git")' query by changing
the default remote pattern to something like 'remote=~exact:"git"'. I don't
know which will be better overall, but we don't have support for negative
patterns anyway.
  • Loading branch information
yuja committed Nov 7, 2023
1 parent d1b0c4c commit 0e6f471
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Breaking changes

* The `remote_branches()` revset no longer includes branches exported to the Git
repository (so called Git-tracking branches.)

### New features

* `jj workspace add` can now take _multiple_ `--revision` arguments, which will
Expand All @@ -33,8 +36,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
clone to see the full speedup.

* The `remote_branches()` revset now includes branches exported to the Git
repository (so called Git-tracking branches.) Use
`remote_branches(remote=exact:"origin")` to query branches of certain remote.
repository (so called Git-tracking branches.) *This change will be reverted
in 0.12.0.*

* Status messages are now printed to stderr.

Expand Down
5 changes: 2 additions & 3 deletions cli/tests/test_git_import_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ fn test_resolution_of_git_tracking_branches() {
insta::assert_snapshot!(query("main@git"), @r###"
16d541ca40f42baf2dea41aa61a0b5f1cbf1f91b old_message
"###);
insta::assert_snapshot!(query(r#"remote_branches(exact:"main", exact:"git")"#), @r###"
16d541ca40f42baf2dea41aa61a0b5f1cbf1f91b old_message
"###);
// Can't be selected by remote_branches()
insta::assert_snapshot!(query(r#"remote_branches(exact:"main", exact:"git")"#), @"");
}

#[test]
Expand Down
3 changes: 3 additions & 0 deletions docs/revsets.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ revsets (expressions) as arguments.
`main@origin` or `main@upstream`. If a branch is in a conflicted state, all
its possible targets are included.

While Git-tracking branches can be selected by `<name>@git`, these branches
aren't included in `remote_branches()`.

* `tags()`: All tag targets. If a tag is in a conflicted state, all its
possible targets are included.

Expand Down
2 changes: 2 additions & 0 deletions lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2147,9 +2147,11 @@ fn resolve_commit_ref(
branch_pattern,
remote_pattern,
} => {
// TODO: should we allow to select @git branches explicitly?
let commit_ids = repo
.view()
.remote_branches_matching(branch_pattern, remote_pattern)
.filter(|&((_, remote_name), _)| remote_name != git::REMOTE_NAME_FOR_LOCAL_GIT_REPO)
.flat_map(|(_, remote_ref)| remote_ref.target.added_ids())
.cloned()
.collect();
Expand Down
7 changes: 7 additions & 0 deletions lib/tests/test_revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1836,12 +1836,19 @@ fn test_evaluate_expression_remote_branches() {
let commit2 = write_random_commit(mut_repo, &settings);
let commit3 = write_random_commit(mut_repo, &settings);
let commit4 = write_random_commit(mut_repo, &settings);
let commit_git_remote = write_random_commit(mut_repo, &settings);

// Can get branches when there are none
assert_eq!(resolve_commit_ids(mut_repo, "remote_branches()"), vec![]);
// Can get a few branches
mut_repo.set_remote_branch("branch1", "origin", normal_remote_ref(commit1.id()));
mut_repo.set_remote_branch("branch2", "private", normal_remote_ref(commit2.id()));
// Git-tracking branches aren't included
mut_repo.set_remote_branch(
"branch",
git::REMOTE_NAME_FOR_LOCAL_GIT_REPO,
normal_remote_ref(commit_git_remote.id()),
);
assert_eq!(
resolve_commit_ids(mut_repo, "remote_branches()"),
vec![commit2.id().clone(), commit1.id().clone()]
Expand Down

0 comments on commit 0e6f471

Please sign in to comment.