This is Gentoo's testing wiki. It is a non-operational environment and its textual content is outdated.
Please visit our production wiki at https://wiki.gentoo.org
Patches
This article exists since there was not a section detailing how to actually "create patches". The article on clean patches should eventually be integrated into this article as well. This article belongs on the user wiki as any user may create their own patches under /etc/portage/patches and it is not exclusively a "developer thing"
This page describes how to create a source code patch and tells about patching best practices.
Creating a patch
A source code patch for an existing package can easily be created using git. Of course for doing so, dev-vcs/git needs to be installed.
Step 1: Unpack the questionable package using the ebuild command:
user $
ebuild $(portageq get_repo_path / gentoo)/sys-fs/lvm2/lvm2-2.02.145-r2.ebuild clean unpack
... >>> Unpacking source... >>> Unpacking LVM2.2.02.145.tgz to /var/tmp/portage/sys-fs/lvm2-2.02.145-r2/work >>> Source unpacked in /var/tmp/portage/sys-fs/lvm2-2.02.145-r2/work
Step 2: Step into the package directory which was created inside work:
user $
cd /var/tmp/portage/sys-fs/lvm2-2.02.145-r2/work/LVM2.2.02.145/
In case the unpacked turns out to be a git directory, steps 3 and 4 can be skipped. If not, don't skip.
Step 3: Initialize the unpacked package sources as a git repository:
user $
git init
Initialized empty Git repository in /var/tmp/portage/sys-fs/lvm2-2.02.145-r2/work/LVM2.2.02.145/.git/
Step 4: Add all the existing files to git and do a commit:
user $
git add .
user $
git commit
Step 5: Do the necessary changes. Change one file or even more. Of course, the needed changes should be known.
user $
sed -e 's/MAKEDEV:-"debian"/MAKEDEV:-"gentoo"/' -i scripts/lvm2create_initrd/lvm2create_initrd
Step 6: When changes are done, let git show the diff and tee it into the patch file:
user $
git diff | tee /tmp/foobar.patch
diff --git a/scripts/lvm2create_initrd/lvm2create_initrd b/scripts/lvm2create_initrd/lvm2create_initrd index 6e70c55..1e46b5e 100644 --- a/scripts/lvm2create_initrd/lvm2create_initrd +++ b/scripts/lvm2create_initrd/lvm2create_initrd @@ -57,7 +57,7 @@ DEVRAM=/tmp/initrd.$$ BINFILES=${BINFILES:-"`which lvm` `which bash` `which busybox` `which pivot_root`"} BASICDEVICES=${BASICDEVICES:-"std consoleonly fd"} BLOCKDEVICES=${BLOCKDEVICES:-"md hda hdb hdc hdd sda sdb sdc sdd"} -MAKEDEV=${MAKEDEV:-"debian"} +MAKEDEV=${MAKEDEV:-"gentoo"} # Uncomment this if you want to disable automatic size detection #INITRDSIZE=4096
Adjusting a malformed patch
TortoiseGit and Git for Windows
Patches generated by Tortoise Git and Git for Windows are padded with additional information that patch
does not understand.
PATCH.patch
Typical Tortoise Git/Git for Windows PatchFrom HASH Day Mon DD hh:mm:ss YYYY From: AUTHOR <AUTHOR@E-MAIL.TLD> Date: Day, DD MMM YYYY hh:mm:ss +hhmm Subject: [PATCH] COMMIT MESSAGE --- PATH/TO/FILE.EXT 1 + 1 file changed, 1 insertion(+) diff --git a/PATH/TO/FILE.EXT b/PATH/TO/FILE.EXT index HA..SH CODE --- a/PATH/TO/FILE.EXT +++ b/PATH/TO/FILE.EXT @@ -56,6 +56,7 @@ + some new code - some old code -- VERSION.windows.REVISION
The individual generating the patch has to strip these additional lines to make a viable patch file.
PATCH.patch
Acceptable Tortoise Git/Git for Windows Patch--- a/PATH/TO/FILE.EXT +++ b/PATH/TO/FILE.EXT @@ -56,6 +56,7 @@ + some new code - some old code
Nesting
If the file(s) being patched are not unpacked into the root of the working directory, PATH/TO/A/FILE, but rather some sub-directory of the working directory, SOME/OTHER/PATH/TO/A/FILE, then the patch must be modified to accommodate it.
PATCH.patch
--- a/SOME/OTHER/PATH/TO/FILE.EXT +++ b/SOME/OTHER/PATH/TO/FILE.EXT ...
This is necessary for, atleast, Go based ebuilds which nest the source code under the repository path e.g. http://git.com/project/repository gets unpacked into the working directory under src/git.com/project/repository
See also
- /etc/portage/patches — provide a way for users to apply patches to package source code
- User:Veremit/Patch_format —
- GLEP 25 - Technical information about the formal inclusion and usage of patches within portage.
External resources
- How to write clean patches when not using git-format-patch.
- The developer manual also discusses patches
- base.eclass - Describes how ebuilds should use PATCHES=( "${FILESDIR}/mypatch.patch" "${FILESDIR}/patches_folder/" )