Actions

patch (utility)

From Sega Retro


This short article is in need of work. You can help Sega Retro by adding to it.


patch is a utility used to apply source code patches to a source code tree. Source code patches are commonly used to distribute code changes to developers working on open-source projects.

The diff utility is used to create patches, which can then be applied by patch.

Unified Diff

The most common format for patches is the unified diff. The unified diff shows both line additions and removals, which are indicated by '+' and '-' symbols at the front of each line, respectively.

Example unified diff:

--- a/pyl.asm
+++ b/pyl.asm
@@ -55,9 +55,9 @@ START:
        jsr     draw_active_square

 main:
-       ; Wait 16 frames. (1/3 second)
-       ; TODO: Support PAL timing.
-       moveq   #15, d0
+       ; Wait a bit to advance the square.
+       moveq   #0, d0
+       move.b  RAM_Frames_SquareAdvance, d0
 .vsync_loop:
                jsr     VSync
                dbf     d0, .vsync_loop

In order to make a Unified Diff with the diff utility, simply use this example:

diff -U old_source new_source

where old_source is your unaltered disassembly and new_source is end result. You will need to redirect the output to a file of some sort, possibly with a pipe or an external program on your system.

On the other hand, you can also apply these patches in this manner:

patch -Np1 -i path_to_patch_file

from the inside of the disassembly being patched. The 1 assumes that the folder depth is 1. If the patch was made with a larger folder depth, then simply count the number of /'s in the paths in the patch itself. This will prevent errors down the rode and is a good reason to keep the folder depth to 1 in all patches, it makes life simpler for the rest of us.

POSIX Diff

The diff utility can also run in POSIX mode and create a POSIX compliant Diff, which uses > and < instead of + and -. The patch utility also supports these.

Example posix diff:

Binary files s1hive/alink.msg and s1hive-as/alink.msg differ
Common subdirectories: s1hive/_anim and s1hive-as/_anim
Binary files s1hive/as.msg and s1hive-as/as.msg differ
Binary files s1hive/asw.exe and s1hive-as/asw.exe differ
diff -P s1hive/build.bat s1hive-as/build.bat
2c2
< include.exe sonic1.asm s1comb.asm
---
> REM include.exe sonic1.asm s1comb.asm
13c13,15
< snasm68k.exe -emax 0 -p -o ae- s1comb.asm, s1built.bin
---
> cls
> asw  -xx -c -A sonic1.asm
> p2bin sonic1.p s1built.bin -l 0 -r $-$
Binary files s1hive/cmdarg.msg and s1hive-as/cmdarg.msg differ
Common subdirectories: s1hive/_dlls and s1hive-as/_dlls
Common subdirectories: s1hive/_inc and s1hive-as/_inc


In order to make a POSIX Diff with the diff utility, simply use this example:

diff -P old_source new_source

where old_source is your unaltered disassembly and new_source is end result. You will need to redirect the output to a file of some sort, possibly with a pipe or an external program on your system.

On the other hand, the patch utility can apply these in the exact same manner as with Unified Diffs.

Reverse Patching

Sometimes you end up with a patch that does not agree with your hack and makes it defunct. In that case you will want to apply a Reversed Patch to undo the changes.

Here is how you do that:

patch -Np1 -R -i patch_in_question

where patch_in_question is the patch you want to undo.

Downloads