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.
|
|
|
|
|
2021-01-03 08:26:57 +00:00
|
|
|
use std::collections::{BTreeMap, HashSet};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-07-19 06:04:21 +00:00
|
|
|
use crate::index::IndexRef;
|
2020-12-12 08:00:42 +00:00
|
|
|
use crate::op_store;
|
2021-07-11 17:58:01 +00:00
|
|
|
use crate::op_store::RefTarget;
|
2021-07-19 06:04:21 +00:00
|
|
|
use crate::refs::merge_ref_targets;
|
2021-03-14 05:38:37 +00:00
|
|
|
use crate::store::CommitId;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-05-31 17:52:30 +00:00
|
|
|
pub struct View {
|
2020-12-12 08:00:42 +00:00
|
|
|
data: op_store::View,
|
|
|
|
}
|
|
|
|
|
2021-05-31 17:52:30 +00:00
|
|
|
impl View {
|
2021-03-14 18:08:31 +00:00
|
|
|
pub fn new(op_store_view: op_store::View) -> Self {
|
2021-05-31 17:52:30 +00:00
|
|
|
View {
|
2021-03-12 05:45:04 +00:00
|
|
|
data: op_store_view,
|
2021-01-04 17:40:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 17:52:30 +00:00
|
|
|
pub fn start_modification(&self) -> View {
|
2020-12-12 08:00:42 +00:00
|
|
|
// TODO: Avoid the cloning of the sets here.
|
2021-05-31 17:52:30 +00:00
|
|
|
View {
|
2020-12-12 08:00:42 +00:00
|
|
|
data: self.data.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 07:41:59 +00:00
|
|
|
pub fn checkout(&self) -> &CommitId {
|
|
|
|
&self.data.checkout
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn heads(&self) -> &HashSet<CommitId> {
|
|
|
|
&self.data.head_ids
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn public_heads(&self) -> &HashSet<CommitId> {
|
|
|
|
&self.data.public_head_ids
|
|
|
|
}
|
|
|
|
|
2021-07-11 17:58:01 +00:00
|
|
|
pub fn git_refs(&self) -> &BTreeMap<String, RefTarget> {
|
2021-02-01 07:41:59 +00:00
|
|
|
&self.data.git_refs
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn set_checkout(&mut self, id: CommitId) {
|
|
|
|
self.data.checkout = id;
|
|
|
|
}
|
|
|
|
|
2021-03-15 22:29:34 +00:00
|
|
|
pub fn add_head(&mut self, head_id: &CommitId) {
|
|
|
|
self.data.head_ids.insert(head_id.clone());
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 22:29:34 +00:00
|
|
|
pub fn remove_head(&mut self, head_id: &CommitId) {
|
|
|
|
self.data.head_ids.remove(head_id);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 22:29:34 +00:00
|
|
|
pub fn add_public_head(&mut self, head_id: &CommitId) {
|
|
|
|
self.data.public_head_ids.insert(head_id.clone());
|
2021-01-16 18:42:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 22:29:34 +00:00
|
|
|
pub fn remove_public_head(&mut self, head_id: &CommitId) {
|
|
|
|
self.data.public_head_ids.remove(head_id);
|
2021-01-16 18:42:22 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 15:42:16 +00:00
|
|
|
pub fn set_git_ref(&mut self, name: String, target: RefTarget) {
|
2021-07-11 17:58:01 +00:00
|
|
|
self.data.git_refs.insert(name, target);
|
2021-01-03 08:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_git_ref(&mut self, name: &str) {
|
|
|
|
self.data.git_refs.remove(name);
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn set_view(&mut self, data: op_store::View) {
|
|
|
|
self.data = data;
|
|
|
|
}
|
|
|
|
|
2021-03-14 05:23:30 +00:00
|
|
|
pub fn store_view(&self) -> &op_store::View {
|
|
|
|
&self.data
|
2021-03-10 22:43:40 +00:00
|
|
|
}
|
2021-03-14 18:08:31 +00:00
|
|
|
|
|
|
|
pub fn store_view_mut(&mut self) -> &mut op_store::View {
|
|
|
|
&mut self.data
|
|
|
|
}
|
2021-06-02 15:47:33 +00:00
|
|
|
|
2021-07-19 06:04:21 +00:00
|
|
|
pub fn merge(&mut self, index: IndexRef, base: &View, other: &View) {
|
2021-06-02 15:47:33 +00:00
|
|
|
if other.checkout() == base.checkout() || other.checkout() == self.checkout() {
|
|
|
|
// Keep the self side
|
|
|
|
} else if self.checkout() == base.checkout() {
|
|
|
|
self.set_checkout(other.checkout().clone());
|
|
|
|
} else {
|
|
|
|
// TODO: Return an error here. Or should we just pick one of the
|
|
|
|
// sides and emit a warning?
|
|
|
|
}
|
|
|
|
|
2021-06-14 07:18:38 +00:00
|
|
|
for removed_head in base.public_heads().difference(other.public_heads()) {
|
2021-06-02 15:47:33 +00:00
|
|
|
self.remove_public_head(removed_head);
|
|
|
|
}
|
2021-06-14 07:18:38 +00:00
|
|
|
for added_head in other.public_heads().difference(base.public_heads()) {
|
2021-06-02 15:47:33 +00:00
|
|
|
self.add_public_head(added_head);
|
|
|
|
}
|
|
|
|
|
2021-06-14 07:18:38 +00:00
|
|
|
for removed_head in base.heads().difference(other.heads()) {
|
2021-06-02 15:47:33 +00:00
|
|
|
self.remove_head(removed_head);
|
|
|
|
}
|
2021-06-14 07:18:38 +00:00
|
|
|
for added_head in other.heads().difference(base.heads()) {
|
2021-06-02 15:47:33 +00:00
|
|
|
self.add_head(added_head);
|
|
|
|
}
|
|
|
|
// TODO: Should it be considered a conflict if a commit-head is removed on one
|
|
|
|
// side while a child or successor is created on another side? Maybe a
|
|
|
|
// warning?
|
|
|
|
|
|
|
|
// Merge git refs
|
2021-07-19 06:04:21 +00:00
|
|
|
let base_git_ref_names: HashSet<_> = base.git_refs().keys().cloned().collect();
|
|
|
|
let other_git_ref_names: HashSet<_> = other.git_refs().keys().cloned().collect();
|
|
|
|
for maybe_modified_git_ref_name in other_git_ref_names.union(&base_git_ref_names) {
|
|
|
|
let base_target = base.git_refs().get(maybe_modified_git_ref_name);
|
|
|
|
let other_target = other.git_refs().get(maybe_modified_git_ref_name);
|
2021-07-11 17:58:01 +00:00
|
|
|
if base_target == other_target {
|
2021-06-02 15:47:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-07-19 06:04:21 +00:00
|
|
|
let self_target = self.git_refs().get(maybe_modified_git_ref_name);
|
|
|
|
let merged_target = merge_ref_targets(index, self_target, base_target, other_target);
|
|
|
|
match merged_target {
|
|
|
|
None => {
|
|
|
|
self.remove_git_ref(maybe_modified_git_ref_name);
|
|
|
|
}
|
|
|
|
Some(target) => {
|
2021-08-04 15:42:16 +00:00
|
|
|
self.set_git_ref(maybe_modified_git_ref_name.clone(), target);
|
2021-07-19 06:04:21 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-02 15:47:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|