Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various improvements #1772

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions gix/src/repository/impls.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use gix_hash::ObjectId;
use gix_object::Exists;
use std::ops::DerefMut;

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

impl gix_object::FindHeader for crate::Repository {
fn try_header(&self, id: &gix_hash::oid) -> Result<Option<gix_object::Header>, gix_object::find::Error> {
if id == ObjectId::empty_tree(self.object_hash()) {
return Ok(Some(gix_object::Header {
kind: gix_object::Kind::Tree,
size: 0,
}));
}
self.objects.try_header(id)
}
}
Expand All @@ -139,12 +146,22 @@ impl gix_object::Find for crate::Repository {
id: &gix_hash::oid,
buffer: &'a mut Vec<u8>,
) -> Result<Option<gix_object::Data<'a>>, gix_object::find::Error> {
if id == ObjectId::empty_tree(self.object_hash()) {
buffer.clear();
return Ok(Some(gix_object::Data {
kind: gix_object::Kind::Tree,
data: &[],
}));
}
self.objects.try_find(id, buffer)
}
}

impl gix_object::Exists for crate::Repository {
fn exists(&self, id: &gix_hash::oid) -> bool {
if id == ObjectId::empty_tree(self.object_hash()) {
return true;
}
self.objects.exists(id)
}
}
2 changes: 1 addition & 1 deletion gix/src/repository/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl crate::Repository {
/// Note that this is an expensive operation as it requires recursively traversing the entire tree to unpack it into the index.
pub fn index_from_tree(&self, tree: &gix_hash::oid) -> Result<gix_index::File, super::index_from_tree::Error> {
Ok(gix_index::File::from_state(
gix_index::State::from_tree(tree, &self.objects, self.config.protect_options()?).map_err(|err| {
gix_index::State::from_tree(tree, self, self.config.protect_options()?).map_err(|err| {
super::index_from_tree::Error::IndexFromTree {
id: tree.into(),
source: err,
Expand Down
2 changes: 1 addition & 1 deletion gix/src/status/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ where
crate::head::peel::to_object::Error::Unborn { .. },
),
),
)) => None,
)) => Some(gix_hash::ObjectId::empty_tree(self.repo.object_hash())),
Err(err) => return Err(err.into()),
},
Some(Some(tree_id)) => Some(tree_id),
Expand Down
Binary file modified gix/tests/fixtures/generated-archives/make_status_repos.tar
Binary file not shown.
8 changes: 7 additions & 1 deletion gix/tests/fixtures/make_status_repos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ git init racy-git
git init untracked-unborn
(cd untracked-unborn
touch untracked
)
)

git init untracked-added
(cd untracked-added
echo content >added
git add added
)
26 changes: 26 additions & 0 deletions gix/tests/gix/repository/object.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
use crate::util::named_subrepo_opts;
use gix_testtools::tempfile;

mod object_database_impl {
use gix_object::{Exists, Find, FindHeader};

#[test]
fn empty_tree_is_always_present() -> crate::Result {
let repo = crate::named_subrepo_opts("make_basic_repo.sh", "unborn", gix::open::Options::isolated())?;
let empty_tree = gix::ObjectId::empty_tree(repo.object_hash());
assert!(repo.exists(&empty_tree));
assert_eq!(
repo.try_header(&empty_tree)?.expect("tree present"),
gix_object::Header {
kind: gix_object::Kind::Tree,
size: 0
}
);
let mut buf = repo.empty_reusable_buffer();
buf.push(42);
assert_eq!(
repo.try_find(&empty_tree, &mut buf)?.expect("tree present").kind,
gix_object::Kind::Tree
);
assert_eq!(buf.len(), 0, "the data in the buffer matches the empty tree");
Ok(())
}
}

#[cfg(feature = "tree-editor")]
mod edit_tree {
use crate::util::hex_to_id;
Expand Down
23 changes: 23 additions & 0 deletions gix/tests/gix/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ mod into_iter {
Ok(())
}

#[test]
fn untracked_added() -> crate::Result {
let repo = repo("untracked-added")?;
let mut status = repo.status(gix::progress::Discard)?.into_iter(None)?;
let mut items: Vec<_> = status.by_ref().filter_map(Result::ok).collect();
items.sort_by(|a, b| a.location().cmp(b.location()));
insta::assert_debug_snapshot!(items, @r#"
[
TreeIndex(
Addition {
location: "added",
index: 0,
entry_mode: Mode(
FILE,
),
id: Sha1(d95f3ad14dee633a758d2e331151e950dd13e4ed),
},
),
]
"#);
Ok(())
}

#[test]
fn error_during_tree_traversal_causes_failure() -> crate::Result {
let repo = repo("untracked-only")?;
Expand Down
Loading