diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-08-05 13:02:06 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-08-05 13:02:06 +0200 |
commit | a55d80408c2ea2b4eb941a324bc3d0848491c6da (patch) | |
tree | bd697fdc04016d89c8e54ee98c04024ff8ac7a06 | |
parent | b8763bfb38bbd9eef607d6a4f96d0a35b25923ae (diff) |
Open git2::Repository for every request
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/model/backend/error.rs | 6 | ||||
-rw-r--r-- | src/model/backend/worker.rs | 15 |
2 files changed, 14 insertions, 7 deletions
diff --git a/src/model/backend/error.rs b/src/model/backend/error.rs index 2126807..b524197 100644 --- a/src/model/backend/error.rs +++ b/src/model/backend/error.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use derive_more::Display; use derive_more::Error; use actix_web::http::StatusCode; @@ -20,6 +22,10 @@ pub enum BackendError { #[display(fmt = "Backend Error: task spawning failed")] TaskError, + #[display(fmt = "Backend Error: Cannot open repository at {}", _0)] + #[error(ignore)] + RepositoryOpenError(String), + #[display(fmt = "Backend Error: Finding branch {} failed", _0)] #[error(ignore)] FindBranchError(String), diff --git a/src/model/backend/worker.rs b/src/model/backend/worker.rs index 33c32d8..8cd6f82 100644 --- a/src/model/backend/worker.rs +++ b/src/model/backend/worker.rs @@ -1,4 +1,5 @@ use std::path::Path; +use std::path::PathBuf; use anyhow::anyhow; use anyhow::Result; @@ -14,14 +15,15 @@ use crate::model::backend::channels::BackendResponseSender; pub struct BackendWorker { repo_name: String, - repo: Mutex<git2::Repository>, + repo_path: PathBuf, receiver: BackendRequestReceiver, } impl BackendWorker { pub fn new<P: AsRef<Path>>(repo_name: String, repo_path: P, receiver: BackendRequestReceiver) -> Result<Self> { - let repo = git2::Repository::open(repo_path).map(tokio::sync::Mutex::new)?; - Ok(BackendWorker { repo_name, repo, receiver }) + let _ = git2::Repository::open(repo_path.as_ref()) // to test whether the repo can be opened + .map_err(|_| BackendError::RepositoryOpenError(repo_path.as_ref().display().to_string()))?; + Ok(BackendWorker { repo_name, repo_path: repo_path.as_ref().to_path_buf(), receiver }) } pub async fn run(mut self) { @@ -56,10 +58,9 @@ impl BackendWorker { where R: Sized + Send, F: FnOnce(&git2::Repository) -> std::result::Result<R, BackendError>, { - let repo = self.repo.lock().await; - let res = f(&repo); - drop(repo); - res + let repo = git2::Repository::open(&self.repo_path) + .map_err(|_| BackendError::RepositoryOpenError(self.repo_path.display().to_string()))?; + f(&repo) } async fn send_branch_list(&self) -> std::result::Result<BackendResponse, BackendError> { |