Skip to content

Commit

Permalink
feat: gix attributes validate to validate attributes and ignore aga…
Browse files Browse the repository at this point in the history
…inst `git` as baseline.

Use this command to test the entire index for ignored paths and their attributes and use
`git check-attr` and `git check-ignore` to validate that `git` agrees. Collect all
mismatches and print them.
  • Loading branch information
Byron committed May 10, 2023
1 parent c86ca69 commit e1fcc7f
Show file tree
Hide file tree
Showing 7 changed files with 479 additions and 67 deletions.
3 changes: 2 additions & 1 deletion gitoxide-core/src/pack/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ mod async_io {
&mut self.ctx,
futures_lite::io::BlockOn::new(input),
progress,
&refs,
refs,
)
}
}
Expand All @@ -241,6 +241,7 @@ mod async_io {
{
let transport = net::connect(
url,
#[allow(clippy::needless_update)]
gix::protocol::transport::client::connect::Options {
version: protocol.unwrap_or_default().into(),
..Default::default()
Expand Down
66 changes: 0 additions & 66 deletions gitoxide-core/src/repository/attributes.rs

This file was deleted.

5 changes: 5 additions & 0 deletions gitoxide-core/src/repository/attributes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod query;
pub use query::function::query;

pub mod validate_baseline;
pub use validate_baseline::function::validate_baseline;
69 changes: 69 additions & 0 deletions gitoxide-core/src/repository/attributes/query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::OutputFormat;

pub struct Options {
pub format: OutputFormat,
pub statistics: bool,
}

pub(crate) mod function {
use crate::repository::attributes::query::{attributes_cache, Options};
use crate::OutputFormat;
use std::io;

use anyhow::bail;
use gix::prelude::FindExt;

pub fn query(
repo: gix::Repository,
pathspecs: impl Iterator<Item = gix::path::Spec>,
mut out: impl io::Write,
mut err: impl io::Write,
Options { format, statistics }: Options,
) -> anyhow::Result<()> {
if format != OutputFormat::Human {
bail!("JSON output isn't implemented yet");
}

let mut cache = attributes_cache(&repo)?;
let prefix = repo.prefix().expect("worktree - we have an index by now")?;
let mut matches = cache.attribute_matches();

for mut spec in pathspecs {
for path in spec.apply_prefix(&prefix).items() {
let is_dir = gix::path::from_bstr(path).metadata().ok().map(|m| m.is_dir());
let entry = cache.at_entry(path, is_dir, |oid, buf| repo.objects.find_blob(oid, buf))?;

if !entry.matching_attributes(&mut matches) {
continue;
}
for m in matches.iter() {
writeln!(
out,
"{}:{}:{}\t{}\t{}",
m.location.source.map(|p| p.to_string_lossy()).unwrap_or_default(),
m.location.sequence_number,
m.pattern,
path,
m.assignment
)?;
}
}
}

if let Some(stats) = statistics.then(|| cache.take_statistics()) {
out.flush()?;
writeln!(err, "{:#?}", stats).ok();
}
Ok(())
}
}

pub(crate) fn attributes_cache(repo: &gix::Repository) -> anyhow::Result<gix::worktree::Cache> {
let index = repo.index()?;
Ok(repo.attributes(
&index,
gix::worktree::cache::state::attributes::Source::WorktreeThenIdMapping,
gix::worktree::cache::state::ignore::Source::IdMapping,
None,
)?)
}
Loading

0 comments on commit e1fcc7f

Please sign in to comment.