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".
This commit is contained in:
Yuya Nishihara 2024-07-19 18:07:44 +09:00
parent b4bf1358a5
commit 337dcef6ee

View file

@ -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<Commit> {
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<Commit> {
write_to_store(&self.store, self.commit.clone(), &self.sign_settings)
}
}
fn write_to_store(