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

repo: propagate I/O errors gracefully from ReadonlyRepo::init

This commit is contained in:
Benjamin Saunders 2022-10-27 21:27:53 -07:00
parent cfa46c50e2
commit 3d1ac8b933
4 changed files with 19 additions and 13 deletions

View file

@ -114,18 +114,19 @@ impl ReadonlyRepo {
user_settings: &UserSettings,
repo_path: &Path,
backend_factory: impl FnOnce(&Path) -> Box<dyn Backend>,
) -> Arc<ReadonlyRepo> {
let repo_path = repo_path.canonicalize().unwrap();
) -> Result<Arc<ReadonlyRepo>, PathError> {
let repo_path = repo_path.canonicalize().context(repo_path)?;
let store_path = repo_path.join("store");
fs::create_dir(&store_path).unwrap();
fs::create_dir(&store_path).context(&store_path)?;
let backend = backend_factory(&store_path);
fs::write(&store_path.join("backend"), backend.name()).unwrap();
let backend_path = store_path.join("backend");
fs::write(&backend_path, backend.name()).context(&backend_path)?;
let store = Store::new(backend);
let repo_settings = user_settings.with_repo(&repo_path).unwrap();
let op_store_path = repo_path.join("op_store");
fs::create_dir(&op_store_path).unwrap();
fs::create_dir(&op_store_path).context(&op_store_path)?;
let op_store: Arc<dyn OpStore> = Arc::new(SimpleOpStore::init(op_store_path));
let mut root_view = op_store::View::default();
root_view.head_ids.insert(store.root_commit_id().clone());
@ -134,16 +135,16 @@ impl ReadonlyRepo {
.insert(store.root_commit_id().clone());
let op_heads_path = repo_path.join("op_heads");
fs::create_dir(&op_heads_path).unwrap();
fs::create_dir(&op_heads_path).context(&op_heads_path)?;
let (op_heads_store, init_op) = OpHeadsStore::init(op_heads_path, &op_store, &root_view);
let op_heads_store = Arc::new(op_heads_store);
let index_path = repo_path.join("index");
fs::create_dir(&index_path).unwrap();
fs::create_dir(&index_path).context(&index_path)?;
let index_store = Arc::new(IndexStore::init(index_path));
let view = View::new(root_view);
Arc::new(ReadonlyRepo {
Ok(Arc::new(ReadonlyRepo {
repo_path,
store,
op_store,
@ -153,7 +154,7 @@ impl ReadonlyRepo {
index_store,
index: Mutex::new(None),
view,
})
}))
}
pub fn load_at_head(

View file

@ -96,10 +96,12 @@ impl TestRepo {
ReadonlyRepo::init(&settings, &repo_dir, |store_path| {
Box::new(GitBackend::init_external(store_path, &git_path))
})
.unwrap()
} else {
ReadonlyRepo::init(&settings, &repo_dir, |store_path| {
Box::new(LocalBackend::init(store_path))
})
.unwrap()
};
Self {

View file

@ -148,7 +148,7 @@ impl Workspace {
let jj_dir = create_jj_dir(workspace_root)?;
let repo_dir = jj_dir.join("repo");
std::fs::create_dir(&repo_dir).context(&repo_dir)?;
let repo = ReadonlyRepo::init(user_settings, &repo_dir, backend_factory);
let repo = ReadonlyRepo::init(user_settings, &repo_dir, backend_factory)?;
let (working_copy, repo) = init_working_copy(
user_settings,
&repo,

View file

@ -287,7 +287,8 @@ impl GitRepoData {
std::fs::create_dir(&jj_repo_dir).unwrap();
let repo = ReadonlyRepo::init(&settings, &jj_repo_dir, |store_path| {
Box::new(GitBackend::init_external(store_path, &git_repo_dir))
});
})
.unwrap();
Self {
settings,
_temp_dir: temp_dir,
@ -525,7 +526,8 @@ fn test_init() {
std::fs::create_dir(&jj_repo_dir).unwrap();
let repo = ReadonlyRepo::init(&settings, &jj_repo_dir, |store_path| {
Box::new(GitBackend::init_external(store_path, &git_repo_dir))
});
})
.unwrap();
// The refs were *not* imported -- it's the caller's responsibility to import
// any refs they care about.
assert!(!repo.view().heads().contains(&initial_commit_id));
@ -689,7 +691,8 @@ fn set_up_push_repos(settings: &UserSettings, temp_dir: &TempDir) -> PushTestSet
std::fs::create_dir(&jj_repo_dir).unwrap();
let jj_repo = ReadonlyRepo::init(settings, &jj_repo_dir, |store_path| {
Box::new(GitBackend::init_external(store_path, &clone_repo_dir))
});
})
.unwrap();
let mut tx = jj_repo.start_transaction("test");
let new_commit = testutils::create_random_commit(settings, &jj_repo)
.set_parents(vec![initial_commit_id])