~/Blog

Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

Creating Patch Files

Published on

2 minute reading time

Distributing software can be difficult. Is the shared library called x on this distribution or is it called y? This may lead to a lot of distribution specific code.

It might not make sense to commit these changes to the main software repository. An alternative is to create patch files. These are files that capture exactly how you want to update a given file.

Let’s take the following C code as an example:

#include <stdio.h>

int main(void)
{
  printf("Goodbye World\n");
  return 0;
}

Instead, we want for it to print out the classic “Hello World”. To do this, first let us save the original file.

cp hello-world.c hello-world.c.orig

Then let’s edit hello-world.c to produce the desired result

#include <stdio.h>

int main(void)
{
  printf("Hello World\n");
  return 0;
}

Now we can create the patch file.

diff -Naur hello-world.c.orig hello-world.c > hello.patch

The options of diff are the following:

Flag Description
-N Treat absent files as empty
-a Treat all files as text
-u Output 3 lines of unified context
-r Recursively compare any subdirectories found

This will output the following patch file

--- hello-world.c.orig  2024-05-12 20:41:27.708297085 -0400
+++ hello-world.c       2024-05-12 20:41:36.742348955 -0400
@@ -2,7 +2,7 @@
 
 int main(void)
 {
-  printf("Goodbye World\n");
+  printf("Hello World\n");
   return 0;
 }

In order to test out the patch, let us revert the hello-world.c file to it’s original state.

cp hello-world.c.orig hello-world.c

Now apply the patch

patch < hello.patch

To undo the patch

patch -R hello-world.c < hello.patch
Reply via Email Buy me a Coffee
Was this useful? Feel free to share: Hacker News Reddit Twitter