| 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 |
|
| 17 |
use std::cmp::Ordering; |
| 18 |
use std::fmt::Debug; |
| 19 |
use std::fmt::Error; |
| 20 |
use std::fmt::Formatter; |
| 21 |
use std::hash::Hash; |
| 22 |
use std::hash::Hasher; |
| 23 |
use std::iter; |
| 24 |
use std::sync::Arc; |
| 25 |
|
| 26 |
use futures::future::try_join_all; |
| 27 |
|
| 28 |
use crate::backend::CommitId; |
| 29 |
use crate::op_store; |
| 30 |
use crate::op_store::OpStore; |
| 31 |
use crate::op_store::OpStoreResult; |
| 32 |
use crate::op_store::OperationId; |
| 33 |
use crate::op_store::OperationMetadata; |
| 34 |
use crate::op_store::ViewId; |
| 35 |
use crate::view::View; |
| 36 |
|
| 37 |
/// A wrapper around [`op_store::Operation`] that defines additional methods and |
| 38 |
/// stores a pointer to the `OpStore` the operation belongs to. |
| 39 |
#[derive(Clone, serde::Serialize)] |
| 40 |
pub struct Operation { |
| 41 |
#[serde(skip)] |
| 42 |
op_store: Arc<dyn OpStore>, |
| 43 |
id: OperationId, |
| 44 |
#[serde(flatten)] |
| 45 |
data: Arc<op_store::Operation>, // allow cheap clone |
| 46 |
} |
| 47 |
|
| 48 |
impl Debug for Operation { |
| 49 |
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { |
| 50 |
f.debug_struct("Operation").field("id", &self.id).finish() |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
impl PartialEq for Operation { |
| 55 |
fn eq(&self, other: &Self) -> bool { |
| 56 |
self.id == other.id |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
impl Eq for Operation {} |
| 61 |
|
| 62 |
impl Ord for Operation { |
| 63 |
fn cmp(&self, other: &Self) -> Ordering { |
| 64 |
self.id.cmp(&other.id) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
impl PartialOrd for Operation { |
| 69 |
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 70 |
Some(self.cmp(other)) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
impl Hash for Operation { |
| 75 |
fn hash<H: Hasher>(&self, state: &mut H) { |
| 76 |
self.id.hash(state); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
impl Operation { |
| 81 |
pub fn new( |
| 82 |
op_store: Arc<dyn OpStore>, |
| 83 |
id: OperationId, |
| 84 |
data: impl Into<Arc<op_store::Operation>>, |
| 85 |
) -> Self { |
| 86 |
Self { |
| 87 |
op_store, |
| 88 |
id, |
| 89 |
data: data.into(), |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
pub fn op_store(&self) -> Arc<dyn OpStore> { |
| 94 |
self.op_store.clone() |
| 95 |
} |
| 96 |
|
| 97 |
pub fn id(&self) -> &OperationId { |
| 98 |
&self.id |
| 99 |
} |
| 100 |
|
| 101 |
pub fn view_id(&self) -> &ViewId { |
| 102 |
&self.data.view_id |
| 103 |
} |
| 104 |
|
| 105 |
pub fn parent_ids(&self) -> &[OperationId] { |
| 106 |
&self.data.parents |
| 107 |
} |
| 108 |
|
| 109 |
pub async fn parents(&self) -> OpStoreResult<Vec<Self>> { |
| 110 |
try_join_all(self.data.parents.iter().map(async |parent_id| { |
| 111 |
let data = self.op_store.read_operation(parent_id).await?; |
| 112 |
Ok(Self::new(self.op_store.clone(), parent_id.clone(), data)) |
| 113 |
})) |
| 114 |
.await |
| 115 |
} |
| 116 |
|
| 117 |
pub async fn view(&self) -> OpStoreResult<View> { |
| 118 |
let data = self.op_store.read_view(&self.data.view_id).await?; |
| 119 |
// head ids read from the backend should always be normalized. |
| 120 |
let head_normalized = true; |
| 121 |
Ok(View::new(data, head_normalized)) |
| 122 |
} |
| 123 |
|
| 124 |
pub fn metadata(&self) -> &OperationMetadata { |
| 125 |
&self.data.metadata |
| 126 |
} |
| 127 |
|
| 128 |
/// Returns true if predecessors are recorded in this operation. |
| 129 |
/// |
| 130 |
/// This returns false only if the operation was written by jj < 0.30. |
| 131 |
pub fn stores_commit_predecessors(&self) -> bool { |
| 132 |
self.data.commit_predecessors.is_some() |
| 133 |
} |
| 134 |
|
| 135 |
/// Returns predecessors of the specified commit if recorded. |
| 136 |
pub fn predecessors_for_commit(&self, commit_id: &CommitId) -> Option<&[CommitId]> { |
| 137 |
let map = self.data.commit_predecessors.as_ref()?; |
| 138 |
Some(map.get(commit_id)?) |
| 139 |
} |
| 140 |
|
| 141 |
/// Iterates all commit ids referenced by this operation ignoring the view. |
| 142 |
/// |
| 143 |
/// Use this in addition to [`View::all_referenced_commit_ids()`] to build |
| 144 |
/// commit index from scratch. The predecessor commit ids are also included, |
| 145 |
/// which ensures that the old commits to be returned by |
| 146 |
/// [`Self::predecessors_for_commit()`] are still reachable. |
| 147 |
/// |
| 148 |
/// The iteration order is unspecified. |
| 149 |
pub fn all_referenced_commit_ids(&self) -> impl Iterator<Item = &CommitId> { |
| 150 |
self.data.commit_predecessors.iter().flat_map(|map| { |
| 151 |
map.iter() |
| 152 |
.flat_map(|(new_id, old_ids)| iter::once(new_id).chain(old_ids)) |
| 153 |
}) |
| 154 |
} |
| 155 |
|
| 156 |
pub fn store_operation(&self) -> &op_store::Operation { |
| 157 |
&self.data |
| 158 |
} |
| 159 |
} |