From 4f419944745199c7f6e1ad8c9190445fceba7bf9 Mon Sep 17 00:00:00 2001 From: Noah Mayr Date: Wed, 1 May 2024 21:49:26 +0200 Subject: [PATCH] lib: add topic metadata to view --- cli/src/commands/operation.rs | 1 + lib/src/op_store.rs | 1 + lib/src/protos/op_store.proto | 6 ++++++ lib/src/protos/op_store.rs | 10 ++++++++++ lib/src/simple_op_store.rs | 22 ++++++++++++++++++++++ lib/src/view.rs | 2 ++ 6 files changed, 42 insertions(+) diff --git a/cli/src/commands/operation.rs b/cli/src/commands/operation.rs index 782cb85335..844727ee72 100644 --- a/cli/src/commands/operation.rs +++ b/cli/src/commands/operation.rs @@ -247,6 +247,7 @@ fn view_with_desired_portions_restored( head_ids: repo_source.head_ids.clone(), local_branches: repo_source.local_branches.clone(), tags: repo_source.tags.clone(), + topics: repo_source.topics.clone(), remote_views: remote_source.remote_views.clone(), git_refs: current_view.git_refs.clone(), git_head: current_view.git_head.clone(), diff --git a/lib/src/op_store.rs b/lib/src/op_store.rs index 1eb072c80a..6c7cab6f13 100644 --- a/lib/src/op_store.rs +++ b/lib/src/op_store.rs @@ -261,6 +261,7 @@ pub struct View { pub head_ids: HashSet, pub local_branches: BTreeMap, pub tags: BTreeMap, + pub topics: BTreeMap>, pub remote_views: BTreeMap, pub git_refs: BTreeMap, /// The commit the Git HEAD points to. diff --git a/lib/src/protos/op_store.proto b/lib/src/protos/op_store.proto index d455b1bd0a..6d95139435 100644 --- a/lib/src/protos/op_store.proto +++ b/lib/src/protos/op_store.proto @@ -75,6 +75,11 @@ message Tag { RefTarget target = 2; } +message Topic { + string name = 1; + repeated bytes commit_ids = 2; +} + message View { repeated bytes head_ids = 1; reserved 4; @@ -82,6 +87,7 @@ message View { map wc_commit_ids = 8; repeated Branch branches = 5; repeated Tag tags = 6; + repeated Topic topics = 11; // Only a subset of the refs. For example, does not include refs/notes/. repeated GitRef git_refs = 3; // This field is just for historical reasons (before we had the RefTarget diff --git a/lib/src/protos/op_store.rs b/lib/src/protos/op_store.rs index 0497696397..7e484900cd 100644 --- a/lib/src/protos/op_store.rs +++ b/lib/src/protos/op_store.rs @@ -96,6 +96,14 @@ pub struct Tag { } #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] +pub struct Topic { + #[prost(string, tag = "1")] + pub name: ::prost::alloc::string::String, + #[prost(bytes = "vec", repeated, tag = "2")] + pub commit_ids: ::prost::alloc::vec::Vec<::prost::alloc::vec::Vec>, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] pub struct View { #[prost(bytes = "vec", repeated, tag = "1")] pub head_ids: ::prost::alloc::vec::Vec<::prost::alloc::vec::Vec>, @@ -111,6 +119,8 @@ pub struct View { pub branches: ::prost::alloc::vec::Vec, #[prost(message, repeated, tag = "6")] pub tags: ::prost::alloc::vec::Vec, + #[prost(message, repeated, tag = "11")] + pub topics: ::prost::alloc::vec::Vec, /// Only a subset of the refs. For example, does not include refs/notes/. #[prost(message, repeated, tag = "3")] pub git_refs: ::prost::alloc::vec::Vec, diff --git a/lib/src/simple_op_store.rs b/lib/src/simple_op_store.rs index 1b43841c5d..97027dea9b 100644 --- a/lib/src/simple_op_store.rs +++ b/lib/src/simple_op_store.rs @@ -424,6 +424,13 @@ fn view_to_proto(view: &View) -> crate::protos::op_store::View { }); } + for (name, commit_ids) in &view.topics { + proto.topics.push(crate::protos::op_store::Topic { + name: name.clone(), + commit_ids: commit_ids.iter().map(|id| id.to_bytes()).collect(), + }); + } + for (git_ref_name, target) in &view.git_refs { proto.git_refs.push(crate::protos::op_store::GitRef { name: git_ref_name.clone(), @@ -463,6 +470,18 @@ fn view_from_proto(proto: crate::protos::op_store::View) -> View { .insert(tag_proto.name, ref_target_from_proto(tag_proto.target)); } + for topic_proto in proto.topics { + view.topics.insert( + topic_proto.name, + topic_proto + .commit_ids + .iter() + .cloned() + .map(CommitId::new) + .collect(), + ); + } + for git_ref in proto.git_refs { let target = if git_ref.target.is_some() { ref_target_from_proto(git_ref.target) @@ -716,6 +735,9 @@ mod tests { tags: btreemap! { "v1.0".to_string() => tag_v1_target, }, + topics: btreemap! { + "topic".to_string() => hashset![CommitId::from_hex("ccc111"), CommitId::from_hex("ccc222")], + }, remote_views: btreemap! { "origin".to_string() => RemoteView { branches: btreemap! { diff --git a/lib/src/view.rs b/lib/src/view.rs index 1125bd0b2b..573c5ec279 100644 --- a/lib/src/view.rs +++ b/lib/src/view.rs @@ -344,6 +344,7 @@ impl View { head_ids, local_branches, tags, + topics, remote_views, git_refs, git_head, @@ -352,6 +353,7 @@ impl View { itertools::chain!( head_ids, local_branches.values().flat_map(ref_target_ids), + topics.values().flatten(), tags.values().flat_map(ref_target_ids), remote_views.values().flat_map(|remote_view| { let op_store::RemoteView { branches } = remote_view;