Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revset: do not lookup unimported tags or remote branches by unqualified name #2211

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
rename` to rename the existing remote.
[#1690](https://github.com/martinvonz/jj/issues/1690)

* Revset expression like `origin/main` will no longer resolve to a
remote-tracking branch. Use `main@origin` instead.

### New features

* Default template for `jj log` now does not show irrelevant information
Expand Down
4 changes: 1 addition & 3 deletions lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1899,9 +1899,7 @@ fn filter_map_values_by_key_pattern<'a: 'b, 'b, V>(

fn resolve_git_ref(repo: &dyn Repo, symbol: &str) -> Option<Vec<CommitId>> {
let view = repo.view();
// TODO: We should remove `refs/remotes` from this list once we have a better
// way to address local git repo's remote-tracking branches.
for git_ref_prefix in &["", "refs/", "refs/tags/", "refs/remotes/"] {
for git_ref_prefix in &["", "refs/"] {
let target = view.get_git_ref(&(git_ref_prefix.to_string() + symbol));
if target.is_present() {
return Some(target.added_ids().cloned().collect());
Expand Down
51 changes: 39 additions & 12 deletions lib/tests/test_revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,38 @@ fn test_resolve_symbol_branches() {
"###);
}

#[test]
fn test_resolve_symbol_tags() {
let settings = testutils::user_settings();
let test_repo = TestRepo::init(true);
let repo = &test_repo.repo;

let mut tx = repo.start_transaction(&settings, "test");
let mut_repo = tx.mut_repo();

let commit1 = write_random_commit(mut_repo, &settings);
let commit2 = write_random_commit(mut_repo, &settings);
let commit3 = write_random_commit(mut_repo, &settings);

mut_repo.set_tag_target("tag-branch", RefTarget::normal(commit1.id().clone()));
mut_repo.set_local_branch_target("tag-branch", RefTarget::normal(commit2.id().clone()));
mut_repo.set_git_ref_target(
"refs/tags/unimported",
RefTarget::normal(commit3.id().clone()),
);

// Tag precedes branch
assert_eq!(
resolve_symbol(mut_repo, "tag-branch").unwrap(),
vec![commit1.id().clone()],
);

assert_matches!(
resolve_symbol(mut_repo, "unimported"),
Err(RevsetResolutionError::NoSuchRevision { .. })
);
}

#[test]
fn test_resolve_symbol_git_head() {
let settings = testutils::user_settings();
Expand Down Expand Up @@ -685,6 +717,7 @@ fn test_resolve_symbol_git_refs() {

// Qualified with only heads/
mut_repo.set_git_ref_target("refs/heads/branch", RefTarget::normal(commit5.id().clone()));
mut_repo.set_git_ref_target("refs/tags/branch", RefTarget::normal(commit4.id().clone()));
// branch alone is not recognized
insta::assert_debug_snapshot!(
resolve_symbol(mut_repo, "branch").unwrap_err(), @r###"
Expand All @@ -697,12 +730,6 @@ fn test_resolve_symbol_git_refs() {
],
}
"###);
mut_repo.set_git_ref_target("refs/tags/branch", RefTarget::normal(commit4.id().clone()));
// The *tag* branch is recognized
assert_eq!(
resolve_symbol(mut_repo, "branch").unwrap(),
vec![commit4.id().clone()]
);
// heads/branch does get resolved to the git ref refs/heads/branch
assert_eq!(
resolve_symbol(mut_repo, "heads/branch").unwrap(),
Expand All @@ -711,19 +738,19 @@ fn test_resolve_symbol_git_refs() {

// Unqualified tag name
mut_repo.set_git_ref_target("refs/tags/tag", RefTarget::normal(commit4.id().clone()));
assert_eq!(
resolve_symbol(mut_repo, "tag").unwrap(),
vec![commit4.id().clone()]
assert_matches!(
resolve_symbol(mut_repo, "tag"),
Err(RevsetResolutionError::NoSuchRevision { .. })
);

// Unqualified remote-tracking branch name
mut_repo.set_git_ref_target(
"refs/remotes/origin/remote-branch",
RefTarget::normal(commit2.id().clone()),
);
assert_eq!(
resolve_symbol(mut_repo, "origin/remote-branch").unwrap(),
vec![commit2.id().clone()]
assert_matches!(
resolve_symbol(mut_repo, "origin/remote-branch"),
Err(RevsetResolutionError::NoSuchRevision { .. })
);

// "@" (quoted) can be resolved, and root is a normal symbol.
Expand Down