ok/jj
1
0
Fork 0
forked from mirrors/jj
jj/examples/custom-backend/main.rs
Kevin Liao 86b6a11e63 Fix jj init --git-repo fails and leaves broken .jj folder
This commit fixes #1305

Before this commit, running `jj init --git-repo=./` in a folder that
does not have a .git would cause jj to panick and leave an unfinished corrupted jj repo.

This commit fixes that by changing the call chain to return an error
instead of calling .unwrap() and panicking. This commit also adds logic to delete the unfinished jj
repository when the git backend initialization failed.

Before this commit, running the above command would result in the following
```
Running `jj/target/debug/jj init --git-repo=./`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { code: -3, klass: 2, message: "failed to resolve path '/Users/kevincliao/github/jj/test-repo/.jj/repo/store/../../../.git': No such file or directory" }', lib/src/git_backend.rs:83:75
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```

After this commit, the result is the following and the jj repo is deleted:
```
Running `jj/target/debug/jj init --git-repo=./`
Error: Failed to access the repository: Error: Failed to open git repository: failed to resolve path '/Users/kevincliao/github/jj/test-repo/.jj/repo/store/../../../.git': No such file or directory; class=Os (2); code=NotFound (-3)
```
2023-06-20 11:02:06 -07:00

160 lines
4.5 KiB
Rust

// Copyright 2022 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.
use std::any::Any;
use std::io::Read;
use std::path::Path;
use jujutsu::cli_util::{CliRunner, CommandError, CommandHelper};
use jujutsu::ui::Ui;
use jujutsu_lib::backend::{
Backend, BackendResult, ChangeId, Commit, CommitId, Conflict, ConflictId, FileId, SymlinkId,
Tree, TreeId,
};
use jujutsu_lib::git_backend::GitBackend;
use jujutsu_lib::repo::StoreFactories;
use jujutsu_lib::repo_path::RepoPath;
use jujutsu_lib::workspace::Workspace;
#[derive(clap::Parser, Clone, Debug)]
enum CustomCommands {
/// Initialize a workspace using the Jit backend
InitJit,
}
fn create_store_factories() -> StoreFactories {
let mut store_factories = StoreFactories::default();
// Register the backend so it can be loaded when the repo is loaded. The name
// must match `Backend::name()`.
store_factories.add_backend(
"jit",
Box::new(|store_path| Box::new(JitBackend::load(store_path))),
);
store_factories
}
fn run_custom_command(
_ui: &mut Ui,
command_helper: &CommandHelper,
command: CustomCommands,
) -> Result<(), CommandError> {
match command {
CustomCommands::InitJit => {
let wc_path = command_helper.cwd();
// Initialize a workspace with the custom backend
Workspace::init_with_backend(command_helper.settings(), wc_path, |store_path| {
Ok(Box::new(JitBackend::init(store_path)))
})?;
Ok(())
}
}
}
fn main() -> std::process::ExitCode {
CliRunner::init()
.set_store_factories(create_store_factories())
.add_subcommand(run_custom_command)
.run()
}
/// A commit backend that's extremely similar to the Git backend
#[derive(Debug)]
struct JitBackend {
inner: GitBackend,
}
impl JitBackend {
fn init(store_path: &Path) -> Self {
JitBackend {
inner: GitBackend::init_internal(store_path),
}
}
fn load(store_path: &Path) -> Self {
JitBackend {
inner: GitBackend::load(store_path),
}
}
}
impl Backend for JitBackend {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"jit"
}
fn commit_id_length(&self) -> usize {
self.inner.commit_id_length()
}
fn change_id_length(&self) -> usize {
self.inner.change_id_length()
}
fn read_file(&self, path: &RepoPath, id: &FileId) -> BackendResult<Box<dyn Read>> {
self.inner.read_file(path, id)
}
fn write_file(&self, path: &RepoPath, contents: &mut dyn Read) -> BackendResult<FileId> {
self.inner.write_file(path, contents)
}
fn read_symlink(&self, path: &RepoPath, id: &SymlinkId) -> BackendResult<String> {
self.inner.read_symlink(path, id)
}
fn write_symlink(&self, path: &RepoPath, target: &str) -> BackendResult<SymlinkId> {
self.inner.write_symlink(path, target)
}
fn root_commit_id(&self) -> &CommitId {
self.inner.root_commit_id()
}
fn root_change_id(&self) -> &ChangeId {
self.inner.root_change_id()
}
fn empty_tree_id(&self) -> &TreeId {
self.inner.empty_tree_id()
}
fn read_tree(&self, path: &RepoPath, id: &TreeId) -> BackendResult<Tree> {
self.inner.read_tree(path, id)
}
fn write_tree(&self, path: &RepoPath, contents: &Tree) -> BackendResult<TreeId> {
self.inner.write_tree(path, contents)
}
fn read_conflict(&self, path: &RepoPath, id: &ConflictId) -> BackendResult<Conflict> {
self.inner.read_conflict(path, id)
}
fn write_conflict(&self, path: &RepoPath, contents: &Conflict) -> BackendResult<ConflictId> {
self.inner.write_conflict(path, contents)
}
fn read_commit(&self, id: &CommitId) -> BackendResult<Commit> {
self.inner.read_commit(id)
}
fn write_commit(&self, contents: Commit) -> BackendResult<(CommitId, Commit)> {
self.inner.write_commit(contents)
}
}