diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d04c0330..0083c13a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * `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. +* `jj git fetch` will no longer import unrelated branches from the underlying + Git repo. + ### New features * `jj git push --deleted` will remove all locally deleted branches from the remote. diff --git a/lib/src/git.rs b/lib/src/git.rs index 6eecd7704..a096fe80f 100644 --- a/lib/src/git.rs +++ b/lib/src/git.rs @@ -64,6 +64,13 @@ fn to_git_ref_name(parsed_ref: &RefName) -> String { } } +fn to_remote_branch<'a>(parsed_ref: &'a RefName, remote_name: &str) -> Option<&'a str> { + match parsed_ref { + RefName::RemoteBranch { branch, remote } => (remote == remote_name).then_some(branch), + RefName::LocalBranch(..) | RefName::Tag(..) | RefName::GitRef(..) => None, + } +} + /// Checks if `git_ref` points to a Git commit object, and returns its id. /// /// If the ref points to the previously `known_target` (i.e. unchanged), this @@ -570,15 +577,13 @@ pub fn fetch( None }; let git_ref_filter = |ref_name: &RefName| -> bool { - if let Some(branch_regex) = &branch_regex { - match ref_name { - RefName::RemoteBranch { branch, remote } => { - remote == remote_name && branch_regex.is_match(branch) - } - RefName::LocalBranch(..) | RefName::Tag(..) | RefName::GitRef(..) => false, - } + if let Some(branch) = to_remote_branch(ref_name, remote_name) { + branch_regex + .as_ref() + .map(|r| r.is_match(branch)) + .unwrap_or(true) } else { - true + false } };