From 5d127aeb50d6b87028d07cafc3f2fc66cfbe3092 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sat, 20 Jul 2024 12:40:14 +0900 Subject: [PATCH] repo: use DetachedCommitBuilder constructors I think this makes it clear that the builder doesn't add any rewrite records to the mut_repo. --- lib/src/commit_builder.rs | 31 ++++++++----------------------- lib/src/repo.rs | 6 +++--- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/lib/src/commit_builder.rs b/lib/src/commit_builder.rs index 2bb63938940..b1f2f6f7363 100644 --- a/lib/src/commit_builder.rs +++ b/lib/src/commit_builder.rs @@ -30,27 +30,6 @@ pub struct CommitBuilder<'repo> { } impl CommitBuilder<'_> { - /// Only called from [`MutRepo::new_commit`]. Use that function instead. - pub(crate) fn for_new_commit<'repo>( - mut_repo: &'repo mut MutableRepo, - settings: &UserSettings, - parents: Vec, - tree_id: MergedTreeId, - ) -> CommitBuilder<'repo> { - let inner = DetachedCommitBuilder::for_new_commit(mut_repo, settings, parents, tree_id); - CommitBuilder { mut_repo, inner } - } - - /// Only called from [`MutRepo::rewrite_commit`]. Use that function instead. - pub(crate) fn for_rewrite_from<'repo>( - mut_repo: &'repo mut MutableRepo, - settings: &UserSettings, - predecessor: &Commit, - ) -> CommitBuilder<'repo> { - let inner = DetachedCommitBuilder::for_rewrite_from(mut_repo, settings, predecessor); - CommitBuilder { mut_repo, inner } - } - /// Detaches from `&'repo mut` lifetime. The returned builder can be used in /// order to obtain a temporary commit object. pub fn detach(self) -> DetachedCommitBuilder { @@ -155,7 +134,8 @@ pub struct DetachedCommitBuilder { } impl DetachedCommitBuilder { - fn for_new_commit( + /// Only called from [`MutRepo::new_commit`]. Use that function instead. + pub(crate) fn for_new_commit( repo: &dyn Repo, settings: &UserSettings, parents: Vec, @@ -185,7 +165,12 @@ impl DetachedCommitBuilder { } } - fn for_rewrite_from(repo: &dyn Repo, settings: &UserSettings, predecessor: &Commit) -> Self { + /// Only called from [`MutRepo::rewrite_commit`]. Use that function instead. + pub(crate) fn for_rewrite_from( + repo: &dyn Repo, + settings: &UserSettings, + predecessor: &Commit, + ) -> Self { let store = repo.store().clone(); let mut commit = predecessor.store_commit().clone(); commit.predecessors = vec![predecessor.id().clone()]; diff --git a/lib/src/repo.rs b/lib/src/repo.rs index dc8f28e4047..1737995580b 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -33,7 +33,7 @@ use crate::backend::{ MergedTreeId, }; use crate::commit::{Commit, CommitByCommitterTimestamp}; -use crate::commit_builder::CommitBuilder; +use crate::commit_builder::{CommitBuilder, DetachedCommitBuilder}; use crate::default_index::{DefaultIndexStore, DefaultMutableIndex}; use crate::default_submodule_store::DefaultSubmoduleStore; use crate::file_util::{IoResultExt as _, PathError}; @@ -817,7 +817,7 @@ impl MutableRepo { parents: Vec, tree_id: MergedTreeId, ) -> CommitBuilder { - CommitBuilder::for_new_commit(self, settings, parents, tree_id) + DetachedCommitBuilder::for_new_commit(self, settings, parents, tree_id).attach(self) } /// Returns a [`CommitBuilder`] to rewrite an existing commit in the repo. @@ -826,7 +826,7 @@ impl MutableRepo { settings: &UserSettings, predecessor: &Commit, ) -> CommitBuilder { - CommitBuilder::for_rewrite_from(self, settings, predecessor) + DetachedCommitBuilder::for_rewrite_from(self, settings, predecessor).attach(self) // CommitBuilder::write will record the rewrite in // `self.rewritten_commits` }