When you're done with the `CommitBuilder`, you're going to have to call `write_to_repo()`, passing it a mutable `MutableRepo` reference. It's a bit simpler to pass that reference when we create the `CommitBuilder` instead, so that's what this patch does. A drawback of passing in the mutable reference when we create the builder is that we can't have multiple unfinished `CommitBuilder` instance live at the same time. We don't have any such use cases yet, and it's not hard to work around them, so I think this change is worth it.
Showing the changes to lib/src/commit_builder.rs (1 of 21 files). Show all
Unified · Split
| @@ -20,8 +20,8 @@ use crate::commit::Commit; · 19 unmodified lines | ||
| 20 | 20 | use crate::repo::MutableRepo; |
| 21 | 21 | use crate::settings::UserSettings; |
| 22 | 22 | |
| 23 | −#[derive(Debug)] | |
| 24 | −pub struct CommitBuilder { | |
| 23 | +pub struct CommitBuilder<'repo> { | |
| 24 | + mut_repo: &'repo mut MutableRepo, | |
| 25 | 25 | commit: backend::Commit, |
| 26 | 26 | rewrite_source: Option<Commit>, |
| 27 | 27 | } |
| @@ -30,12 +30,13 @@ pub fn new_change_id() -> ChangeId { · 2 unmodified lines | ||
| 30 | 30 | ChangeId::from_bytes(Uuid::new_v4().as_bytes()) |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | −impl CommitBuilder { | |
| 34 | − pub fn for_new_commit( | |
| 33 | +impl CommitBuilder<'_> { | |
| 34 | + pub fn for_new_commit<'repo>( | |
| 35 | + mut_repo: &'repo mut MutableRepo, | |
| 35 | 36 | settings: &UserSettings, |
| 36 | 37 | parents: Vec<CommitId>, |
| 37 | 38 | tree_id: TreeId, |
| 38 | − ) -> CommitBuilder { | |
| 39 | + ) -> CommitBuilder<'repo> { | |
| 39 | 40 | let signature = settings.signature(); |
| 40 | 41 | assert!(!parents.is_empty()); |
| 41 | 42 | let commit = backend::Commit { |
| @@ -48,12 +49,17 @@ impl CommitBuilder { · 6 unmodified lines | ||
| 48 | 49 | committer: signature, |
| 49 | 50 | }; |
| 50 | 51 | CommitBuilder { |
| 52 | + mut_repo, | |
| 51 | 53 | commit, |
| 52 | 54 | rewrite_source: None, |
| 53 | 55 | } |
| 54 | 56 | } |
| 55 | 57 | |
| 56 | − pub fn for_rewrite_from(settings: &UserSettings, predecessor: &Commit) -> CommitBuilder { | |
| 58 | + pub fn for_rewrite_from<'repo>( | |
| 59 | + mut_repo: &'repo mut MutableRepo, | |
| 60 | + settings: &UserSettings, | |
| 61 | + predecessor: &Commit, | |
| 62 | + ) -> CommitBuilder<'repo> { | |
| 57 | 63 | let mut commit = predecessor.store_commit().clone(); |
| 58 | 64 | commit.predecessors = vec![predecessor.id().clone()]; |
| 59 | 65 | commit.committer = settings.signature(); |
| @@ -66,6 +72,7 @@ impl CommitBuilder { · 6 unmodified lines | ||
| 66 | 72 | commit.author.email = commit.committer.email.clone(); |
| 67 | 73 | } |
| 68 | 74 | CommitBuilder { |
| 75 | + mut_repo, | |
| 69 | 76 | commit, |
| 70 | 77 | rewrite_source: Some(predecessor.clone()), |
| 71 | 78 | } |
| @@ -112,16 +119,17 @@ impl CommitBuilder { · 40 unmodified lines | ||
| 112 | 119 | self |
| 113 | 120 | } |
| 114 | 121 | |
| 115 | − pub fn write_to_repo(self, repo: &mut MutableRepo) -> Commit { | |
| 122 | + pub fn write(self) -> Commit { | |
| 116 | 123 | let mut rewrite_source_id = None; |
| 117 | 124 | if let Some(rewrite_source) = self.rewrite_source { |
| 118 | 125 | if *rewrite_source.change_id() == self.commit.change_id { |
| 119 | 126 | rewrite_source_id.replace(rewrite_source.id().clone()); |
| 120 | 127 | } |
| 121 | 128 | } |
| 122 | − let commit = repo.write_commit(self.commit); | |
| 129 | + let commit = self.mut_repo.write_commit(self.commit); | |
| 123 | 130 | if let Some(rewrite_source_id) = rewrite_source_id { |
| 124 | − repo.record_rewritten_commit(rewrite_source_id, commit.id().clone()) | |
| 131 | + self.mut_repo | |
| 132 | + .record_rewritten_commit(rewrite_source_id, commit.id().clone()) | |
| 125 | 133 | } |
| 126 | 134 | commit |
| 127 | 135 | } |