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.
|
|
|
|
|
2022-01-30 19:58:05 +00:00
|
|
|
use std::fs::File;
|
2022-01-30 19:58:05 +00:00
|
|
|
use std::io::{Read, Write};
|
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;
|
|
|
|
|
2022-07-13 05:04:04 +00:00
|
|
|
use crate::backend::Backend;
|
|
|
|
use crate::git_backend::GitBackend;
|
|
|
|
use crate::local_backend::LocalBackend;
|
2021-11-17 21:06:02 +00:00
|
|
|
use crate::op_store::WorkspaceId;
|
2022-09-23 21:50:19 +00:00
|
|
|
use crate::repo::{BackendFactories, 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
|
|
|
|
2022-08-10 04:05:38 +00:00
|
|
|
#[derive(Error, Debug, PartialEq, Eq)]
|
2021-11-24 07:54:30 +00:00
|
|
|
pub enum WorkspaceInitError {
|
|
|
|
#[error("The destination repo ({0}) already exists")]
|
|
|
|
DestinationExists(PathBuf),
|
|
|
|
}
|
|
|
|
|
2022-08-10 04:05:38 +00:00
|
|
|
#[derive(Error, Debug, PartialEq, Eq)]
|
2021-11-21 07:03:54 +00:00
|
|
|
pub enum WorkspaceLoadError {
|
2022-01-30 19:58:05 +00:00
|
|
|
#[error("The repo appears to no longer be at {0}")]
|
|
|
|
RepoDoesNotExist(PathBuf),
|
2021-11-21 07:03:54 +00:00
|
|
|
#[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(
|
2022-01-30 20:27:18 +00:00
|
|
|
user_settings: &UserSettings,
|
2021-11-24 08:18:21 +00:00
|
|
|
repo: &Arc<ReadonlyRepo>,
|
|
|
|
workspace_root: &Path,
|
|
|
|
jj_dir: &Path,
|
2022-01-30 19:58:05 +00:00
|
|
|
workspace_id: WorkspaceId,
|
2022-01-30 20:27:18 +00:00
|
|
|
) -> (WorkingCopy, Arc<ReadonlyRepo>) {
|
2022-01-29 03:39:42 +00:00
|
|
|
let working_copy_state_path = jj_dir.join("working_copy");
|
|
|
|
std::fs::create_dir(&working_copy_state_path).unwrap();
|
2022-01-30 20:27:18 +00:00
|
|
|
|
|
|
|
let mut tx = repo.start_transaction(&format!("add workspace '{}'", workspace_id.as_str()));
|
2022-02-13 01:22:54 +00:00
|
|
|
tx.mut_repo().check_out(
|
2022-01-30 20:27:18 +00:00
|
|
|
workspace_id.clone(),
|
|
|
|
user_settings,
|
|
|
|
&repo.store().root_commit(),
|
|
|
|
);
|
|
|
|
let repo = tx.commit();
|
|
|
|
|
|
|
|
let working_copy = WorkingCopy::init(
|
2021-11-24 08:18:21 +00:00
|
|
|
repo.store().clone(),
|
|
|
|
workspace_root.to_path_buf(),
|
2022-01-29 03:39:42 +00:00
|
|
|
working_copy_state_path,
|
2021-11-27 06:33:36 +00:00
|
|
|
repo.op_id().clone(),
|
2021-11-17 21:06:02 +00:00
|
|
|
workspace_id,
|
2022-01-30 20:27:18 +00:00
|
|
|
);
|
|
|
|
(working_copy, repo)
|
2021-11-24 08:18:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 05:58:36 +00:00
|
|
|
impl Workspace {
|
2022-09-23 21:03:43 +00:00
|
|
|
fn new(workspace_root: &Path, working_copy: WorkingCopy, repo_loader: RepoLoader) -> Workspace {
|
2022-03-30 05:14:39 +00:00
|
|
|
let workspace_root = workspace_root.canonicalize().unwrap();
|
|
|
|
Workspace {
|
|
|
|
workspace_root,
|
|
|
|
repo_loader,
|
|
|
|
working_copy,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 06:55:31 +00:00
|
|
|
pub fn init_local(
|
|
|
|
user_settings: &UserSettings,
|
2022-09-23 21:03:43 +00:00
|
|
|
workspace_root: &Path,
|
2021-11-24 07:54:30 +00:00
|
|
|
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
|
2022-07-13 05:04:04 +00:00
|
|
|
Self::init_with_backend(user_settings, workspace_root, |store_path| {
|
|
|
|
Box::new(LocalBackend::init(store_path))
|
|
|
|
})
|
2021-11-22 06:55:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-24 00:56:11 +00:00
|
|
|
/// Initializes a workspace with a new Git backend in .jj/git/ (bare Git
|
|
|
|
/// repo)
|
2021-11-22 06:55:31 +00:00
|
|
|
pub fn init_internal_git(
|
|
|
|
user_settings: &UserSettings,
|
2022-09-23 21:03:43 +00:00
|
|
|
workspace_root: &Path,
|
2021-11-24 07:54:30 +00:00
|
|
|
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
|
2022-07-13 05:04:04 +00:00
|
|
|
Self::init_with_backend(user_settings, workspace_root, |store_path| {
|
|
|
|
Box::new(GitBackend::init_internal(store_path))
|
|
|
|
})
|
2021-11-22 06:55:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-24 00:56:11 +00:00
|
|
|
/// Initializes a workspace with an existing Git backend at the specified
|
|
|
|
/// path
|
2021-11-22 06:55:31 +00:00
|
|
|
pub fn init_external_git(
|
|
|
|
user_settings: &UserSettings,
|
2022-09-23 21:03:43 +00:00
|
|
|
workspace_root: &Path,
|
|
|
|
git_repo_path: &Path,
|
2022-07-13 05:04:04 +00:00
|
|
|
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
|
|
|
|
Self::init_with_backend(user_settings, workspace_root, |store_path| {
|
2022-09-23 21:03:43 +00:00
|
|
|
Box::new(GitBackend::init_external(store_path, git_repo_path))
|
2022-07-13 05:04:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init_with_backend(
|
|
|
|
user_settings: &UserSettings,
|
2022-09-23 21:03:43 +00:00
|
|
|
workspace_root: &Path,
|
|
|
|
backend_factory: impl FnOnce(&Path) -> Box<dyn Backend>,
|
2021-11-24 07:54:30 +00:00
|
|
|
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
|
2022-09-23 21:03:43 +00:00
|
|
|
let jj_dir = create_jj_dir(workspace_root)?;
|
2022-01-29 04:26:16 +00:00
|
|
|
let repo_dir = jj_dir.join("repo");
|
|
|
|
std::fs::create_dir(&repo_dir).unwrap();
|
2022-09-23 21:03:43 +00:00
|
|
|
let repo = ReadonlyRepo::init(user_settings, &repo_dir, backend_factory);
|
2022-01-30 19:58:05 +00:00
|
|
|
let (working_copy, repo) = init_working_copy(
|
|
|
|
user_settings,
|
|
|
|
&repo,
|
2022-09-23 21:03:43 +00:00
|
|
|
workspace_root,
|
2022-01-30 19:58:05 +00:00
|
|
|
&jj_dir,
|
|
|
|
WorkspaceId::default(),
|
|
|
|
);
|
2021-11-22 06:55:31 +00:00
|
|
|
let repo_loader = repo.loader();
|
2022-03-30 05:14:39 +00:00
|
|
|
let workspace = Workspace::new(workspace_root, working_copy, repo_loader);
|
2021-11-22 06:55:31 +00:00
|
|
|
Ok((workspace, repo))
|
|
|
|
}
|
|
|
|
|
2022-01-30 19:58:05 +00:00
|
|
|
pub fn init_workspace_with_existing_repo(
|
|
|
|
user_settings: &UserSettings,
|
2022-09-23 21:03:43 +00:00
|
|
|
workspace_root: &Path,
|
2022-01-30 19:58:05 +00:00
|
|
|
repo: &Arc<ReadonlyRepo>,
|
|
|
|
workspace_id: WorkspaceId,
|
|
|
|
) -> Result<(Self, Arc<ReadonlyRepo>), WorkspaceInitError> {
|
2022-09-23 21:03:43 +00:00
|
|
|
let jj_dir = create_jj_dir(workspace_root)?;
|
2022-01-30 19:58:05 +00:00
|
|
|
|
2022-03-30 06:13:13 +00:00
|
|
|
let repo_dir = repo.repo_path().canonicalize().unwrap();
|
2022-01-30 19:58:05 +00:00
|
|
|
let mut repo_file = File::create(jj_dir.join("repo")).unwrap();
|
|
|
|
repo_file
|
|
|
|
.write_all(repo_dir.to_str().unwrap().as_bytes())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let (working_copy, repo) =
|
2022-09-23 21:03:43 +00:00
|
|
|
init_working_copy(user_settings, repo, workspace_root, &jj_dir, workspace_id);
|
2022-09-24 00:16:00 +00:00
|
|
|
let workspace = Workspace::new(workspace_root, working_copy, repo.loader());
|
2022-01-30 19:58:05 +00:00
|
|
|
Ok((workspace, repo))
|
|
|
|
}
|
|
|
|
|
2021-11-18 05:58:36 +00:00
|
|
|
pub fn load(
|
|
|
|
user_settings: &UserSettings,
|
2022-09-23 21:03:43 +00:00
|
|
|
workspace_path: &Path,
|
2021-11-21 07:03:54 +00:00
|
|
|
) -> Result<Self, WorkspaceLoadError> {
|
2022-09-23 21:03:43 +00:00
|
|
|
let jj_dir = find_jj_dir(workspace_path)
|
|
|
|
.ok_or_else(|| WorkspaceLoadError::NoWorkspaceHere(workspace_path.to_owned()))?;
|
2022-01-29 03:47:38 +00:00
|
|
|
let workspace_root = jj_dir.parent().unwrap().to_owned();
|
2022-01-30 19:58:05 +00:00
|
|
|
let mut repo_dir = jj_dir.join("repo");
|
|
|
|
// If .jj/repo is a file, then we interpret its contents as a relative path to
|
|
|
|
// the actual repo directory (typically in another workspace).
|
|
|
|
if repo_dir.is_file() {
|
|
|
|
let mut repo_file = File::open(repo_dir).unwrap();
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
repo_file.read_to_end(&mut buf).unwrap();
|
|
|
|
let repo_path_str = String::from_utf8(buf).unwrap();
|
2022-03-30 06:13:13 +00:00
|
|
|
repo_dir = jj_dir.join(repo_path_str).canonicalize().unwrap();
|
2022-01-30 19:58:05 +00:00
|
|
|
if !repo_dir.is_dir() {
|
|
|
|
return Err(WorkspaceLoadError::RepoDoesNotExist(repo_dir));
|
|
|
|
}
|
|
|
|
}
|
2022-09-23 21:50:19 +00:00
|
|
|
let repo_loader = RepoLoader::init(user_settings, &repo_dir, &BackendFactories::default());
|
2022-01-29 04:26:16 +00:00
|
|
|
let working_copy_state_path = jj_dir.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,
|
|
|
|
);
|
2022-09-23 21:03:43 +00:00
|
|
|
Ok(Workspace::new(&workspace_root, working_copy, repo_loader))
|
2021-11-18 05:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn workspace_root(&self) -> &PathBuf {
|
|
|
|
&self.workspace_root
|
|
|
|
}
|
|
|
|
|
2021-11-17 21:06:02 +00:00
|
|
|
pub fn workspace_id(&self) -> WorkspaceId {
|
|
|
|
self.working_copy.workspace_id()
|
|
|
|
}
|
|
|
|
|
2021-11-18 05:58:36 +00:00
|
|
|
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
|
|
|
|
2022-01-29 03:47:38 +00:00
|
|
|
fn find_jj_dir(mut workspace_root: &Path) -> Option<PathBuf> {
|
2021-11-21 07:03:54 +00:00
|
|
|
loop {
|
2022-01-29 03:47:38 +00:00
|
|
|
let jj_path = workspace_root.join(".jj");
|
|
|
|
if jj_path.is_dir() {
|
|
|
|
return Some(jj_path);
|
2021-11-21 07:03:54 +00:00
|
|
|
}
|
2022-04-23 01:01:49 +00:00
|
|
|
workspace_root = workspace_root.parent()?;
|
2021-11-21 07:03:54 +00:00
|
|
|
}
|
|
|
|
}
|