pub fn compress(input: &str) -> String {
let mut out: Vec<&str> = Vec::new();
let mut current_file: Option<&str> = None;
for line in input.lines() {
if line.starts_with("diff --git") {
current_file = Some(line);
out.push(line);
} else if line.starts_with("--- ") || line.starts_with("+++ ") {
out.push(line);
} else if line.starts_with("@@") {
out.push("---");
} else if line.starts_with('+') || line.starts_with('-') {
let _ = current_file; out.push(line);
}
}
out.join("\n")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn strips_context_lines() {
let diff = "\
diff --git a/src/main.rs b/src/main.rs
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,5 @@
use std::io;
-fn old() {}
+fn new() {}
fn other() {}
";
let out = compress(diff);
assert!(out.contains("+fn new()"));
assert!(out.contains("-fn old()"));
assert!(!out.contains(" fn other")); assert!(!out.contains("@@ -1,5")); assert!(out.contains("---")); }
#[test]
fn preserves_file_headers() {
let diff = "diff --git a/foo.rs b/foo.rs\n--- a/foo.rs\n+++ b/foo.rs\n";
let out = compress(diff);
assert!(out.contains("diff --git"));
assert!(out.contains("--- a/foo.rs"));
assert!(out.contains("+++ b/foo.rs"));
}
}