2023-10-09 17:27:11 +00:00
|
|
|
// Copyright 2023 The Jujutsu Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! Defines the interface for the working copy. See `LocalWorkingCopy` for the
|
|
|
|
//! default local-disk implementation.
|
|
|
|
|
|
|
|
use std::any::Any;
|
2023-10-12 05:39:20 +00:00
|
|
|
use std::ffi::OsString;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::sync::Arc;
|
2023-10-09 17:27:11 +00:00
|
|
|
|
2023-10-12 05:39:20 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
use crate::backend::{BackendError, MergedTreeId};
|
2023-10-16 17:21:54 +00:00
|
|
|
use crate::commit::Commit;
|
2023-10-12 05:39:20 +00:00
|
|
|
use crate::fsmonitor::FsmonitorKind;
|
2024-02-16 17:54:09 +00:00
|
|
|
use crate::gitignore::{GitIgnoreError, GitIgnoreFile};
|
2023-10-09 17:27:11 +00:00
|
|
|
use crate::op_store::{OperationId, WorkspaceId};
|
2023-11-26 07:12:36 +00:00
|
|
|
use crate::repo_path::{RepoPath, RepoPathBuf};
|
2023-10-12 05:39:20 +00:00
|
|
|
use crate::settings::HumanByteSize;
|
2024-01-25 16:30:11 +00:00
|
|
|
use crate::store::Store;
|
2023-10-09 17:27:11 +00:00
|
|
|
|
|
|
|
/// The trait all working-copy implementations must implement.
|
2024-02-17 02:42:58 +00:00
|
|
|
pub trait WorkingCopy: Send {
|
2023-10-09 17:27:11 +00:00
|
|
|
/// Should return `self`. For down-casting purposes.
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
|
|
|
|
/// The name/id of the implementation. Used for choosing the right
|
|
|
|
/// implementation when loading a working copy.
|
|
|
|
fn name(&self) -> &str;
|
|
|
|
|
|
|
|
/// The working copy's root directory.
|
2023-10-12 21:36:16 +00:00
|
|
|
fn path(&self) -> &Path;
|
2023-10-09 17:27:11 +00:00
|
|
|
|
|
|
|
/// The working copy's workspace ID.
|
|
|
|
fn workspace_id(&self) -> &WorkspaceId;
|
|
|
|
|
|
|
|
/// The operation this working copy was most recently updated to.
|
|
|
|
fn operation_id(&self) -> &OperationId;
|
2023-10-12 16:44:24 +00:00
|
|
|
|
2023-10-14 05:46:28 +00:00
|
|
|
/// The ID of the tree this working copy was most recently updated to.
|
|
|
|
fn tree_id(&self) -> Result<&MergedTreeId, WorkingCopyStateError>;
|
|
|
|
|
2023-10-12 16:44:24 +00:00
|
|
|
/// Patterns that decide which paths from the current tree should be checked
|
|
|
|
/// out in the working copy. An empty list means that no paths should be
|
|
|
|
/// checked out in the working copy. A single `RepoPath::root()` entry means
|
|
|
|
/// that all files should be checked out.
|
2023-11-26 07:12:36 +00:00
|
|
|
fn sparse_patterns(&self) -> Result<&[RepoPathBuf], WorkingCopyStateError>;
|
2023-10-14 05:18:16 +00:00
|
|
|
|
|
|
|
/// Locks the working copy and returns an instance with methods for updating
|
|
|
|
/// the working copy files and state.
|
2023-10-14 06:12:57 +00:00
|
|
|
fn start_mutation(&self) -> Result<Box<dyn LockedWorkingCopy>, WorkingCopyStateError>;
|
2023-10-09 17:27:11 +00:00
|
|
|
}
|
2023-10-11 06:11:15 +00:00
|
|
|
|
2024-01-25 16:30:11 +00:00
|
|
|
/// The factory which creates and loads a specific type of working copy.
|
|
|
|
pub trait WorkingCopyFactory {
|
|
|
|
/// Create a new working copy from scratch.
|
|
|
|
fn init_working_copy(
|
|
|
|
&self,
|
|
|
|
store: Arc<Store>,
|
|
|
|
working_copy_path: PathBuf,
|
|
|
|
state_path: PathBuf,
|
|
|
|
operation_id: OperationId,
|
|
|
|
workspace_id: WorkspaceId,
|
|
|
|
) -> Result<Box<dyn WorkingCopy>, WorkingCopyStateError>;
|
|
|
|
|
|
|
|
/// Load an existing working copy.
|
|
|
|
fn load_working_copy(
|
|
|
|
&self,
|
|
|
|
store: Arc<Store>,
|
|
|
|
working_copy_path: PathBuf,
|
|
|
|
state_path: PathBuf,
|
|
|
|
) -> Box<dyn WorkingCopy>;
|
|
|
|
}
|
|
|
|
|
2023-10-11 06:11:15 +00:00
|
|
|
/// A working copy that's being modified.
|
|
|
|
pub trait LockedWorkingCopy {
|
|
|
|
/// Should return `self`. For down-casting purposes.
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
|
2023-10-14 05:46:28 +00:00
|
|
|
/// Should return `self`. For down-casting purposes.
|
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any;
|
|
|
|
|
2023-10-11 06:11:15 +00:00
|
|
|
/// The operation at the time the lock was taken
|
|
|
|
fn old_operation_id(&self) -> &OperationId;
|
|
|
|
|
|
|
|
/// The tree at the time the lock was taken
|
|
|
|
fn old_tree_id(&self) -> &MergedTreeId;
|
2023-10-12 05:39:20 +00:00
|
|
|
|
|
|
|
/// Snapshot the working copy and return the tree id.
|
|
|
|
fn snapshot(&mut self, options: SnapshotOptions) -> Result<MergedTreeId, SnapshotError>;
|
2023-10-12 13:10:31 +00:00
|
|
|
|
2023-10-16 17:21:54 +00:00
|
|
|
/// Check out the specified commit in the working copy.
|
|
|
|
fn check_out(&mut self, commit: &Commit) -> Result<CheckoutStats, CheckoutError>;
|
2023-10-12 16:13:58 +00:00
|
|
|
|
2024-01-23 18:13:37 +00:00
|
|
|
/// Update to another commit without touching the files in the working copy.
|
|
|
|
fn reset(&mut self, commit: &Commit) -> Result<(), ResetError>;
|
2023-10-12 16:44:24 +00:00
|
|
|
|
workspace: recover from missing operation
If the operation corresponding to a workspace is missing for some reason
(the specific situation in the test in this commit is that an operation
was abandoned and garbage-collected from another workspace), currently,
jj fails with a 255 error code. Teach jj a way to recover from this
situation.
When jj detects such a situation, it prints a message and stops
operation, similar to when a workspace is stale. The message tells the
user what command to run.
When that command is run, jj loads the repo at the @ operation (instead
of the operation of the workspace), creates a new commit on the @
commit with an empty tree, and then proceeds as usual - in particular,
including the auto-snapshotting of the working tree, which creates
another commit that obsoletes the newly created commit.
There are several design points I considered.
1) Whether the recovery should be automatic, or (as in this commit)
manual in that the user should be prompted to run a command. The user
might prefer to recover in another way (e.g. by simply deleting the
workspace) and this situation is (hopefully) rare enough that I think
it's better to prompt the user.
2) Which command the user should be prompted to run (and thus, which
command should be taught to perform the recovery). I chose "workspace
update-stale" because the circumstances are very similar to it: it's
symptom is that the regular jj operation is blocked somewhere at the
beginning, and "workspace update-stale" already does some special work
before the blockage (this commit adds more of such special work). But it
might be better for something more explicitly named, or even a sequence
of commands (e.g. "create a new operation that becomes @ that no
workspace points to", "low-level command that makes a workspace point to
the operation @") but I can see how this can be unnecessarily confusing
for the user.
3) How we recover. I can think of several ways:
a) Always create a commit, and allow the automatic snapshotting to
create another commit that obsoletes this commit.
b) Create a commit but somehow teach the automatic snapshotting to
replace the created commit in-place (so it has no predecessor, as viewed
in "obslog").
c) Do either a) or b), with the added improvement that if there is no
diff between the newly created commit and the former @, to behave as if
no new commit was created (@ remains as the former @).
I chose a) since it was the simplest and most easily reasoned about,
which I think is the best way to go when recovering from a rare
situation.
2024-02-03 05:26:23 +00:00
|
|
|
/// Update to the empty tree without touching the files in the working copy.
|
|
|
|
fn reset_to_empty(&mut self) -> Result<(), ResetError>;
|
|
|
|
|
2023-10-12 16:44:24 +00:00
|
|
|
/// See `WorkingCopy::sparse_patterns()`
|
2023-11-26 07:12:36 +00:00
|
|
|
fn sparse_patterns(&self) -> Result<&[RepoPathBuf], WorkingCopyStateError>;
|
2023-10-12 16:44:24 +00:00
|
|
|
|
|
|
|
/// Updates the patterns that decide which paths from the current tree
|
|
|
|
/// should be checked out in the working copy.
|
|
|
|
// TODO: Use a different error type here so we can include a
|
|
|
|
// `SparseNotSupported` variants for working copies that don't support sparse
|
|
|
|
// checkouts (e.g. because they use a virtual file system so there's no reason
|
|
|
|
// to use sparse).
|
|
|
|
fn set_sparse_patterns(
|
|
|
|
&mut self,
|
2023-11-26 07:12:36 +00:00
|
|
|
new_sparse_patterns: Vec<RepoPathBuf>,
|
2023-10-12 16:44:24 +00:00
|
|
|
) -> Result<CheckoutStats, CheckoutError>;
|
2023-10-14 05:18:16 +00:00
|
|
|
|
|
|
|
/// Finish the modifications to the working copy by writing the updated
|
|
|
|
/// states to disk. Returns the new (unlocked) working copy.
|
2023-10-14 05:46:28 +00:00
|
|
|
fn finish(
|
2023-10-14 06:12:57 +00:00
|
|
|
self: Box<Self>,
|
2023-10-14 05:46:28 +00:00
|
|
|
operation_id: OperationId,
|
|
|
|
) -> Result<Box<dyn WorkingCopy>, WorkingCopyStateError>;
|
2023-10-11 06:11:15 +00:00
|
|
|
}
|
2023-10-12 05:39:20 +00:00
|
|
|
|
|
|
|
/// An error while snapshotting the working copy.
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum SnapshotError {
|
|
|
|
/// A path in the working copy was not valid UTF-8.
|
|
|
|
#[error("Working copy path {} is not valid UTF-8", path.to_string_lossy())]
|
|
|
|
InvalidUtf8Path {
|
|
|
|
/// The path with invalid UTF-8.
|
|
|
|
path: OsString,
|
|
|
|
},
|
|
|
|
/// A symlink target in the working copy was not valid UTF-8.
|
|
|
|
#[error("Symlink {path} target is not valid UTF-8")]
|
|
|
|
InvalidUtf8SymlinkTarget {
|
|
|
|
/// The path of the symlink that has a target that's not valid UTF-8.
|
|
|
|
/// This path itself is valid UTF-8.
|
|
|
|
path: PathBuf,
|
|
|
|
},
|
|
|
|
/// Reading or writing from the commit backend failed.
|
2024-02-03 07:38:03 +00:00
|
|
|
#[error("Internal backend error")]
|
2023-10-12 05:39:20 +00:00
|
|
|
InternalBackendError(#[from] BackendError),
|
|
|
|
/// A file was larger than the specified maximum file size for new
|
|
|
|
/// (previously untracked) files.
|
|
|
|
#[error("New file {path} of size ~{size} exceeds snapshot.max-new-file-size ({max_size})")]
|
|
|
|
NewFileTooLarge {
|
|
|
|
/// The path of the large file.
|
|
|
|
path: PathBuf,
|
|
|
|
/// The size of the large file.
|
|
|
|
size: HumanByteSize,
|
|
|
|
/// The maximum allowed size.
|
|
|
|
max_size: HumanByteSize,
|
|
|
|
},
|
2024-02-16 17:54:09 +00:00
|
|
|
/// Checking path with ignore patterns failed.
|
|
|
|
#[error(transparent)]
|
|
|
|
GitIgnoreError(#[from] GitIgnoreError),
|
2023-10-12 05:39:20 +00:00
|
|
|
/// Some other error happened while snapshotting the working copy.
|
2024-02-03 07:38:03 +00:00
|
|
|
#[error("{message}")]
|
2023-10-12 05:39:20 +00:00
|
|
|
Other {
|
|
|
|
/// Error message.
|
|
|
|
message: String,
|
|
|
|
/// The underlying error.
|
|
|
|
#[source]
|
|
|
|
err: Box<dyn std::error::Error + Send + Sync>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Options used when snapshotting the working copy. Some of them may be ignored
|
|
|
|
/// by some `WorkingCopy` implementations.
|
|
|
|
pub struct SnapshotOptions<'a> {
|
|
|
|
/// The `.gitignore`s to use while snapshotting. The typically come from the
|
|
|
|
/// user's configured patterns combined with per-repo patterns.
|
|
|
|
// The base_ignores are passed in here rather than being set on the TreeState
|
|
|
|
// because the TreeState may be long-lived if the library is used in a
|
|
|
|
// long-lived process.
|
|
|
|
pub base_ignores: Arc<GitIgnoreFile>,
|
|
|
|
/// The fsmonitor (e.g. Watchman) to use, if any.
|
|
|
|
// TODO: Should we make this a field on `LocalWorkingCopy` instead since it's quite specific to
|
|
|
|
// that implementation?
|
2024-02-19 22:00:51 +00:00
|
|
|
pub fsmonitor_kind: FsmonitorKind,
|
2023-10-12 05:39:20 +00:00
|
|
|
/// A callback for the UI to display progress.
|
|
|
|
pub progress: Option<&'a SnapshotProgress<'a>>,
|
|
|
|
/// The size of the largest file that should be allowed to become tracked
|
|
|
|
/// (already tracked files are always snapshotted). If there are larger
|
|
|
|
/// files in the working copy, then `LockedWorkingCopy::snapshot()` may
|
|
|
|
/// (depending on implementation)
|
|
|
|
/// return `SnapshotError::NewFileTooLarge`.
|
|
|
|
pub max_new_file_size: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SnapshotOptions<'_> {
|
|
|
|
/// Create an instance for use in tests.
|
|
|
|
pub fn empty_for_test() -> Self {
|
|
|
|
SnapshotOptions {
|
|
|
|
base_ignores: GitIgnoreFile::empty(),
|
2024-02-19 22:00:51 +00:00
|
|
|
fsmonitor_kind: FsmonitorKind::None,
|
2023-10-12 05:39:20 +00:00
|
|
|
progress: None,
|
|
|
|
max_new_file_size: u64::MAX,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A callback for getting progress updates.
|
|
|
|
pub type SnapshotProgress<'a> = dyn Fn(&RepoPath) + 'a + Sync;
|
2023-10-12 13:10:31 +00:00
|
|
|
|
|
|
|
/// Stats about a checkout operation on a working copy. All "files" mentioned
|
|
|
|
/// below may also be symlinks or materialized conflicts.
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub struct CheckoutStats {
|
|
|
|
/// The number of files that were updated in the working copy.
|
|
|
|
/// These files existed before and after the checkout.
|
|
|
|
pub updated_files: u32,
|
|
|
|
/// The number of files added in the working copy.
|
|
|
|
pub added_files: u32,
|
|
|
|
/// The number of files removed in the working copy.
|
|
|
|
pub removed_files: u32,
|
|
|
|
/// The number of files that were supposed to be updated or added in the
|
|
|
|
/// working copy but were skipped because there was an untracked (probably
|
|
|
|
/// ignored) file in its place.
|
|
|
|
pub skipped_files: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The working-copy checkout failed.
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum CheckoutError {
|
|
|
|
/// The current working-copy commit was deleted, maybe by an overly
|
|
|
|
/// aggressive GC that happened while the current process was running.
|
2024-02-03 07:38:03 +00:00
|
|
|
#[error("Current working-copy commit not found")]
|
2023-10-12 13:10:31 +00:00
|
|
|
SourceNotFound {
|
|
|
|
/// The underlying error.
|
|
|
|
source: Box<dyn std::error::Error + Send + Sync>,
|
|
|
|
},
|
|
|
|
/// Another process checked out a commit while the current process was
|
|
|
|
/// running (after the working copy was read by the current process).
|
|
|
|
#[error("Concurrent checkout")]
|
|
|
|
ConcurrentCheckout,
|
|
|
|
/// Reading or writing from the commit backend failed.
|
2024-02-03 07:38:03 +00:00
|
|
|
#[error("Internal backend error")]
|
2023-10-12 13:10:31 +00:00
|
|
|
InternalBackendError(#[from] BackendError),
|
|
|
|
/// Some other error happened while checking out the working copy.
|
2024-02-03 07:38:03 +00:00
|
|
|
#[error("{message}")]
|
2023-10-12 13:10:31 +00:00
|
|
|
Other {
|
|
|
|
/// Error message.
|
|
|
|
message: String,
|
|
|
|
/// The underlying error.
|
|
|
|
#[source]
|
|
|
|
err: Box<dyn std::error::Error + Send + Sync>,
|
|
|
|
},
|
|
|
|
}
|
2023-10-12 16:13:58 +00:00
|
|
|
|
|
|
|
/// An error while resetting the working copy.
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum ResetError {
|
|
|
|
/// The current working-copy commit was deleted, maybe by an overly
|
|
|
|
/// aggressive GC that happened while the current process was running.
|
2024-02-03 07:38:03 +00:00
|
|
|
#[error("Current working-copy commit not found")]
|
2023-10-12 16:13:58 +00:00
|
|
|
SourceNotFound {
|
|
|
|
/// The underlying error.
|
|
|
|
source: Box<dyn std::error::Error + Send + Sync>,
|
|
|
|
},
|
|
|
|
/// Reading or writing from the commit backend failed.
|
2024-02-03 07:38:03 +00:00
|
|
|
#[error("Internal error")]
|
2023-10-12 16:13:58 +00:00
|
|
|
InternalBackendError(#[from] BackendError),
|
|
|
|
/// Some other error happened while checking out the working copy.
|
2024-02-03 07:38:03 +00:00
|
|
|
#[error("{message}")]
|
2023-10-12 16:13:58 +00:00
|
|
|
Other {
|
|
|
|
/// Error message.
|
|
|
|
message: String,
|
|
|
|
/// The underlying error.
|
2024-02-03 07:38:03 +00:00
|
|
|
#[source]
|
2023-10-12 16:13:58 +00:00
|
|
|
err: Box<dyn std::error::Error + Send + Sync>,
|
|
|
|
},
|
|
|
|
}
|
2023-10-12 16:44:24 +00:00
|
|
|
|
|
|
|
/// An error while reading the working copy state.
|
|
|
|
#[derive(Debug, Error)]
|
2024-02-03 07:38:03 +00:00
|
|
|
#[error("{message}")]
|
2023-10-12 16:44:24 +00:00
|
|
|
pub struct WorkingCopyStateError {
|
|
|
|
/// Error message.
|
|
|
|
pub message: String,
|
|
|
|
/// The underlying error.
|
2024-02-03 07:38:03 +00:00
|
|
|
#[source]
|
2023-10-12 16:44:24 +00:00
|
|
|
pub err: Box<dyn std::error::Error + Send + Sync>,
|
|
|
|
}
|