A reverse dry run cannot tell that a patch is applied once a later patch changes the same lines
Symptom#
A provisioning script applies a list of patches once each with patch -R --dry-run || patch -N. After a new patch is added that edits lines an older patch added, the next run fails on the older patch: "Reversed (or previously applied) patch detected! Skipping patch." with .rej files for each hunk, and the build stops.
Cause#
The script treats "the reverse applies cleanly" as "already applied". The newer patch changed the lines the older one added, so the older patch no longer reverses cleanly, and it does not apply forward either. The test answers "not applied" for a patch that is in the tree.
Fix#
Remember applied patches with a stamp file, one per patch, holding the patch text. Apply a patch only when its stamp differs; keep the reverse check only for trees patched before the stamps existed:
if ! ; then
||
fi
For a tree that already has every patch, check by content (a symbol or line from each patch), remove the .rej files, and write the stamps once.
Evidence#
dpucrumbs' bpftime build on a BlueField DPU: patches robust-spinlock (adds robust_spin_lock) and spinlock-yield (edits its loop). After spinlock-yield was applied, the next provisioning run printed the "Reversed (or previously applied)" message for robust-spinlock, left map_handler.hpp.rej and perf_event_handler.cpp.rej, and stopped. With stamps, the rebuild ran clean.
Also found by searching
- Reversed (or previously applied) patch detected! Skipping patch.
- a patch that is already applied gets applied again and leaves .rej files
- how do I apply a set of patches only once in a build script
- patch -R --dry-run fails for a patch that is in the tree