jj_lib/
working_copy.rs

1// Copyright 2023 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//! Defines the interface for the working copy. See `LocalWorkingCopy` for the
16//! default local-disk implementation.
17
18use std::any::Any;
19use std::collections::BTreeMap;
20use std::ffi::OsString;
21use std::path::PathBuf;
22use std::sync::Arc;
23
24use async_trait::async_trait;
25use itertools::Itertools as _;
26use pollster::FutureExt as _;
27use thiserror::Error;
28use tracing::instrument;
29
30use crate::backend::BackendError;
31use crate::backend::MergedTreeId;
32use crate::commit::Commit;
33use crate::dag_walk;
34use crate::gitignore::GitIgnoreError;
35use crate::gitignore::GitIgnoreFile;
36use crate::matchers::Matcher;
37use crate::op_store::OpStoreError;
38use crate::op_store::OperationId;
39use crate::operation::Operation;
40use crate::ref_name::WorkspaceName;
41use crate::ref_name::WorkspaceNameBuf;
42use crate::repo::ReadonlyRepo;
43use crate::repo::Repo as _;
44use crate::repo::RewriteRootCommit;
45use crate::repo_path::InvalidRepoPathError;
46use crate::repo_path::RepoPath;
47use crate::repo_path::RepoPathBuf;
48use crate::settings::UserSettings;
49use crate::store::Store;
50use crate::transaction::TransactionCommitError;
51
52/// The trait all working-copy implementations must implement.
53pub trait WorkingCopy: Any + Send {
54    /// The name/id of the implementation. Used for choosing the right
55    /// implementation when loading a working copy.
56    fn name(&self) -> &str;
57
58    /// The working copy's workspace name (or identifier.)
59    fn workspace_name(&self) -> &WorkspaceName;
60
61    /// The operation this working copy was most recently updated to.
62    fn operation_id(&self) -> &OperationId;
63
64    /// The ID of the tree this working copy was most recently updated to.
65    fn tree_id(&self) -> Result<&MergedTreeId, WorkingCopyStateError>;
66
67    /// Patterns that decide which paths from the current tree should be checked
68    /// out in the working copy. An empty list means that no paths should be
69    /// checked out in the working copy. A single `RepoPath::root()` entry means
70    /// that all files should be checked out.
71    fn sparse_patterns(&self) -> Result<&[RepoPathBuf], WorkingCopyStateError>;
72
73    /// Locks the working copy and returns an instance with methods for updating
74    /// the working copy files and state.
75    fn start_mutation(&self) -> Result<Box<dyn LockedWorkingCopy>, WorkingCopyStateError>;
76}
77
78impl dyn WorkingCopy {
79    /// Returns reference of the implementation type.
80    pub fn downcast_ref<T: WorkingCopy>(&self) -> Option<&T> {
81        (self as &dyn Any).downcast_ref()
82    }
83}
84
85/// The factory which creates and loads a specific type of working copy.
86pub trait WorkingCopyFactory {
87    /// Create a new working copy from scratch.
88    fn init_working_copy(
89        &self,
90        store: Arc<Store>,
91        working_copy_path: PathBuf,
92        state_path: PathBuf,
93        operation_id: OperationId,
94        workspace_name: WorkspaceNameBuf,
95        settings: &UserSettings,
96    ) -> Result<Box<dyn WorkingCopy>, WorkingCopyStateError>;
97
98    /// Load an existing working copy.
99    fn load_working_copy(
100        &self,
101        store: Arc<Store>,
102        working_copy_path: PathBuf,
103        state_path: PathBuf,
104        settings: &UserSettings,
105    ) -> Result<Box<dyn WorkingCopy>, WorkingCopyStateError>;
106}
107
108/// A working copy that's being modified.
109#[async_trait]
110pub trait LockedWorkingCopy: Any + Send {
111    /// The operation at the time the lock was taken
112    fn old_operation_id(&self) -> &OperationId;
113
114    /// The tree at the time the lock was taken
115    fn old_tree_id(&self) -> &MergedTreeId;
116
117    /// Snapshot the working copy. Returns the tree id and stats.
118    async fn snapshot(
119        &mut self,
120        options: &SnapshotOptions,
121    ) -> Result<(MergedTreeId, SnapshotStats), SnapshotError>;
122
123    /// Check out the specified commit in the working copy.
124    async fn check_out(&mut self, commit: &Commit) -> Result<CheckoutStats, CheckoutError>;
125
126    /// Update the workspace name.
127    fn rename_workspace(&mut self, new_workspace_name: WorkspaceNameBuf);
128
129    /// Update to another commit without touching the files in the working copy.
130    async fn reset(&mut self, commit: &Commit) -> Result<(), ResetError>;
131
132    /// Update to another commit without touching the files in the working copy,
133    /// without assuming that the previous tree exists.
134    async fn recover(&mut self, commit: &Commit) -> Result<(), ResetError>;
135
136    /// See `WorkingCopy::sparse_patterns()`
137    fn sparse_patterns(&self) -> Result<&[RepoPathBuf], WorkingCopyStateError>;
138
139    /// Updates the patterns that decide which paths from the current tree
140    /// should be checked out in the working copy.
141    // TODO: Use a different error type here so we can include a
142    // `SparseNotSupported` variants for working copies that don't support sparse
143    // checkouts (e.g. because they use a virtual file system so there's no reason
144    // to use sparse).
145    async fn set_sparse_patterns(
146        &mut self,
147        new_sparse_patterns: Vec<RepoPathBuf>,
148    ) -> Result<CheckoutStats, CheckoutError>;
149
150    /// Finish the modifications to the working copy by writing the updated
151    /// states to disk. Returns the new (unlocked) working copy.
152    async fn finish(
153        self: Box<Self>,
154        operation_id: OperationId,
155    ) -> Result<Box<dyn WorkingCopy>, WorkingCopyStateError>;
156}
157
158impl dyn LockedWorkingCopy {
159    /// Returns reference of the implementation type.
160    pub fn downcast_ref<T: LockedWorkingCopy>(&self) -> Option<&T> {
161        (self as &dyn Any).downcast_ref()
162    }
163
164    /// Returns mutable reference of the implementation type.
165    pub fn downcast_mut<T: LockedWorkingCopy>(&mut self) -> Option<&mut T> {
166        (self as &mut dyn Any).downcast_mut()
167    }
168}
169
170/// An error while snapshotting the working copy.
171#[derive(Debug, Error)]
172pub enum SnapshotError {
173    /// A tracked path contained invalid component such as `..`.
174    #[error(transparent)]
175    InvalidRepoPath(#[from] InvalidRepoPathError),
176    /// A path in the working copy was not valid UTF-8.
177    #[error("Working copy path {} is not valid UTF-8", path.to_string_lossy())]
178    InvalidUtf8Path {
179        /// The path with invalid UTF-8.
180        path: OsString,
181    },
182    /// A symlink target in the working copy was not valid UTF-8.
183    #[error("Symlink {path} target is not valid UTF-8")]
184    InvalidUtf8SymlinkTarget {
185        /// The path of the symlink that has a target that's not valid UTF-8.
186        /// This path itself is valid UTF-8.
187        path: PathBuf,
188    },
189    /// Reading or writing from the commit backend failed.
190    #[error(transparent)]
191    BackendError(#[from] BackendError),
192    /// Checking path with ignore patterns failed.
193    #[error(transparent)]
194    GitIgnoreError(#[from] GitIgnoreError),
195    /// Failed to load the working copy state.
196    #[error(transparent)]
197    WorkingCopyStateError(#[from] WorkingCopyStateError),
198    /// Some other error happened while snapshotting the working copy.
199    #[error("{message}")]
200    Other {
201        /// Error message.
202        message: String,
203        /// The underlying error.
204        #[source]
205        err: Box<dyn std::error::Error + Send + Sync>,
206    },
207}
208
209/// Options used when snapshotting the working copy. Some of them may be ignored
210/// by some `WorkingCopy` implementations.
211#[derive(Clone)]
212pub struct SnapshotOptions<'a> {
213    /// The `.gitignore`s to use while snapshotting. The typically come from the
214    /// user's configured patterns combined with per-repo patterns.
215    // The base_ignores are passed in here rather than being set on the TreeState
216    // because the TreeState may be long-lived if the library is used in a
217    // long-lived process.
218    pub base_ignores: Arc<GitIgnoreFile>,
219    /// A callback for the UI to display progress.
220    pub progress: Option<&'a SnapshotProgress<'a>>,
221    /// For new files that are not already tracked, start tracking them if they
222    /// match this.
223    pub start_tracking_matcher: &'a dyn Matcher,
224    /// The size of the largest file that should be allowed to become tracked
225    /// (already tracked files are always snapshotted). If there are larger
226    /// files in the working copy, then `LockedWorkingCopy::snapshot()` may
227    /// (depending on implementation)
228    /// return `SnapshotError::NewFileTooLarge`.
229    pub max_new_file_size: u64,
230}
231
232/// A callback for getting progress updates.
233pub type SnapshotProgress<'a> = dyn Fn(&RepoPath) + 'a + Sync;
234
235/// Stats about a snapshot operation on a working copy.
236#[derive(Clone, Debug, Default)]
237pub struct SnapshotStats {
238    /// List of new (previously untracked) files which are still untracked.
239    pub untracked_paths: BTreeMap<RepoPathBuf, UntrackedReason>,
240}
241
242/// Reason why the new path isn't tracked.
243#[derive(Clone, Debug)]
244pub enum UntrackedReason {
245    /// File was larger than the specified maximum file size.
246    FileTooLarge {
247        /// Actual size of the large file.
248        size: u64,
249        /// Maximum allowed size.
250        max_size: u64,
251    },
252    /// File does not match the fileset specified in snapshot.auto-track.
253    FileNotAutoTracked,
254}
255
256/// Stats about a checkout operation on a working copy. All "files" mentioned
257/// below may also be symlinks or materialized conflicts.
258#[derive(Debug, PartialEq, Eq, Clone, Default)]
259pub struct CheckoutStats {
260    /// The number of files that were updated in the working copy.
261    /// These files existed before and after the checkout.
262    pub updated_files: u32,
263    /// The number of files added in the working copy.
264    pub added_files: u32,
265    /// The number of files removed in the working copy.
266    pub removed_files: u32,
267    /// The number of files that were supposed to be updated or added in the
268    /// working copy but were skipped because there was an untracked (probably
269    /// ignored) file in its place.
270    pub skipped_files: u32,
271}
272
273/// The working-copy checkout failed.
274#[derive(Debug, Error)]
275pub enum CheckoutError {
276    /// The current working-copy commit was deleted, maybe by an overly
277    /// aggressive GC that happened while the current process was running.
278    #[error("Current working-copy commit not found")]
279    SourceNotFound {
280        /// The underlying error.
281        source: Box<dyn std::error::Error + Send + Sync>,
282    },
283    /// Another process checked out a commit while the current process was
284    /// running (after the working copy was read by the current process).
285    #[error("Concurrent checkout")]
286    ConcurrentCheckout,
287    /// Path in the commit contained invalid component such as `..`.
288    #[error(transparent)]
289    InvalidRepoPath(#[from] InvalidRepoPathError),
290    /// Path contained reserved name which cannot be checked out to disk.
291    #[error("Reserved path component {name} in {path}")]
292    ReservedPathComponent {
293        /// The file or directory path.
294        path: PathBuf,
295        /// The reserved path component.
296        name: &'static str,
297    },
298    /// Reading or writing from the commit backend failed.
299    #[error("Internal backend error")]
300    InternalBackendError(#[from] BackendError),
301    /// Failed to load the working copy state.
302    #[error(transparent)]
303    WorkingCopyStateError(#[from] WorkingCopyStateError),
304    /// Some other error happened while checking out the working copy.
305    #[error("{message}")]
306    Other {
307        /// Error message.
308        message: String,
309        /// The underlying error.
310        #[source]
311        err: Box<dyn std::error::Error + Send + Sync>,
312    },
313}
314
315/// An error while resetting the working copy.
316#[derive(Debug, Error)]
317pub enum ResetError {
318    /// The current working-copy commit was deleted, maybe by an overly
319    /// aggressive GC that happened while the current process was running.
320    #[error("Current working-copy commit not found")]
321    SourceNotFound {
322        /// The underlying error.
323        source: Box<dyn std::error::Error + Send + Sync>,
324    },
325    /// Reading or writing from the commit backend failed.
326    #[error("Internal error")]
327    InternalBackendError(#[from] BackendError),
328    /// Failed to load the working copy state.
329    #[error(transparent)]
330    WorkingCopyStateError(#[from] WorkingCopyStateError),
331    /// Some other error happened while resetting the working copy.
332    #[error("{message}")]
333    Other {
334        /// Error message.
335        message: String,
336        /// The underlying error.
337        #[source]
338        err: Box<dyn std::error::Error + Send + Sync>,
339    },
340}
341
342/// Whether the working copy is stale or not.
343#[derive(Clone, Debug, Eq, PartialEq)]
344pub enum WorkingCopyFreshness {
345    /// The working copy isn't stale, and no need to reload the repo.
346    Fresh,
347    /// The working copy was updated since we loaded the repo. The repo must be
348    /// reloaded at the working copy's operation.
349    Updated(Box<Operation>),
350    /// The working copy is behind the latest operation.
351    WorkingCopyStale,
352    /// The working copy is a sibling of the latest operation.
353    SiblingOperation,
354}
355
356impl WorkingCopyFreshness {
357    /// Determine the freshness of the provided working copy relative to the
358    /// target commit.
359    #[instrument(skip_all)]
360    pub fn check_stale(
361        locked_wc: &dyn LockedWorkingCopy,
362        wc_commit: &Commit,
363        repo: &ReadonlyRepo,
364    ) -> Result<Self, OpStoreError> {
365        // Check if the working copy's tree matches the repo's view
366        let wc_tree_id = locked_wc.old_tree_id();
367        if wc_commit.tree_id() == wc_tree_id {
368            // The working copy isn't stale, and no need to reload the repo.
369            Ok(Self::Fresh)
370        } else {
371            let wc_operation = repo.loader().load_operation(locked_wc.old_operation_id())?;
372            let repo_operation = repo.operation();
373            let ancestor_op = dag_walk::closest_common_node_ok(
374                [Ok(wc_operation.clone())],
375                [Ok(repo_operation.clone())],
376                |op: &Operation| op.id().clone(),
377                |op: &Operation| op.parents().collect_vec(),
378            )?
379            .expect("unrelated operations");
380            if ancestor_op.id() == repo_operation.id() {
381                // The working copy was updated since we loaded the repo. The repo must be
382                // reloaded at the working copy's operation.
383                Ok(Self::Updated(Box::new(wc_operation)))
384            } else if ancestor_op.id() == wc_operation.id() {
385                // The working copy was not updated when some repo operation committed,
386                // meaning that it's stale compared to the repo view.
387                Ok(Self::WorkingCopyStale)
388            } else {
389                Ok(Self::SiblingOperation)
390            }
391        }
392    }
393}
394
395/// An error while recovering a stale working copy.
396#[derive(Debug, Error)]
397pub enum RecoverWorkspaceError {
398    /// Backend error.
399    #[error(transparent)]
400    Backend(#[from] BackendError),
401    /// Error during checkout.
402    #[error(transparent)]
403    Reset(#[from] ResetError),
404    /// Checkout attempted to modify the root commit.
405    #[error(transparent)]
406    RewriteRootCommit(#[from] RewriteRootCommit),
407    /// Error during transaction.
408    #[error(transparent)]
409    TransactionCommit(#[from] TransactionCommitError),
410    /// Working copy commit is missing.
411    #[error(r#""{}" doesn't have a working-copy commit"#, .0.as_symbol())]
412    WorkspaceMissingWorkingCopy(WorkspaceNameBuf),
413}
414
415/// Recover this workspace to its last known checkout.
416pub fn create_and_check_out_recovery_commit(
417    locked_wc: &mut dyn LockedWorkingCopy,
418    repo: &Arc<ReadonlyRepo>,
419    workspace_name: WorkspaceNameBuf,
420    description: &str,
421) -> Result<(Arc<ReadonlyRepo>, Commit), RecoverWorkspaceError> {
422    let mut tx = repo.start_transaction();
423    let repo_mut = tx.repo_mut();
424
425    let commit_id = repo
426        .view()
427        .get_wc_commit_id(&workspace_name)
428        .ok_or_else(|| {
429            RecoverWorkspaceError::WorkspaceMissingWorkingCopy(workspace_name.clone())
430        })?;
431    let commit = repo.store().get_commit(commit_id)?;
432    let new_commit = repo_mut
433        .new_commit(vec![commit_id.clone()], commit.tree_id().clone())
434        .set_description(description)
435        .write()?;
436    repo_mut.set_wc_commit(workspace_name, new_commit.id().clone())?;
437
438    let repo = tx.commit("recovery commit")?;
439    locked_wc.recover(&new_commit).block_on()?;
440
441    Ok((repo, new_commit))
442}
443
444/// An error while reading the working copy state.
445#[derive(Debug, Error)]
446#[error("{message}")]
447pub struct WorkingCopyStateError {
448    /// Error message.
449    pub message: String,
450    /// The underlying error.
451    #[source]
452    pub err: Box<dyn std::error::Error + Send + Sync>,
453}