Skip to content

diff: date a missing -N operand at the Epoch in headers - #367

Open
sarankumar1325 wants to merge 3 commits into
uutils:mainfrom
sarankumar1325:fix/new-file-epoch-date
Open

sarankumar1325 wants to merge 3 commits into
uutils:mainfrom
sarankumar1325:fix/new-file-epoch-date

Conversation

@sarankumar1325

Copy link
Copy Markdown
Contributor

Fixes #361

With -N / --new-file, a missing operand is treated as empty. GNU dates it at the Unix Epoch (1970-01-01 00:00:00) in the unified and context headers. That is the convention patch uses to tell that a file must be created or removed. get_modification_time fell back to the current time for any metadata error, so the missing side got today's date and patch left an empty file behind instead of deleting it:

$ TZ=UTC0 diff -N -u A/only B/only
--- A/only	2026-11-09 15:18:23.146917271 +0000
+++ B/only	2026-11-09 15:18:23.167832210 +0000   # GNU: 1970-01-01 00:00:00.000000000 +0000

get_modification_time now returns the Epoch when the file does not exist (ErrorKind::NotFound). Standard input (-) and other errors keep the current time as before. This covers -u, -c (both timestamp styles) and -r -N directory comparisons, since they all go through this function. Output now matches GNU diff 3.x on the box for all three.

Tests:

  • tests/by-util/test_diff.rs: new_file_missing_side_dated_at_epoch (unix only, since it sets TZ like context_timestamp_follows_time_locale) checks -u, -c with an ISO locale and -c with LC_ALL=C, with the missing file on either side, plus -r -N -u over two directories. It fails on main and passes with this change.
  • src/utils.rs: the invalid_file unit test asserted the old "now" fallback for a missing file. It is replaced by missing_file (expects the Epoch) and stdin (expects the current time for -).

cargo test --test tests (43 passed), cargo test --lib modification_time, cargo fmt --check and cargo clippy -- -D warnings are clean.

I used an AI assistant to write this change. I reviewed and tested it myself.

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 uutils#361
Copilot AI balanced review requested due to automatic review settings November 9, 2026 15:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Comment thread src/utils.rs Outdated
/// 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.

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.

could be shorter, no? something like "A missing file (-N) is dated at the Epoch, other errors at now."

missing.display()
)));
}
}

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 upstream testsuite on this?
TESTS=new-file ./tests/run-upstream-testsuite.sh

Copilot AI balanced review requested due to automatic review settings November 9, 2026 19:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@sylvestre

Copy link
Copy Markdown
Collaborator

sorry but it needs to be rebased

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

diff - -N: a missing file is dated "now" instead of the Epoch in -u/-c headers

3 participants