2022-11-26 23:57:50 +00:00
|
|
|
// Copyright 2020 The Jujutsu Authors
|
2020-12-12 08:00:42 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-07-10 15:17:00 +00:00
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
2022-12-30 03:59:01 +00:00
|
|
|
use std::sync::Arc;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-11-24 21:08:16 +00:00
|
|
|
use crate::backend::{self, BackendResult, ChangeId, CommitId, MergedTreeId, Signature, SigningFn};
|
2020-12-12 08:00:42 +00:00
|
|
|
use crate::commit::Commit;
|
2023-02-14 21:51:55 +00:00
|
|
|
use crate::repo::{MutableRepo, Repo};
|
2023-11-24 21:08:16 +00:00
|
|
|
use crate::settings::{JJRng, SignSettings, UserSettings};
|
|
|
|
use crate::signing::SignBehavior;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-12-25 16:36:13 +00:00
|
|
|
#[must_use]
|
2022-12-24 17:01:11 +00:00
|
|
|
pub struct CommitBuilder<'repo> {
|
|
|
|
mut_repo: &'repo mut MutableRepo,
|
2022-12-30 03:59:01 +00:00
|
|
|
rng: Arc<JJRng>,
|
2021-09-12 06:52:38 +00:00
|
|
|
commit: backend::Commit,
|
2021-09-29 15:44:00 +00:00
|
|
|
rewrite_source: Option<Commit>,
|
2023-11-24 21:08:16 +00:00
|
|
|
sign_settings: SignSettings,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-24 17:01:11 +00:00
|
|
|
impl CommitBuilder<'_> {
|
2023-12-18 01:44:32 +00:00
|
|
|
// Use MutRepo::new_commit() instead
|
|
|
|
pub(crate) fn for_new_commit<'repo>(
|
2022-12-24 17:01:11 +00:00
|
|
|
mut_repo: &'repo mut MutableRepo,
|
2022-09-19 05:26:45 +00:00
|
|
|
settings: &UserSettings,
|
|
|
|
parents: Vec<CommitId>,
|
2023-08-27 14:18:54 +00:00
|
|
|
tree_id: MergedTreeId,
|
2022-12-24 17:01:11 +00:00
|
|
|
) -> CommitBuilder<'repo> {
|
2022-03-06 16:30:00 +00:00
|
|
|
let signature = settings.signature();
|
2022-09-19 04:59:49 +00:00
|
|
|
assert!(!parents.is_empty());
|
2022-12-30 03:59:01 +00:00
|
|
|
let rng = settings.get_rng();
|
2023-02-06 18:15:01 +00:00
|
|
|
let change_id = rng.new_change_id(mut_repo.store().change_id_length());
|
2021-09-12 06:52:38 +00:00
|
|
|
let commit = backend::Commit {
|
2022-09-19 05:26:45 +00:00
|
|
|
parents,
|
2020-12-12 08:00:42 +00:00
|
|
|
predecessors: vec![],
|
2023-08-27 14:18:54 +00:00
|
|
|
root_tree: tree_id,
|
2023-02-06 18:15:01 +00:00
|
|
|
change_id,
|
2020-12-12 08:00:42 +00:00
|
|
|
description: String::new(),
|
|
|
|
author: signature.clone(),
|
|
|
|
committer: signature,
|
2023-11-09 01:10:39 +00:00
|
|
|
secure_sig: None,
|
2020-12-12 08:00:42 +00:00
|
|
|
};
|
|
|
|
CommitBuilder {
|
2022-12-24 17:01:11 +00:00
|
|
|
mut_repo,
|
2022-12-30 03:59:01 +00:00
|
|
|
rng,
|
2020-12-12 08:00:42 +00:00
|
|
|
commit,
|
2021-09-29 15:44:00 +00:00
|
|
|
rewrite_source: None,
|
2023-11-24 21:08:16 +00:00
|
|
|
sign_settings: settings.sign_settings(),
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 01:44:32 +00:00
|
|
|
// Use MutRepo::rewrite_commit() instead
|
|
|
|
pub(crate) fn for_rewrite_from<'repo>(
|
2022-12-24 17:01:11 +00:00
|
|
|
mut_repo: &'repo mut MutableRepo,
|
|
|
|
settings: &UserSettings,
|
|
|
|
predecessor: &Commit,
|
|
|
|
) -> CommitBuilder<'repo> {
|
2020-12-12 08:00:42 +00:00
|
|
|
let mut commit = predecessor.store_commit().clone();
|
|
|
|
commit.predecessors = vec![predecessor.id().clone()];
|
2022-03-06 16:30:00 +00:00
|
|
|
commit.committer = settings.signature();
|
2022-06-04 23:18:03 +00:00
|
|
|
// If the user had not configured a name and email before but now they have,
|
2022-09-09 17:32:38 +00:00
|
|
|
// update the author fields with the new information.
|
2023-08-16 21:10:54 +00:00
|
|
|
if commit.author.name.is_empty()
|
|
|
|
|| commit.author.name == UserSettings::USER_NAME_PLACEHOLDER
|
|
|
|
{
|
2022-06-04 23:18:03 +00:00
|
|
|
commit.author.name = commit.committer.name.clone();
|
|
|
|
}
|
2023-08-16 21:10:54 +00:00
|
|
|
if commit.author.email.is_empty()
|
|
|
|
|| commit.author.email == UserSettings::USER_EMAIL_PLACEHOLDER
|
|
|
|
{
|
2022-06-04 23:18:03 +00:00
|
|
|
commit.author.email = commit.committer.email.clone();
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
CommitBuilder {
|
2022-12-24 17:01:11 +00:00
|
|
|
mut_repo,
|
2020-12-12 08:00:42 +00:00
|
|
|
commit,
|
2022-12-30 03:59:01 +00:00
|
|
|
rng: settings.get_rng(),
|
2021-09-29 15:44:00 +00:00
|
|
|
rewrite_source: Some(predecessor.clone()),
|
2023-11-24 21:08:16 +00:00
|
|
|
sign_settings: settings.sign_settings(),
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-18 05:34:25 +00:00
|
|
|
pub fn parents(&self) -> &[CommitId] {
|
|
|
|
&self.commit.parents
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn set_parents(mut self, parents: Vec<CommitId>) -> Self {
|
2022-09-19 04:59:49 +00:00
|
|
|
assert!(!parents.is_empty());
|
2020-12-12 08:00:42 +00:00
|
|
|
self.commit.parents = parents;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-03-18 05:34:25 +00:00
|
|
|
pub fn predecessors(&self) -> &[CommitId] {
|
|
|
|
&self.commit.predecessors
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn set_predecessors(mut self, predecessors: Vec<CommitId>) -> Self {
|
|
|
|
self.commit.predecessors = predecessors;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-08-28 04:14:13 +00:00
|
|
|
pub fn tree_id(&self) -> &MergedTreeId {
|
|
|
|
&self.commit.root_tree
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-25 20:37:42 +00:00
|
|
|
pub fn set_tree_id(mut self, tree_id: MergedTreeId) -> Self {
|
|
|
|
self.commit.root_tree = tree_id;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-03-18 05:34:25 +00:00
|
|
|
pub fn change_id(&self) -> &ChangeId {
|
|
|
|
&self.commit.change_id
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn set_change_id(mut self, change_id: ChangeId) -> Self {
|
|
|
|
self.commit.change_id = change_id;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_new_change_id(mut self) -> Self {
|
2023-02-06 18:15:01 +00:00
|
|
|
self.commit.change_id = self
|
|
|
|
.rng
|
|
|
|
.new_change_id(self.mut_repo.store().change_id_length());
|
2020-12-12 08:00:42 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-03-18 05:34:25 +00:00
|
|
|
pub fn description(&self) -> &str {
|
|
|
|
&self.commit.description
|
|
|
|
}
|
|
|
|
|
2022-12-21 09:13:56 +00:00
|
|
|
pub fn set_description(mut self, description: impl Into<String>) -> Self {
|
|
|
|
self.commit.description = description.into();
|
2020-12-12 08:00:42 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-03-18 05:34:25 +00:00
|
|
|
pub fn author(&self) -> &Signature {
|
|
|
|
&self.commit.author
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn set_author(mut self, author: Signature) -> Self {
|
|
|
|
self.commit.author = author;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-03-18 05:34:25 +00:00
|
|
|
pub fn committer(&self) -> &Signature {
|
|
|
|
&self.commit.committer
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn set_committer(mut self, committer: Signature) -> Self {
|
|
|
|
self.commit.committer = committer;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-11-24 21:08:16 +00:00
|
|
|
pub fn sign_settings(&self) -> &SignSettings {
|
|
|
|
&self.sign_settings
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_sign_behavior(mut self, sign_behavior: SignBehavior) -> Self {
|
|
|
|
self.sign_settings.behavior = sign_behavior;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_sign_key(mut self, sign_key: Option<String>) -> Self {
|
|
|
|
self.sign_settings.key = sign_key;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write(mut self) -> BackendResult<Commit> {
|
2021-09-29 15:44:00 +00:00
|
|
|
let mut rewrite_source_id = None;
|
|
|
|
if let Some(rewrite_source) = self.rewrite_source {
|
2021-10-07 03:48:35 +00:00
|
|
|
if *rewrite_source.change_id() == self.commit.change_id {
|
2021-09-29 15:44:00 +00:00
|
|
|
rewrite_source_id.replace(rewrite_source.id().clone());
|
|
|
|
}
|
|
|
|
}
|
2023-11-24 21:08:16 +00:00
|
|
|
|
2023-11-28 06:34:02 +00:00
|
|
|
let sign_settings = &self.sign_settings;
|
2023-11-24 21:08:16 +00:00
|
|
|
let store = self.mut_repo.store();
|
|
|
|
|
2023-11-28 06:34:02 +00:00
|
|
|
let mut signing_fn = (store.signer().can_sign() && sign_settings.should_sign(&self.commit))
|
|
|
|
.then(|| -> Box<SigningFn> {
|
2023-11-24 21:08:16 +00:00
|
|
|
let store = store.clone();
|
|
|
|
Box::new(move |data: &_| store.signer().sign(data, sign_settings.key.as_deref()))
|
|
|
|
});
|
|
|
|
|
|
|
|
// Commit backend doesn't use secure_sig for writing and enforces it with an
|
|
|
|
// assert, but sign_settings.should_sign check above will want to know
|
|
|
|
// if we're rewriting a signed commit
|
|
|
|
self.commit.secure_sig = None;
|
|
|
|
|
2023-11-28 06:34:02 +00:00
|
|
|
let commit = self
|
|
|
|
.mut_repo
|
|
|
|
.write_commit(self.commit, signing_fn.as_deref_mut())?;
|
2021-09-29 15:44:00 +00:00
|
|
|
if let Some(rewrite_source_id) = rewrite_source_id {
|
2022-12-24 17:01:11 +00:00
|
|
|
self.mut_repo
|
|
|
|
.record_rewritten_commit(rewrite_source_id, commit.id().clone())
|
2021-09-29 15:44:00 +00:00
|
|
|
}
|
2022-12-24 05:09:19 +00:00
|
|
|
Ok(commit)
|
2021-03-07 23:11:34 +00:00
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|