jj_lib/
commit_builder.rs

1// Copyright 2020 The Jujutsu Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![expect(missing_docs)]
16
17use std::sync::Arc;
18
19use pollster::FutureExt as _;
20
21use crate::backend;
22use crate::backend::BackendError;
23use crate::backend::BackendResult;
24use crate::backend::ChangeId;
25use crate::backend::CommitId;
26use crate::backend::Signature;
27use crate::backend::TreeId;
28use crate::commit::Commit;
29use crate::commit::is_backend_commit_empty;
30use crate::conflict_labels::ConflictLabels;
31use crate::merge::Merge;
32use crate::merged_tree::MergedTree;
33use crate::repo::MutableRepo;
34use crate::repo::Repo;
35use crate::settings::JJRng;
36use crate::settings::SignSettings;
37use crate::settings::UserSettings;
38use crate::signing::SignBehavior;
39use crate::store::Store;
40
41#[must_use]
42pub struct CommitBuilder<'repo> {
43    mut_repo: &'repo mut MutableRepo,
44    inner: DetachedCommitBuilder,
45}
46
47impl CommitBuilder<'_> {
48    /// Detaches from `&'repo mut` lifetime. The returned builder can be used in
49    /// order to obtain a temporary commit object.
50    pub fn detach(self) -> DetachedCommitBuilder {
51        self.inner
52    }
53
54    /// Clears the source commit to not record new commit as rewritten from it.
55    ///
56    /// The caller should also assign new change id to not create divergence.
57    pub fn clear_rewrite_source(mut self) -> Self {
58        self.inner.clear_rewrite_source();
59        self
60    }
61
62    pub fn parents(&self) -> &[CommitId] {
63        self.inner.parents()
64    }
65
66    pub fn set_parents(mut self, parents: Vec<CommitId>) -> Self {
67        self.inner.set_parents(parents);
68        self
69    }
70
71    pub fn predecessors(&self) -> &[CommitId] {
72        self.inner.predecessors()
73    }
74
75    pub fn set_predecessors(mut self, predecessors: Vec<CommitId>) -> Self {
76        self.inner.set_predecessors(predecessors);
77        self
78    }
79
80    pub fn tree(&self) -> MergedTree {
81        self.inner.tree()
82    }
83
84    pub fn tree_ids(&self) -> &Merge<TreeId> {
85        self.inner.tree_ids()
86    }
87
88    pub fn set_tree(mut self, tree: MergedTree) -> Self {
89        self.inner.set_tree(tree);
90        self
91    }
92
93    /// [`Commit::is_empty()`] for the new commit.
94    pub fn is_empty(&self) -> BackendResult<bool> {
95        self.inner.is_empty(self.mut_repo)
96    }
97
98    pub fn change_id(&self) -> &ChangeId {
99        self.inner.change_id()
100    }
101
102    pub fn set_change_id(mut self, change_id: ChangeId) -> Self {
103        self.inner.set_change_id(change_id);
104        self
105    }
106
107    pub fn generate_new_change_id(mut self) -> Self {
108        self.inner.generate_new_change_id();
109        self
110    }
111
112    pub fn description(&self) -> &str {
113        self.inner.description()
114    }
115
116    pub fn set_description(mut self, description: impl Into<String>) -> Self {
117        self.inner.set_description(description);
118        self
119    }
120
121    pub fn author(&self) -> &Signature {
122        self.inner.author()
123    }
124
125    pub fn set_author(mut self, author: Signature) -> Self {
126        self.inner.set_author(author);
127        self
128    }
129
130    pub fn committer(&self) -> &Signature {
131        self.inner.committer()
132    }
133
134    pub fn set_committer(mut self, committer: Signature) -> Self {
135        self.inner.set_committer(committer);
136        self
137    }
138
139    /// [`Commit::is_discardable()`] for the new commit.
140    pub fn is_discardable(&self) -> BackendResult<bool> {
141        self.inner.is_discardable(self.mut_repo)
142    }
143
144    pub fn sign_settings(&self) -> &SignSettings {
145        self.inner.sign_settings()
146    }
147
148    pub fn set_sign_behavior(mut self, sign_behavior: SignBehavior) -> Self {
149        self.inner.set_sign_behavior(sign_behavior);
150        self
151    }
152
153    pub fn set_sign_key(mut self, sign_key: String) -> Self {
154        self.inner.set_sign_key(sign_key);
155        self
156    }
157
158    pub fn clear_sign_key(mut self) -> Self {
159        self.inner.clear_sign_key();
160        self
161    }
162
163    pub fn write(self) -> BackendResult<Commit> {
164        self.inner.write(self.mut_repo)
165    }
166
167    /// Records the old commit as abandoned instead of writing new commit. This
168    /// is noop for the builder created by [`MutableRepo::new_commit()`].
169    pub fn abandon(self) {
170        self.inner.abandon(self.mut_repo);
171    }
172}
173
174/// Like `CommitBuilder`, but doesn't mutably borrow `MutableRepo`.
175#[derive(Debug)]
176pub struct DetachedCommitBuilder {
177    store: Arc<Store>,
178    rng: Arc<JJRng>,
179    commit: backend::Commit,
180    predecessors: Vec<CommitId>,
181    rewrite_source: Option<Commit>,
182    sign_settings: SignSettings,
183    record_predecessors_in_commit: bool,
184}
185
186impl DetachedCommitBuilder {
187    /// Only called from [`MutableRepo::new_commit`]. Use that function instead.
188    pub(crate) fn for_new_commit(
189        repo: &dyn Repo,
190        settings: &UserSettings,
191        parents: Vec<CommitId>,
192        tree: MergedTree,
193    ) -> Self {
194        let store = repo.store().clone();
195        let signature = settings.signature();
196        assert!(!parents.is_empty());
197        let rng = settings.get_rng();
198        let (root_tree, conflict_labels) = tree.into_tree_ids_and_labels();
199        let change_id = rng.new_change_id(store.change_id_length());
200        let commit = backend::Commit {
201            parents,
202            predecessors: vec![],
203            root_tree,
204            conflict_labels: conflict_labels.into_merge(),
205            change_id,
206            description: String::new(),
207            author: signature.clone(),
208            committer: signature,
209            secure_sig: None,
210        };
211        let record_predecessors_in_commit = settings
212            .get_bool("experimental.record-predecessors-in-commit")
213            .unwrap();
214        Self {
215            store,
216            rng,
217            commit,
218            rewrite_source: None,
219            predecessors: vec![],
220            sign_settings: settings.sign_settings(),
221            record_predecessors_in_commit,
222        }
223    }
224
225    /// Only called from [`MutableRepo::rewrite_commit`]. Use that function
226    /// instead.
227    pub(crate) fn for_rewrite_from(
228        repo: &dyn Repo,
229        settings: &UserSettings,
230        predecessor: &Commit,
231    ) -> Self {
232        let store = repo.store().clone();
233        let mut commit = backend::Commit::clone(predecessor.store_commit());
234        commit.predecessors = vec![];
235        commit.committer = settings.signature();
236        // If the user had not configured a name and email before but now they have,
237        // update the author fields with the new information.
238        if commit.author.name.is_empty()
239            || commit.author.name == UserSettings::USER_NAME_PLACEHOLDER
240        {
241            commit.author.name.clone_from(&commit.committer.name);
242        }
243        if commit.author.email.is_empty()
244            || commit.author.email == UserSettings::USER_EMAIL_PLACEHOLDER
245        {
246            commit.author.email.clone_from(&commit.committer.email);
247        }
248
249        // Reset author timestamp on discardable commits if the author is the
250        // committer. While it's unlikely we'll have somebody else's commit
251        // with no description in our repo, we'd like to be extra safe.
252        if commit.author.name == commit.committer.name
253            && commit.author.email == commit.committer.email
254            && predecessor.is_discardable(repo).unwrap_or_default()
255        {
256            commit.author.timestamp = commit.committer.timestamp;
257        }
258
259        let record_predecessors_in_commit = settings
260            .get_bool("experimental.record-predecessors-in-commit")
261            .unwrap();
262        Self {
263            store,
264            commit,
265            rng: settings.get_rng(),
266            rewrite_source: Some(predecessor.clone()),
267            predecessors: vec![predecessor.id().clone()],
268            sign_settings: settings.sign_settings(),
269            record_predecessors_in_commit,
270        }
271    }
272
273    /// Attaches the underlying `mut_repo`.
274    pub fn attach(self, mut_repo: &mut MutableRepo) -> CommitBuilder<'_> {
275        assert!(Arc::ptr_eq(&self.store, mut_repo.store()));
276        CommitBuilder {
277            mut_repo,
278            inner: self,
279        }
280    }
281
282    /// Clears the source commit to not record new commit as rewritten from it.
283    ///
284    /// The caller should also assign new change id to not create divergence.
285    pub fn clear_rewrite_source(&mut self) {
286        self.rewrite_source = None;
287    }
288
289    pub fn parents(&self) -> &[CommitId] {
290        &self.commit.parents
291    }
292
293    pub fn set_parents(&mut self, parents: Vec<CommitId>) -> &mut Self {
294        assert!(!parents.is_empty());
295        self.commit.parents = parents;
296        self
297    }
298
299    pub fn predecessors(&self) -> &[CommitId] {
300        &self.predecessors
301    }
302
303    pub fn set_predecessors(&mut self, predecessors: Vec<CommitId>) -> &mut Self {
304        self.predecessors = predecessors;
305        self
306    }
307
308    pub fn tree(&self) -> MergedTree {
309        MergedTree::new(
310            self.store.clone(),
311            self.commit.root_tree.clone(),
312            ConflictLabels::from_merge(self.commit.conflict_labels.clone()),
313        )
314    }
315
316    pub fn tree_ids(&self) -> &Merge<TreeId> {
317        &self.commit.root_tree
318    }
319
320    pub fn set_tree(&mut self, tree: MergedTree) -> &mut Self {
321        assert!(Arc::ptr_eq(tree.store(), &self.store));
322        let (root_tree, conflict_labels) = tree.into_tree_ids_and_labels();
323        self.commit.root_tree = root_tree;
324        self.commit.conflict_labels = conflict_labels.into_merge();
325        self
326    }
327
328    /// [`Commit::is_empty()`] for the new commit.
329    pub fn is_empty(&self, repo: &dyn Repo) -> BackendResult<bool> {
330        is_backend_commit_empty(repo, &self.store, &self.commit)
331    }
332
333    pub fn change_id(&self) -> &ChangeId {
334        &self.commit.change_id
335    }
336
337    pub fn set_change_id(&mut self, change_id: ChangeId) -> &mut Self {
338        self.commit.change_id = change_id;
339        self
340    }
341
342    pub fn generate_new_change_id(&mut self) -> &mut Self {
343        self.commit.change_id = self.rng.new_change_id(self.store.change_id_length());
344        self
345    }
346
347    pub fn description(&self) -> &str {
348        &self.commit.description
349    }
350
351    pub fn set_description(&mut self, description: impl Into<String>) -> &mut Self {
352        self.commit.description = description.into();
353        self
354    }
355
356    pub fn author(&self) -> &Signature {
357        &self.commit.author
358    }
359
360    pub fn set_author(&mut self, author: Signature) -> &mut Self {
361        self.commit.author = author;
362        self
363    }
364
365    pub fn committer(&self) -> &Signature {
366        &self.commit.committer
367    }
368
369    pub fn set_committer(&mut self, committer: Signature) -> &mut Self {
370        self.commit.committer = committer;
371        self
372    }
373
374    /// [`Commit::is_discardable()`] for the new commit.
375    pub fn is_discardable(&self, repo: &dyn Repo) -> BackendResult<bool> {
376        Ok(self.description().is_empty() && self.is_empty(repo)?)
377    }
378
379    pub fn sign_settings(&self) -> &SignSettings {
380        &self.sign_settings
381    }
382
383    pub fn set_sign_behavior(&mut self, sign_behavior: SignBehavior) -> &mut Self {
384        self.sign_settings.behavior = sign_behavior;
385        self
386    }
387
388    pub fn set_sign_key(&mut self, sign_key: String) -> &mut Self {
389        self.sign_settings.key = Some(sign_key);
390        self
391    }
392
393    pub fn clear_sign_key(&mut self) -> &mut Self {
394        self.sign_settings.key = None;
395        self
396    }
397
398    /// Writes new commit and makes it visible in the `mut_repo`.
399    pub fn write(mut self, mut_repo: &mut MutableRepo) -> BackendResult<Commit> {
400        if self.record_predecessors_in_commit {
401            self.commit.predecessors = self.predecessors.clone();
402        }
403        let commit = write_to_store(&self.store, self.commit, &self.sign_settings)?;
404        // FIXME: Google's index.has_id() always returns true.
405        if mut_repo.is_backed_by_default_index()
406            && mut_repo
407                .index()
408                .has_id(commit.id())
409                // TODO: indexing error shouldn't be a "BackendError"
410                .map_err(|err| BackendError::Other(err.into()))?
411        {
412            // Recording existing commit as new would create cycle in
413            // predecessors/parent mappings within the current transaction, and
414            // in predecessors graph globally.
415            return Err(BackendError::Other(
416                format!("Newly-created commit {id} already exists", id = commit.id()).into(),
417            ));
418        }
419        mut_repo.add_head(&commit)?;
420        mut_repo.set_predecessors(commit.id().clone(), self.predecessors);
421        if let Some(rewrite_source) = self.rewrite_source {
422            mut_repo.set_rewritten_commit(rewrite_source.id().clone(), commit.id().clone());
423        }
424        Ok(commit)
425    }
426
427    /// Writes new commit without making it visible in the repo.
428    ///
429    /// This does not consume the builder, so you can reuse the current
430    /// configuration to create another commit later.
431    pub fn write_hidden(&self) -> BackendResult<Commit> {
432        let mut commit = self.commit.clone();
433        if self.record_predecessors_in_commit {
434            commit.predecessors = self.predecessors.clone();
435        }
436        write_to_store(&self.store, commit, &self.sign_settings)
437    }
438
439    /// Records the old commit as abandoned in the `mut_repo`.
440    ///
441    /// This is noop if there's no old commit that would be rewritten to the new
442    /// commit by `write()`.
443    pub fn abandon(self, mut_repo: &mut MutableRepo) {
444        let commit = self.commit;
445        if let Some(rewrite_source) = &self.rewrite_source {
446            mut_repo
447                .record_abandoned_commit_with_parents(rewrite_source.id().clone(), commit.parents);
448        }
449    }
450}
451
452fn write_to_store(
453    store: &Arc<Store>,
454    mut commit: backend::Commit,
455    sign_settings: &SignSettings,
456) -> BackendResult<Commit> {
457    let should_sign = store.signer().can_sign() && sign_settings.should_sign(&commit);
458    let sign_fn = |data: &[u8]| store.signer().sign(data, sign_settings.key.as_deref());
459
460    // Commit backend doesn't use secure_sig for writing and enforces it with an
461    // assert, but sign_settings.should_sign check above will want to know
462    // if we're rewriting a signed commit
463    commit.secure_sig = None;
464
465    store
466        .write_commit(commit, should_sign.then_some(&mut &sign_fn))
467        .block_on()
468}