Skip to content

Commit

Permalink
repo: use DetachedCommitBuilder constructors
Browse files Browse the repository at this point in the history
I think this makes it clear that the builder doesn't add any rewrite records
to the mut_repo.
  • Loading branch information
yuja committed Jul 23, 2024
1 parent 337dcef commit 626aa90
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
31 changes: 8 additions & 23 deletions lib/src/commit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommitId>,
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 {
Expand Down Expand Up @@ -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<CommitId>,
Expand Down Expand Up @@ -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()];
Expand Down
6 changes: 3 additions & 3 deletions lib/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -843,7 +843,7 @@ impl MutableRepo {
parents: Vec<CommitId>,
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.
Expand All @@ -852,7 +852,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`
}
Expand Down

0 comments on commit 626aa90

Please sign in to comment.