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

filter programs #914

Merged
merged 6 commits into from
Jun 30, 2023
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ members = [
"gix-worktree",
"gix-revision",
"gix-packetline",
"gix-packetline-blocking",
"gix-mailmap",
"gix-note",
"gix-negotiate",
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ is usable to some extent.
* [gix-validate](https://github.com/Byron/gitoxide/blob/main/crate-status.md#gix-validate)
* [gix-url](https://github.com/Byron/gitoxide/blob/main/crate-status.md#gix-url)
* [gix-packetline](https://github.com/Byron/gitoxide/blob/main/crate-status.md#gix-packetline)
* [gix-packetline-blocking](https://github.com/Byron/gitoxide/blob/main/crate-status.md#gix-packetline)
* [gix-transport](https://github.com/Byron/gitoxide/blob/main/crate-status.md#gix-transport)
* [gix-protocol](https://github.com/Byron/gitoxide/blob/main/crate-status.md#gix-protocol)
* [gix-pack](https://github.com/Byron/gitoxide/blob/main/crate-status.md#gix-pack)
Expand Down
9 changes: 9 additions & 0 deletions gix-filter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ doctest = false
gix-hash = { version = "^0.11.3", path = "../gix-hash" }
gix-trace = { version = "^0.1.2", path = "../gix-trace" }
gix-object = { version = "^0.32.0", path = "../gix-object" }
gix-command = { version = "^0.2.6", path = "../gix-command" }
gix-quote = { version = "^0.4.5", path = "../gix-quote" }
gix-path = { version = "^0.8.2", path = "../gix-path" }
gix-packetline = { package = "gix-packetline-blocking", version = "^0.16.3", path = "../gix-packetline-blocking" }

encoding_rs = "0.8.32"
bstr = { version = "1.5.0", default-features = false, features = ["std"] }
thiserror = "1.0.38"


[dev-dependencies]
once_cell = "1.18.0"
gix-testtools = { path = "../tests/tools" }
204 changes: 204 additions & 0 deletions gix-filter/examples/ident.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
use bstr::{ByteSlice, ByteVec};
use gix_filter::driver::process;
use std::io::{stdin, stdout, Read, Write};
use std::time::Duration;

static PREFIX: &str = "➡";

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args = std::env::args();
let sub_command = args.nth(1).ok_or("Need sub-command")?;
let next_arg = args.next(); // possibly %f
let needs_failure = next_arg.as_deref().map_or(false, |file| file.ends_with("fail"));
if needs_failure {
panic!("failure requested for {sub_command}");
}

match sub_command.as_str() {
"process" => {
let disallow_delay = next_arg.as_deref().map_or(false, |arg| arg == "disallow-delay");
let mut srv = gix_filter::driver::process::Server::handshake(
stdin(),
stdout(),
"git-filter",
|versions| versions.contains(&2).then_some(2),
if disallow_delay {
&["clean", "smudge"]
} else {
&["clean", "smudge", "delay"]
},
)?;

let mut next_smudge_aborts = false;
let mut next_smudge_fails_permanently = false; // a test validates that we don't actually hang
let mut delayed = Vec::new();
while let Some(mut request) = srv.next_request()? {
let needs_failure = request
.meta
.iter()
.find_map(|(key, value)| (key == "pathname").then_some(value))
.map_or(false, |path| path.ends_with(b"fail"));
let pathname = request
.meta
.iter()
.find_map(|(key, value)| (key == "pathname").then(|| value.clone()));
if needs_failure {
panic!("process failure requested: {:?}", request.meta);
}
let can_delay = request
.meta
.iter()
.any(|(key, value)| key == "can-delay" && value == "1");
match request.command.as_str() {
"clean" => {
let mut buf = Vec::new();
request.as_read().read_to_end(&mut buf)?;
request.write_status(if can_delay {
process::Status::delayed()
} else {
process::Status::success()
})?;

let lines = if let Some(delayed_lines) = buf
.is_empty()
.then(|| {
delayed
.iter()
.position(|(cmd, path, _)| {
*cmd == request.command.as_str() && Some(path) == pathname.as_ref()
})
.map(|pos| delayed.remove(pos).2)
})
.flatten()
{
delayed_lines
} else {
let mut lines = Vec::new();
for mut line in buf.lines_with_terminator() {
if line.starts_with(PREFIX.as_bytes()) {
line = &line[PREFIX.len()..];
}
lines.push_str(line);
}
lines
};
if can_delay {
delayed.push(("clean", pathname.expect("needed for delayed operation"), lines));
} else {
request.as_write().write_all(&lines)?;
request.write_status(process::Status::Previous)?;
}
}
"smudge" => {
let mut buf = Vec::new();
request.as_read().read_to_end(&mut buf)?;
let status = if next_smudge_aborts {
next_smudge_aborts = false;
process::Status::abort()
} else if next_smudge_fails_permanently {
process::Status::exit()
} else if can_delay {
process::Status::delayed()
} else {
process::Status::success()
};
request.write_status(status)?;

let lines = if let Some(delayed_lines) = buf
.is_empty()
.then(|| {
delayed
.iter()
.position(|(cmd, path, _)| {
*cmd == request.command.as_str() && Some(path) == pathname.as_ref()
})
.map(|pos| delayed.remove(pos).2)
})
.flatten()
{
delayed_lines
} else {
let mut lines = Vec::new();
for line in buf.lines_with_terminator() {
if !line.starts_with(PREFIX.as_bytes()) {
lines.push_str(PREFIX.as_bytes());
}
lines.push_str(line);
}
lines
};

if can_delay {
delayed.push(("smudge", pathname.expect("needed for delayed operation"), lines));
} else {
request.as_write().write_all(&lines)?;
request.write_status(process::Status::Previous)?;
}
}
"list_available_blobs" => {
{
let mut out = request.as_write();
let mut last_cmd = None;
let mut buf = Vec::<u8>::new();
for (cmd, path, _) in &delayed {
if last_cmd.get_or_insert(*cmd) != cmd {
panic!("the API doesn't support mixing cmds as paths might not be unique anymore")
}
buf.clear();
buf.push_str("pathname=");
buf.extend_from_slice(path);
out.write_all(&buf)?
}
}
request.write_status(process::Status::success())?;
}
"wait-1-s" => {
std::io::copy(&mut request.as_read(), &mut std::io::sink())?;
request.write_status(process::Status::success())?;
std::thread::sleep(Duration::from_secs(1));
}
"next-smudge-aborts" => {
std::io::copy(&mut request.as_read(), &mut std::io::sink())?;
request.write_status(process::Status::success())?;
next_smudge_aborts = true;
}
"next-invocation-returns-strange-status-and-smudge-fails-permanently" => {
std::io::copy(&mut request.as_read(), &mut std::io::sink())?;
request.write_status(process::Status::success())?;
next_smudge_fails_permanently = true;
}
unknown => panic!("Unknown capability requested: {unknown}"),
}
}
}
// simple filters actually don't support streaming - they have to first read all input, then produce all output,
// but can't mix reading stdin and write to stdout at the same time as `git` (or `gitoxide`) don't read the output while
// writing the input.
"clean" => {
let mut stdin = stdin().lock();
let mut stdout = stdout().lock();
let mut buf = Vec::new();
std::io::copy(&mut stdin, &mut buf)?;
for mut line in buf.lines_with_terminator() {
if line.starts_with(PREFIX.as_bytes()) {
line = &line[PREFIX.len()..];
}
stdout.write_all(line).map(|_| true)?;
}
}
"smudge" => {
let mut stdin = stdin().lock();
let mut stdout = stdout().lock();
let mut buf = Vec::new();
std::io::copy(&mut stdin, &mut buf)?;
for line in buf.lines_with_terminator() {
if !line.starts_with(PREFIX.as_bytes()) {
stdout.write_all(PREFIX.as_bytes())?;
}
stdout.write_all(line).map(|_| true)?;
}
}
unknown => panic!("Unknown sub-command: {unknown}"),
}
Ok(())
}
Loading
Loading