From f7d34ee190806517e4a79287b75caf837e361683 Mon Sep 17 00:00:00 2001 From: jj Date: Thu, 19 Nov 2026 19:02:56 +0000 Subject: [PATCH 1/2] p2: fix timestamp deletion and merge parity coverage --- p2/src/apply.rs | 6 +++--- p2/src/timestamp.rs | 35 +++++++++++++++++++++++++++++------ p2/tests/parity.rs | 33 ++++++++++++++++++++++----------- 3 files changed, 54 insertions(+), 20 deletions(-) diff --git a/p2/src/apply.rs b/p2/src/apply.rs index 6435e3f..848120d 100644 --- a/p2/src/apply.rs +++ b/p2/src/apply.rs @@ -195,7 +195,7 @@ fn grouped_apply_file_target( || patch.old_file_mode.is_some() || patch.new_file_mode.is_some() || patch_changes_symlink(patch, options.reverse) - || has_deletion_timestamp(patch, options.reverse) + || has_deletion_timestamp(patch, options) || is_creation_only_patch(patch) || empties_file(patch) { @@ -352,8 +352,8 @@ fn apply_file_patch( canonical_root: &Path, ) -> Result { let creates_file = source_path(patch, options.reverse).is_none(); - let deletes_file = target_path(patch, options.reverse).is_none() - || has_deletion_timestamp(patch, options.reverse); + let deletes_file = + target_path(patch, options.reverse).is_none() || has_deletion_timestamp(patch, options); let full_path = root.join(&target); if path_parent_is_invalid(canonical_root, &target)? { diff --git a/p2/src/timestamp.rs b/p2/src/timestamp.rs index 4df15c0..5cc7322 100644 --- a/p2/src/timestamp.rs +++ b/p2/src/timestamp.rs @@ -43,11 +43,31 @@ fn target_label(patch: &FilePatch, reverse: bool) -> Option<&str> { } } -/// GNU diff marks a deleted file with an epoch timestamp on the new side. -pub(crate) fn has_deletion_timestamp(patch: &FilePatch, reverse: bool) -> bool { - target_label(patch, reverse) - .and_then(|label| parse_header_timestamp(label, TimestampMode::Local)) - .is_some_and(|timestamp| timestamp.seconds() == 0) +/// GNU diff marks a deleted file with an empty target and an epoch timestamp. +pub(crate) fn has_deletion_timestamp(patch: &FilePatch, options: &ApplyOptions) -> bool { + let target_starts_at_zero = patch.hunks.first().is_some_and(|hunk| { + if options.reverse { + hunk.old_start == 0 + } else { + hunk.new_start == 0 + } + }); + if !target_starts_at_zero { + return false; + } + + let mode = TimestampMode::from_options(options); + let timestamp = target_label(patch, options.reverse) + .and_then(|label| parse_header_timestamp(label, mode.unwrap_or(TimestampMode::Local))); + timestamp.is_some_and(|timestamp| { + if mode.is_some() { + timestamp.seconds() == 0 + } else { + // GNU patch allows for an epoch written in a different local timezone. + let time = (timestamp.seconds(), timestamp.nanoseconds()); + time > (-25 * 3600, 0) && time < (26 * 3600, 0) + } + }) } pub(crate) fn source_timestamp( @@ -254,7 +274,10 @@ mod tests { "--- a/deleted\n+++ b/deleted\t1970-01-01 01:00:00.000000000 +0100\n@@ -1 +0,0 @@\n-data\n", ) .unwrap(); - assert!(has_deletion_timestamp(&patch.files[0], false)); + assert!(has_deletion_timestamp( + &patch.files[0], + &ApplyOptions::default() + )); } #[test] diff --git a/p2/tests/parity.rs b/p2/tests/parity.rs index 5bc0374..d139052 100644 --- a/p2/tests/parity.rs +++ b/p2/tests/parity.rs @@ -402,16 +402,6 @@ fn merge_option_writes_conflict_markers_like_gnu_patch() { ); } -#[test] -fn short_merge_option_writes_conflict_markers_like_gnu_patch() { - assert_matches_gnu_patch( - &[("f", "ours\n")], - "--- f\n+++ f\n@@ -1 +1 @@\n-base\n+theirs\n", - &["-m", "-f", "-p0"], - &["f"], - ); -} - #[test] fn merge_diff3_option_writes_original_conflict_markers_like_gnu_patch() { assert_matches_gnu_patch( @@ -550,10 +540,31 @@ fn refuses_to_delete_when_content_remains_like_gnu_patch() { #[test] fn refuses_timestamp_marked_deletion_when_content_remains_like_gnu_patch() { - assert_matches_gnu_patch( + assert_matches_gnu_patch_with_env( &[("target", "1\n2\n")], "--- target\t2012-09-25 03:09:15\n+++ target\t1970-01-01 00:00:00\n@@ -1 +0,0 @@\n-1\n", &["-p0"], + &[("TZ", "EST5")], + &["target"], + ); +} + +#[test] +fn set_utc_does_not_treat_near_epoch_as_deletion_like_gnu_patch() { + assert_matches_gnu_patch( + &[("target", "1\n2\n")], + "--- target\t2012-09-25 03:09:15\n+++ target\t1970-01-01 05:00:00\n@@ -1 +0,0 @@\n-1\n", + &["-p0", "-Z"], + &["target"], + ); +} + +#[test] +fn epoch_timestamp_with_nonempty_target_range_is_not_a_deletion_like_gnu_patch() { + assert_matches_gnu_patch( + &[("target", "one\n")], + "--- target\n+++ target\t1970-01-01 00:00:00 +0000\n@@ -1 +1 @@\n-one\n+two\n", + &["-p0"], &["target"], ); } From 73500492b5fd24b178df992c022c9a96a8340b51 Mon Sep 17 00:00:00 2001 From: jj Date: Thu, 19 Nov 2026 19:02:59 +0000 Subject: [PATCH 2/2] ci: test p2 against GNU patch 2.8 --- .github/workflows/ci.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9a5179d..5e6ccc5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,6 +31,34 @@ jobs: - name: Test run: cargo test --all-features + p2-tests: + name: p2 tests against GNU patch + runs-on: ubuntu-latest + env: + CARGO_TARGET_DIR: ${{ github.workspace }}/target + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup CI + uses: ./.github/actions/setup-ci + with: { apt-packages: "build-essential ed" } + + - name: Build GNU patch 2.8 + working-directory: ${{ runner.temp }} + run: | + curl --fail --silent --show-error --location --retry 3 \ + --output patch-2.8.tar.xz https://ftp.gnu.org/gnu/patch/patch-2.8.tar.xz + tar -xf patch-2.8.tar.xz + cd patch-2.8 + ./configure --quiet + make -s -j"$(nproc)" + + - name: Run p2 tests + run: | + P2_REFERENCE_PATCH="$RUNNER_TEMP/patch-2.8/src/patch" \ + cargo test --locked --manifest-path p2/Cargo.toml + q-tests-against-upstream-quilt: name: q tests against upstream quilt runs-on: ubuntu-latest