Skip to content

Commit

Permalink
lib/git.rs: inline functions used only once, rename others
Browse files Browse the repository at this point in the history
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
jj-vcs#1666 (comment)

More refactoring will likely happen when that bug is fixed.
  • Loading branch information
ilyagr committed Jun 30, 2023
1 parent c65f9fc commit 9b54e48
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
22 changes: 6 additions & 16 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ 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
Expand Down Expand Up @@ -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_git_tracking_branches(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> {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::BTreeSet;
use clap::builder::NonEmptyStringValueParser;
use itertools::Itertools;
use jujutsu_lib::backend::{CommitId, ObjectId};
use jujutsu_lib::git::{self, git_tracking_branches};
use jujutsu_lib::git::{self, local_git_tracking_branches};
use jujutsu_lib::git_backend::GitBackend;
use jujutsu_lib::op_store::RefTarget;
use jujutsu_lib::repo::Repo;
Expand Down Expand Up @@ -320,7 +320,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_git_tracking_branches(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
Expand Down

0 comments on commit 9b54e48

Please sign in to comment.