JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Fix being able to view a repository when nothing has been committed

Emilia - ⁨1⁩ year ago

parent: tbd commit: ⁨810cd2a

Showing ⁨⁨2⁩ changed files⁩ with ⁨⁨15⁩ insertions⁩ and ⁨⁨6⁩ deletions⁩

giterated-daemon/src/backend/git.rs

View file
@@ -344,7 +344,7 @@ impl GitBackend {
344 344 pub fn get_oid_from_reference(
345 345 git: &git2::Repository,
346 346 rev: Option<&str>,
347 ) -> anyhow::Result<git2::Oid> {
347 ) -> Result<git2::Oid, GitBackendError> {
348 348 // If the rev is None try and get the repository head
349 349 let Some(rev) = rev else {
350 350 if let Ok(head) = git.head() {
@@ -353,7 +353,7 @@ impl GitBackend {
353 353 return Ok(head.target().unwrap());
354 354 } else {
355 355 // Nothing in database, render empty tree.
356 return Err(GitBackendError::HeadNotFound.into());
356 return Err(GitBackendError::HeadNotFound);
357 357 }
358 358 };
359 359
@@ -370,7 +370,7 @@ impl GitBackend {
370 370 } else if let Ok(object) = git.revparse_single(rev) {
371 371 Ok(object.id())
372 372 } else {
373 Err(Box::new(GitBackendError::RefNotFound(rev.to_string())).into())
373 Err(GitBackendError::RefNotFound(rev.to_string()))
374 374 }
375 375 }
376 376
@@ -502,6 +502,7 @@ impl RepositoryBackend for GitBackend {
502 502 }
503 503 }
504 504
505 /// If the OID can't be found because there's no repository head, this will return an empty `Vec`.
505 506 async fn repository_file_inspect(
506 507 &mut self,
507 508 requester: &Option<AuthenticatedUser>,
@@ -513,7 +514,11 @@ impl RepositoryBackend for GitBackend {
513 514 .await?;
514 515
515 516 // Try and find the tree_id/branch
516 let tree_id = Self::get_oid_from_reference(&git, request.rev.as_deref())?;
517 let tree_id = match Self::get_oid_from_reference(&git, request.rev.as_deref()) {
518 Ok(oid) => oid,
519 Err(GitBackendError::HeadNotFound) => return Ok(vec![]),
520 Err(err) => return Err(err.into()),
521 };
517 522
518 523 // Get the commit from the oid
519 524 let commit = match git.find_commit(tree_id) {
@@ -730,6 +735,7 @@ impl RepositoryBackend for GitBackend {
730 735 Ok(commit)
731 736 }
732 737
738 /// Returns zero for all statistics if an OID wasn't found
733 739 async fn repository_get_statistics(
734 740 &mut self,
735 741 requester: &Option<AuthenticatedUser>,
@@ -740,7 +746,10 @@ impl RepositoryBackend for GitBackend {
740 746 .open_repository_and_check_permissions(&repository.owner, &repository.name, requester)
741 747 .await?;
742 748
743 let tree_id = Self::get_oid_from_reference(&git, request.rev.as_deref())?;
749 let tree_id = match Self::get_oid_from_reference(&git, request.rev.as_deref()) {
750 Ok(oid) => oid,
751 Err(_) => return Ok(RepositoryStatistics::default()),
752 };
744 753
745 754 // Count the amount of branches and tags
746 755 let mut branches = 0;

giterated-models/src/repository/mod.rs

View file
@@ -144,7 +144,7 @@ pub struct RepositoryView {
144 144 }
145 145
146 146 /// Generic repository statistics
147 #[derive(Clone, Debug, Serialize, Deserialize)]
147 #[derive(Clone, Debug, Serialize, Deserialize, Default)]
148 148 pub struct RepositoryStatistics {
149 149 /// Amount of commits made to this branch in the repository
150 150 pub commits: usize,