templater: extract trackable RefName constructors

They'll be called from cmd_branch_list().
This commit is contained in:
Yuya Nishihara 2024-04-30 12:06:04 +09:00
parent 9a5b001d58
commit e27421d2bd

View file

@ -26,7 +26,7 @@ use jj_lib::git;
use jj_lib::hex_util::to_reverse_hex;
use jj_lib::id_prefix::IdPrefixContext;
use jj_lib::object_id::ObjectId as _;
use jj_lib::op_store::{RefTarget, WorkspaceId};
use jj_lib::op_store::{RefTarget, RemoteRef, WorkspaceId};
use jj_lib::repo::Repo;
use jj_lib::revset::{self, Revset, RevsetExpression, RevsetParseContext};
use once_cell::unsync::OnceCell;
@ -735,13 +735,43 @@ pub struct RefName {
}
impl RefName {
/// Creates local ref representation which doesn't track any remote refs.
pub fn local_only(name: impl Into<String>, target: RefTarget) -> Self {
/// Creates local ref representation which might track some of the
/// `remote_refs`.
pub fn local<'a>(
name: impl Into<String>,
target: RefTarget,
remote_refs: impl IntoIterator<Item = &'a RemoteRef>,
) -> Self {
let synced = remote_refs
.into_iter()
.all(|remote_ref| !remote_ref.is_tracking() || remote_ref.target == target);
RefName {
name: name.into(),
remote: None,
target,
synced: true, // has no tracking remotes
synced,
}
}
/// Creates local ref representation which doesn't track any remote refs.
pub fn local_only(name: impl Into<String>, target: RefTarget) -> Self {
Self::local(name, target, [])
}
/// Creates remote ref representation which might be tracked by a local ref
/// pointing to the `local_target`.
pub fn remote(
name: impl Into<String>,
remote_name: impl Into<String>,
remote_ref: RemoteRef,
local_target: &RefTarget,
) -> Self {
let synced = remote_ref.is_tracking() && remote_ref.target == *local_target;
RefName {
name: name.into(),
remote: Some(remote_name.into()),
target: remote_ref.target,
synced,
}
}
@ -891,23 +921,16 @@ fn build_branches_index(repo: &dyn Repo) -> RefNamesIndex {
let local_target = branch_target.local_target;
let remote_refs = branch_target.remote_refs;
if local_target.is_present() {
let ref_name = RefName {
name: branch_name.to_owned(),
remote: None,
target: local_target.clone(),
synced: remote_refs.iter().all(|&(_, remote_ref)| {
!remote_ref.is_tracking() || remote_ref.target == *local_target
}),
};
let ref_name = RefName::local(
branch_name,
local_target.clone(),
remote_refs.iter().map(|&(_, remote_ref)| remote_ref),
);
index.insert(local_target.added_ids(), ref_name);
}
for &(remote_name, remote_ref) in &remote_refs {
let ref_name = RefName {
name: branch_name.to_owned(),
remote: Some(remote_name.to_owned()),
target: remote_ref.target.clone(),
synced: remote_ref.is_tracking() && remote_ref.target == *local_target,
};
let ref_name =
RefName::remote(branch_name, remote_name, remote_ref.clone(), local_target);
index.insert(remote_ref.target.added_ids(), ref_name);
}
}