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.
|
|
|
|
|
2022-12-30 03:59:01 +00:00
|
|
|
use std::sync::Arc;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-12-30 03:59:01 +00:00
|
|
|
use crate::backend::{self, BackendResult, ChangeId, CommitId, Signature, TreeId};
|
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};
|
2022-12-30 03:59:01 +00:00
|
|
|
use crate::settings::{JJRng, UserSettings};
|
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>,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-24 17:01:11 +00:00
|
|
|
impl CommitBuilder<'_> {
|
|
|
|
pub fn for_new_commit<'repo>(
|
|
|
|
mut_repo: &'repo mut MutableRepo,
|
2022-09-19 05:26:45 +00:00
|
|
|
settings: &UserSettings,
|
|
|
|
parents: Vec<CommitId>,
|
|
|
|
tree_id: TreeId,
|
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![],
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
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,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-24 17:01:11 +00:00
|
|
|
pub fn for_rewrite_from<'repo>(
|
|
|
|
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.
|
2022-06-04 23:18:03 +00:00
|
|
|
if commit.author.name == UserSettings::user_name_placeholder() {
|
|
|
|
commit.author.name = commit.committer.name.clone();
|
|
|
|
}
|
|
|
|
if commit.author.email == UserSettings::user_email_placeholder() {
|
|
|
|
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()),
|
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-03-18 05:34:25 +00:00
|
|
|
pub fn tree(&self) -> &TreeId {
|
|
|
|
&self.commit.root_tree
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn set_tree(mut self, tree_id: TreeId) -> 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
|
|
|
|
}
|
|
|
|
|
2022-12-24 05:09:19 +00:00
|
|
|
pub fn write(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());
|
|
|
|
}
|
|
|
|
}
|
2022-12-24 05:09:19 +00:00
|
|
|
let commit = self.mut_repo.write_commit(self.commit)?;
|
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
|
|
|
}
|