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
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,35 @@
- 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:

Check warning

Code scanning / CodeQL

Workflow does not contain permissions Medium

Actions job or workflow does not limit the permissions of the GITHUB_TOKEN. Consider setting an explicit permissions block, using the following as a minimal starting point: {contents: read}
name: q tests against upstream quilt
runs-on: ubuntu-latest
steps:
Expand Down
6 changes: 3 additions & 3 deletions p2/src/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -352,8 +352,8 @@ fn apply_file_patch(
canonical_root: &Path,
) -> Result<FileReport> {
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)? {
Expand Down
35 changes: 29 additions & 6 deletions p2/src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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]
Expand Down
33 changes: 22 additions & 11 deletions p2/tests/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"],
);
}
Expand Down
Loading