Skip to content

Commit e087960

Browse files
CopilotByron
andcommitted
fix: remove special handling for empty blob hash to match Git behaviour
This feature was recently introduced, but was never released. Co-authored-by: Byron <[email protected]>
1 parent f891c37 commit e087960

File tree

2 files changed

+25
-50
lines changed

2 files changed

+25
-50
lines changed

gix/src/repository/object.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,6 @@ impl crate::Repository {
5050
repo: self,
5151
});
5252
}
53-
if id == ObjectId::empty_blob(self.object_hash()) {
54-
return Ok(Object {
55-
id,
56-
kind: gix_object::Kind::Blob,
57-
data: Vec::new(),
58-
repo: self,
59-
});
60-
}
6153
let mut buf = self.free_buf();
6254
let kind = self.objects.find(&id, &mut buf)?.kind;
6355
Ok(Object::from_data(id, kind, buf, self))
@@ -106,12 +98,6 @@ impl crate::Repository {
10698
size: 0,
10799
});
108100
}
109-
if id == ObjectId::empty_blob(self.object_hash()) {
110-
return Ok(gix_odb::find::Header::Loose {
111-
kind: gix_object::Kind::Blob,
112-
size: 0,
113-
});
114-
}
115101
self.objects.header(id)
116102
}
117103

@@ -126,7 +112,7 @@ impl crate::Repository {
126112
#[doc(alias = "exists", alias = "git2")]
127113
pub fn has_object(&self, id: impl AsRef<gix_hash::oid>) -> bool {
128114
let id = id.as_ref();
129-
if id.to_owned().is_empty_tree() || id.to_owned().is_empty_blob() {
115+
if id.to_owned().is_empty_tree() {
130116
true
131117
} else {
132118
self.objects.exists(id)
@@ -147,12 +133,6 @@ impl crate::Repository {
147133
size: 0,
148134
}));
149135
}
150-
if id == ObjectId::empty_blob(self.object_hash()) {
151-
return Ok(Some(gix_odb::find::Header::Loose {
152-
kind: gix_object::Kind::Blob,
153-
size: 0,
154-
}));
155-
}
156136
self.objects.try_header(&id).map_err(Into::into)
157137
}
158138

@@ -167,14 +147,6 @@ impl crate::Repository {
167147
repo: self,
168148
}));
169149
}
170-
if id == ObjectId::empty_blob(self.object_hash()) {
171-
return Ok(Some(Object {
172-
id,
173-
kind: gix_object::Kind::Blob,
174-
data: Vec::new(),
175-
repo: self,
176-
}));
177-
}
178150

179151
let mut buf = self.free_buf();
180152
match self.objects.try_find(&id, &mut buf)? {

gix/tests/gix/repository/object.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,11 @@ mod find {
512512
}
513513

514514
#[test]
515-
fn empty_blob_can_always_be_found() -> crate::Result {
515+
fn empty_blob_can_be_found_if_it_exists() -> crate::Result {
516516
let repo = basic_repo()?;
517517
let empty_blob = gix::hash::ObjectId::empty_blob(repo.object_hash());
518+
519+
// The basic_repo fixture contains an empty blob, so these should work
518520
assert_eq!(repo.find_object(empty_blob)?.into_blob().data.len(), 0);
519521
assert!(repo.has_object(empty_blob));
520522
assert_eq!(
@@ -523,7 +525,7 @@ mod find {
523525
kind: gix_object::Kind::Blob,
524526
size: 0,
525527
},
526-
"empty blob is considered a loose object"
528+
"empty blob is found when it exists in the repository"
527529
);
528530
assert_eq!(
529531
repo.try_find_object(empty_blob)?
@@ -539,10 +541,22 @@ mod find {
539541
kind: gix_object::Kind::Blob,
540542
size: 0,
541543
}),
542-
"empty blob is considered a loose object"
544+
"empty blob is found when it exists in the repository"
543545
);
544546
Ok(())
545547
}
548+
549+
#[test]
550+
fn empty_blob_method_creates_correct_object() -> crate::Result {
551+
let repo = basic_repo()?;
552+
let empty_blob = repo.empty_blob();
553+
554+
// The empty_blob method should create an object with the right ID and empty data
555+
assert_eq!(empty_blob.id, gix::hash::ObjectId::empty_blob(repo.object_hash()));
556+
assert_eq!(empty_blob.data.len(), 0);
557+
558+
Ok(())
559+
}
546560
}
547561

548562
#[test]
@@ -551,33 +565,22 @@ fn empty_objects_are_always_present_but_not_in_plumbing() -> crate::Result {
551565
let empty_blob_id = gix::hash::ObjectId::empty_blob(repo.object_hash());
552566

553567
assert!(
554-
repo.has_object(empty_blob_id),
555-
"empty object is always present even if it's not"
568+
!repo.has_object(empty_blob_id),
569+
"empty blob is not present unless it actually exists"
556570
);
557571
assert!(!repo.objects.contains(&empty_blob_id));
558572

559-
let header = repo.find_header(empty_blob_id)?;
560-
assert_eq!(header.kind(), gix_object::Kind::Blob);
561-
assert_eq!(header.size(), 0);
573+
// Empty blob should cause errors when it doesn't exist
574+
assert!(repo.find_header(empty_blob_id).is_err());
562575
assert_eq!(repo.objects.try_header(&empty_blob_id)?, None);
563576

564-
let header = repo.try_find_header(empty_blob_id)?.expect("should find header");
565-
assert_eq!(header.kind(), gix_object::Kind::Blob);
566-
assert_eq!(header.size(), 0);
567-
568-
let obj = repo.find_object(empty_blob_id)?;
569-
assert_eq!(obj.kind, gix_object::Kind::Blob);
570-
assert_eq!(obj.data.len(), 0);
577+
assert_eq!(repo.try_find_header(empty_blob_id)?, None);
578+
assert!(repo.find_object(empty_blob_id).is_err());
571579

572580
let mut buf = Vec::new();
573581
assert_eq!(repo.objects.try_find(&empty_blob_id, &mut buf)?, None);
574582

575-
let obj = repo.try_find_object(empty_blob_id)?.expect("should find object");
576-
assert_eq!(obj.kind, gix_object::Kind::Blob);
577-
assert_eq!(obj.data.len(), 0);
578-
579-
let blob = obj.try_into_blob()?;
580-
assert_eq!(blob.data.len(), 0);
583+
assert!(repo.try_find_object(empty_blob_id)?.is_none());
581584

582585
Ok(())
583586
}

0 commit comments

Comments
 (0)