Managing multiple patches for a source file
Joshua Root
jmr at macports.org
Tue Sep 1 09:27:27 UTC 2026
On 1/9/2026 16:46, Seth McDonald via macports-dev wrote:
> Hi all,
>
> Following the MacPorts guide on patching source code (see [0], section
> 4.6.2), it encourages using separate patchfiles for each logical change
> and says to diff against the original file.
>
> Given this, I'd like to ask what the about the situation in which
> multiple logical changes need to be applied to the same source file.
> Since each change may alter the line count of the file, I worry that
> just diffing each change against the original file may cause later
> patches to not apply due to the expected line numbers being changed.
>
> I can think of three ways this can be managed:
>
> (0) Keep all changes to the same source file within the same patchfile.
> This way the problem never occurs, but removes the benefit of logically
> separate patches.
>
> (1) Diff each change against the original file. Perhaps I'm wrong and
> MacPorts is actually smart enough to handle sequential patches which
> each alter line numbers without the patchfiles accounting for it.
The patch command is smart enough to apply patches at an offset from the
original line numbers if lines have been added or removed. You will get
patch failures if multiple patches are modifying the same lines, or even
lines that are too close to each other.
<https://www.gnu.org/software/diffutils/manual/html_node/Inexact.html>
Applying with an offset is almost always fine, but patches that need a
nonzero fuzz factor to apply should be avoided, as that has been known
to result in code that compiles but has subtle bugs.
> (2) Diff each change against the previous change. This defines an order
> in which the patchfiles must be applied. This option makes the most
> sense in my head.
>
> Is one of these three methods the "correct" way in which multiple
> patches should be managed?
All three options are acceptable as long as the patches apply cleanly.
Use whichever one makes maintenance easiest in your case. If you end up
managing patch series, you may find the quilt port useful.
- Josh
More information about the macports-dev
mailing list