diff --git a/giterated-daemon/src/backend/git.rs b/giterated-daemon/src/backend/git.rs index 20d24bd..e6f18a8 100644 --- a/giterated-daemon/src/backend/git.rs +++ b/giterated-daemon/src/backend/git.rs @@ -344,7 +344,7 @@ impl GitBackend { pub fn get_oid_from_reference( git: &git2::Repository, rev: Option<&str>, - ) -> anyhow::Result { + ) -> Result { // If the rev is None try and get the repository head let Some(rev) = rev else { if let Ok(head) = git.head() { @@ -353,7 +353,7 @@ impl GitBackend { return Ok(head.target().unwrap()); } else { // Nothing in database, render empty tree. - return Err(GitBackendError::HeadNotFound.into()); + return Err(GitBackendError::HeadNotFound); } }; @@ -370,7 +370,7 @@ impl GitBackend { } else if let Ok(object) = git.revparse_single(rev) { Ok(object.id()) } else { - Err(Box::new(GitBackendError::RefNotFound(rev.to_string())).into()) + Err(GitBackendError::RefNotFound(rev.to_string())) } } @@ -502,6 +502,7 @@ impl RepositoryBackend for GitBackend { } } + /// If the OID can't be found because there's no repository head, this will return an empty `Vec`. async fn repository_file_inspect( &mut self, requester: &Option, @@ -513,7 +514,11 @@ impl RepositoryBackend for GitBackend { .await?; // Try and find the tree_id/branch - let tree_id = Self::get_oid_from_reference(&git, request.rev.as_deref())?; + let tree_id = match Self::get_oid_from_reference(&git, request.rev.as_deref()) { + Ok(oid) => oid, + Err(GitBackendError::HeadNotFound) => return Ok(vec![]), + Err(err) => return Err(err.into()), + }; // Get the commit from the oid let commit = match git.find_commit(tree_id) { @@ -730,6 +735,7 @@ impl RepositoryBackend for GitBackend { Ok(commit) } + /// Returns zero for all statistics if an OID wasn't found async fn repository_get_statistics( &mut self, requester: &Option, @@ -740,7 +746,10 @@ impl RepositoryBackend for GitBackend { .open_repository_and_check_permissions(&repository.owner, &repository.name, requester) .await?; - let tree_id = Self::get_oid_from_reference(&git, request.rev.as_deref())?; + let tree_id = match Self::get_oid_from_reference(&git, request.rev.as_deref()) { + Ok(oid) => oid, + Err(_) => return Ok(RepositoryStatistics::default()), + }; // Count the amount of branches and tags let mut branches = 0; diff --git a/giterated-models/src/repository/mod.rs b/giterated-models/src/repository/mod.rs index 6ab085f..5aee7bb 100644 --- a/giterated-models/src/repository/mod.rs +++ b/giterated-models/src/repository/mod.rs @@ -144,7 +144,7 @@ pub struct RepositoryView { } /// Generic repository statistics -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct RepositoryStatistics { /// Amount of commits made to this branch in the repository pub commits: usize,