Fix interleaved output when diffing directories

Fixes #437
pull/441/head
Wilfred Hughes 2022-12-08 09:58:19 +07:00
parent 436edb2ab4
commit 554fb18b7c
2 changed files with 22 additions and 3 deletions

@ -1,5 +1,10 @@
## 0.39 (unreleased)
### Display
Fixed a race condition where diffing directories would lead to
interleaved output from different files.
## 0.38 (released 14th November 2022)
### Parsing

@ -180,6 +180,19 @@ fn main() {
options::FileArgument::NamedPath(lhs_path),
options::FileArgument::NamedPath(rhs_path),
) if lhs_path.is_dir() && rhs_path.is_dir() => {
// We want to diff files in the directory in
// parallel, but print the results serially (to
// prevent display interleaving).
// https://github.com/rayon-rs/rayon/issues/210#issuecomment-551319338
let (send, recv) = std::sync::mpsc::sync_channel(1);
let print_options = display_options.clone();
let printing_thread = std::thread::spawn(move || {
for diff_result in recv.into_iter() {
print_diff_result(&print_options, &diff_result);
}
});
diff_directories(
lhs_path,
rhs_path,
@ -188,9 +201,10 @@ fn main() {
byte_limit,
language_override,
)
.for_each(|diff_result| {
print_diff_result(&display_options, &diff_result);
});
.try_for_each_with(send, |s, diff_result| s.send(diff_result))
.expect("Receiver should be connected");
printing_thread.join().expect("Printing thread should not panic");
}
_ => {
let diff_result = diff_file(