ok/jj
1
0
Fork 0
forked from mirrors/jj

working copy: start defining a working copy trait

This just extracts a trait for the trivial bits to start with.
This commit is contained in:
Martin von Zweigbergk 2023-10-09 10:27:11 -07:00 committed by Martin von Zweigbergk
parent 69a30b47af
commit 33d27ed09f
7 changed files with 69 additions and 12 deletions

View file

@ -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;

View file

@ -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;

View file

@ -67,4 +67,5 @@ pub mod transaction;
pub mod tree;
pub mod tree_builder;
pub mod view;
pub mod working_copy;
pub mod workspace;

View file

@ -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<TreeState>,
}
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(|| {

40
lib/src/working_copy.rs Normal file
View file

@ -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;
}

View file

@ -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 {

View file

@ -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]