jj_lib/
op_store.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#![expect(missing_docs)]
16
17use std::any::Any;
18use std::collections::BTreeMap;
19use std::collections::HashMap;
20use std::collections::HashSet;
21use std::fmt::Debug;
22use std::iter;
23use std::sync::LazyLock;
24use std::time::SystemTime;
25
26use itertools::Itertools as _;
27use thiserror::Error;
28
29use crate::backend::CommitId;
30use crate::backend::MillisSinceEpoch;
31use crate::backend::Timestamp;
32use crate::content_hash::ContentHash;
33use crate::merge::Merge;
34use crate::object_id::HexPrefix;
35use crate::object_id::ObjectId as _;
36use crate::object_id::PrefixResolution;
37use crate::object_id::id_type;
38use crate::ref_name::GitRefNameBuf;
39use crate::ref_name::RefName;
40use crate::ref_name::RefNameBuf;
41use crate::ref_name::RemoteName;
42use crate::ref_name::RemoteNameBuf;
43use crate::ref_name::RemoteRefSymbol;
44use crate::ref_name::WorkspaceNameBuf;
45
46id_type!(pub ViewId { hex() });
47id_type!(pub OperationId { hex() });
48
49#[derive(ContentHash, PartialEq, Eq, Hash, Clone, Debug, serde::Serialize)]
50#[serde(transparent)]
51pub struct RefTarget {
52    merge: Merge<Option<CommitId>>,
53}
54
55impl Default for RefTarget {
56    fn default() -> Self {
57        Self::absent()
58    }
59}
60
61impl RefTarget {
62    /// Creates non-conflicting target pointing to no commit.
63    pub fn absent() -> Self {
64        Self::from_merge(Merge::absent())
65    }
66
67    /// Returns non-conflicting target pointing to no commit.
68    ///
69    /// This will typically be used in place of `None` returned by map lookup.
70    pub fn absent_ref() -> &'static Self {
71        static TARGET: LazyLock<RefTarget> = LazyLock::new(RefTarget::absent);
72        &TARGET
73    }
74
75    /// Creates non-conflicting target that optionally points to a commit.
76    pub fn resolved(maybe_id: Option<CommitId>) -> Self {
77        Self::from_merge(Merge::resolved(maybe_id))
78    }
79
80    /// Creates non-conflicting target pointing to a commit.
81    pub fn normal(id: CommitId) -> Self {
82        Self::from_merge(Merge::normal(id))
83    }
84
85    /// Creates target from removed/added ids.
86    pub fn from_legacy_form(
87        removed_ids: impl IntoIterator<Item = CommitId>,
88        added_ids: impl IntoIterator<Item = CommitId>,
89    ) -> Self {
90        Self::from_merge(Merge::from_legacy_form(removed_ids, added_ids))
91    }
92
93    pub fn from_merge(merge: Merge<Option<CommitId>>) -> Self {
94        Self { merge }
95    }
96
97    /// Returns the underlying value if this target is non-conflicting.
98    pub fn as_resolved(&self) -> Option<&Option<CommitId>> {
99        self.merge.as_resolved()
100    }
101
102    /// Returns id if this target is non-conflicting and points to a commit.
103    pub fn as_normal(&self) -> Option<&CommitId> {
104        self.merge.as_normal()
105    }
106
107    /// Returns true if this target points to no commit.
108    pub fn is_absent(&self) -> bool {
109        self.merge.is_absent()
110    }
111
112    /// Returns true if this target points to any commit. Conflicting target is
113    /// always "present" as it should have at least one commit id.
114    pub fn is_present(&self) -> bool {
115        self.merge.is_present()
116    }
117
118    /// Whether this target has conflicts.
119    pub fn has_conflict(&self) -> bool {
120        !self.merge.is_resolved()
121    }
122
123    pub fn removed_ids(&self) -> impl Iterator<Item = &CommitId> {
124        self.merge.removes().flatten()
125    }
126
127    pub fn added_ids(&self) -> impl Iterator<Item = &CommitId> {
128        self.merge.adds().flatten()
129    }
130
131    pub fn as_merge(&self) -> &Merge<Option<CommitId>> {
132        &self.merge
133    }
134}
135
136/// Remote bookmark or tag.
137#[derive(ContentHash, Clone, Debug, Eq, Hash, PartialEq)]
138pub struct RemoteRef {
139    pub target: RefTarget,
140    pub state: RemoteRefState,
141}
142
143impl RemoteRef {
144    /// Creates remote ref pointing to no commit.
145    pub fn absent() -> Self {
146        Self {
147            target: RefTarget::absent(),
148            state: RemoteRefState::New,
149        }
150    }
151
152    /// Returns remote ref pointing to no commit.
153    ///
154    /// This will typically be used in place of `None` returned by map lookup.
155    pub fn absent_ref() -> &'static Self {
156        static TARGET: LazyLock<RemoteRef> = LazyLock::new(RemoteRef::absent);
157        &TARGET
158    }
159
160    /// Returns true if the target points to no commit.
161    pub fn is_absent(&self) -> bool {
162        self.target.is_absent()
163    }
164
165    /// Returns true if the target points to any commit.
166    pub fn is_present(&self) -> bool {
167        self.target.is_present()
168    }
169
170    /// Returns true if the ref is supposed to be merged in to the local ref.
171    pub fn is_tracked(&self) -> bool {
172        self.state == RemoteRefState::Tracked
173    }
174
175    /// Target that should have been merged in to the local ref.
176    ///
177    /// Use this as the base or known target when merging new remote ref in to
178    /// local or pushing local ref to remote.
179    pub fn tracked_target(&self) -> &RefTarget {
180        if self.is_tracked() {
181            &self.target
182        } else {
183            RefTarget::absent_ref()
184        }
185    }
186}
187
188/// Whether the ref is tracked or not.
189#[derive(ContentHash, Clone, Copy, Debug, Eq, Hash, PartialEq)]
190pub enum RemoteRefState {
191    /// Remote ref is not merged in to the local ref.
192    New,
193    /// Remote ref has been merged in to the local ref. Incoming ref will be
194    /// merged, too.
195    Tracked,
196}
197
198/// Helper to strip redundant `Option<T>` from `RefTarget` lookup result.
199pub trait RefTargetOptionExt {
200    type Value;
201
202    fn flatten(self) -> Self::Value;
203}
204
205impl RefTargetOptionExt for Option<RefTarget> {
206    type Value = RefTarget;
207
208    fn flatten(self) -> Self::Value {
209        self.unwrap_or_else(RefTarget::absent)
210    }
211}
212
213impl<'a> RefTargetOptionExt for Option<&'a RefTarget> {
214    type Value = &'a RefTarget;
215
216    fn flatten(self) -> Self::Value {
217        self.unwrap_or_else(|| RefTarget::absent_ref())
218    }
219}
220
221impl RefTargetOptionExt for Option<RemoteRef> {
222    type Value = RemoteRef;
223
224    fn flatten(self) -> Self::Value {
225        self.unwrap_or_else(RemoteRef::absent)
226    }
227}
228
229impl<'a> RefTargetOptionExt for Option<&'a RemoteRef> {
230    type Value = &'a RemoteRef;
231
232    fn flatten(self) -> Self::Value {
233        self.unwrap_or_else(|| RemoteRef::absent_ref())
234    }
235}
236
237/// Local and remote refs of the same name.
238#[derive(PartialEq, Eq, Clone, Debug)]
239pub struct LocalRemoteRefTarget<'a> {
240    /// The commit the ref points to locally.
241    pub local_target: &'a RefTarget,
242    /// `(remote_name, remote_ref)` pairs in lexicographical order.
243    pub remote_refs: Vec<(&'a RemoteName, &'a RemoteRef)>,
244}
245
246/// Represents the way the repo looks at a given time, just like how a Tree
247/// object represents how the file system looks at a given time.
248#[derive(ContentHash, PartialEq, Eq, Clone, Debug)]
249pub struct View {
250    /// All head commits. There should be at least one head commit.
251    pub head_ids: HashSet<CommitId>,
252    pub local_bookmarks: BTreeMap<RefNameBuf, RefTarget>,
253    pub local_tags: BTreeMap<RefNameBuf, RefTarget>,
254    pub remote_views: BTreeMap<RemoteNameBuf, RemoteView>,
255    pub git_refs: BTreeMap<GitRefNameBuf, RefTarget>,
256    /// The commit the Git HEAD points to.
257    // TODO: Support multiple Git worktrees?
258    // TODO: Do we want to store the current bookmark name too?
259    pub git_head: RefTarget,
260    // The commit that *should be* checked out in the workspace. Note that the working copy
261    // (.jj/working_copy/) has the source of truth about which commit *is* checked out (to be
262    // precise: the commit to which we most recently completed an update to).
263    pub wc_commit_ids: BTreeMap<WorkspaceNameBuf, CommitId>,
264}
265
266impl View {
267    /// Creates new (mostly empty) view containing the given commit as the head.
268    pub fn make_root(root_commit_id: CommitId) -> Self {
269        Self {
270            head_ids: HashSet::from([root_commit_id]),
271            local_bookmarks: BTreeMap::new(),
272            local_tags: BTreeMap::new(),
273            remote_views: BTreeMap::new(),
274            git_refs: BTreeMap::new(),
275            git_head: RefTarget::absent(),
276            wc_commit_ids: BTreeMap::new(),
277        }
278    }
279}
280
281/// Represents the state of the remote repo.
282#[derive(ContentHash, Clone, Debug, Default, Eq, PartialEq)]
283pub struct RemoteView {
284    // TODO: Do we need to support tombstones for remote bookmarks? For example, if the bookmark
285    // has been deleted locally and you pull from a remote, maybe it should make a difference
286    // whether the bookmark is known to have existed on the remote. We may not want to resurrect
287    // the bookmark if the bookmark's state on the remote was just not known.
288    pub bookmarks: BTreeMap<RefNameBuf, RemoteRef>,
289    pub tags: BTreeMap<RefNameBuf, RemoteRef>,
290}
291
292/// Iterates pair of local and remote refs by name.
293pub(crate) fn merge_join_ref_views<'a>(
294    local_refs: &'a BTreeMap<RefNameBuf, RefTarget>,
295    remote_views: &'a BTreeMap<RemoteNameBuf, RemoteView>,
296    get_remote_refs: impl FnMut(&RemoteView) -> &BTreeMap<RefNameBuf, RemoteRef>,
297) -> impl Iterator<Item = (&'a RefName, LocalRemoteRefTarget<'a>)> {
298    let mut local_refs_iter = local_refs
299        .iter()
300        .map(|(name, target)| (&**name, target))
301        .peekable();
302    let mut remote_refs_iter = flatten_remote_refs(remote_views, get_remote_refs).peekable();
303
304    iter::from_fn(move || {
305        // Pick earlier bookmark name
306        let (name, local_target) = if let Some((symbol, _)) = remote_refs_iter.peek() {
307            local_refs_iter
308                .next_if(|&(local_name, _)| local_name <= symbol.name)
309                .unwrap_or((symbol.name, RefTarget::absent_ref()))
310        } else {
311            local_refs_iter.next()?
312        };
313        let remote_refs = remote_refs_iter
314            .peeking_take_while(|(symbol, _)| symbol.name == name)
315            .map(|(symbol, remote_ref)| (symbol.remote, remote_ref))
316            .collect();
317        let local_remote_target = LocalRemoteRefTarget {
318            local_target,
319            remote_refs,
320        };
321        Some((name, local_remote_target))
322    })
323}
324
325/// Iterates `(symbol, remote_ref)`s in lexicographical order.
326pub(crate) fn flatten_remote_refs(
327    remote_views: &BTreeMap<RemoteNameBuf, RemoteView>,
328    mut get_remote_refs: impl FnMut(&RemoteView) -> &BTreeMap<RefNameBuf, RemoteRef>,
329) -> impl Iterator<Item = (RemoteRefSymbol<'_>, &RemoteRef)> {
330    remote_views
331        .iter()
332        .map(|(remote, remote_view)| {
333            get_remote_refs(remote_view)
334                .iter()
335                .map(move |(name, remote_ref)| (name.to_remote_symbol(remote), remote_ref))
336        })
337        .kmerge_by(|(symbol1, _), (symbol2, _)| symbol1 < symbol2)
338}
339
340#[derive(Clone, ContentHash, Debug, Eq, PartialEq, serde::Serialize)]
341pub struct TimestampRange {
342    // Could be aliased to Range<Timestamp> if needed.
343    pub start: Timestamp,
344    pub end: Timestamp,
345}
346
347/// Represents an operation (transaction) on the repo view, just like how a
348/// Commit object represents an operation on the tree.
349///
350/// Operations and views are not meant to be exchanged between repos or users;
351/// they represent local state and history.
352///
353/// The operation history will almost always be linear. It will only have
354/// forks when parallel operations occurred. The parent is determined when
355/// the transaction starts. When the transaction commits, a lock will be
356/// taken and it will be checked that the current head of the operation
357/// graph is unchanged. If the current head has changed, there has been
358/// concurrent operation.
359#[derive(ContentHash, PartialEq, Eq, Clone, Debug, serde::Serialize)]
360pub struct Operation {
361    #[serde(skip)] // TODO: should be exposed?
362    pub view_id: ViewId,
363    pub parents: Vec<OperationId>,
364    #[serde(flatten)]
365    pub metadata: OperationMetadata,
366    /// Mapping from new commit to its predecessors, or `None` if predecessors
367    /// weren't recorded when the operation was written.
368    ///
369    /// * `commit_id: []` if the commit was newly created.
370    /// * `commit_id: [predecessor_id, ..]` if the commit was rewritten.
371    ///
372    /// This mapping preserves all transitive predecessors if a commit was
373    /// rewritten multiple times within the same transaction. For example, if
374    /// `X` was rewritten as `Y`, then rebased as `Z`, these modifications are
375    /// recorded as `{Y: [X], Z: [Y]}`.
376    ///
377    /// Existing commits (including commits imported from Git) aren't tracked
378    /// even if they became visible at this operation.
379    // BTreeMap for ease of deterministic serialization. If the deserialization
380    // cost matters, maybe this can be changed to sorted Vec.
381    #[serde(skip)] // TODO: should be exposed?
382    pub commit_predecessors: Option<BTreeMap<CommitId, Vec<CommitId>>>,
383}
384
385impl Operation {
386    pub fn make_root(root_view_id: ViewId) -> Self {
387        let timestamp = Timestamp {
388            timestamp: MillisSinceEpoch(0),
389            tz_offset: 0,
390        };
391        let metadata = OperationMetadata {
392            time: TimestampRange {
393                start: timestamp,
394                end: timestamp,
395            },
396            description: "".to_string(),
397            hostname: "".to_string(),
398            username: "".to_string(),
399            is_snapshot: false,
400            tags: HashMap::new(),
401        };
402        Self {
403            view_id: root_view_id,
404            parents: vec![],
405            metadata,
406            // The root operation is guaranteed to have no new commits. The root
407            // commit could be considered born at the root operation, but there
408            // may be other commits created within the abandoned operations.
409            // They don't have any predecessors records as well.
410            commit_predecessors: Some(BTreeMap::new()),
411        }
412    }
413}
414
415#[derive(ContentHash, PartialEq, Eq, Clone, Debug, serde::Serialize)]
416pub struct OperationMetadata {
417    pub time: TimestampRange,
418    // Whatever is useful to the user, such as exact command line call
419    pub description: String,
420    pub hostname: String,
421    pub username: String,
422    /// Whether this operation represents a pure snapshotting of the working
423    /// copy.
424    pub is_snapshot: bool,
425    pub tags: HashMap<String, String>,
426}
427
428/// Data to be loaded into the root operation/view.
429#[derive(Clone, Debug)]
430pub struct RootOperationData {
431    /// The root commit ID, which should exist in the root view.
432    pub root_commit_id: CommitId,
433}
434
435#[derive(Debug, Error)]
436pub enum OpStoreError {
437    #[error("Object {hash} of type {object_type} not found")]
438    ObjectNotFound {
439        object_type: String,
440        hash: String,
441        source: Box<dyn std::error::Error + Send + Sync>,
442    },
443    #[error("Error when reading object {hash} of type {object_type}")]
444    ReadObject {
445        object_type: String,
446        hash: String,
447        source: Box<dyn std::error::Error + Send + Sync>,
448    },
449    #[error("Could not write object of type {object_type}")]
450    WriteObject {
451        object_type: &'static str,
452        source: Box<dyn std::error::Error + Send + Sync>,
453    },
454    #[error(transparent)]
455    Other(Box<dyn std::error::Error + Send + Sync>),
456}
457
458pub type OpStoreResult<T> = Result<T, OpStoreError>;
459
460pub trait OpStore: Any + Send + Sync + Debug {
461    fn name(&self) -> &str;
462
463    fn root_operation_id(&self) -> &OperationId;
464
465    fn read_view(&self, id: &ViewId) -> OpStoreResult<View>;
466
467    fn write_view(&self, contents: &View) -> OpStoreResult<ViewId>;
468
469    fn read_operation(&self, id: &OperationId) -> OpStoreResult<Operation>;
470
471    fn write_operation(&self, contents: &Operation) -> OpStoreResult<OperationId>;
472
473    /// Resolves an unambiguous operation ID prefix.
474    fn resolve_operation_id_prefix(
475        &self,
476        prefix: &HexPrefix,
477    ) -> OpStoreResult<PrefixResolution<OperationId>>;
478
479    /// Prunes unreachable operations and views.
480    ///
481    /// All operations and views reachable from the `head_ids` won't be
482    /// removed. In addition to that, objects created after `keep_newer` will be
483    /// preserved. This mitigates a risk of deleting new heads created
484    /// concurrently by another process.
485    // TODO: return stats?
486    fn gc(&self, head_ids: &[OperationId], keep_newer: SystemTime) -> OpStoreResult<()>;
487}
488
489impl dyn OpStore {
490    /// Returns reference of the implementation type.
491    pub fn downcast_ref<T: OpStore>(&self) -> Option<&T> {
492        (self as &dyn Any).downcast_ref()
493    }
494}
495
496#[cfg(test)]
497mod tests {
498    use maplit::btreemap;
499
500    use super::*;
501
502    #[test]
503    fn test_merge_join_bookmark_views() {
504        let remote_ref = |target: &RefTarget| RemoteRef {
505            target: target.clone(),
506            state: RemoteRefState::Tracked, // doesn't matter
507        };
508        let local_bookmark1_target = RefTarget::normal(CommitId::from_hex("111111"));
509        let local_bookmark2_target = RefTarget::normal(CommitId::from_hex("222222"));
510        let git_bookmark1_remote_ref = remote_ref(&RefTarget::normal(CommitId::from_hex("333333")));
511        let git_bookmark2_remote_ref = remote_ref(&RefTarget::normal(CommitId::from_hex("444444")));
512        let remote1_bookmark1_remote_ref =
513            remote_ref(&RefTarget::normal(CommitId::from_hex("555555")));
514        let remote2_bookmark2_remote_ref =
515            remote_ref(&RefTarget::normal(CommitId::from_hex("666666")));
516
517        let local_bookmarks = btreemap! {
518            "bookmark1".into() => local_bookmark1_target.clone(),
519            "bookmark2".into() => local_bookmark2_target.clone(),
520        };
521        let remote_views = btreemap! {
522            "git".into() => RemoteView {
523                bookmarks: btreemap! {
524                    "bookmark1".into() => git_bookmark1_remote_ref.clone(),
525                    "bookmark2".into() => git_bookmark2_remote_ref.clone(),
526                },
527                tags: btreemap! {},
528            },
529            "remote1".into() => RemoteView {
530                bookmarks: btreemap! {
531                    "bookmark1".into() => remote1_bookmark1_remote_ref.clone(),
532                },
533                tags: btreemap! {},
534            },
535            "remote2".into() => RemoteView {
536                bookmarks: btreemap! {
537                    "bookmark2".into() => remote2_bookmark2_remote_ref.clone(),
538                },
539                tags: btreemap! {},
540            },
541        };
542        assert_eq!(
543            merge_join_ref_views(&local_bookmarks, &remote_views, |view| &view.bookmarks)
544                .collect_vec(),
545            vec![
546                (
547                    "bookmark1".as_ref(),
548                    LocalRemoteRefTarget {
549                        local_target: &local_bookmark1_target,
550                        remote_refs: vec![
551                            ("git".as_ref(), &git_bookmark1_remote_ref),
552                            ("remote1".as_ref(), &remote1_bookmark1_remote_ref),
553                        ],
554                    },
555                ),
556                (
557                    "bookmark2".as_ref(),
558                    LocalRemoteRefTarget {
559                        local_target: &local_bookmark2_target.clone(),
560                        remote_refs: vec![
561                            ("git".as_ref(), &git_bookmark2_remote_ref),
562                            ("remote2".as_ref(), &remote2_bookmark2_remote_ref),
563                        ],
564                    },
565                ),
566            ],
567        );
568
569        // Local only
570        let local_bookmarks = btreemap! {
571            "bookmark1".into() => local_bookmark1_target.clone(),
572        };
573        let remote_views = btreemap! {};
574        assert_eq!(
575            merge_join_ref_views(&local_bookmarks, &remote_views, |view| &view.bookmarks)
576                .collect_vec(),
577            vec![(
578                "bookmark1".as_ref(),
579                LocalRemoteRefTarget {
580                    local_target: &local_bookmark1_target,
581                    remote_refs: vec![]
582                },
583            )],
584        );
585
586        // Remote only
587        let local_bookmarks = btreemap! {};
588        let remote_views = btreemap! {
589            "remote1".into() => RemoteView {
590                bookmarks: btreemap! {
591                    "bookmark1".into() => remote1_bookmark1_remote_ref.clone(),
592                },
593                tags: btreemap! {},
594            },
595        };
596        assert_eq!(
597            merge_join_ref_views(&local_bookmarks, &remote_views, |view| &view.bookmarks)
598                .collect_vec(),
599            vec![(
600                "bookmark1".as_ref(),
601                LocalRemoteRefTarget {
602                    local_target: RefTarget::absent_ref(),
603                    remote_refs: vec![("remote1".as_ref(), &remote1_bookmark1_remote_ref)],
604                },
605            )],
606        );
607    }
608}