From b6640abdea627ae5f499d76e631dcf4faee2bd63 Mon Sep 17 00:00:00 2001 From: Brandon Rozek Date: Sat, 18 May 2024 22:07:33 -0400 Subject: [PATCH] New post --- content/blog/patch-files.md | 91 +++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 content/blog/patch-files.md diff --git a/content/blog/patch-files.md b/content/blog/patch-files.md new file mode 100644 index 0000000..c50d6da --- /dev/null +++ b/content/blog/patch-files.md @@ -0,0 +1,91 @@ +--- +title: "Creating Patch Files" +date: 2024-05-18T20:10:21-04:00 +draft: false +tags: [] +math: false +medium_enabled: false +--- + +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: + +```c +#include + +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. + +```bash +cp hello-world.c hello-world.c.orig +``` + +Then let's edit `hello-world.c` to produce the desired result + +```c +#include + +int main(void) +{ + printf("Hello World\n"); + return 0; +} +``` + +Now we can create the patch file. + +```bash +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 + +```diff +--- 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. + +```bash +cp hello-world.c.orig hello-world.c +``` + +Now apply the patch + +```bash +patch < hello.patch +``` + +To undo the patch + +```bash +patch -R hello-world.c < hello.patch +``` +