From 84060d750bc640a9c8e78287c9892ffa597124e8 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Wed, 5 Jul 2023 22:37:29 +0900 Subject: [PATCH] git_backend: propagate init_internal() error to caller --- examples/custom-backend/main.rs | 9 ++++----- lib/src/git_backend.rs | 18 +++++++++++------- lib/src/workspace.rs | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/examples/custom-backend/main.rs b/examples/custom-backend/main.rs index d7cd34fbc..3ed73b565 100644 --- a/examples/custom-backend/main.rs +++ b/examples/custom-backend/main.rs @@ -54,7 +54,7 @@ fn run_custom_command( 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(Box::new(JitBackend::init(store_path)?)) })?; Ok(()) } @@ -75,10 +75,9 @@ struct JitBackend { } impl JitBackend { - fn init(store_path: &Path) -> Self { - JitBackend { - inner: GitBackend::init_internal(store_path), - } + fn init(store_path: &Path) -> BackendResult { + let inner = GitBackend::init_internal(store_path)?; + Ok(JitBackend { inner }) } fn load(store_path: &Path) -> Self { diff --git a/lib/src/git_backend.rs b/lib/src/git_backend.rs index 55876872c..d6a77a626 100644 --- a/lib/src/git_backend.rs +++ b/lib/src/git_backend.rs @@ -42,6 +42,8 @@ const CONFLICT_SUFFIX: &str = ".jjconflict"; #[derive(Debug, Error)] pub enum GitBackendInitError { + #[error("Failed to initialize git repository: {0}")] + InitRepository(#[source] git2::Error), #[error("Failed to open git repository: {0}")] OpenRepository(#[source] git2::Error), #[error(transparent)] @@ -78,13 +80,15 @@ impl GitBackend { } } - pub fn init_internal(store_path: &Path) -> Self { - let git_repo = git2::Repository::init_bare(store_path.join("git")).unwrap(); + pub fn init_internal(store_path: &Path) -> Result { + let git_repo = git2::Repository::init_bare(store_path.join("git")) + .map_err(GitBackendInitError::InitRepository)?; let extra_path = store_path.join("extra"); - fs::create_dir(&extra_path).unwrap(); - fs::write(store_path.join("git_target"), b"git").unwrap(); + fs::create_dir(&extra_path).context(&extra_path)?; + let target_path = store_path.join("git_target"); + fs::write(&target_path, b"git").context(&target_path)?; let extra_metadata_store = TableStore::init(extra_path, HASH_LENGTH); - GitBackend::new(git_repo, extra_metadata_store) + Ok(GitBackend::new(git_repo, extra_metadata_store)) } pub fn init_external( @@ -912,7 +916,7 @@ mod tests { #[test] fn commit_has_ref() { let temp_dir = testutils::new_temp_dir(); - let store = GitBackend::init_internal(temp_dir.path()); + let store = GitBackend::init_internal(temp_dir.path()).unwrap(); let signature = Signature { name: "Someone".to_string(), email: "someone@example.com".to_string(), @@ -943,7 +947,7 @@ mod tests { #[test] fn overlapping_git_commit_id() { let temp_dir = testutils::new_temp_dir(); - let store = GitBackend::init_internal(temp_dir.path()); + let store = GitBackend::init_internal(temp_dir.path()).unwrap(); let mut commit1 = Commit { parents: vec![store.root_commit_id().clone()], predecessors: vec![], diff --git a/lib/src/workspace.rs b/lib/src/workspace.rs index d8309bef1..59db7e137 100644 --- a/lib/src/workspace.rs +++ b/lib/src/workspace.rs @@ -144,7 +144,7 @@ impl Workspace { workspace_root: &Path, ) -> Result<(Self, Arc), WorkspaceInitError> { Self::init_with_backend(user_settings, workspace_root, |store_path| { - Ok(Box::new(GitBackend::init_internal(store_path))) + Ok(Box::new(GitBackend::init_internal(store_path)?)) }) }