From Gentoo Wiki
Jump to:navigation Jump to:search

This page amends the Devmanual's patches article and describes how to create a patch for source code.

Creating a patch

Using git

It may be necessary to use ebuild ... clean unpack instead to avoid applying any patches already used within the ebuild.

A source code patch for an existing package can easily be created using git diff.

Unpack the package to be patched using the ebuild command:

user $ebuild $(portageq get_repo_path / gentoo)/sys-fs/lvm2/lvm2-2.02.145-r2.ebuild clean prepare
>>> 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 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/

Initialize a git repository

If the unpacked directory is a git repository, skip the following initialization of the git repository.

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/

Add all the existing files to git and do a commit. Remember to write a commit message if you want!

user $git add .
user $git commit -m "Initial commit"

Make changes

Now make the necessary changes to the unpacked source code, one or more files may be changed. Of course, the required changes should probably be known in advance.

Here is an example of a command making changes to the source code:

user $sed -e 's/MAKEDEV:-"debian"/MAKEDEV:-"gentoo"/' -i scripts/lvm2create_initrd/lvm2create_initrd

Create diff using git

When changes are done, have git output 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"}
 # Uncomment this if you want to disable automatic size detection
According to the Devmanual the patch should be cleaned of metadata. So the command could be enhanced like this:
git diff | grep -v '^diff\|^index' | tee /tmp/foobar.patch. Or use scrub-patch from app-portage/iwdevtools.

Nesting patches in the directory hierarchy

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.

This is necessary for, at least, Go based ebuilds which nest the source code under the repository path e.g. gets unpacked into the working directory under src/


Adjusting a malformed patch from TortoiseGit or Git for Windows

Patches generated by Tortoise Git and Git for Windows are padded with additional information that patch does not understand.

FILE PATCH.patchTypical Tortoise Git/Git for Windows Patch
From HASH Day Mon DD hh:mm:ss YYYY
Date: Day, DD MMM YYYY hh:mm:ss +hhmm

 1 file changed, 1 insertion(+)

index HA..SH CODE
@@ -56,6 +56,7 @@ 
+  some new code
-  some old code

The individual generating the patch has to strip these additional lines to make a viable patch file.

FILE PATCH.patchAcceptable Tortoise Git/Git for Windows Patch
@@ -56,6 +56,7 @@ 
+  some new code
-  some old code

See also

External resources

  • How to write clean patches when not using git-format-patch.
  • base.eclass - Describes how ebuilds should use PATCHES=( "${FILESDIR}/mypatch.patch" "${FILESDIR}/patches_folder/" )