Skip to content

Commit 4c8200f

Browse files
authored
Merge pull request #1772 from GitoxideLabs/improvements
various improvements
2 parents 90fef01 + cd8fabf commit 4c8200f

File tree

7 files changed

+75
-3
lines changed

7 files changed

+75
-3
lines changed

gix/src/repository/impls.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use gix_hash::ObjectId;
12
use gix_object::Exists;
23
use std::ops::DerefMut;
34

@@ -129,6 +130,12 @@ impl gix_object::Write for crate::Repository {
129130

130131
impl gix_object::FindHeader for crate::Repository {
131132
fn try_header(&self, id: &gix_hash::oid) -> Result<Option<gix_object::Header>, gix_object::find::Error> {
133+
if id == ObjectId::empty_tree(self.object_hash()) {
134+
return Ok(Some(gix_object::Header {
135+
kind: gix_object::Kind::Tree,
136+
size: 0,
137+
}));
138+
}
132139
self.objects.try_header(id)
133140
}
134141
}
@@ -139,12 +146,22 @@ impl gix_object::Find for crate::Repository {
139146
id: &gix_hash::oid,
140147
buffer: &'a mut Vec<u8>,
141148
) -> Result<Option<gix_object::Data<'a>>, gix_object::find::Error> {
149+
if id == ObjectId::empty_tree(self.object_hash()) {
150+
buffer.clear();
151+
return Ok(Some(gix_object::Data {
152+
kind: gix_object::Kind::Tree,
153+
data: &[],
154+
}));
155+
}
142156
self.objects.try_find(id, buffer)
143157
}
144158
}
145159

146160
impl gix_object::Exists for crate::Repository {
147161
fn exists(&self, id: &gix_hash::oid) -> bool {
162+
if id == ObjectId::empty_tree(self.object_hash()) {
163+
return true;
164+
}
148165
self.objects.exists(id)
149166
}
150167
}

gix/src/repository/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl crate::Repository {
140140
/// Note that this is an expensive operation as it requires recursively traversing the entire tree to unpack it into the index.
141141
pub fn index_from_tree(&self, tree: &gix_hash::oid) -> Result<gix_index::File, super::index_from_tree::Error> {
142142
Ok(gix_index::File::from_state(
143-
gix_index::State::from_tree(tree, &self.objects, self.config.protect_options()?).map_err(|err| {
143+
gix_index::State::from_tree(tree, self, self.config.protect_options()?).map_err(|err| {
144144
super::index_from_tree::Error::IndexFromTree {
145145
id: tree.into(),
146146
source: err,

gix/src/status/iter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ where
5353
crate::head::peel::to_object::Error::Unborn { .. },
5454
),
5555
),
56-
)) => None,
56+
)) => Some(gix_hash::ObjectId::empty_tree(self.repo.object_hash())),
5757
Err(err) => return Err(err.into()),
5858
},
5959
Some(Some(tree_id)) => Some(tree_id),
Binary file not shown.

gix/tests/fixtures/make_status_repos.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ git init racy-git
3434
git init untracked-unborn
3535
(cd untracked-unborn
3636
touch untracked
37-
)
37+
)
38+
39+
git init untracked-added
40+
(cd untracked-added
41+
echo content >added
42+
git add added
43+
)

gix/tests/gix/repository/object.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
use crate::util::named_subrepo_opts;
22
use gix_testtools::tempfile;
33

4+
mod object_database_impl {
5+
use gix_object::{Exists, Find, FindHeader};
6+
7+
#[test]
8+
fn empty_tree_is_always_present() -> crate::Result {
9+
let repo = crate::named_subrepo_opts("make_basic_repo.sh", "unborn", gix::open::Options::isolated())?;
10+
let empty_tree = gix::ObjectId::empty_tree(repo.object_hash());
11+
assert!(repo.exists(&empty_tree));
12+
assert_eq!(
13+
repo.try_header(&empty_tree)?.expect("tree present"),
14+
gix_object::Header {
15+
kind: gix_object::Kind::Tree,
16+
size: 0
17+
}
18+
);
19+
let mut buf = repo.empty_reusable_buffer();
20+
buf.push(42);
21+
assert_eq!(
22+
repo.try_find(&empty_tree, &mut buf)?.expect("tree present").kind,
23+
gix_object::Kind::Tree
24+
);
25+
assert_eq!(buf.len(), 0, "the data in the buffer matches the empty tree");
26+
Ok(())
27+
}
28+
}
29+
430
#[cfg(feature = "tree-editor")]
531
mod edit_tree {
632
use crate::util::hex_to_id;

gix/tests/gix/status.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,29 @@ mod into_iter {
113113
Ok(())
114114
}
115115

116+
#[test]
117+
fn untracked_added() -> crate::Result {
118+
let repo = repo("untracked-added")?;
119+
let mut status = repo.status(gix::progress::Discard)?.into_iter(None)?;
120+
let mut items: Vec<_> = status.by_ref().filter_map(Result::ok).collect();
121+
items.sort_by(|a, b| a.location().cmp(b.location()));
122+
insta::assert_debug_snapshot!(items, @r#"
123+
[
124+
TreeIndex(
125+
Addition {
126+
location: "added",
127+
index: 0,
128+
entry_mode: Mode(
129+
FILE,
130+
),
131+
id: Sha1(d95f3ad14dee633a758d2e331151e950dd13e4ed),
132+
},
133+
),
134+
]
135+
"#);
136+
Ok(())
137+
}
138+
116139
#[test]
117140
fn error_during_tree_traversal_causes_failure() -> crate::Result {
118141
let repo = repo("untracked-only")?;

0 commit comments

Comments
 (0)