-
-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frame for traversing tree entries (#301)
- Loading branch information
Showing
7 changed files
with
311 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use anyhow::bail; | ||
use std::io; | ||
use std::path::PathBuf; | ||
|
||
use crate::OutputFormat; | ||
use git_repository as git; | ||
use git_repository::prelude::ObjectIdExt; | ||
|
||
mod entries { | ||
use git_repository as git; | ||
|
||
use git::hash::oid; | ||
use git::objs::{bstr::BStr, tree::EntryRef}; | ||
use git::traverse::tree::visit::Action; | ||
|
||
pub struct Traverse { | ||
pub num_trees: usize, | ||
pub num_links: usize, | ||
pub num_blobs: usize, | ||
pub num_blobs_exec: usize, | ||
pub num_submodules: usize, | ||
pub num_bytes: u64, | ||
pub repo: git::Repository, | ||
} | ||
|
||
impl Traverse { | ||
pub fn new(repo: git::Repository) -> Self { | ||
Traverse { | ||
num_trees: 0, | ||
num_links: 0, | ||
num_blobs: 0, | ||
num_blobs_exec: 0, | ||
num_submodules: 0, | ||
num_bytes: 0, | ||
repo, | ||
} | ||
} | ||
|
||
pub(crate) fn count_bytes(&mut self, oid: &oid) { | ||
if let Ok(obj) = self.repo.find_object(oid) { | ||
self.num_bytes += obj.data.len() as u64; | ||
} | ||
} | ||
} | ||
|
||
impl git::traverse::tree::Visit for Traverse { | ||
fn pop_front_tracked_path_and_set_current(&mut self) {} | ||
|
||
fn push_back_tracked_path_component(&mut self, _component: &BStr) {} | ||
|
||
fn push_path_component(&mut self, _component: &BStr) {} | ||
|
||
fn pop_path_component(&mut self) {} | ||
|
||
fn visit_tree(&mut self, _entry: &EntryRef<'_>) -> Action { | ||
self.num_trees += 1; | ||
Action::Continue | ||
} | ||
|
||
fn visit_nontree(&mut self, entry: &EntryRef<'_>) -> Action { | ||
use git::objs::tree::EntryMode::*; | ||
match entry.mode { | ||
Commit => self.num_submodules += 1, | ||
Blob => { | ||
self.count_bytes(entry.oid); | ||
self.num_blobs += 1 | ||
} | ||
BlobExecutable => { | ||
self.count_bytes(entry.oid); | ||
self.num_blobs_exec += 1 | ||
} | ||
Link => self.num_links += 1, | ||
Tree => unreachable!("BUG"), | ||
} | ||
Action::Continue | ||
} | ||
} | ||
} | ||
|
||
pub fn entries( | ||
repository: PathBuf, | ||
treeish: Option<&str>, | ||
recursive: bool, | ||
extended: bool, | ||
format: OutputFormat, | ||
out: &mut dyn io::Write, | ||
_err: &mut dyn io::Write, | ||
) -> anyhow::Result<()> { | ||
if format == OutputFormat::Json { | ||
bail!("Only human output format is supported at the moment"); | ||
} | ||
|
||
let tree_repo = git::open(repository)?; | ||
let mut repo = tree_repo.clone().apply_environment(); | ||
repo.object_cache_size(128 * 1024); | ||
|
||
let tree = match treeish { | ||
Some(hex) => git::hash::ObjectId::from_hex(hex.as_bytes()) | ||
.map(|id| id.attach(&repo))? | ||
.object()? | ||
.try_into_tree()?, | ||
None => repo.head()?.peel_to_commit_in_place()?.tree()?, | ||
}; | ||
|
||
if recursive { | ||
} else { | ||
for entry in tree.iter() { | ||
let entry = entry?; | ||
format_entry( | ||
&mut *out, | ||
&entry.inner, | ||
extended | ||
.then(|| entry.id().object().map(|o| o.data.len())) | ||
.transpose()?, | ||
)?; | ||
} | ||
} | ||
|
||
let mut delegate = entries::Traverse::new(tree_repo); | ||
tree.traverse().breadthfirst(&mut delegate)?; | ||
Ok(()) | ||
} | ||
|
||
fn format_entry( | ||
mut _out: impl io::Write, | ||
_entry: &git::objs::tree::EntryRef<'_>, | ||
_size: Option<usize>, | ||
) -> std::io::Result<()> { | ||
todo!() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::{path::PathBuf, sync::atomic::AtomicBool}; | ||
|
||
use git_repository as git; | ||
use git_repository::Progress; | ||
|
||
use crate::{pack, OutputFormat}; | ||
|
||
/// A general purpose context for many operations provided here | ||
pub struct Context { | ||
/// If set, provide statistics to `out` in the given format | ||
pub output_statistics: Option<OutputFormat>, | ||
/// If set, don't use more than this amount of threads. | ||
/// Otherwise, usually use as many threads as there are logical cores. | ||
/// A value of 0 is interpreted as no-limit | ||
pub thread_limit: Option<usize>, | ||
pub verify_mode: pack::verify::Mode, | ||
pub algorithm: pack::verify::Algorithm, | ||
} | ||
|
||
pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 1..=3; | ||
|
||
pub fn integrity( | ||
repo: PathBuf, | ||
mut out: impl std::io::Write, | ||
progress: impl Progress, | ||
should_interrupt: &AtomicBool, | ||
Context { | ||
output_statistics, | ||
thread_limit, | ||
verify_mode, | ||
algorithm, | ||
}: Context, | ||
) -> anyhow::Result<()> { | ||
let repo = git_repository::open(repo)?; | ||
#[cfg_attr(not(feature = "serde1"), allow(unused))] | ||
let mut outcome = repo.objects.store_ref().verify_integrity( | ||
progress, | ||
should_interrupt, | ||
git_repository::odb::pack::index::verify::integrity::Options { | ||
verify_mode, | ||
traversal: algorithm.into(), | ||
thread_limit, | ||
// TODO: a way to get the pack cache from a handle | ||
make_pack_lookup_cache: || git_repository::odb::pack::cache::Never, | ||
}, | ||
)?; | ||
// TODO: make this work for indices in multiple workspaces, once we have workspace support | ||
if let Some(index) = repo.load_index().transpose()? { | ||
index.verify_integrity()?; | ||
index.verify_entries()?; | ||
index.verify_extensions(true, { | ||
use git::odb::FindExt; | ||
let objects = repo.objects; | ||
move |oid, buf: &mut Vec<u8>| objects.find_tree_iter(oid, buf).ok() | ||
})?; | ||
outcome.progress.info(format!("Index at '{}' OK", index.path.display())); | ||
} | ||
match output_statistics { | ||
Some(OutputFormat::Human) => writeln!(out, "Human output is currently unsupported, use JSON instead")?, | ||
#[cfg(feature = "serde1")] | ||
Some(OutputFormat::Json) => { | ||
serde_json::to_writer_pretty( | ||
out, | ||
&serde_json::json!({ | ||
"index_statistics" : outcome.index_statistics, | ||
"loose_object-stores" : outcome.loose_object_stores | ||
}), | ||
)?; | ||
} | ||
None => {} | ||
} | ||
Ok(()) | ||
} |
Oops, something went wrong.