From e27421d2bd62948578af21d57948fffa23da4547 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 30 Apr 2024 12:06:04 +0900 Subject: [PATCH] templater: extract trackable RefName constructors They'll be called from cmd_branch_list(). --- cli/src/commit_templater.rs | 59 ++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/cli/src/commit_templater.rs b/cli/src/commit_templater.rs index 43a6d9d16..80bb8baf4 100644 --- a/cli/src/commit_templater.rs +++ b/cli/src/commit_templater.rs @@ -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, target: RefTarget) -> Self { + /// Creates local ref representation which might track some of the + /// `remote_refs`. + pub fn local<'a>( + name: impl Into, + target: RefTarget, + remote_refs: impl IntoIterator, + ) -> 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, 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, + remote_name: impl Into, + 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); } }