2021-11-18 05:58:36 +00:00
|
|
|
// Copyright 2021 Google LLC
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2021-11-21 07:03:54 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-11-22 06:55:31 +00:00
|
|
|
use std::sync::Arc;
|
2021-11-18 05:58:36 +00:00
|
|
|
|
2021-11-21 07:03:54 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2021-11-24 07:54:30 +00:00
|
|
|
use crate::repo::{ReadonlyRepo, RepoLoader};
|
2021-11-18 05:58:36 +00:00
|
|
|
use crate::settings::UserSettings;
|
2021-11-17 17:19:41 +00:00
|
|
|
use crate::working_copy::WorkingCopy;
|
2021-11-18 05:58:36 +00:00
|
|
|
|
2021-11-24 07:54:30 +00:00
|
|
|
#[derive(Error, Debug, PartialEq)]
|
|
|
|
pub enum WorkspaceInitError {
|
|
|
|
#[error("The destination repo ({0}) already exists")]
|
|
|
|
DestinationExists(PathBuf),
|
|
|
|
}
|
|
|
|
|
2021-11-21 07:03:54 +00:00
|
|
|
#[derive(Error, Debug, PartialEq)]
|
|
|
|
pub enum WorkspaceLoadError {
|
|
|
|
#[error("There is no Jujutsu repo in {0}")]
|
|
|
|
NoWorkspaceHere(PathBuf),
|
|
|
|
}
|
|
|
|
|
2021-11-18 05:58:36 +00:00
|
|
|
/// Represents a workspace, i.e. what's typically the .jj/ directory and its
|
|
|
|
/// parent.
|
|
|
|
pub struct Workspace {
|
|
|
|
// Path to the workspace root (typically the parent of a .jj/ directory), which is where
|
|
|
|
// working copy files live.
|
|
|
|
workspace_root: PathBuf,
|
|
|
|
repo_loader: RepoLoader,
|
2021-11-17 17:19:41 +00:00
|
|
|
working_copy: WorkingCopy,
|
2021-11-18 05:58:36 +00:00
|
|
|
}
|
|
|
|
|
2021-11-24 07:54:30 +00:00
|
|
|
fn create_jj_dir(workspace_root: &Path) -> Result<PathBuf, WorkspaceInitError> {
|
|
|
|
let jj_dir = workspace_root.join(".jj");
|
|
|
|
if jj_dir.exists() {
|
|
|
|
Err(WorkspaceInitError::DestinationExists(jj_dir))
|
|
|
|
} else {
|
|
|
|
std::fs::create_dir(&jj_dir).unwrap();
|
|
|
|
Ok(jj_dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 08:18:21 +00:00
|
|
|
fn init_working_copy(
|
|
|
|
repo: &Arc<ReadonlyRepo>,
|
|
|
|
workspace_root: &Path,
|
|
|
|
jj_dir: &Path,
|
|
|
|
) -> WorkingCopy {
|
2021-11-26 06:02:17 +00:00
|
|
|
WorkingCopy::init(
|
2021-11-24 08:18:21 +00:00
|
|
|
repo.store().clone(),
|
|
|
|
workspace_root.to_path_buf(),
|
|
|
|
jj_dir.join("working_copy"),
|
2021-11-26 06:02:17 +00:00
|
|
|
repo.view().checkout().clone(),
|
|
|
|
)
|
2021-11-24 08:18:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 05:58:36 +00:00
|
|
|
impl Workspace {
|
2021-11-22 06:55:31 +00:00
|
|
|
pub fn init_local(
|
|
|
|
user_settings: &UserSettings,
|
|
|
|
workspace_root: PathBuf,
|
2021-11-24 07:54:30 +00:00
|
|
|
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
|
|
|
|
let jj_dir = create_jj_dir(&workspace_root)?;
|
2021-11-24 08:18:21 +00:00
|
|
|
let repo = ReadonlyRepo::init_local(user_settings, jj_dir.clone());
|
|
|
|
let working_copy = init_working_copy(&repo, &workspace_root, &jj_dir);
|
2021-11-22 06:55:31 +00:00
|
|
|
let repo_loader = repo.loader();
|
2021-11-24 08:18:21 +00:00
|
|
|
let workspace = Workspace {
|
|
|
|
workspace_root,
|
|
|
|
repo_loader,
|
|
|
|
working_copy,
|
|
|
|
};
|
2021-11-22 06:55:31 +00:00
|
|
|
Ok((workspace, repo))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init_internal_git(
|
|
|
|
user_settings: &UserSettings,
|
|
|
|
workspace_root: PathBuf,
|
2021-11-24 07:54:30 +00:00
|
|
|
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
|
|
|
|
let jj_dir = create_jj_dir(&workspace_root)?;
|
2021-11-24 08:18:21 +00:00
|
|
|
let repo = ReadonlyRepo::init_internal_git(user_settings, jj_dir.clone());
|
|
|
|
let working_copy = init_working_copy(&repo, &workspace_root, &jj_dir);
|
2021-11-22 06:55:31 +00:00
|
|
|
let repo_loader = repo.loader();
|
2021-11-24 08:18:21 +00:00
|
|
|
let workspace = Workspace {
|
|
|
|
workspace_root,
|
|
|
|
repo_loader,
|
|
|
|
working_copy,
|
|
|
|
};
|
2021-11-22 06:55:31 +00:00
|
|
|
Ok((workspace, repo))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init_external_git(
|
|
|
|
user_settings: &UserSettings,
|
|
|
|
workspace_root: PathBuf,
|
|
|
|
git_repo_path: PathBuf,
|
2021-11-24 07:54:30 +00:00
|
|
|
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
|
|
|
|
let jj_dir = create_jj_dir(&workspace_root)?;
|
2021-11-24 08:18:21 +00:00
|
|
|
let repo = ReadonlyRepo::init_external_git(user_settings, jj_dir.clone(), git_repo_path);
|
|
|
|
let working_copy = init_working_copy(&repo, &workspace_root, &jj_dir);
|
2021-11-22 06:55:31 +00:00
|
|
|
let repo_loader = repo.loader();
|
2021-11-24 08:18:21 +00:00
|
|
|
let workspace = Workspace {
|
|
|
|
workspace_root,
|
|
|
|
repo_loader,
|
|
|
|
working_copy,
|
|
|
|
};
|
2021-11-22 06:55:31 +00:00
|
|
|
Ok((workspace, repo))
|
|
|
|
}
|
|
|
|
|
2021-11-18 05:58:36 +00:00
|
|
|
pub fn load(
|
|
|
|
user_settings: &UserSettings,
|
2021-11-21 07:03:54 +00:00
|
|
|
workspace_path: PathBuf,
|
|
|
|
) -> Result<Self, WorkspaceLoadError> {
|
|
|
|
let repo_path = find_repo_dir(&workspace_path)
|
|
|
|
.ok_or(WorkspaceLoadError::NoWorkspaceHere(workspace_path))?;
|
|
|
|
let workspace_root = repo_path.parent().unwrap().to_owned();
|
|
|
|
let repo_loader = RepoLoader::init(user_settings, repo_path);
|
2021-11-22 06:55:31 +00:00
|
|
|
let working_copy_state_path = repo_loader.repo_path().join("working_copy");
|
2021-11-17 17:19:41 +00:00
|
|
|
let working_copy = WorkingCopy::load(
|
|
|
|
repo_loader.store().clone(),
|
|
|
|
workspace_root.clone(),
|
|
|
|
working_copy_state_path,
|
|
|
|
);
|
2021-11-24 08:18:21 +00:00
|
|
|
Ok(Self {
|
2021-11-18 05:58:36 +00:00
|
|
|
workspace_root,
|
|
|
|
repo_loader,
|
2021-11-17 17:19:41 +00:00
|
|
|
working_copy,
|
2021-11-24 08:18:21 +00:00
|
|
|
})
|
2021-11-18 05:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn workspace_root(&self) -> &PathBuf {
|
|
|
|
&self.workspace_root
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn repo_path(&self) -> &PathBuf {
|
|
|
|
self.repo_loader.repo_path()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn repo_loader(&self) -> &RepoLoader {
|
|
|
|
&self.repo_loader
|
|
|
|
}
|
2021-11-17 17:19:41 +00:00
|
|
|
|
|
|
|
pub fn working_copy(&self) -> &WorkingCopy {
|
|
|
|
&self.working_copy
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn working_copy_mut(&mut self) -> &mut WorkingCopy {
|
|
|
|
&mut self.working_copy
|
|
|
|
}
|
2021-11-18 05:58:36 +00:00
|
|
|
}
|
2021-11-21 07:03:54 +00:00
|
|
|
|
|
|
|
fn find_repo_dir(mut workspace_root: &Path) -> Option<PathBuf> {
|
|
|
|
loop {
|
|
|
|
let repo_path = workspace_root.join(".jj");
|
|
|
|
if repo_path.is_dir() {
|
|
|
|
return Some(repo_path);
|
|
|
|
}
|
|
|
|
if let Some(wc_dir_parent) = workspace_root.parent() {
|
|
|
|
workspace_root = wc_dir_parent;
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|