Table of Contents

Ubuntu - Patching - Creating a patch

If you are a developer you might come across bugs that you fixed yourself.

To share the fix with others you can use the command diff which scans two files and lists the differences.

If you output this into a file e.g. differences.patch the users can use your file as the input for the command patch.


Creating the patch file

# Run diff to get all the differences of two entities. These can be files but also folders.
# The result is piped into 
#   c => output NUM (default 3) lines of copied context
#   r => recursive
#   B => ignore changes whose lines are all blank
diff -crB Folder1 Folder2 > bugfix.patch

Copy the patch file into the directory of the file or folder you want to patch.

If the patch was created of two folders change into the directory that should be patched and run the patch from there.

Thus the patch command knows what folder should be patched.


Running the patch

# patching files  
patch –dry-run -p1 -i bugfix.patch # Patch simulation
patch -p1 -i bugfix.patch          # Patch in action
 
# patching folders
cd folder_to_be_patched
patch –dry-run -p1 -i ../bugfix.patch # Patch simulation
patch -p1 -i ../bugfix.patch          # Patch in action