Skip to content

Commit

Permalink
revsets: stop jj parsing br as a git_ref refs/heads/br
Browse files Browse the repository at this point in the history
Use `br@git` instead.

Before, if there is not a local branch `br`, jj tried to resolve
it as a git ref `refs/heads/br`. Unchanged from before, `br` can
still be resolved as a tag `refs/tag/br`.
  • Loading branch information
ilyagr committed Jun 12, 2023
1 parent a483252 commit 096538b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `jj debug completion`, `jj debug mangen` and `jj debug config-schema` have
been moved from `jj debug` to `jj util`.

* `jj` will no longer parse `br` as a git_ref `refs/heads/br` when a branch `br`
does not exist but the git_ref does (this is rare). Use `br@git` instead.

### New features

* `jj git push --deleted` will remove all locally deleted branches from the remote.
Expand Down
4 changes: 3 additions & 1 deletion lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,9 @@ pub fn walk_revs<'index>(

fn resolve_git_ref(repo: &dyn Repo, symbol: &str) -> Option<Vec<CommitId>> {
let view = repo.view();
for git_ref_prefix in &["", "refs/", "refs/heads/", "refs/tags/", "refs/remotes/"] {
// 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/"] {
if let Some(ref_target) = view.git_refs().get(&(git_ref_prefix.to_string() + symbol)) {
return Some(ref_target.adds());
}
Expand Down
29 changes: 14 additions & 15 deletions lib/tests/test_revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ fn test_resolve_symbol_git_refs() {
// Nonexistent ref
assert_matches!(
resolve_symbol(mut_repo, "nonexistent", None),
Err(RevsetResolutionError::NoSuchRevision{name, candidates}) if name == "nonexistent" && candidates.is_empty()
Err(RevsetResolutionError::NoSuchRevision{name, candidates})
if name == "nonexistent" && candidates.is_empty()
);

// Full ref
Expand All @@ -470,27 +471,25 @@ fn test_resolve_symbol_git_refs() {
"refs/heads/branch".to_string(),
RefTarget::Normal(commit5.id().clone()),
);
mut_repo.set_git_ref(
"refs/tags/branch".to_string(),
RefTarget::Normal(commit4.id().clone()),
);
assert_eq!(
resolve_symbol(mut_repo, "heads/branch", None).unwrap(),
vec![commit5.id().clone()]
);

// Unqualified branch name
mut_repo.set_git_ref(
"refs/heads/branch".to_string(),
RefTarget::Normal(commit3.id().clone()),
// branch alone is not recognized
assert_matches!(
resolve_symbol(mut_repo, "branch", None),
Err(RevsetResolutionError::NoSuchRevision{name, candidates})
if name == "branch" && candidates.is_empty()
);
mut_repo.set_git_ref(
"refs/tags/branch".to_string(),
RefTarget::Normal(commit4.id().clone()),
);
// The *tag* branch is recognized
assert_eq!(
resolve_symbol(mut_repo, "branch", None).unwrap(),
vec![commit3.id().clone()]
vec![commit4.id().clone()]
);
// heads/branch does get resolved to the git ref refs/heads/branch
assert_eq!(
resolve_symbol(mut_repo, "heads/branch", None).unwrap(),
vec![commit5.id().clone()]
);

// Unqualified tag name
Expand Down
10 changes: 5 additions & 5 deletions tests/test_branch_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ fn test_branch_forget_export() {
foo (deleted)
@git: 65b6b74e0897 (no description set)
"###);

// Aside: the way we currently resolve git refs means that `foo`
// resolves to `foo@git` when actual `foo` doesn't exist.
// Short-term TODO: This behavior will be changed in a subsequent commit.
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-r=foo", "--no-graph"]);
let stderr = test_env.jj_cmd_failure(&repo_path, &["log", "-r=foo", "--no-graph"]);
insta::assert_snapshot!(stderr, @r###"
Error: Revision "foo" doesn't exist
"###);
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-r=foo@git", "--no-graph"]);
insta::assert_snapshot!(stdout, @r###"
rlvkpnrzqnoo [email protected] 2001-02-03 04:05:08.000 +07:00 65b6b74e0897
(empty) (no description set)
Expand Down

0 comments on commit 096538b

Please sign in to comment.