2020-12-12 08:00:42 +00:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
use crate::commit::Commit;
|
|
|
|
use crate::commit_builder::CommitBuilder;
|
|
|
|
use crate::conflicts;
|
2021-01-31 02:50:27 +00:00
|
|
|
use crate::dag_walk::topo_order_reverse;
|
2021-02-13 21:30:45 +00:00
|
|
|
use crate::evolution::MutableEvolution;
|
2021-01-31 02:50:27 +00:00
|
|
|
use crate::index::MutableIndex;
|
2020-12-12 08:00:42 +00:00
|
|
|
use crate::op_store;
|
|
|
|
use crate::operation::Operation;
|
2021-01-31 07:44:31 +00:00
|
|
|
use crate::repo::{MutableRepo, ReadonlyRepo, RepoRef};
|
2020-12-12 08:00:42 +00:00
|
|
|
use crate::settings::UserSettings;
|
|
|
|
use crate::store;
|
2020-12-26 07:55:18 +00:00
|
|
|
use crate::store::{CommitId, Timestamp};
|
2020-12-12 08:00:42 +00:00
|
|
|
use crate::store_wrapper::StoreWrapper;
|
2021-02-13 21:30:45 +00:00
|
|
|
use crate::view::MutableView;
|
2020-12-12 08:00:42 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
pub struct Transaction<'r> {
|
|
|
|
repo: Option<Arc<MutableRepo<'r>>>,
|
|
|
|
description: String,
|
|
|
|
start_time: Timestamp,
|
|
|
|
closed: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'r> Transaction<'r> {
|
2021-02-01 02:07:37 +00:00
|
|
|
pub fn new(mut_repo: Arc<MutableRepo<'r>>, description: &str) -> Transaction<'r> {
|
2021-02-01 01:12:40 +00:00
|
|
|
Transaction {
|
|
|
|
repo: Some(mut_repo),
|
2020-12-12 08:00:42 +00:00
|
|
|
description: description.to_owned(),
|
|
|
|
start_time: Timestamp::now(),
|
|
|
|
closed: false,
|
2021-02-01 01:12:40 +00:00
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn base_repo(&self) -> &'r ReadonlyRepo {
|
2021-01-31 18:03:47 +00:00
|
|
|
self.repo.as_ref().unwrap().base_repo()
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn store(&self) -> &Arc<StoreWrapper> {
|
2021-01-31 18:03:47 +00:00
|
|
|
self.repo.as_ref().unwrap().store()
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 07:44:31 +00:00
|
|
|
pub fn as_repo_ref(&self) -> RepoRef {
|
|
|
|
self.repo.as_ref().unwrap().as_repo_ref()
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_repo_mut(&mut self) -> &mut MutableRepo<'r> {
|
|
|
|
Arc::get_mut(self.repo.as_mut().unwrap()).unwrap()
|
|
|
|
}
|
|
|
|
|
2021-01-31 02:50:27 +00:00
|
|
|
pub fn index(&self) -> &MutableIndex {
|
|
|
|
self.repo.as_ref().unwrap().index()
|
|
|
|
}
|
|
|
|
|
2021-02-13 21:30:45 +00:00
|
|
|
pub fn view(&self) -> &MutableView {
|
|
|
|
self.repo.as_ref().unwrap().view()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn evolution(&self) -> &MutableEvolution {
|
|
|
|
self.repo.as_ref().unwrap().evolution()
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn write_commit(&mut self, commit: store::Commit) -> Commit {
|
2021-01-31 02:38:46 +00:00
|
|
|
let commit = self.store().write_commit(commit);
|
2020-12-12 08:00:42 +00:00
|
|
|
self.add_head(&commit);
|
|
|
|
commit
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_out(&mut self, settings: &UserSettings, commit: &Commit) -> Commit {
|
2021-02-13 21:30:45 +00:00
|
|
|
let current_checkout_id = self.view().checkout().clone();
|
2020-12-12 08:00:42 +00:00
|
|
|
let current_checkout = self.store().get_commit(¤t_checkout_id).unwrap();
|
|
|
|
assert!(current_checkout.is_open(), "current checkout is closed");
|
|
|
|
if current_checkout.is_empty()
|
2021-02-13 21:30:45 +00:00
|
|
|
&& !(current_checkout.is_pruned() || self.evolution().is_obsolete(¤t_checkout_id))
|
2020-12-12 08:00:42 +00:00
|
|
|
{
|
|
|
|
// Prune the checkout we're leaving if it's empty.
|
|
|
|
// TODO: Also prune it if the only changes are conflicts that got materialized.
|
|
|
|
CommitBuilder::for_rewrite_from(settings, self.store(), ¤t_checkout)
|
|
|
|
.set_pruned(true)
|
|
|
|
.write_to_transaction(self);
|
|
|
|
}
|
|
|
|
let store = self.store();
|
|
|
|
// Create a new tree with any conflicts resolved.
|
|
|
|
let mut tree_builder = store.tree_builder(commit.tree().id().clone());
|
|
|
|
for (path, conflict_id) in commit.tree().conflicts() {
|
|
|
|
let conflict = store.read_conflict(&conflict_id).unwrap();
|
2020-12-26 07:55:18 +00:00
|
|
|
let materialized_value =
|
|
|
|
conflicts::conflict_to_materialized_value(store, &path, &conflict);
|
|
|
|
tree_builder.set(path, materialized_value);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
let tree_id = tree_builder.write_tree();
|
|
|
|
let open_commit;
|
2021-01-22 06:11:03 +00:00
|
|
|
if !commit.is_open() || &tree_id != commit.tree().id() {
|
|
|
|
// If the commit is closed, or if it had conflicts, create a new open commit on
|
|
|
|
// top
|
2020-12-12 08:00:42 +00:00
|
|
|
open_commit = CommitBuilder::for_open_commit(
|
|
|
|
settings,
|
|
|
|
self.store(),
|
|
|
|
commit.id().clone(),
|
|
|
|
tree_id,
|
|
|
|
)
|
|
|
|
.write_to_transaction(self);
|
|
|
|
} else {
|
|
|
|
// Otherwise the commit was open and didn't have any conflicts, so just use
|
|
|
|
// that commit as is.
|
|
|
|
open_commit = commit.clone();
|
|
|
|
}
|
|
|
|
let id = open_commit.id().clone();
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-01-31 18:03:47 +00:00
|
|
|
mut_repo.view_mut().set_checkout(id);
|
2020-12-12 08:00:42 +00:00
|
|
|
open_commit
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_checkout(&mut self, id: CommitId) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-01-31 18:03:47 +00:00
|
|
|
mut_repo.view_mut().set_checkout(id);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_head(&mut self, head: &Commit) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-02-14 08:11:04 +00:00
|
|
|
let view = mut_repo.view();
|
|
|
|
let current_heads = view.heads();
|
|
|
|
// Use incremental update for common case of adding a single commit on top a
|
|
|
|
// current head. TODO: Also use incremental update when adding a single
|
|
|
|
// commit on top a non-head.
|
|
|
|
if head
|
|
|
|
.parent_ids()
|
|
|
|
.iter()
|
|
|
|
.all(|parent_id| current_heads.contains(parent_id))
|
|
|
|
{
|
2021-01-31 02:50:27 +00:00
|
|
|
mut_repo.index_mut().add_commit(head);
|
2021-02-14 08:11:04 +00:00
|
|
|
mut_repo.view_mut().add_head(head);
|
|
|
|
mut_repo.evolution_mut().add_commit(head);
|
|
|
|
} else {
|
2021-02-15 02:53:52 +00:00
|
|
|
let index = mut_repo.index();
|
2021-01-31 02:50:27 +00:00
|
|
|
let missing_commits = topo_order_reverse(
|
|
|
|
vec![head.clone()],
|
|
|
|
Box::new(|commit: &Commit| commit.id().clone()),
|
|
|
|
Box::new(|commit: &Commit| -> Vec<Commit> {
|
|
|
|
commit
|
|
|
|
.parents()
|
|
|
|
.into_iter()
|
2021-02-15 02:53:52 +00:00
|
|
|
.filter(|parent| !index.has_id(parent.id()))
|
2021-01-31 02:50:27 +00:00
|
|
|
.collect()
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
let mut_index = mut_repo.index_mut();
|
|
|
|
for missing_commit in missing_commits.iter().rev() {
|
|
|
|
mut_index.add_commit(missing_commit);
|
|
|
|
}
|
2021-02-14 08:11:04 +00:00
|
|
|
mut_repo.view_mut().add_head(head);
|
|
|
|
mut_repo.evolution_mut().invalidate();
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_head(&mut self, head: &Commit) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-01-31 18:03:47 +00:00
|
|
|
mut_repo.view_mut().remove_head(head);
|
|
|
|
mut_repo.evolution_mut().invalidate();
|
2021-01-16 18:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_public_head(&mut self, head: &Commit) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-01-31 18:03:47 +00:00
|
|
|
mut_repo.view_mut().add_public_head(head);
|
|
|
|
mut_repo.evolution_mut().add_commit(head);
|
2021-01-16 18:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_public_head(&mut self, head: &Commit) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-01-31 18:03:47 +00:00
|
|
|
mut_repo.view_mut().remove_public_head(head);
|
|
|
|
mut_repo.evolution_mut().invalidate();
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 08:26:57 +00:00
|
|
|
pub fn insert_git_ref(&mut self, name: String, commit_id: CommitId) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-01-31 18:03:47 +00:00
|
|
|
mut_repo.view_mut().insert_git_ref(name, commit_id);
|
2021-01-03 08:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_git_ref(&mut self, name: &str) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-01-31 18:03:47 +00:00
|
|
|
mut_repo.view_mut().remove_git_ref(name);
|
2021-01-03 08:26:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn set_view(&mut self, data: op_store::View) {
|
|
|
|
let mut_repo = Arc::get_mut(self.repo.as_mut().unwrap()).unwrap();
|
2021-01-31 18:03:47 +00:00
|
|
|
mut_repo.view_mut().set_view(data);
|
|
|
|
mut_repo.evolution_mut().invalidate();
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn commit(mut self) -> Operation {
|
2021-01-31 18:03:47 +00:00
|
|
|
let mut_repo = Arc::try_unwrap(self.repo.take().unwrap()).ok().unwrap();
|
|
|
|
let view = mut_repo.take_view();
|
2020-12-12 08:00:42 +00:00
|
|
|
let operation = view.save(self.description.clone(), self.start_time.clone());
|
|
|
|
self.closed = true;
|
|
|
|
operation
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn discard(mut self) {
|
|
|
|
self.closed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'r> Drop for Transaction<'r> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if !std::thread::panicking() {
|
2021-02-07 07:33:15 +00:00
|
|
|
debug_assert!(self.closed, "Transaction was dropped without being closed.");
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|