Patching packages
This article covers how to create and how to apply patches to packages in the Arch Build System (ABS).
A patch describes a set of line changes for one or multiple files. Patches are typically used to automate the changing of source code.
Creating patches
The diff tool compares files line by line. If you save its output you have got a patch, e.g. diff --unified --recursive --text foo bar > patch. If you pass directories diff will compare the files they contain.
- Delete the
srcdirectory if you have already built the package. - Run
makepkg --nobuildwhich will download and extract the source files, specified inPKGBUILD, but not build them. - Create two copies of the extracted directory in the
srcdirectory, one as a pristine copy and one for your altered version. We will call thempackage.origandpackage.new. - Make your changes in the
package.newdirectory. - Run
diff --unified --recursive --text package.orig package.new --colorand check if the patch looks good. - Run
diff --unified --recursive --text package.orig package.new > package.patchto create the patch. - Change into the
package.origdirectory and apply the patch usingpatch --strip=1 < ../package.patch. Verify that the patch is working by building and installing the modified package withmakepkg --noextract --install.
See diff(1) and git-diff(1) for more info.
Applying patches
This section outlines how to apply patches you created or downloaded from the Internet from within a PKGBUILD's prepare() function. Follow these steps:
- Add an entry to the
sourcearray of thePKGBUILDfor the patch file, separated from the original source url by a space. If the file is available online, you can provide the full URL and it will automatically be downloaded and placed in thesrcdirectory. If it is a patch you created yourself, or is otherwise not available, you should place the patch file in the same directory as thePKGBUILDfile, and just add the name of the file to the source array so that it is copied into thesrcdirectory. If you redistribute thePKGBUILD, you should, of course, include the patch with thePKGBUILD. - Then use
updpkgsums(from pacman-contrib) to update themd5sumsarray. Or manually add an entry to themd5sumsarray; you can generate the sum of your patch using md5sum tool. - Create the
prepare()function in thePKGBUILDif one is not already present. - The first step is to change into the directory that needs to be patched (in the
prepare()function, not on your terminal! You want to automate the process of applying the patch). You can do this with something likecd "$srcdir/$pkgname-$pkgver"or something similar.$pkgname-$pkgveris often the name of a directory created by untarring a downloaded source file, but not in all cases. - Now you simply need to apply the patch from within this directory. This is very simply done by adding
patch --strip=1 --input=pkgname.patchto yourprepare()function, changingpkgname.patchto the name of the file containing the diff (the file that was automatically copied into yoursrcdirectory because it was in thesourcearray of thePKGBUILDfile).
An example prepare-function:
prepare() {
cd $pkgname-$pkgver
patch --forward --strip=1 --input="${srcdir}/eject.patch"
}
Run makepkg (from the terminal now). If all goes well, the patch will be automatically applied, and your new package will contain whatever changes were included in the patch. If not, you may have to experiment with the --strip option of patch. While experimenting, you might find --dry-run, --reverse or --verbose options usable. Read patch(1) for more information.
Basically it works as follows. If the diff file was created to apply patches to files in myversion/, the diff files will be applied to myversion/file. You are running it from within the yourversion/ directory (because you would cd into that directory in the PKGBUILD), so when patch applies the file, you want it to apply it to the file file, taking off the myversion/ part. --strip=1 does this, by removing one directory from the path. However, if the developer patched in myfiles/myversion, you need to remove two directories, so you use --strip=2.
If you do not apply a --strip option, it will take off all directory structure. This is ok if all the files are in the base directory, but if the patch was created on myversion/ and one of the edited files was myversion/src/file, and you run the patch without a --strip option from within yourversion, it will try to patch a file named yourversion/file.
Most developers create patches from the parent directory of the directory that is being patched, so --strip=1 will usually be right.
Using quilt
A simpler way to create patches is using quilt which provides better support for managing many patches, such as applying patches, refreshing patches, and reverting patched files to original state. quilt is used on Debian to manage their patches. See Using Quilt for basic information about basic quilt usage to generate, apply patches, and reverting patched files.
See also
- http://www.kegel.com/academy/opensource.html — Useful information on patching files