This way, if someone else wants to make changes too, they can easily see what was done and why.
This means extracting all those fancy .tar.gz or .zip files into a nice neat directory on your computer. Once you’ve got everything spread out like a game of Tetris gone wrong, it’s time to apply the patches.
To do this, navigate to the directory where your source code is located and run:
# This script is used to apply patches to source code files.
# The "patch" command is used to apply the patch file to the source code.
# The "-p1" option specifies the number of leading slashes to ignore in the file names mentioned in the patch file.
# The "<" symbol is used to redirect the contents of the patch file to the "patch" command.
patch -p1 < patch_file.diff
This will take the contents of `patch_file.diff`, which contains instructions for making changes to specific lines in the original files, and apply them to those same lines in your unpacked source code. The `-p1` flag tells `patch` to look for matches at column 1 (i.e., the beginning of each line), rather than starting from a certain position within that line.
For example, let’s say you have a patch file called `fix_bug.diff`, which contains these instructions:
# This script is used to apply a patch file to a source code file.
# The patch file contains instructions on how to modify the source code.
# The `-p1` flag tells `patch` to look for matches at column 1 (i.e., the beginning of each line),
# rather than starting from a certain position within that line.
# For example, let's say you have a patch file called `fix_bug.diff`,
# which contains these instructions:
# The following lines are used to specify the source code file and the modified version of it.
# The first line specifies the original source code file, while the second line specifies the modified version.
# The dates and times are used to track when the modifications were made.
# The `-0700` indicates the time zone offset.
# The `@@` symbol is used to indicate the start of a patch instruction.
# The numbers after it specify the line numbers in the source code file that the patch will be applied to.
# In this case, the patch will be applied to line 58.
# The `int main(int argc, char *argv[])` line is the start of the `main` function in the source code file.
# The `// do some stuff here...` line is a placeholder for the actual code that will be executed.
# The `return 0;` line is used to end the `main` function.
# The `}` symbol indicates the end of the `main` function.
# The `/* added comment to explain what this does */` is a comment that explains the purpose of the `}` symbol.
# Comments are important for understanding code and should be used to explain complex or important sections.
--- src/main.c 2019-05-31 14:28:06.000000000 -0700
+++ src/main.c 2019-06-01 10:12:34.000000000 -0700
@@ -58,7 +58,7 @@
int main(int argc, char *argv[]) {
// do some stuff here...
return 0;
-} /* added comment to explain what this does */
+} /* end of main function */
This patch tells `patch` to replace the line that starts with “return 0;” (which is on line 58 in the original file) with a new version of that same line, which includes an additional comment explaining its purpose. The `@@ -58,7 +58,7 @@` part specifies the range of lines being changed: from line 58 to line 64 (inclusive), and then again from line 58 to line 64 (also inclusive).
So when you run `patch -p1 < fix_bug.diff`, it will replace that section of your source code with the new version, including the added comment. Pretty cool, right?