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

fix tree parsing #1102

Merged
merged 4 commits into from
Nov 10, 2023
Merged
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
23 changes: 9 additions & 14 deletions examples/ls-tree.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use std::io::{stdout, Write};

use clap::Parser;
use gix::{
bstr::BString,
objs::tree::{EntryMode, EntryMode::Tree},
traverse::tree::Recorder,
ObjectId,
};
use gix::{bstr::BString, objs::tree::EntryMode, traverse::tree::Recorder, ObjectId};

fn main() {
let args = Args::parse_from(gix::env::args_os());
@@ -43,14 +38,14 @@ fn run(args: Args) -> anyhow::Result<()> {
recorder
.records
.into_iter()
.filter(|entry| args.tree_recursing || args.tree_only || entry.mode != Tree)
.filter(|entry| !args.tree_only || (entry.mode == Tree))
.filter(|entry| args.tree_recursing || args.tree_only || entry.mode.is_no_tree())
.filter(|entry| !args.tree_only || (entry.mode.is_tree()))
.map(|entry| Entry::new(entry.mode, entry.oid, entry.filepath))
.collect::<Vec<_>>()
} else {
tree.iter()
.filter_map(|res| res.ok().map(|entry| entry.inner)) // dropping errors silently
.filter(|entry| !args.tree_only || (entry.mode == Tree))
.filter(|entry| !args.tree_only || (entry.mode.is_tree()))
.map(|entry| Entry::new(entry.mode, entry.oid.to_owned(), entry.filename.to_owned()))
.collect::<Vec<_>>()
};
@@ -60,8 +55,8 @@ fn run(args: Args) -> anyhow::Result<()> {
writeln!(
out,
"{:06o} {:4} {} {}",
entry.kind as u16,
entry.kind.as_str(),
*entry.mode,
entry.mode.as_str(),
entry.hash,
entry.path
)?;
@@ -71,13 +66,13 @@ fn run(args: Args) -> anyhow::Result<()> {
}

struct Entry {
kind: EntryMode,
mode: EntryMode,
hash: ObjectId,
path: BString,
}

impl Entry {
fn new(kind: EntryMode, hash: ObjectId, path: BString) -> Self {
Self { kind, hash, path }
fn new(mode: EntryMode, hash: ObjectId, path: BString) -> Self {
Self { mode, hash, path }
}
}
2 changes: 1 addition & 1 deletion gitoxide-core/src/repository/archive.rs
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ pub fn stream(
for (path, content) in files {
stream.add_entry(gix::worktree::stream::AdditionalEntry {
id: gix::hash::Kind::Sha1.null(),
mode: gix::object::tree::EntryMode::Blob,
mode: gix::object::tree::EntryKind::Blob.into(),
relative_path: path.into(),
source: gix::worktree::stream::entry::Source::Memory(content.into()),
});
8 changes: 4 additions & 4 deletions gitoxide-core/src/repository/tree.rs
Original file line number Diff line number Diff line change
@@ -89,7 +89,6 @@ mod entries {
}

fn visit_nontree(&mut self, entry: &EntryRef<'_>) -> Action {
use gix::objs::tree::EntryMode::*;
let size = self
.repo
.and_then(|repo| repo.find_object(entry.oid).map(|o| o.data.len()).ok());
@@ -100,7 +99,8 @@ mod entries {
self.stats.num_bytes += size as u64;
}

match entry.mode {
use gix::object::tree::EntryKind::*;
match entry.mode.kind() {
Commit => self.stats.num_submodules += 1,
Blob => self.stats.num_blobs += 1,
BlobExecutable => self.stats.num_blobs_exec += 1,
@@ -184,11 +184,11 @@ fn format_entry(
filename: &gix::bstr::BStr,
size: Option<usize>,
) -> std::io::Result<()> {
use gix::objs::tree::EntryMode::*;
use gix::objs::tree::EntryKind::*;
writeln!(
out,
"{} {}{} {}",
match entry.mode {
match entry.mode.kind() {
Tree => "TREE",
Blob => "BLOB",
BlobExecutable => " EXE",
34 changes: 13 additions & 21 deletions gix-archive/src/write.rs
Original file line number Diff line number Diff line change
@@ -169,23 +169,19 @@ fn append_zip_entry<W: std::io::Write + std::io::Seek>(
.compression_level(compression_level)
.large_file(entry.bytes_remaining().map_or(true, |len| len > u32::MAX as usize))
.last_modified_time(mtime)
.unix_permissions(if matches!(entry.mode, gix_object::tree::EntryMode::BlobExecutable) {
0o755
} else {
0o644
});
.unix_permissions(if entry.mode.is_executable() { 0o755 } else { 0o644 });
let path = add_prefix(entry.relative_path(), tree_prefix).into_owned();
match entry.mode {
gix_object::tree::EntryMode::Blob | gix_object::tree::EntryMode::BlobExecutable => {
match entry.mode.kind() {
gix_object::tree::EntryKind::Blob | gix_object::tree::EntryKind::BlobExecutable => {
ar.start_file(path.to_string(), file_opts)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?;
std::io::copy(&mut entry, ar)?;
}
gix_object::tree::EntryMode::Tree | gix_object::tree::EntryMode::Commit => {
gix_object::tree::EntryKind::Tree | gix_object::tree::EntryKind::Commit => {
ar.add_directory(path.to_string(), file_opts)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?;
}
gix_object::tree::EntryMode::Link => {
gix_object::tree::EntryKind::Link => {
use bstr::ByteSlice;
std::io::copy(&mut entry, buf)?;
ar.add_symlink(path.to_string(), buf.as_bstr().to_string(), file_opts)
@@ -206,18 +202,14 @@ fn append_tar_entry<W: std::io::Write>(
let mut header = tar::Header::new_gnu();
header.set_mtime(mtime_seconds_since_epoch as u64);
header.set_entry_type(tar_entry_type(entry.mode));
header.set_mode(if matches!(entry.mode, gix_object::tree::EntryMode::BlobExecutable) {
0o755
} else {
0o644
});
header.set_mode(if entry.mode.is_executable() { 0o755 } else { 0o644 });
buf.clear();
std::io::copy(&mut entry, buf)?;

let path = gix_path::from_bstr(add_prefix(entry.relative_path(), opts.tree_prefix.as_ref()));
header.set_size(buf.len() as u64);

if entry.mode == gix_object::tree::EntryMode::Link {
if entry.mode.is_link() {
use bstr::ByteSlice;
let target = gix_path::from_bstr(buf.as_bstr());
header.set_entry_type(tar::EntryType::Symlink);
@@ -231,13 +223,13 @@ fn append_tar_entry<W: std::io::Write>(

#[cfg(any(feature = "tar", feature = "tar_gz"))]
fn tar_entry_type(mode: gix_object::tree::EntryMode) -> tar::EntryType {
use gix_object::tree::EntryMode;
use gix_object::tree::EntryKind;
use tar::EntryType;
match mode {
EntryMode::Tree | EntryMode::Commit => EntryType::Directory,
EntryMode::Blob => EntryType::Regular,
EntryMode::BlobExecutable => EntryType::Regular,
EntryMode::Link => EntryType::Link,
match mode.kind() {
EntryKind::Tree | EntryKind::Commit => EntryType::Directory,
EntryKind::Blob => EntryType::Regular,
EntryKind::BlobExecutable => EntryType::Regular,
EntryKind::Link => EntryType::Link,
}
}

22 changes: 11 additions & 11 deletions gix-archive/tests/archive.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ mod from_tree {

use gix_archive::Format;
use gix_attributes::glob::pattern::Case;
use gix_object::tree::EntryMode;
use gix_object::tree::EntryKind;
use gix_testtools::bstr::ByteSlice;
use gix_worktree::stack::state::attributes::Source;

@@ -22,32 +22,32 @@ mod from_tree {
let mut stream = gix_worktree_stream::Stream::from_read(std::io::Cursor::new(buf));
let mut paths_and_modes = Vec::new();
while let Some(mut entry) = stream.next_entry().expect("entry retrieval does not fail") {
paths_and_modes.push((entry.relative_path().to_owned(), entry.mode, entry.id));
paths_and_modes.push((entry.relative_path().to_owned(), entry.mode.kind(), entry.id));
let mut buf = Vec::new();
entry.read_to_end(&mut buf).expect("stream can always be read");
}

let expected_link_mode = if cfg!(windows) {
EntryMode::Blob
EntryKind::Blob
} else {
EntryMode::Link
EntryKind::Link
};
let expected_exe_mode = if cfg!(windows) {
EntryMode::Blob
EntryKind::Blob
} else {
EntryMode::BlobExecutable
EntryKind::BlobExecutable
};
assert_eq!(
paths_and_modes,
&[
(
".gitattributes".into(),
EntryMode::Blob,
EntryKind::Blob,
hex_to_id("45c160c35c17ad264b96431cceb9793160396e99")
),
(
"a".into(),
EntryMode::Blob,
EntryKind::Blob,
hex_to_id("45b983be36b73c0788dc9cbcb76cbb80fc7bb057")
),
(
@@ -61,7 +61,7 @@ mod from_tree {
),
(
"dir/b".into(),
EntryMode::Blob,
EntryKind::Blob,
hex_to_id("ab4a98190cf776b43cb0fe57cef231fb93fd07e6")
),
(
@@ -71,7 +71,7 @@ mod from_tree {
),
(
"extra-file".into(),
EntryMode::Blob,
EntryKind::Blob,
hex_to_id("0000000000000000000000000000000000000000")
),
(
@@ -81,7 +81,7 @@ mod from_tree {
),
(
"extra-dir-empty".into(),
EntryMode::Tree,
EntryKind::Tree,
hex_to_id("0000000000000000000000000000000000000000")
),
(
23 changes: 11 additions & 12 deletions gix-diff/src/tree/changes.rs
Original file line number Diff line number Diff line change
@@ -250,9 +250,8 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>(
queue: &mut VecDeque<TreeInfoPair>,
delegate: &mut R,
) -> Result<(), Error> {
use gix_object::tree::EntryMode::*;
match (lhs.mode, rhs.mode) {
(Tree, Tree) => {
match (lhs.mode.is_tree(), rhs.mode.is_tree()) {
(true, true) => {
delegate.push_back_tracked_path_component(lhs.filename);
if lhs.oid != rhs.oid
&& delegate
@@ -268,7 +267,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>(
}
queue.push_back((Some(lhs.oid.to_owned()), Some(rhs.oid.to_owned())));
}
(_, Tree) => {
(_, true) => {
delegate.push_back_tracked_path_component(lhs.filename);
if delegate
.visit(Change::Deletion {
@@ -290,7 +289,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>(
};
queue.push_back((None, Some(rhs.oid.to_owned())));
}
(Tree, _) => {
(true, _) => {
delegate.push_back_tracked_path_component(lhs.filename);
if delegate
.visit(Change::Deletion {
@@ -312,9 +311,9 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>(
};
queue.push_back((Some(lhs.oid.to_owned()), None));
}
(lhs_non_tree, rhs_non_tree) => {
(false, false) => {
delegate.push_path_component(lhs.filename);
debug_assert!(lhs_non_tree.is_no_tree() && rhs_non_tree.is_no_tree());
debug_assert!(lhs.mode.is_no_tree() && lhs.mode.is_no_tree());
if lhs.oid != rhs.oid
&& delegate
.visit(Change::Modification {
@@ -342,7 +341,7 @@ fn peekable<I: Iterator>(iter: I) -> IteratorType<I> {
mod tests {
use std::cmp::Ordering;

use gix_object::tree::EntryMode;
use gix_object::tree::EntryKind;

use super::*;

@@ -351,25 +350,25 @@ mod tests {
let null = gix_hash::ObjectId::null(gix_hash::Kind::Sha1);
let actual = compare(
&EntryRef {
mode: EntryMode::Blob,
mode: EntryKind::Blob.into(),
filename: "plumbing-cli.rs".into(),
oid: &null,
},
&EntryRef {
mode: EntryMode::Tree,
mode: EntryKind::Tree.into(),
filename: "plumbing".into(),
oid: &null,
},
);
assert_eq!(actual, Ordering::Less);
let actual = compare(
&EntryRef {
mode: EntryMode::Tree,
mode: EntryKind::Tree.into(),
filename: "plumbing-cli.rs".into(),
oid: &null,
},
&EntryRef {
mode: EntryMode::Blob,
mode: EntryKind::Blob.into(),
filename: "plumbing".into(),
oid: &null,
},
Loading