1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use std::io::{ErrorKind, Read, Result, Write};

use ext::{ReadExt, WriteExt};

use tracing::{error, info_span};
use util::{ReadPktUntilFlush, WritePkt};
pub(crate) mod ext;
mod processor;
mod util;
pub use processor::*;

#[macro_export]
macro_rules! parse_error {
    ($e:expr) => {
        std::io::Error::new(std::io::ErrorKind::InvalidData, $e)
    };
}

pub struct GitFilterServer<P>(P);

impl<P> GitFilterServer<P> {
    pub fn new(processor: P) -> Self {
        Self(processor)
    }
}

impl<P: Processor> GitFilterServer<P> {
    fn communicate_internal<R: Read, W: Write>(
        &mut self,
        mut input: &mut R,
        mut output: &mut W,
    ) -> Result<()> {
        let mut buf = Vec::new();
        {
            if input.pkt_text_read(&mut buf)? != Some("git-filter-client") {
                return Err(parse_error!("bad prelude"));
            }
            if input.pkt_text_read(&mut buf)? != Some("version=2") {
                return Err(parse_error!("unknown version"));
            }
            if input.pkt_text_read(&mut buf)? != None {
                return Err(parse_error!("unexpected text after client hello"));
            }
        }
        {
            output.pkt_text_write("git-filter-server")?;
            output.pkt_text_write("version=2")?;
            output.pkt_end()?;
        }
        {
            let mut filter = false;
            let mut smudge = false;
            let mut delay = false;
            while let Some(command) = input.pkt_text_read(&mut buf)? {
                match command {
                    "capability=clean" => filter = true,
                    "capability=smudge" => smudge = true,
                    "capability=delay" => delay = true,
                    _ => {}
                }
            }
            if filter && self.0.supports_processing(ProcessingType::Clean) {
                output.pkt_text_write("capability=clean")?;
            }
            if smudge && self.0.supports_processing(ProcessingType::Smudge) {
                output.pkt_text_write("capability=smudge")?;
            }
            if delay {
                output.pkt_text_write("capability=delay")?;
            }
            output.pkt_end()?;
        }

        let mut waiting_for_blobs = false;
        loop {
            let mut command = None;
            let mut pathname = None;
            let mut can_delay = false;
            while let Some(input) = input.pkt_text_read(&mut buf)? {
                if let Some(command_val) = input.strip_prefix("command=") {
                    command = Some(command_val.to_owned());
                } else if let Some(pathname_val) = input.strip_prefix("pathname=") {
                    pathname = Some(pathname_val.to_owned())
                } else if input == "can-delay=1" {
                    can_delay = true;
                }
            }
            let command = command.ok_or_else(|| parse_error!("missing command"))?;
            let _span = info_span!("command", command = format_args!("{:?}", command),).entered();

            match command.as_str() {
                t @ "clean" | t @ "smudge" => {
                    let process_type = match t {
                        "clean" => ProcessingType::Clean,
                        "smudge" => ProcessingType::Smudge,
                        _ => unreachable!(),
                    };
                    let pathname = pathname.ok_or_else(|| parse_error!("missing pathname"))?;
                    let mut process_input = ReadPktUntilFlush::new(&mut input);
                    if waiting_for_blobs {
                        let _span = info_span!(
                            "resolving delayed",
                            pathname = format_args!("{}", pathname)
                        )
                        .entered();
                        let mut sink = [0; 1];
                        process_input
                            .read_exact(&mut sink)
                            .map_err(|_| parse_error!("delayed blob should have no data"))?;
                        assert!(process_input.finished());

                        output.pkt_text_write("status=success")?;
                        output.pkt_end()?;
                        let mut process_output = WritePkt::new(&mut output);
                        if let Err(e) =
                            self.0
                                .get_scheduled(&pathname, process_type, &mut process_output)
                        {
                            process_output.flush()?;
                            drop(process_output);
                            error!("{:#}", e);
                            output.pkt_end()?;
                            output.pkt_text_write("status=error")?;
                            output.pkt_end()?;
                            return Ok(());
                        } else {
                            process_output.flush()?;
                            drop(process_output);
                            output.pkt_end()?;
                            // Keep status
                            output.pkt_end()?;
                        }
                    } else if can_delay && self.0.should_delay(&pathname, process_type) {
                        let _span =
                            info_span!("scheduling", pathname = format_args!("{}", pathname))
                                .entered();
                        if let Err(e) =
                            self.0
                                .schedule_process(&pathname, process_type, &mut process_input)
                        {
                            error!("{:#}", e);
                            output.pkt_text_write("status=error")?;
                            output.pkt_end()?;
                            return Ok(());
                        } else {
                            output.pkt_text_write("status=delayed")?;
                            output.pkt_end()?;
                        }
                    } else {
                        let _span =
                            info_span!("processing", pathname = format_args!("{}", pathname))
                                .entered();
                        output.pkt_text_write("status=success")?;
                        output.pkt_end()?;
                        let mut process_output = WritePkt::new(&mut output);
                        if let Err(e) = self.0.process(
                            &pathname,
                            process_type,
                            &mut process_input,
                            &mut process_output,
                        ) {
                            process_output.flush()?;
                            drop(process_output);
                            error!("{:#}", e);
                            output.pkt_end()?;
                            output.pkt_text_write("status=error")?;
                            output.pkt_end()?;
                            return Ok(());
                        } else {
                            process_output.flush()?;
                            drop(process_output);
                            output.pkt_end()?;
                            // Keep status
                            output.pkt_end()?;
                        }
                    }
                    // Input should be stopped at flush
                    assert!(process_input.finished());
                }
                "list_available_blobs" => {
                    self.0.switch_to_wait();
                    waiting_for_blobs = true;
                }
                cmd => return Err(parse_error!(format!("unknown command: {}", cmd))),
            }
        }
    }

    pub fn communicate<R: Read, W: Write>(&mut self, input: &mut R, output: &mut W) -> Result<()> {
        match self.communicate_internal(input, output) {
            Ok(_) => Ok(()),
            // Communication is done, not a error
            Err(e) if e.kind() == ErrorKind::UnexpectedEof => Ok(()),
            Err(e) => Err(e),
        }
    }

    pub fn communicate_stdio(&mut self) -> Result<()> {
        let stdin = std::io::stdin();
        let stdout = std::io::stdout();

        self.communicate(&mut stdin.lock(), &mut stdout.lock())?;
        Ok(())
    }
}