From a0758bad2afe8d4391f640e2c4fbc490f218d1ad Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:48:55 +0530 Subject: [PATCH 1/2] diff: date a missing -N operand at the Epoch in headers With -N, a missing operand is treated as empty. GNU dates it at 1970-01-01 00:00:00 in the -u/-c headers, which is how patch knows to create or remove the file. We used the current time for any stat error, so patch left an empty file behind. Use the Epoch when the file does not exist; stdin and other errors keep the current time. Fixes #361 --- src/utils.rs | 36 ++++++++++++++++++------ tests/by-util/test_diff.rs | 56 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 408e13b2..549024b0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -241,16 +241,21 @@ pub fn is_posix_locale(category: &str) -> bool { locale == "C" || locale == "POSIX" } -/// Retrieves the modification time of the input file specified by file path -/// If an error occurs, it returns the current system time +/// Retrieves the modification time of the input file specified by file path. +/// A file that does not exist (treated as empty with `-N`) is dated at the +/// Unix Epoch, as GNU diff does, so that `patch` knows to create or delete +/// it. If any other error occurs, it returns the current system time. pub fn get_modification_time(file_path: &str, style: TimestampStyle) -> String { use chrono::{DateTime, Local}; use std::fs; + use std::io::ErrorKind; use std::time::SystemTime; - let modification_time: SystemTime = fs::metadata(file_path) - .and_then(|m| m.modified()) - .unwrap_or(SystemTime::now()); + let modification_time: SystemTime = match fs::metadata(file_path).and_then(|m| m.modified()) { + Ok(time) => time, + Err(e) if e.kind() == ErrorKind::NotFound && file_path != "-" => SystemTime::UNIX_EPOCH, + Err(_) => SystemTime::now(), + }; let modification_time: DateTime = modification_time.into(); let format = match style { @@ -526,17 +531,30 @@ mod tests { } #[test] - fn invalid_file() { + fn missing_file() { use chrono::{DateTime, Local}; use std::time::SystemTime; - let invalid_file = "target/utils/invalid-file"; + let missing_file = "target/utils/missing-file"; + + // A missing file is dated at the Unix Epoch, like GNU diff does + let m_time: DateTime = get_modification_time(missing_file, TimestampStyle::Iso) + .parse() + .unwrap(); + + assert_eq!(m_time, DateTime::::from(SystemTime::UNIX_EPOCH)); + } + + #[test] + fn stdin() { + use chrono::{DateTime, Local}; + use std::time::SystemTime; // store current time before calling `get_modification_time` - // Because the file is invalid, it will return SystemTime::now() + // Standard input has no path to stat, so it is dated "now", // which will be greater than previously saved time let current_time: DateTime = SystemTime::now().into(); - let m_time: DateTime = get_modification_time(invalid_file, TimestampStyle::Iso) + let m_time: DateTime = get_modification_time("-", TimestampStyle::Iso) .parse() .unwrap(); diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 71bfcad2..473cf309 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -246,6 +246,62 @@ fn missing_file_as_empty() -> Result<(), Box> { Ok(()) } +// TZ is not honored on Windows. +#[cfg(unix)] +#[test] +fn new_file_missing_side_dated_at_epoch() -> Result<(), Box> { + let tmp_dir = tempdir()?; + let a = tmp_dir.path().join("a"); + let b = tmp_dir.path().join("b"); + std::fs::create_dir(&a)?; + std::fs::create_dir(&b)?; + let old = a.join("only"); + std::fs::write(&old, "x\n")?; + let missing = b.join("only"); + + // GNU dates the missing side at the Epoch, which tells patch to remove it. + for (option, locale, epoch) in [ + ("-u", "en_US.UTF-8", "1970-01-01 00:00:00.000000000 +0000"), + ("-c", "en_US.UTF-8", "1970-01-01 00:00:00.000000000 +0000"), + ("-c", "C", "Thu Jan 1 00:00:00 1970"), + ] { + for (from, to) in [(&old, &missing), (&missing, &old)] { + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.env("TZ", "UTC0") + .env("LC_ALL", locale) + .arg("diff") + .arg("-N") + .arg(option) + .arg(from) + .arg(to); + cmd.assert() + .code(predicate::eq(1)) + .stdout(predicate::str::contains(format!( + " {}\t{epoch}\n", + missing.display() + ))); + } + } + + // The same goes for files that only exist on one side of a directory diff. + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.env("TZ", "UTC0") + .env("LC_ALL", "C") + .arg("diff") + .arg("-r") + .arg("-N") + .arg("-u") + .arg(&a) + .arg(&b); + cmd.assert() + .code(predicate::eq(1)) + .stdout(predicate::str::contains(format!( + "+++ {}\t1970-01-01 00:00:00.000000000 +0000\n", + missing.display() + ))); + Ok(()) +} + #[test] fn binary_files() -> Result<(), Box> { let tmp_dir = tempdir()?; From 67102b0251fe159393e2f8745e75af0c05d11dd1 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Tue, 10 Nov 2026 01:25:15 +0530 Subject: [PATCH 2/2] diff: shorten the get_modification_time doc comment --- src/utils.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 549024b0..adcb2488 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -242,9 +242,7 @@ pub fn is_posix_locale(category: &str) -> bool { } /// Retrieves the modification time of the input file specified by file path. -/// A file that does not exist (treated as empty with `-N`) is dated at the -/// Unix Epoch, as GNU diff does, so that `patch` knows to create or delete -/// it. If any other error occurs, it returns the current system time. +/// A missing file (-N) is dated at the Epoch, other errors at now. pub fn get_modification_time(file_path: &str, style: TimestampStyle) -> String { use chrono::{DateTime, Local}; use std::fs;