There is a common need to create patch files. They are used everywhere, starting from mailing lists to build systems such as BuildRoot or Yocto. Patches are used widely by package managers, such as dpkg and RPM.
Before we will get started, let’s answer a simple question: what’s a patch? It is a text file that contains information about changes in the project’s sources. They are human-readable, but it’s not always so clear what they do because they don’t always have enough context regarding the changes. However, today’s topic is not about the patches themselves, but about the very first step of how to create them.
And, as usual, we have different approaches to creating patches.
Create patches with git
Git is the most popular Version Control System in the Universe, and it has a vast amount of functions. If you have Git installed on your machine it’s probably the best tool to create a patch. To do so, you should have Git repo with sources you want to create a patch for. If you downloaded it as a tar.gz archive you can easily create a repo with a first commit containing a base state:
cd source-dir
git init
git add -A .
git commit -m “First commit”
Then you can make your changes, stage them with `git add` as usual and create a new commit.
git add changed-file.c
git commit -m “Add comments to changed-file.c”
Now you are ready to create your patch with Git.
git format-patch -1
will create a patch file for the last commit. If you made several commits and want to create patches for all of them, just pass the number of commits you need instead of `-1`.
For example, let’s say you want to create patches from 3 last commits:
git format-patch -3
As you can see, this is a very simple method for creating patches.
Create patches with quilt
Another popular way to create patches is by using a special utility named quilt. This lightweight tool will help you to create a patch almost as easily as with Git. Let’s look at the instructions you need to use for creating a patch with quilt:
quilt new patchname.diff
The instruction above will create a new patch file. Then you should add the files you want to change:
quilt add filename filename2 filename3
We are almost done. Now you make your changes in these files and just do one simple command:
quilt refresh
Then you can see the patch `patchname.diff` in `pwd`/../patches.
Quilt has a lot of functions to work with a series of patches, but in this case – that’s it.
NOTE: Pay attention to paths used in patches. Sometimes you will want to strip some extra levels of directories. If you use `patch` utility to apply the patches, just pass the number of directories you need to strip after `-p` argument:
patch -p2 < patchname.diff
If you have an error that says that the patch cannot be applied because ‘No such file or directory`, it’s probably because of this issue. You can always strip directories manually since patch files are just text files, so it’s not a huge problem.
Create patches with diff tool
This method can be used in almost every machine since the diff tool is installed almost everywhere.
Firstly, we should create a backup folder with sources:
cp -rf my-sources my-sources.orig
Then we can make our changes in my-sources directory. my-sources.orig folder should be untouched. After making changes it is time to create our patch:
diff -ruN my-sources.orig/ my-sources/ > patchname.patch
So, which method to choose?
In most cases, the method with Git is way better than any other, because you can easily create several patches without any extra routine. However, knowing alternative ways can be helpful, especially when you don’t have the necessary packets installed on a machine you want to use for creating a patch.