lib/git.rs: inline functions used only once, rename others

I now believe that jj will need to store git-tracking refs for both local and
remote-tracking branches of the git repo for the long term. See
https://github.com/martinvonz/jj/issues/1666#issuecomment-1597806451

More refactoring will likely happen when that bug is fixed.
This commit is contained in:
Ilya Grigoriev 2023-06-28 19:22:09 -07:00
parent 59b354992a
commit a50cfec008
3 changed files with 10 additions and 20 deletions

View file

@ -64,14 +64,6 @@ pub fn to_git_ref_name(parsed_ref: &RefName) -> String {
}
}
fn ref_name_to_local_branch_name(ref_name: &str) -> Option<&str> {
ref_name.strip_prefix("refs/heads/")
}
fn local_branch_name_to_ref_name(branch: &str) -> String {
format!("refs/heads/{branch}")
}
/// 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
@ -110,18 +102,16 @@ fn resolve_git_ref_to_commit_id(
Some(CommitId::from_bytes(git_commit.id().as_bytes()))
}
// TODO: Eventually, git-tracking branches should no longer be stored in
// git_refs but with the other remote-tracking branches in BranchTarget. Note
// that there are important but subtle differences in behavior for, e.g. `jj
// branch forget`.
pub fn git_tracking_branches(view: &View) -> impl Iterator<Item = (&str, &RefTarget)> {
pub fn local_branch_git_tracking_refs(view: &View) -> impl Iterator<Item = (&str, &RefTarget)> {
view.git_refs().iter().filter_map(|(ref_name, target)| {
ref_name_to_local_branch_name(ref_name).map(|branch_name| (branch_name, target))
ref_name
.strip_prefix("refs/heads/")
.map(|branch_name| (branch_name, target))
})
}
pub fn get_git_tracking_branch<'a>(view: &'a View, branch: &str) -> Option<&'a RefTarget> {
view.git_refs().get(&local_branch_name_to_ref_name(branch))
pub fn get_local_git_tracking_branch<'a>(view: &'a View, branch: &str) -> Option<&'a RefTarget> {
view.git_refs().get(&format!("refs/heads/{branch}"))
}
fn prevent_gc(git_repo: &git2::Repository, id: &CommitId) -> Result<(), git2::Error> {

View file

@ -31,7 +31,7 @@ use thiserror::Error;
use crate::backend::{BackendError, BackendResult, ChangeId, CommitId, ObjectId};
use crate::commit::Commit;
use crate::git::get_git_tracking_branch;
use crate::git::get_local_git_tracking_branch;
use crate::hex_util::to_forward_hex;
use crate::index::{HexPrefix, PrefixResolution};
use crate::op_store::WorkspaceId;
@ -1658,7 +1658,7 @@ fn resolve_branch(repo: &dyn Repo, symbol: &str) -> Option<Vec<CommitId>> {
}
// A remote with name "git" will shadow local-git tracking branches
if remote_name == "git" {
if let Some(target) = get_git_tracking_branch(repo.view(), name) {
if let Some(target) = get_local_git_tracking_branch(repo.view(), name) {
return Some(target.adds().to_vec());
}
}

View file

@ -3,7 +3,7 @@ use std::collections::{BTreeSet, HashSet};
use clap::builder::NonEmptyStringValueParser;
use itertools::Itertools;
use jujutsu_lib::backend::{CommitId, ObjectId};
use jujutsu_lib::git::git_tracking_branches;
use jujutsu_lib::git::local_branch_git_tracking_refs;
use jujutsu_lib::op_store::{BranchTarget, RefTarget};
use jujutsu_lib::repo::Repo;
use jujutsu_lib::revset::{self, RevsetExpression};
@ -324,7 +324,7 @@ fn cmd_branch_list(
let repo = workspace_command.repo();
let mut all_branches = repo.view().branches().clone();
for (branch_name, git_tracking_target) in git_tracking_branches(repo.view()) {
for (branch_name, git_tracking_target) in local_branch_git_tracking_refs(repo.view()) {
let branch_target = all_branches.entry(branch_name.to_owned()).or_default();
if branch_target.remote_targets.contains_key("git") {
// TODO(#1690): There should be a mechanism to prevent importing a