use similar::{ChangeTag, TextDiff};
pub const DEFAULT_CONTEXT_LINES: usize = 3;
pub const MAX_TEXT_DIFF_BYTES: usize = 16 * 1024 * 1024;
pub fn is_binary(bytes: &[u8]) -> bool {
bytes.contains(&0)
}
pub fn binary_or_large_notice(path: &str, old_bytes: &[u8], new_bytes: &[u8]) -> Option<String> {
binary_or_oversize_notice(path, old_bytes, new_bytes, Some(MAX_TEXT_DIFF_BYTES))
}
pub fn binary_or_oversize_notice(
path: &str,
old_bytes: &[u8],
new_bytes: &[u8],
max_text_bytes: Option<usize>,
) -> Option<String> {
if is_binary(old_bytes) || is_binary(new_bytes) {
return Some(format!("Binary files a/{path} and b/{path} differ"));
}
if max_text_bytes.is_some_and(|limit| old_bytes.len() > limit || new_bytes.len() > limit) {
return Some(format!(
"Binary files a/{path} and b/{path} differ ({} -> {} bytes; text diff omitted, use --text to force)",
old_bytes.len(),
new_bytes.len()
));
}
None
}
pub type InlineRanges = Vec<std::ops::Range<usize>>;
pub fn inline_diff_ranges(old: &str, new: &str) -> Option<(InlineRanges, InlineRanges)> {
let diff = TextDiff::from_words(old, new);
if diff.ratio() < 0.4 {
return None;
}
let mut old_ranges: InlineRanges = Vec::new();
let mut new_ranges: InlineRanges = Vec::new();
let (mut old_pos, mut new_pos) = (0usize, 0usize);
for change in diff.iter_all_changes() {
let len = change.value().len();
match change.tag() {
ChangeTag::Equal => {
old_pos += len;
new_pos += len;
}
ChangeTag::Delete => {
match old_ranges.last_mut() {
Some(last) if last.end == old_pos => last.end = old_pos + len,
_ => old_ranges.push(old_pos..old_pos + len),
}
old_pos += len;
}
ChangeTag::Insert => {
match new_ranges.last_mut() {
Some(last) if last.end == new_pos => last.end = new_pos + len,
_ => new_ranges.push(new_pos..new_pos + len),
}
new_pos += len;
}
}
}
Some((old_ranges, new_ranges))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DiffLine {
Context(String),
Added(String),
Removed(String),
}
pub const NO_NEWLINE_MARKER: &str = "\\ No newline at end of file";
fn split_lines_keep_newline(text: &str) -> Vec<&str> {
let mut lines = Vec::new();
let mut rest = text;
while let Some(idx) = rest.find('\n') {
lines.push(&rest[..=idx]);
rest = &rest[idx + 1..];
}
if !rest.is_empty() {
lines.push(rest);
}
lines
}
pub fn unified_side_headers(path: &str, old_exists: bool, new_exists: bool) -> [String; 2] {
[
if old_exists {
format!("--- a/{path}")
} else {
"--- /dev/null".to_string()
},
if new_exists {
format!("+++ b/{path}")
} else {
"+++ /dev/null".to_string()
},
]
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct FileDiff {
pub path: String,
pub lines: Vec<DiffLine>,
pub has_changes: bool,
pub missing_newline_at: Vec<usize>,
}
impl FileDiff {
pub fn new(path: &str, old: &str, new: &str) -> Self {
let old_lines = split_lines_keep_newline(old);
let new_lines = split_lines_keep_newline(new);
let diff = TextDiff::configure().diff_slices(&old_lines, &new_lines);
let mut lines = Vec::new();
let mut has_changes = false;
let mut missing_newline_at = Vec::new();
for change in diff.iter_all_changes() {
let value: &str = change.value();
if !value.ends_with('\n') {
missing_newline_at.push(lines.len());
}
let line = value.strip_suffix('\n').unwrap_or(value).to_string();
match change.tag() {
ChangeTag::Equal => {
lines.push(DiffLine::Context(line));
}
ChangeTag::Delete => {
lines.push(DiffLine::Removed(line));
has_changes = true;
}
ChangeTag::Insert => {
lines.push(DiffLine::Added(line));
has_changes = true;
}
}
}
FileDiff {
path: path.to_string(),
lines,
has_changes,
missing_newline_at,
}
}
pub fn to_unified(&self) -> String {
self.to_unified_with_context(DEFAULT_CONTEXT_LINES)
}
pub fn to_unified_with_context(&self, context: usize) -> String {
self.to_unified_with_sides(context, true, true)
}
pub fn to_unified_with_sides(
&self,
context: usize,
old_exists: bool,
new_exists: bool,
) -> String {
let mut output = String::new();
if !self.has_changes {
return output;
}
output.push_str(&format!("diff --oak a/{} b/{}\n", self.path, self.path));
for header in unified_side_headers(&self.path, old_exists, new_exists) {
output.push_str(&header);
output.push('\n');
}
let hunks = self.compute_hunks(context);
for hunk in hunks {
for line in hunk.unified_lines() {
output.push_str(&line);
output.push('\n');
}
}
output
}
pub fn hunks(&self, context: usize) -> Vec<Hunk> {
self.compute_hunks(context)
}
fn compute_hunks(&self, context: usize) -> Vec<Hunk> {
if self.lines.is_empty() {
return vec![];
}
let change_indices: Vec<usize> = self
.lines
.iter()
.enumerate()
.filter(|(_, line)| !matches!(line, DiffLine::Context(_)))
.map(|(i, _)| i)
.collect();
if change_indices.is_empty() {
return vec![];
}
let mut hunks = Vec::new();
let mut hunk_start = change_indices[0];
let mut hunk_end = change_indices[0];
for &idx in &change_indices[1..] {
if idx <= hunk_end + 2 * context + 1 {
hunk_end = idx;
} else {
hunks.push(self.create_hunk(hunk_start, hunk_end, context));
hunk_start = idx;
hunk_end = idx;
}
}
hunks.push(self.create_hunk(hunk_start, hunk_end, context));
hunks
}
fn create_hunk(&self, first_change: usize, last_change: usize, context: usize) -> Hunk {
let start = first_change.saturating_sub(context);
let end = (last_change + context + 1).min(self.lines.len());
let lines: Vec<DiffLine> = self.lines[start..end].to_vec();
let missing_newline_at: Vec<usize> = self
.missing_newline_at
.iter()
.filter(|&&idx| idx >= start && idx < end)
.map(|&idx| idx - start)
.collect();
let mut old_line = 1;
let mut new_line = 1;
for line in &self.lines[..start] {
match line {
DiffLine::Context(_) => {
old_line += 1;
new_line += 1;
}
DiffLine::Removed(_) => {
old_line += 1;
}
DiffLine::Added(_) => {
new_line += 1;
}
}
}
let old_start = old_line;
let new_start = new_line;
let mut old_count = 0;
let mut new_count = 0;
for line in &lines {
match line {
DiffLine::Context(_) => {
old_count += 1;
new_count += 1;
}
DiffLine::Removed(_) => {
old_count += 1;
}
DiffLine::Added(_) => {
new_count += 1;
}
}
}
Hunk::new(
old_start,
old_count,
new_start,
new_count,
lines,
missing_newline_at,
)
}
}
#[non_exhaustive]
pub struct Hunk {
pub old_start: usize,
pub old_count: usize,
pub new_start: usize,
pub new_count: usize,
pub lines: Vec<DiffLine>,
pub missing_newline_at: Vec<usize>,
}
impl Hunk {
pub fn new(
old_start: usize,
old_count: usize,
new_start: usize,
new_count: usize,
lines: Vec<DiffLine>,
missing_newline_at: Vec<usize>,
) -> Self {
Hunk {
old_start,
old_count,
new_start,
new_count,
lines,
missing_newline_at,
}
}
pub fn header(&self) -> String {
let range = |start: usize, count: usize| {
let start = if count == 0 {
start.saturating_sub(1)
} else {
start
};
format!("{start},{count}")
};
format!(
"@@ -{} +{} @@",
range(self.old_start, self.old_count),
range(self.new_start, self.new_count)
)
}
pub fn unified_lines(&self) -> Vec<String> {
let mut out = Vec::with_capacity(self.lines.len() + 1 + self.missing_newline_at.len());
out.push(self.header());
for (idx, line) in self.lines.iter().enumerate() {
out.push(match line {
DiffLine::Context(s) => format!(" {s}"),
DiffLine::Added(s) => format!("+{s}"),
DiffLine::Removed(s) => format!("-{s}"),
});
if self.missing_newline_at.contains(&idx) {
out.push(NO_NEWLINE_MARKER.to_string());
}
}
out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_diff_no_changes() {
let content = "line1\nline2\nline3\n";
let diff = FileDiff::new("test.txt", content, content);
assert!(!diff.has_changes);
}
#[test]
fn test_diff_added_lines() {
let old = "line1\nline2\n";
let new = "line1\nline2\nline3\n";
let diff = FileDiff::new("test.txt", old, new);
assert!(diff.has_changes);
assert!(diff.lines.iter().any(|l| matches!(l, DiffLine::Added(_))));
}
#[test]
fn test_diff_removed_lines() {
let old = "line1\nline2\nline3\n";
let new = "line1\nline2\n";
let diff = FileDiff::new("test.txt", old, new);
assert!(diff.has_changes);
assert!(diff.lines.iter().any(|l| matches!(l, DiffLine::Removed(_))));
}
#[test]
fn test_diff_modified_lines() {
let old = "line1\nline2\nline3\n";
let new = "line1\nmodified\nline3\n";
let diff = FileDiff::new("test.txt", old, new);
assert!(diff.has_changes);
}
#[test]
fn test_unified_output() {
let old = "line1\nline2\n";
let new = "line1\nline2\nline3\n";
let diff = FileDiff::new("test.txt", old, new);
let output = diff.to_unified();
assert!(output.contains("diff --oak"));
assert!(output.contains("@@"));
assert!(output.contains("+line3"));
}
#[test]
fn test_unified_shows_limited_context() {
let old = (1..=20)
.map(|i| format!("line{i}"))
.collect::<Vec<_>>()
.join("\n")
+ "\n";
let new = (1..=20)
.map(|i| {
if i == 10 {
"modified".to_string()
} else {
format!("line{i}")
}
})
.collect::<Vec<_>>()
.join("\n")
+ "\n";
let diff = FileDiff::new("test.txt", &old, &new);
let output = diff.to_unified();
assert!(output.contains("@@"));
assert!(
!output.contains(" line1\n"),
"Should not show line1 (too far from change)"
);
assert!(
!output.contains(" line2\n"),
"Should not show line2 (too far from change)"
);
assert!(
!output.contains(" line3\n"),
"Should not show line3 (too far from change)"
);
assert!(
!output.contains(" line4\n"),
"Should not show line4 (too far from change)"
);
assert!(
!output.contains(" line5\n"),
"Should not show line5 (too far from change)"
);
assert!(
!output.contains(" line6\n"),
"Should not show line6 (too far from change)"
);
assert!(
output.contains(" line7\n"),
"Should show line7 (context before)"
);
assert!(
output.contains(" line8\n"),
"Should show line8 (context before)"
);
assert!(
output.contains(" line9\n"),
"Should show line9 (context before)"
);
assert!(output.contains("-line10\n"), "Should show removed line10");
assert!(output.contains("+modified\n"), "Should show added modified");
assert!(
output.contains(" line11\n"),
"Should show line11 (context after)"
);
assert!(
output.contains(" line12\n"),
"Should show line12 (context after)"
);
assert!(
output.contains(" line13\n"),
"Should show line13 (context after)"
);
assert!(
!output.contains(" line20\n"),
"Should not show line20 (too far from change)"
);
}
#[test]
fn test_multiple_hunks() {
let old = (1..=20)
.map(|i| format!("line{i}"))
.collect::<Vec<_>>()
.join("\n")
+ "\n";
let new = (1..=20)
.map(|i| {
if i == 2 {
"change1".to_string()
} else if i == 15 {
"change2".to_string()
} else {
format!("line{i}")
}
})
.collect::<Vec<_>>()
.join("\n")
+ "\n";
let diff = FileDiff::new("test.txt", &old, &new);
let output = diff.to_unified();
let hunk_count = output.matches("@@").count() / 2; assert!(
hunk_count >= 2,
"Should have at least 2 hunks, got {hunk_count}"
);
}
#[test]
fn test_diff_empty_files() {
let diff = FileDiff::new("empty.txt", "", "");
assert!(!diff.has_changes);
let output = diff.to_unified();
assert!(output.is_empty());
}
#[test]
fn test_diff_new_file_from_empty() {
let diff = FileDiff::new("new.txt", "", "hello\nworld\n");
assert!(diff.has_changes);
let output = diff.to_unified();
assert!(output.contains("+hello"));
assert!(output.contains("+world"));
}
#[test]
fn test_diff_delete_entire_file() {
let diff = FileDiff::new("gone.txt", "hello\nworld\n", "");
assert!(diff.has_changes);
let output = diff.to_unified();
assert!(output.contains("-hello"));
assert!(output.contains("-world"));
}
#[test]
fn test_diff_single_line_change() {
let diff = FileDiff::new("f.txt", "one line\n", "different line\n");
assert!(diff.has_changes);
let output = diff.to_unified();
assert!(output.contains("-one line"));
assert!(output.contains("+different line"));
}
#[test]
fn test_diff_no_trailing_newline() {
let diff = FileDiff::new("f.txt", "no newline", "no newline");
assert!(!diff.has_changes);
}
#[test]
fn test_diff_crlf_vs_lf() {
let diff = FileDiff::new("f.txt", "line\r\n", "line\n");
assert!(diff.has_changes);
}
fn hunk_body(path: &str, old: &str, new: &str, context: usize) -> String {
let unified = FileDiff::new(path, old, new).to_unified_with_context(context);
let header = format!("diff --oak a/{path} b/{path}\n--- a/{path}\n+++ b/{path}\n");
unified
.strip_prefix(&header)
.unwrap_or_else(|| panic!("unexpected file header in {unified:?}"))
.to_string()
}
#[test]
fn missing_newline_on_both_sides_marks_removed_and_added_final_lines() {
let diff = FileDiff::new("f.txt", "a\nb\nc", "a\nB\nc");
assert_eq!(diff.missing_newline_at, vec![3]);
assert_eq!(
hunk_body("f.txt", "a\nb\nc", "a\nB\nc", 3),
"@@ -1,3 +1,3 @@\n a\n-b\n+B\n c\n\\ No newline at end of file\n"
);
}
#[test]
fn newline_only_transitions_render_as_a_marked_replace() {
assert_eq!(
hunk_body("f.txt", "a\nb\nc", "a\nb\nc\n", 3),
"@@ -1,3 +1,3 @@\n a\n b\n-c\n\\ No newline at end of file\n+c\n"
);
assert_eq!(
hunk_body("f.txt", "a\nb\nc\n", "a\nb\nc", 3),
"@@ -1,3 +1,3 @@\n a\n b\n-c\n+c\n\\ No newline at end of file\n"
);
}
#[test]
fn unterminated_final_line_that_changes_gets_a_marker_on_each_side() {
let diff = FileDiff::new("f.txt", "a\nb\nc", "a\nb\nC");
assert_eq!(diff.missing_newline_at, vec![2, 3]);
assert_eq!(
hunk_body("f.txt", "a\nb\nc", "a\nb\nC", 3),
"@@ -1,3 +1,3 @@\n a\n b\n-c\n\\ No newline at end of file\n+C\n\\ No newline at end of file\n"
);
assert_eq!(
hunk_body("f.txt", "a\nb\nc", "a\nb\nc\nd", 3),
"@@ -1,3 +1,4 @@\n a\n b\n-c\n\\ No newline at end of file\n+c\n+d\n\\ No newline at end of file\n"
);
}
#[test]
fn marker_follows_the_final_line_only_when_the_hunk_reaches_it() {
let old = "a\nb\nc\nd\ne\nf\ng\nh";
let new = "a\nX\nc\nd\ne\nf\ng\nh";
assert_eq!(
hunk_body("f.txt", old, new, 3),
"@@ -1,5 +1,5 @@\n a\n-b\n+X\n c\n d\n e\n"
);
assert_eq!(
hunk_body("f.txt", old, new, 10),
"@@ -1,8 +1,8 @@\n a\n-b\n+X\n c\n d\n e\n f\n g\n h\n\\ No newline at end of file\n"
);
let diff = FileDiff::new("f.txt", old, new);
let hunks = diff.hunks(10);
assert_eq!(hunks.len(), 1);
assert_eq!(hunks[0].missing_newline_at, vec![hunks[0].lines.len() - 1]);
assert!(diff.hunks(3)[0].missing_newline_at.is_empty());
}
#[test]
fn multiple_hunks_keep_their_count_and_only_the_last_can_carry_a_marker() {
let old = (1..=20)
.map(|i| format!("line{i}"))
.collect::<Vec<_>>()
.join("\n");
let new = old
.replace("line2\n", "change1\n")
.replace("line20", "change2");
let diff = FileDiff::new("f.txt", &old, &new);
let hunks = diff.hunks(3);
assert_eq!(hunks.len(), 2, "two far-apart edits stay two hunks");
assert!(hunks[0].missing_newline_at.is_empty());
assert_eq!(hunks[1].missing_newline_at.len(), 2);
let rendered = diff.to_unified();
assert_eq!(rendered.matches("\n@@ ").count(), 2);
assert!(rendered.ends_with(
"-line20\n\\ No newline at end of file\n+change2\n\\ No newline at end of file\n"
));
}
#[test]
fn added_and_deleted_unterminated_files_are_marked() {
assert_eq!(
hunk_body("new.txt", "", "hello\nworld", 3),
"@@ -0,0 +1,2 @@\n+hello\n+world\n\\ No newline at end of file\n"
);
assert_eq!(
hunk_body("gone.txt", "hello\nworld", "", 3),
"@@ -1,2 +0,0 @@\n-hello\n-world\n\\ No newline at end of file\n"
);
assert_eq!(
hunk_body("new.txt", "", "hello\nworld\n", 3),
"@@ -0,0 +1,2 @@\n+hello\n+world\n"
);
}
#[test]
fn crlf_content_keeps_its_carriage_returns_and_marks_the_missing_lf() {
assert_eq!(
hunk_body("f.txt", "a\r\nb\r\nc", "a\r\nB\r\nc", 3),
"@@ -1,3 +1,3 @@\n a\r\n-b\r\n+B\r\n c\n\\ No newline at end of file\n"
);
assert_eq!(
hunk_body("f.txt", "a\r\nb", "a\r\nb\r\n", 3),
"@@ -1,2 +1,2 @@\n a\r\n-b\n\\ No newline at end of file\n+b\r\n"
);
}
#[test]
fn lone_carriage_return_is_content_not_a_line_terminator() {
let diff = FileDiff::new("f.txt", "a\rb", "a\rB");
assert_eq!(
diff.lines,
vec![
DiffLine::Removed("a\rb".to_string()),
DiffLine::Added("a\rB".to_string())
]
);
assert_eq!(
hunk_body("f.txt", "a\rb", "a\rB", 3),
"@@ -1,1 +1,1 @@\n-a\rb\n\\ No newline at end of file\n+a\rB\n\\ No newline at end of file\n"
);
assert_eq!(
hunk_body("f.txt", "a\nc\r", "a\nc\r\n", 3),
"@@ -1,2 +1,2 @@\n a\n-c\r\n\\ No newline at end of file\n+c\r\n"
);
assert!(!FileDiff::new("f.txt", "x\ry\nz\n", "x\ry\nz\n").has_changes);
}
#[test]
fn split_lines_keep_newline_splits_on_lf_only() {
assert_eq!(split_lines_keep_newline(""), Vec::<&str>::new());
assert_eq!(split_lines_keep_newline("\n"), vec!["\n"]);
assert_eq!(split_lines_keep_newline("a"), vec!["a"]);
assert_eq!(split_lines_keep_newline("a\nb"), vec!["a\n", "b"]);
assert_eq!(
split_lines_keep_newline("a\r\nb\r\n"),
vec!["a\r\n", "b\r\n"]
);
assert_eq!(split_lines_keep_newline("a\rb\n"), vec!["a\rb\n"]);
assert_eq!(split_lines_keep_newline("a\n\n"), vec!["a\n", "\n"]);
}
#[test]
fn terminated_content_never_carries_a_marker() {
for (old, new) in [
("a\nb\nc\n", "a\nB\nc\n"),
("", "\n"),
("\n", ""),
("a\n\n", "a\n"),
] {
let diff = FileDiff::new("f.txt", old, new);
assert!(diff.missing_newline_at.is_empty(), "{old:?} -> {new:?}");
assert!(!diff.to_unified().contains(NO_NEWLINE_MARKER));
}
}
#[test]
fn hunk_unified_lines_match_the_unified_string() {
let diff = FileDiff::new("f.txt", "a\nb\nc", "a\nB\nc\nd");
let mut from_hunks = String::from("diff --oak a/f.txt b/f.txt\n--- a/f.txt\n+++ b/f.txt\n");
for hunk in diff.hunks(3) {
for line in hunk.unified_lines() {
from_hunks.push_str(&line);
from_hunks.push('\n');
}
}
assert_eq!(from_hunks, diff.to_unified());
}
#[test]
fn test_is_binary_detects_nul() {
assert!(is_binary(b"abc\0def"));
assert!(!is_binary(b"plain text\n"));
assert!(!is_binary(b""));
}
#[test]
fn test_binary_notice_for_nul_content() {
let notice = binary_or_large_notice("a.bin", b"\0\0", b"\0\x01");
assert_eq!(
notice.as_deref(),
Some("Binary files a/a.bin and b/a.bin differ")
);
}
#[test]
fn test_large_text_gets_notice() {
let big = vec![b'a'; MAX_TEXT_DIFF_BYTES + 1];
let notice = binary_or_large_notice("big.txt", b"small", &big);
assert!(notice.is_some());
assert!(notice.unwrap().contains("text diff omitted"));
}
#[test]
fn test_force_text_keeps_binary_guard_but_overrides_size_guard() {
let big = vec![b'a'; MAX_TEXT_DIFF_BYTES + 1];
assert!(binary_or_oversize_notice("big.txt", b"small", &big, None).is_none());
assert!(binary_or_oversize_notice("bin.dat", b"\0", &big, None).is_some());
}
#[test]
fn test_normal_text_no_notice() {
assert!(binary_or_large_notice("f.txt", b"old\n", b"new\n").is_none());
}
#[test]
fn test_diff_with_custom_context() {
let old = "line1\nline2\nline3\nline4\nline5\n";
let new = "line1\nline2\nchanged\nline4\nline5\n";
let diff = FileDiff::new("f.txt", old, new);
let output = diff.to_unified_with_context(1);
assert!(output.contains(" line2"));
assert!(output.contains("-line3"));
assert!(output.contains("+changed"));
assert!(output.contains(" line4"));
assert!(!output.contains(" line1"));
}
#[test]
fn inline_diff_ranges_marks_changed_words_with_exact_byte_offsets() {
let old = "the quick brown fox jumps";
let new = "the quick red fox leaps";
let (old_ranges, new_ranges) = inline_diff_ranges(old, new).expect("similar enough");
let old_words: Vec<&str> = old_ranges.iter().map(|r| &old[r.clone()]).collect();
let new_words: Vec<&str> = new_ranges.iter().map(|r| &new[r.clone()]).collect();
assert_eq!(old_words, vec!["brown", "jumps"]);
assert_eq!(new_words, vec!["red", "leaps"]);
}
#[test]
fn inline_diff_ranges_declines_mostly_different_lines() {
assert!(inline_diff_ranges("AAA", "BBB").is_none());
}
#[test]
fn inline_diff_ranges_handles_multibyte_content() {
let old = "prefix héllo suffix";
let new = "prefix wörld suffix";
let (old_ranges, new_ranges) = inline_diff_ranges(old, new).expect("similar enough");
for r in &old_ranges {
let _ = &old[r.clone()];
}
for r in &new_ranges {
let _ = &new[r.clone()];
}
}
#[test]
fn empty_ranges_are_numbered_like_git() {
let diff = FileDiff::new("f.txt", "a\nb\nc\n", "a\nb\nX\nc\n");
let hunks = diff.hunks(0);
assert_eq!(hunks.len(), 1);
assert_eq!(hunks[0].header(), "@@ -2,0 +3,1 @@");
let diff = FileDiff::new("f.txt", "a\nb\nc\n", "a\nc\n");
assert_eq!(diff.hunks(0)[0].header(), "@@ -2,1 +1,0 @@");
}
#[test]
fn side_headers_name_dev_null_for_absent_sides() {
assert_eq!(
unified_side_headers("p", true, true),
["--- a/p".to_string(), "+++ b/p".to_string()]
);
assert_eq!(
unified_side_headers("p", false, true),
["--- /dev/null".to_string(), "+++ b/p".to_string()]
);
assert_eq!(
unified_side_headers("p", true, false),
["--- a/p".to_string(), "+++ /dev/null".to_string()]
);
let added = FileDiff::new("n.txt", "", "hi\n").to_unified_with_sides(3, false, true);
assert_eq!(
added,
"diff --oak a/n.txt b/n.txt\n--- /dev/null\n+++ b/n.txt\n@@ -0,0 +1,1 @@\n+hi\n"
);
}
}