|
| 1 | +use bstr::{ByteSlice, ByteVec}; |
| 2 | +use gix_filter::driver::process; |
| 3 | +use std::io::{stdin, stdout, Read, Write}; |
| 4 | +use std::time::Duration; |
| 5 | + |
| 6 | +static PREFIX: &str = "➡"; |
| 7 | + |
| 8 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 9 | + let mut args = std::env::args(); |
| 10 | + let sub_command = args.nth(1).ok_or("Need sub-command")?; |
| 11 | + let next_arg = args.next(); // possibly %f |
| 12 | + let needs_failure = next_arg.as_deref().map_or(false, |file| file.ends_with("fail")); |
| 13 | + if needs_failure { |
| 14 | + panic!("failure requested for {sub_command}"); |
| 15 | + } |
| 16 | + |
| 17 | + match sub_command.as_str() { |
| 18 | + "process" => { |
| 19 | + let disallow_delay = next_arg.as_deref().map_or(false, |arg| arg == "disallow-delay"); |
| 20 | + let mut srv = gix_filter::driver::process::Server::handshake( |
| 21 | + stdin(), |
| 22 | + stdout(), |
| 23 | + "git-filter", |
| 24 | + |versions| versions.contains(&2).then_some(2), |
| 25 | + if disallow_delay { |
| 26 | + &["clean", "smudge"] |
| 27 | + } else { |
| 28 | + &["clean", "smudge", "delay"] |
| 29 | + }, |
| 30 | + )?; |
| 31 | + |
| 32 | + let mut next_smudge_aborts = false; |
| 33 | + let mut next_smudge_fails_permanently = false; // a test validates that we don't actually hang |
| 34 | + let mut delayed = Vec::new(); |
| 35 | + while let Some(mut request) = srv.next_request()? { |
| 36 | + let needs_failure = request |
| 37 | + .meta |
| 38 | + .iter() |
| 39 | + .find_map(|(key, value)| (key == "pathname").then_some(value)) |
| 40 | + .map_or(false, |path| path.ends_with(b"fail")); |
| 41 | + let pathname = request |
| 42 | + .meta |
| 43 | + .iter() |
| 44 | + .find_map(|(key, value)| (key == "pathname").then(|| value.clone())); |
| 45 | + if needs_failure { |
| 46 | + panic!("process failure requested: {:?}", request.meta); |
| 47 | + } |
| 48 | + let can_delay = request |
| 49 | + .meta |
| 50 | + .iter() |
| 51 | + .any(|(key, value)| key == "can-delay" && value == "1"); |
| 52 | + match request.command.as_str() { |
| 53 | + "clean" => { |
| 54 | + let mut buf = Vec::new(); |
| 55 | + request.as_read().read_to_end(&mut buf)?; |
| 56 | + request.write_status(if can_delay { |
| 57 | + process::Status::delayed() |
| 58 | + } else { |
| 59 | + process::Status::success() |
| 60 | + })?; |
| 61 | + |
| 62 | + let lines = if let Some(delayed_lines) = buf |
| 63 | + .is_empty() |
| 64 | + .then(|| { |
| 65 | + delayed |
| 66 | + .iter() |
| 67 | + .position(|(cmd, path, _)| { |
| 68 | + *cmd == request.command.as_str() && Some(path) == pathname.as_ref() |
| 69 | + }) |
| 70 | + .map(|pos| delayed.remove(pos).2) |
| 71 | + }) |
| 72 | + .flatten() |
| 73 | + { |
| 74 | + delayed_lines |
| 75 | + } else { |
| 76 | + let mut lines = Vec::new(); |
| 77 | + for mut line in buf.lines_with_terminator() { |
| 78 | + if line.starts_with(PREFIX.as_bytes()) { |
| 79 | + line = &line[PREFIX.len()..]; |
| 80 | + } |
| 81 | + lines.push_str(line); |
| 82 | + } |
| 83 | + lines |
| 84 | + }; |
| 85 | + if can_delay { |
| 86 | + delayed.push(("clean", pathname.expect("needed for delayed operation"), lines)); |
| 87 | + } else { |
| 88 | + request.as_write().write_all(&lines)?; |
| 89 | + request.write_status(process::Status::Previous)?; |
| 90 | + } |
| 91 | + } |
| 92 | + "smudge" => { |
| 93 | + let mut buf = Vec::new(); |
| 94 | + request.as_read().read_to_end(&mut buf)?; |
| 95 | + let status = if next_smudge_aborts { |
| 96 | + next_smudge_aborts = false; |
| 97 | + process::Status::abort() |
| 98 | + } else if next_smudge_fails_permanently { |
| 99 | + process::Status::exit() |
| 100 | + } else if can_delay { |
| 101 | + process::Status::delayed() |
| 102 | + } else { |
| 103 | + process::Status::success() |
| 104 | + }; |
| 105 | + request.write_status(status)?; |
| 106 | + |
| 107 | + let lines = if let Some(delayed_lines) = buf |
| 108 | + .is_empty() |
| 109 | + .then(|| { |
| 110 | + delayed |
| 111 | + .iter() |
| 112 | + .position(|(cmd, path, _)| { |
| 113 | + *cmd == request.command.as_str() && Some(path) == pathname.as_ref() |
| 114 | + }) |
| 115 | + .map(|pos| delayed.remove(pos).2) |
| 116 | + }) |
| 117 | + .flatten() |
| 118 | + { |
| 119 | + delayed_lines |
| 120 | + } else { |
| 121 | + let mut lines = Vec::new(); |
| 122 | + for line in buf.lines_with_terminator() { |
| 123 | + if !line.starts_with(PREFIX.as_bytes()) { |
| 124 | + lines.push_str(PREFIX.as_bytes()); |
| 125 | + } |
| 126 | + lines.push_str(line); |
| 127 | + } |
| 128 | + lines |
| 129 | + }; |
| 130 | + |
| 131 | + if can_delay { |
| 132 | + delayed.push(("smudge", pathname.expect("needed for delayed operation"), lines)); |
| 133 | + } else { |
| 134 | + request.as_write().write_all(&lines)?; |
| 135 | + request.write_status(process::Status::Previous)?; |
| 136 | + } |
| 137 | + } |
| 138 | + "list_available_blobs" => { |
| 139 | + { |
| 140 | + let mut out = request.as_write(); |
| 141 | + let mut last_cmd = None; |
| 142 | + let mut buf = Vec::<u8>::new(); |
| 143 | + for (cmd, path, _) in &delayed { |
| 144 | + if last_cmd.get_or_insert(*cmd) != cmd { |
| 145 | + panic!("the API doesn't support mixing cmds as paths might not be unique anymore") |
| 146 | + } |
| 147 | + buf.clear(); |
| 148 | + buf.push_str("pathname="); |
| 149 | + buf.extend_from_slice(path); |
| 150 | + out.write_all(&buf)? |
| 151 | + } |
| 152 | + } |
| 153 | + request.write_status(process::Status::success())?; |
| 154 | + } |
| 155 | + "wait-1-s" => { |
| 156 | + std::io::copy(&mut request.as_read(), &mut std::io::sink())?; |
| 157 | + request.write_status(process::Status::success())?; |
| 158 | + std::thread::sleep(Duration::from_secs(1)); |
| 159 | + } |
| 160 | + "next-smudge-aborts" => { |
| 161 | + std::io::copy(&mut request.as_read(), &mut std::io::sink())?; |
| 162 | + request.write_status(process::Status::success())?; |
| 163 | + next_smudge_aborts = true; |
| 164 | + } |
| 165 | + "next-invocation-returns-strange-status-and-smudge-fails-permanently" => { |
| 166 | + std::io::copy(&mut request.as_read(), &mut std::io::sink())?; |
| 167 | + request.write_status(process::Status::success())?; |
| 168 | + next_smudge_fails_permanently = true; |
| 169 | + } |
| 170 | + unknown => panic!("Unknown capability requested: {unknown}"), |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + // simple filters actually don't support streaming - they have to first read all input, then produce all output, |
| 175 | + // but can't mix reading stdin and write to stdout at the same time as `git` (or `gitoxide`) don't read the output while |
| 176 | + // writing the input. |
| 177 | + "clean" => { |
| 178 | + let mut stdin = stdin().lock(); |
| 179 | + let mut stdout = stdout().lock(); |
| 180 | + let mut buf = Vec::new(); |
| 181 | + std::io::copy(&mut stdin, &mut buf)?; |
| 182 | + for mut line in buf.lines_with_terminator() { |
| 183 | + if line.starts_with(PREFIX.as_bytes()) { |
| 184 | + line = &line[PREFIX.len()..]; |
| 185 | + } |
| 186 | + stdout.write_all(line).map(|_| true)?; |
| 187 | + } |
| 188 | + } |
| 189 | + "smudge" => { |
| 190 | + let mut stdin = stdin().lock(); |
| 191 | + let mut stdout = stdout().lock(); |
| 192 | + let mut buf = Vec::new(); |
| 193 | + std::io::copy(&mut stdin, &mut buf)?; |
| 194 | + for line in buf.lines_with_terminator() { |
| 195 | + if !line.starts_with(PREFIX.as_bytes()) { |
| 196 | + stdout.write_all(PREFIX.as_bytes())?; |
| 197 | + } |
| 198 | + stdout.write_all(line).map(|_| true)?; |
| 199 | + } |
| 200 | + } |
| 201 | + unknown => panic!("Unknown sub-command: {unknown}"), |
| 202 | + } |
| 203 | + Ok(()) |
| 204 | +} |
0 commit comments