From 337dcef6eeeff4a80c3a76c45ec3aca314a63681 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 19 Jul 2024 18:07:44 +0900 Subject: [PATCH] commit_builder: add public interface that writes temporary commit to store In order to render description template, we'll need a Commit object that represents the old state (with new tree and parents) before updating the commit description. The added functions will help generate an intermediate Commit object. Alternatively, we can create an in-memory Commit object with some fake CommitId. It should be lightweight, but might cause weird issue because the fake id wouldn't be found in the store. I think it's okay to write a temporary commit and rely on GC as we do for merge trees. However, I should note that temporary commits are more likely to be preserved as they are pinned by no-gc refs until "jj util gc". --- lib/src/commit_builder.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/src/commit_builder.rs b/lib/src/commit_builder.rs index 7b70229d3..2bb639389 100644 --- a/lib/src/commit_builder.rs +++ b/lib/src/commit_builder.rs @@ -51,6 +51,12 @@ impl CommitBuilder<'_> { 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 { + self.inner + } + pub fn parents(&self) -> &[CommitId] { self.inner.parents() } @@ -216,6 +222,15 @@ impl DetachedCommitBuilder { } } + /// Attaches the underlying `mut_repo`. + pub fn attach(self, mut_repo: &mut MutableRepo) -> CommitBuilder<'_> { + assert!(Arc::ptr_eq(&self.store, mut_repo.store())); + CommitBuilder { + mut_repo, + inner: self, + } + } + pub fn parents(&self) -> &[CommitId] { &self.commit.parents } @@ -299,6 +314,7 @@ impl DetachedCommitBuilder { self } + /// Writes new commit and makes it visible in the `mut_repo`. pub fn write(self, mut_repo: &mut MutableRepo) -> BackendResult { let commit = write_to_store(&self.store, self.commit, &self.sign_settings)?; mut_repo.add_head(&commit)?; @@ -309,6 +325,14 @@ impl DetachedCommitBuilder { } Ok(commit) } + + /// Writes new commit without making it visible in the repo. + /// + /// This does not consume the builder, so you can reuse the current + /// configuration to create another commit later. + pub fn write_hidden(&mut self) -> BackendResult { + write_to_store(&self.store, self.commit.clone(), &self.sign_settings) + } } fn write_to_store(