Skip to content

Commit 689d839

Browse files
committed
refactor
1 parent 2fc9dbe commit 689d839

File tree

2 files changed

+22
-51
lines changed

2 files changed

+22
-51
lines changed

gix/src/repository/object.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ impl crate::Repository {
9494
/// Obtain information about an object without fully decoding it, or fail if the object doesn't exist.
9595
///
9696
/// Note that despite being cheaper than [`Self::find_object()`], there is still some effort traversing delta-chains.
97+
/// Also note that for empty trees and blobs, it will always report it to exist in loose objects, even if they don't
98+
/// exist or if they exist in a pack.
9799
#[doc(alias = "read_header", alias = "git2")]
98100
pub fn find_header(&self, id: impl Into<ObjectId>) -> Result<gix_odb::find::Header, object::find::existing::Error> {
99101
let id = id.into();

gix/tests/gix/repository/object.rs

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use gix_odb::Header;
2+
use gix_pack::Find;
13
use gix_testtools::tempfile;
24

35
use crate::util::named_subrepo_opts;
@@ -538,77 +540,44 @@ mod find {
538540
}),
539541
"empty blob is considered a loose object"
540542
);
541-
542-
// Note: Unlike empty tree, empty blobs might actually exist in the repository.
543-
// The key point is that has_object() should always return true for empty blobs,
544-
// regardless of whether they are physically stored or not.
545543
Ok(())
546544
}
547545
}
548546

549547
#[test]
550-
fn empty_blob_is_always_considered_present() -> crate::Result {
551-
use gix_object::Find;
552-
553-
// Test with an empty in-memory repository to ensure empty blob is considered present
554-
// even when it doesn't physically exist
555-
let repo = empty_bare_in_memory_repo()?;
556-
let empty_blob = gix::hash::ObjectId::empty_blob(repo.object_hash());
557-
558-
// The key behavior being tested: has_object should return true for empty blob
559-
assert!(repo.has_object(empty_blob), "empty blob should always be considered present");
560-
561-
// Verify that the lower-level object database doesn't have it
562-
let mut buf = Vec::new();
563-
let lower_level_result = repo.objects.try_find(&empty_blob, &mut buf)?;
564-
565-
// Empty blob might or might not exist at the lower level - that's implementation dependent
566-
// But has_object should always return true regardless
567-
match lower_level_result {
568-
Some(_) => {
569-
// If it exists at the lower level, that's fine
570-
}
571-
None => {
572-
// If it doesn't exist at the lower level, has_object should still return true
573-
// thanks to our special handling
574-
}
575-
}
576-
577-
Ok(())
578-
}
579-
580-
#[test]
581-
fn empty_blob_edge_cases() -> crate::Result {
548+
fn empty_objects_are_always_present_but_not_in_plumbing() -> crate::Result {
582549
let repo = empty_bare_in_memory_repo()?;
583550
let empty_blob_id = gix::hash::ObjectId::empty_blob(repo.object_hash());
584-
585-
// Test all the related methods for empty blobs
586-
assert!(repo.has_object(&empty_blob_id), "has_object should return true");
587-
588-
// Test find_header
551+
552+
assert!(
553+
repo.has_object(empty_blob_id),
554+
"empty object is always present even if it's not"
555+
);
556+
assert!(!repo.objects.contains(&empty_blob_id));
557+
589558
let header = repo.find_header(empty_blob_id)?;
590559
assert_eq!(header.kind(), gix_object::Kind::Blob);
591560
assert_eq!(header.size(), 0);
592-
593-
// Test try_find_header
561+
assert_eq!(repo.objects.try_header(&empty_blob_id)?, None);
562+
594563
let header = repo.try_find_header(empty_blob_id)?.expect("should find header");
595564
assert_eq!(header.kind(), gix_object::Kind::Blob);
596565
assert_eq!(header.size(), 0);
597-
598-
// Test find_object
566+
599567
let obj = repo.find_object(empty_blob_id)?;
600568
assert_eq!(obj.kind, gix_object::Kind::Blob);
601569
assert_eq!(obj.data.len(), 0);
602-
603-
// Test try_find_object
570+
571+
let mut buf = Vec::new();
572+
assert_eq!(repo.objects.try_find(&empty_blob_id, &mut buf)?, None);
573+
604574
let obj = repo.try_find_object(empty_blob_id)?.expect("should find object");
605575
assert_eq!(obj.kind, gix_object::Kind::Blob);
606576
assert_eq!(obj.data.len(), 0);
607-
608-
// Test that we can get a blob from the object
609-
let blob = obj.into_blob();
577+
578+
let blob = obj.try_into_blob()?;
610579
assert_eq!(blob.data.len(), 0);
611-
580+
612581
Ok(())
613582
}
614583

0 commit comments

Comments
 (0)