Skip to content
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
122 changes: 101 additions & 21 deletions src/dir_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,42 +301,78 @@ enum Entry<'a> {
}

/// Merges two lists of names sorted by `order`, pairing the names that are
/// equal according to it.
/// equal according to it. Where several names of a list are equal (names
/// differing only by case), those that are the same on both sides pair first
/// and the others pair in order.
fn pair_entries<'a>(
from_names: &'a [OsString],
to_names: &'a [OsString],
order: impl Fn(&OsStr, &OsStr) -> Ordering,
) -> Vec<Entry<'a>> {
let mut entries = Vec::with_capacity(from_names.len().max(to_names.len()));
let mut from_names = from_names.iter().map(OsString::as_os_str).peekable();
let mut to_names = to_names.iter().map(OsString::as_os_str).peekable();
let (mut from_names, mut to_names) = (from_names, to_names);
loop {
let entry = match (from_names.peek(), to_names.peek()) {
match (from_names.first(), to_names.first()) {
(None, None) => return entries,
(Some(&from), Some(&to)) => match order(from, to) {
Ordering::Equal => Entry::Both(from, to),
Ordering::Less => Entry::FromOnly(from),
Ordering::Greater => Entry::ToOnly(to),
(Some(from), Some(to)) => match order(from, to) {
Ordering::Equal => {
// The names of each list equal to these two.
let run_len = |names: &[OsString]| {
let equal = names.iter().take_while(|name| order(name, from).is_eq());
equal.count()
};
let (from_run, from_rest) = from_names.split_at(run_len(from_names));
let (to_run, to_rest) = to_names.split_at(run_len(to_names));
pair_equal_names(from_run, to_run, &mut entries);
(from_names, to_names) = (from_rest, to_rest);
}
Ordering::Less => {
entries.push(Entry::FromOnly(from));
from_names = &from_names[1..];
}
Ordering::Greater => {
entries.push(Entry::ToOnly(to));
to_names = &to_names[1..];
}
},
(Some(&from), None) => Entry::FromOnly(from),
(None, Some(&to)) => Entry::ToOnly(to),
};
match entry {
Entry::Both(..) => {
from_names.next();
to_names.next();
}
Entry::FromOnly(_) => {
from_names.next();
(Some(from), None) => {
entries.push(Entry::FromOnly(from));
from_names = &from_names[1..];
}
Entry::ToOnly(_) => {
to_names.next();
(None, Some(to)) => {
entries.push(Entry::ToOnly(to));
to_names = &to_names[1..];
}
}
entries.push(entry);
}
}

/// Pairs names that all compare equal: identical names together, then the
/// remaining ones in order. Like GNU diff, the names left without a pair come
/// after the pairs.
fn pair_equal_names<'a>(
from_names: &'a [OsString],
to_names: &'a [OsString],
entries: &mut Vec<Entry<'a>>,
) {
let is_exact = |name: &OsString, others: &[OsString]| others.contains(name);
let mut inexact_to_names = to_names.iter().filter(|to| !is_exact(to, from_names));
let mut from_only = Vec::new();
for from in from_names {
let to = if is_exact(from, to_names) {
Some(from)
} else {
inexact_to_names.next()
};
match to {
Some(to) => entries.push(Entry::Both(from, to)),
None => from_only.push(Entry::FromOnly(from)),
}
}
entries.extend(from_only);
entries.extend(inexact_to_names.map(|to| Entry::ToOnly(to)));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in a group the "Only in" for the to side now comes after all the pairs, not in sort order
with one/a and two/{A,a} we print the diff of a before "Only in two: A", is that what GNU does?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes for that case: GNU diff 3.12 prints the diff of a and then Only in two: A, whichever of A/a is created first. But checking a few more layouts showed the from side was wrong: with one/{B,b} and two/b, GNU prints the b diff before Only in one: B, and I printed it after. GNU puts the unpaired names of a group after its pairs on both sides, so I changed that in 328168d and added a test with both sides.

}

fn report_only_in(dir: &OsStr, name: &OsStr) -> Outcome {
println!(
"Only in {}: {}",
Expand Down Expand Up @@ -413,4 +449,48 @@ mod tests {
);
assert_eq!(pair_entries(&[], &[], |a, b| a.cmp(b)), []);
}

#[test]
fn names_equal_by_order_pair_exact_matches_first() {
let names =
|names: &[&str]| -> Vec<OsString> { names.iter().map(OsString::from).collect() };
let ignoring_case = |a: &OsStr, b: &OsStr| {
let lowercase = |name: &OsStr| name.as_encoded_bytes().to_ascii_lowercase();
lowercase(a).cmp(&lowercase(b))
};
let name = OsStr::new;

let (from, to) = (names(&["f", "x"]), names(&["F", "f", "y"]));
assert_eq!(
pair_entries(&from, &to, ignoring_case),
[
Entry::Both(name("f"), name("f")),
Entry::ToOnly(name("F")),
Entry::FromOnly(name("x")),
Entry::ToOnly(name("y")),
]
);

// What has no exact match pairs in order; the rest is unpaired, and
// comes after the pairs.
let (from, to) = (names(&["Ab", "aB"]), names(&["AB", "aB", "ab"]));
assert_eq!(
pair_entries(&from, &to, ignoring_case),
[
Entry::Both(name("Ab"), name("AB")),
Entry::Both(name("aB"), name("aB")),
Entry::ToOnly(name("ab")),
]
);
let (from, to) = (names(&["AB", "Ab", "aB", "ab"]), names(&["ab"]));
assert_eq!(
pair_entries(&from, &to, ignoring_case),
[
Entry::Both(name("ab"), name("ab")),
Entry::FromOnly(name("AB")),
Entry::FromOnly(name("Ab")),
Entry::FromOnly(name("aB")),
]
);
}
}
58 changes: 58 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,64 @@ fn directory_name_matching() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

// Among names differing only by case, the one that is the same on both sides
// is the counterpart, whichever sorts first.
#[test]
fn ignore_file_name_case_prefers_exact_match() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempdir()?;
let dir = tmp_dir.path();
std::fs::create_dir(dir.join("one"))?;
std::fs::create_dir(dir.join("two"))?;
std::fs::write(dir.join("one/f"), "same\n")?;
std::fs::write(dir.join("two/f"), "same\n")?;
std::fs::write(dir.join("two/F"), "other\n")?;
if std::fs::read_dir(dir.join("two"))?.count() != 2 {
// The file system is not case-sensitive.
return Ok(());
}

for (from, to, only_in) in [("one", "two", "two"), ("two", "one", "two")] {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you run the GNU testsuite against this?
TESTS= ./tests/run-upstream-testsuite.sh
the script fetches the tests itself

@binhvq binhvq Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had not, sorry. I ran the whole suite now, on this branch and on its base commit: 19 pass / 10 fail / 4 skip on both, with the same failures. Of the tests that use --ignore-file-name-case, colliding-file-names and starting-file pass, and ignore-case fails earlier on the missing -i.

let mut cmd = cargo_bin_cmd!("diffutils");
cmd.current_dir(dir).arg("diff").arg("-r");
cmd.arg("--ignore-file-name-case").arg(from).arg(to);
cmd.assert()
.code(predicate::eq(1))
.stdout(format!("Only in {only_in}: F\n"));
}
Ok(())
}

#[test]
fn ignore_file_name_case_reports_unpaired_after_pairs() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempdir()?;
let dir = tmp_dir.path();
std::fs::create_dir(dir.join("one"))?;
std::fs::create_dir(dir.join("two"))?;
std::fs::write(dir.join("one/B"), "1\n")?;
std::fs::write(dir.join("one/b"), "1\n")?;
std::fs::write(dir.join("two/b"), "2\n")?;
std::fs::write(dir.join("one/c"), "1\n")?;
std::fs::write(dir.join("two/C"), "2\n")?;
std::fs::write(dir.join("two/c"), "2\n")?;
if std::fs::read_dir(dir.join("one"))?.count() != 3 {
// The file system is not case-sensitive.
return Ok(());
}

let mut cmd = cargo_bin_cmd!("diffutils");
cmd.current_dir(dir).arg("diff").arg("-r");
cmd.arg("--ignore-file-name-case").arg("one").arg("two");
cmd.assert().code(predicate::eq(1)).stdout(concat!(
"diff -r --ignore-file-name-case one/b two/b\n",
"1c1\n< 1\n---\n> 2\n",
"Only in one: B\n",
"diff -r --ignore-file-name-case one/c two/c\n",
"1c1\n< 1\n---\n> 2\n",
"Only in two: C\n",
));
Ok(())
}

#[cfg(unix)]
#[test]
fn no_dereference() -> Result<(), Box<dyn std::error::Error>> {
Expand Down
Loading