From 33d27ed09ff76b9bdb07e058cf6859c53c0a32dd Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Mon, 9 Oct 2023 10:27:11 -0700 Subject: [PATCH] working copy: start defining a working copy trait This just extracts a trait for the trivial bits to start with. --- cli/src/cli_util.rs | 1 + cli/src/commands/debug.rs | 1 + lib/src/lib.rs | 1 + lib/src/local_working_copy.rs | 36 ++++++++++++------- lib/src/working_copy.rs | 40 +++++++++++++++++++++ lib/src/workspace.rs | 1 + lib/tests/test_local_working_copy_sparse.rs | 1 + 7 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 lib/src/working_copy.rs diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index a851688c8..29df46758 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -63,6 +63,7 @@ use jj_lib::revset::{ use jj_lib::settings::{ConfigResultExt as _, UserSettings}; use jj_lib::transaction::Transaction; use jj_lib::tree::TreeMergeError; +use jj_lib::working_copy::WorkingCopy; use jj_lib::workspace::{Workspace, WorkspaceInitError, WorkspaceLoadError, WorkspaceLoader}; use jj_lib::{dag_walk, file_util, git, revset}; use once_cell::unsync::OnceCell; diff --git a/cli/src/commands/debug.rs b/cli/src/commands/debug.rs index fee67faf2..6952d6e34 100644 --- a/cli/src/commands/debug.rs +++ b/cli/src/commands/debug.rs @@ -19,6 +19,7 @@ use clap::Subcommand; use jj_lib::backend::ObjectId; use jj_lib::default_index_store::{DefaultIndexStore, ReadonlyIndexWrapper}; use jj_lib::revset; +use jj_lib::working_copy::WorkingCopy; use crate::cli_util::{resolve_op_for_load, user_error, CommandError, CommandHelper}; use crate::template_parser; diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 8802683a4..303bbbb0f 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -67,4 +67,5 @@ pub mod transaction; pub mod tree; pub mod tree_builder; pub mod view; +pub mod working_copy; pub mod workspace; diff --git a/lib/src/local_working_copy.rs b/lib/src/local_working_copy.rs index 02c0b11a1..7540832dd 100644 --- a/lib/src/local_working_copy.rs +++ b/lib/src/local_working_copy.rs @@ -14,6 +14,7 @@ #![allow(missing_docs)] +use std::any::Any; use std::collections::{BTreeMap, HashSet}; use std::error::Error; use std::ffi::OsString; @@ -58,6 +59,7 @@ use crate::repo_path::{FsPathParseError, RepoPath, RepoPathComponent, RepoPathJo use crate::settings::HumanByteSize; use crate::store::Store; use crate::tree::Tree; +use crate::working_copy::WorkingCopy; #[cfg(unix)] type FileExecutableFlag = bool; @@ -1373,6 +1375,28 @@ pub struct LocalWorkingCopy { tree_state: OnceCell, } +impl WorkingCopy for LocalWorkingCopy { + fn as_any(&self) -> &dyn Any { + self + } + + fn name(&self) -> &str { + "local" + } + + fn working_copy_path(&self) -> &Path { + &self.working_copy_path + } + + fn workspace_id(&self) -> &WorkspaceId { + &self.checkout_state().workspace_id + } + + fn operation_id(&self) -> &OperationId { + &self.checkout_state().operation_id + } +} + impl LocalWorkingCopy { /// Initializes a new working copy at `working_copy_path`. The working /// copy's state will be stored in the `state_path` directory. The working @@ -1420,10 +1444,6 @@ impl LocalWorkingCopy { } } - pub fn working_copy_path(&self) -> &Path { - &self.working_copy_path - } - pub fn state_path(&self) -> &Path { &self.state_path } @@ -1461,14 +1481,6 @@ impl LocalWorkingCopy { self.checkout_state.get_mut().unwrap() } - pub fn operation_id(&self) -> &OperationId { - &self.checkout_state().operation_id - } - - pub fn workspace_id(&self) -> &WorkspaceId { - &self.checkout_state().workspace_id - } - #[instrument(skip_all)] fn tree_state(&self) -> Result<&TreeState, TreeStateError> { self.tree_state.get_or_try_init(|| { diff --git a/lib/src/working_copy.rs b/lib/src/working_copy.rs new file mode 100644 index 000000000..0f0715c7e --- /dev/null +++ b/lib/src/working_copy.rs @@ -0,0 +1,40 @@ +// 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; +use std::path::Path; + +use crate::op_store::{OperationId, WorkspaceId}; + +/// The trait all working-copy implementations must implement. +pub trait WorkingCopy { + /// 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. + fn working_copy_path(&self) -> &Path; + + /// 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; +} diff --git a/lib/src/workspace.rs b/lib/src/workspace.rs index 8c1e2b10c..a8175f54b 100644 --- a/lib/src/workspace.rs +++ b/lib/src/workspace.rs @@ -36,6 +36,7 @@ use crate::repo::{ }; use crate::settings::UserSettings; use crate::submodule_store::SubmoduleStore; +use crate::working_copy::WorkingCopy; #[derive(Error, Debug)] pub enum WorkspaceInitError { diff --git a/lib/tests/test_local_working_copy_sparse.rs b/lib/tests/test_local_working_copy_sparse.rs index 22a36d140..fb138830b 100644 --- a/lib/tests/test_local_working_copy_sparse.rs +++ b/lib/tests/test_local_working_copy_sparse.rs @@ -17,6 +17,7 @@ use jj_lib::local_working_copy::{CheckoutStats, LocalWorkingCopy}; use jj_lib::matchers::EverythingMatcher; use jj_lib::repo::Repo; use jj_lib::repo_path::RepoPath; +use jj_lib::working_copy::WorkingCopy; use testutils::{create_tree, TestWorkspace}; #[test]