From f758b646a9e5250920b6da948675f4e7b90ff3b8 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Fri, 17 Mar 2023 22:34:25 -0700 Subject: [PATCH] commit_builder: add accessors for most fields I'd like to be able to access the current committer on a `CommitBuilder`. --- lib/src/commit_builder.rs | 28 ++++++++++++++++++++++ lib/tests/test_commit_builder.rs | 41 +++++++++++++++++++++++++------- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/lib/src/commit_builder.rs b/lib/src/commit_builder.rs index a2dea2ef3..ed6dc1e49 100644 --- a/lib/src/commit_builder.rs +++ b/lib/src/commit_builder.rs @@ -79,22 +79,38 @@ impl CommitBuilder<'_> { } } + pub fn parents(&self) -> &[CommitId] { + &self.commit.parents + } + pub fn set_parents(mut self, parents: Vec) -> Self { assert!(!parents.is_empty()); self.commit.parents = parents; self } + pub fn predecessors(&self) -> &[CommitId] { + &self.commit.predecessors + } + pub fn set_predecessors(mut self, predecessors: Vec) -> Self { self.commit.predecessors = predecessors; self } + pub fn tree(&self) -> &TreeId { + &self.commit.root_tree + } + pub fn set_tree(mut self, tree_id: TreeId) -> Self { self.commit.root_tree = tree_id; self } + pub fn change_id(&self) -> &ChangeId { + &self.commit.change_id + } + pub fn set_change_id(mut self, change_id: ChangeId) -> Self { self.commit.change_id = change_id; self @@ -107,16 +123,28 @@ impl CommitBuilder<'_> { self } + pub fn description(&self) -> &str { + &self.commit.description + } + pub fn set_description(mut self, description: impl Into) -> Self { self.commit.description = description.into(); self } + pub fn author(&self) -> &Signature { + &self.commit.author + } + pub fn set_author(mut self, author: Signature) -> Self { self.commit.author = author; self } + pub fn committer(&self) -> &Signature { + &self.commit.committer + } + pub fn set_committer(mut self, committer: Signature) -> Self { self.commit.committer = committer; self diff --git a/lib/tests/test_commit_builder.rs b/lib/tests/test_commit_builder.rs index ff44a508e..d8844c63c 100644 --- a/lib/tests/test_commit_builder.rs +++ b/lib/tests/test_commit_builder.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use jujutsu_lib::backend::{ChangeId, MillisSinceEpoch, ObjectId, Signature, Timestamp}; use jujutsu_lib::matchers::EverythingMatcher; use jujutsu_lib::repo::Repo; use jujutsu_lib::repo_path::RepoPath; @@ -39,24 +40,48 @@ fn test_initial(use_git: bool) { ); let mut tx = repo.start_transaction(&settings, "test"); - let commit = tx + let author_signature = Signature { + name: "author name".to_string(), + email: "author email".to_string(), + timestamp: Timestamp { + timestamp: MillisSinceEpoch(100), + tz_offset: 60, + }, + }; + let committer_signature = Signature { + name: "committer name".to_string(), + email: "committer email".to_string(), + timestamp: Timestamp { + timestamp: MillisSinceEpoch(200), + tz_offset: -60, + }, + }; + let change_id = ChangeId::new(vec![100u8; 16]); + let builder = tx .mut_repo() .new_commit( &settings, vec![store.root_commit_id().clone()], tree.id().clone(), ) - .write() - .unwrap(); + .set_change_id(change_id.clone()) + .set_description("description") + .set_author(author_signature.clone()) + .set_committer(committer_signature.clone()); + assert_eq!(builder.parents(), &[store.root_commit_id().clone()]); + assert_eq!(builder.predecessors(), &[]); + assert_eq!(builder.tree(), tree.id()); + assert_eq!(builder.change_id(), &change_id); + assert_eq!(builder.author(), &author_signature); + assert_eq!(builder.committer(), &committer_signature); + let commit = builder.write().unwrap(); tx.commit(); assert_eq!(commit.parents(), vec![store.root_commit()]); assert_eq!(commit.predecessors(), vec![]); - assert_eq!(commit.description(), ""); - assert_eq!(commit.author().name, settings.user_name()); - assert_eq!(commit.author().email, settings.user_email()); - assert_eq!(commit.committer().name, settings.user_name()); - assert_eq!(commit.committer().email, settings.user_email()); + assert_eq!(commit.description(), "description"); + assert_eq!(commit.author(), &author_signature); + assert_eq!(commit.committer(), &committer_signature); assert_eq!( store .root_commit()