jj_lib/
operation.rs

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#![allow(missing_docs)]
16
17use std::cmp::Ordering;
18use std::fmt::Debug;
19use std::fmt::Error;
20use std::fmt::Formatter;
21use std::hash::Hash;
22use std::hash::Hasher;
23use std::sync::Arc;
24
25use crate::backend::CommitId;
26use crate::op_store;
27use crate::op_store::OpStore;
28use crate::op_store::OpStoreResult;
29use crate::op_store::OperationId;
30use crate::op_store::OperationMetadata;
31use crate::op_store::ViewId;
32use crate::view::View;
33
34/// A wrapper around [`op_store::Operation`] that defines additional methods and
35/// stores a pointer to the `OpStore` the operation belongs to.
36#[derive(Clone)]
37pub struct Operation {
38    op_store: Arc<dyn OpStore>,
39    id: OperationId,
40    data: Arc<op_store::Operation>, // allow cheap clone
41}
42
43impl Debug for Operation {
44    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
45        f.debug_struct("Operation").field("id", &self.id).finish()
46    }
47}
48
49impl PartialEq for Operation {
50    fn eq(&self, other: &Self) -> bool {
51        self.id == other.id
52    }
53}
54
55impl Eq for Operation {}
56
57impl Ord for Operation {
58    fn cmp(&self, other: &Self) -> Ordering {
59        self.id.cmp(&other.id)
60    }
61}
62
63impl PartialOrd for Operation {
64    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
65        Some(self.cmp(other))
66    }
67}
68
69impl Hash for Operation {
70    fn hash<H: Hasher>(&self, state: &mut H) {
71        self.id.hash(state);
72    }
73}
74
75impl Operation {
76    pub fn new(
77        op_store: Arc<dyn OpStore>,
78        id: OperationId,
79        data: impl Into<Arc<op_store::Operation>>,
80    ) -> Self {
81        Operation {
82            op_store,
83            id,
84            data: data.into(),
85        }
86    }
87
88    pub fn op_store(&self) -> Arc<dyn OpStore> {
89        self.op_store.clone()
90    }
91
92    pub fn id(&self) -> &OperationId {
93        &self.id
94    }
95
96    pub fn view_id(&self) -> &ViewId {
97        &self.data.view_id
98    }
99
100    pub fn parent_ids(&self) -> &[OperationId] {
101        &self.data.parents
102    }
103
104    pub fn parents(&self) -> impl ExactSizeIterator<Item = OpStoreResult<Operation>> + use<'_> {
105        let op_store = &self.op_store;
106        self.data.parents.iter().map(|parent_id| {
107            let data = op_store.read_operation(parent_id)?;
108            Ok(Operation::new(op_store.clone(), parent_id.clone(), data))
109        })
110    }
111
112    pub fn view(&self) -> OpStoreResult<View> {
113        let data = self.op_store.read_view(&self.data.view_id)?;
114        Ok(View::new(data))
115    }
116
117    pub fn metadata(&self) -> &OperationMetadata {
118        &self.data.metadata
119    }
120
121    /// Returns true if predecessors are recorded in this operation.
122    ///
123    /// This returns false only if the operation was written by jj < 0.30.
124    pub fn stores_commit_predecessors(&self) -> bool {
125        self.data.commit_predecessors.is_some()
126    }
127
128    /// Returns predecessors of the specified commit if recorded.
129    pub fn predecessors_for_commit(&self, commit_id: &CommitId) -> Option<&[CommitId]> {
130        let map = self.data.commit_predecessors.as_ref()?;
131        Some(map.get(commit_id)?)
132    }
133
134    pub fn store_operation(&self) -> &op_store::Operation {
135        &self.data
136    }
137}