Skip to content

Commit

Permalink
commit_builder: add public interface that writes temporary commit to …
Browse files Browse the repository at this point in the history
…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".
  • Loading branch information
yuja committed Jul 20, 2024
1 parent 160366e commit d439237
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/src/commit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)?;
Expand All @@ -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(
Expand Down

0 comments on commit d439237

Please sign in to comment.