How to Apply a Patch in a Debian Package

compilingdebianpackage-managementpatch

I put up a bug report and have been asked to apply the patch therein and see if it works. I have tried to find documentation about how to go about doing it but is unclear.

The closest I have been able to figure out is http://www.thegeekstuff.com/2014/12/patch-command-examples/ .

I downloaded the latest source via apt-get under a directory named dpkg –

$ sudo apt-get source dpkg 

This is how it looks –

[shirish@debian] - [~/games/dpkg] - [5692]
└─[$] pwd

/home/shirish/games/dpkg

That is the path and here it is –

┌─[shirish@debian] - [~/games/dpkg] - [5691]
└─[$] ls

d-m-h-verbose-version-check.patch  dpkg-1.18.15  dpkg_1.18.15.dsc  dpkg_1.18.15.tar.xz

I would like to make a backup and do a dry-run before applying the patch but need to know what commands and output I should expect. Also, I usually use –

$ fakeroot debian/rules build
$ fakeroot debian/rules binary 

to build a local deb package. Is this good enough ?

Update 1 – That didn't work –

┌─[shirish@debian] - [~/games/dpkg] - [5710]
└─[$] cd dpkg-1.18.15

┌─[shirish@debian] - [~/games/dpkg/dpkg-1.18.15] - [5711]
└─[$] dch -n "Apply d-m-h fix from #844701."

dch: fatal error at line 569:
debian/changelog is not writable!

So do I need to use sudo to have write access OR use chmod to change the rights/permissions. I want to do it the right way.

Update 2 – Redid the whole thing, the right way this time, stuck at the patching stage –

┌─[shirish@debian] - [~/games] - [5750]
└─[$] apt-get source dpkg

Reading package lists... Done
NOTICE: 'dpkg' packaging is maintained in the 'Git' version control system at:
https://anonscm.debian.org/git/dpkg/dpkg.git
Please use:
git clone https://anonscm.debian.org/git/dpkg/dpkg.git
to retrieve the latest (possibly unreleased) updates to the package.
Skipping already downloaded file 'dpkg_1.18.15.dsc'
Skipping already downloaded file 'dpkg_1.18.15.tar.xz'
Need to get 0 B of source archives.
dpkg-source: info: extracting dpkg in dpkg-1.18.15
dpkg-source: info: unpacking dpkg_1.18.15.tar.xz

Then –

┌─[shirish@debian] - [~] - [5755]
└─[$] cp d-m-h-verbose-version-check.patch games/dpkg-1.18.15 

Then –

┌─[shirish@debian] - [~/games/dpkg-1.18.15] - [5758]
└─[$] ls

ABOUT-NLS   ChangeLog      configure     debian                             dpkg-split   m4           NEWS         run-script  t-func
aclocal.m4  ChangeLog.old  configure.ac  d-m-h-verbose-version-check.patch  dselect      Makefile.am  po           scripts     THANKS
AUTHORS     check.am       COPYING       doc                                get-version  Makefile.in  README       src         TODO
build-aux   config.h.in    data          dpkg-deb                           lib          man          README.l10n  t           utils

and then –

┌─[shirish@debian] - [~/games/dpkg-1.18.15] - [5757]
└─[$] patch < ./d-m-h-verbose-version-check.patch

(Stripping trailing CRs from patch; use --binary to disable.)
can't find file to patch at input line 5
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git i/scripts/dpkg-maintscript-helper.sh w/scripts/dpkg-maintscript-helper.sh
|index f20d82647..8db4a4088 100755
|--- i/scripts/dpkg-maintscript-helper.sh
|+++ w/scripts/dpkg-maintscript-helper.sh
--------------------------
File to patch:

now confused what to do ?

Update 3

Did it with -p1 parameter and did the remaining steps –

Sharing the last 5 odd lines of the build –

dh_md5sums -i
dh_builddeb -i
dpkg-deb: building package 'dpkg-dev' in '../dpkg-dev_1.18.15+nmu1_all.deb'.
dpkg-deb: building package 'libdpkg-perl' in '../libdpkg-perl_1.18.15+nmu1_all.deb'.
 dpkg-genchanges  >../dpkg_1.18.15+nmu1_amd64.changes
dpkg-genchanges: info: including full source code in upload
 dpkg-source --after-build dpkg-1.18.15+nmu1
dpkg-source: info: using options from dpkg-1.18.15+nmu1/debian/source/options: --compression=xz
dpkg-buildpackage: info: full upload; Debian-native package (full source is included)

and have been able to install the newest one –

┌─[shirish@debian] - [~/games] - [5812]
└─[$] sudo dpkg -i dpkg_1.18.15+nmu1_amd64.deb dpkg-dev_1.18.15+nmu1_all.deb dpkg-dbgsym_1.18.15+nmu1_amd64.deb dselect_1.18.15+nmu1_amd64.deb dselect-dbgsym_1.18.15+nmu1_amd64.deb libdpkg-perl_1.18.15+nmu1_all.deb libdpkg-dev_1.18.15+nmu1_amd64.deb

D000001: ensure_diversions: new, (re)loading
D000001: ensure_statoverrides: new, (re)loading
(Reading database ... 1207494 files and directories currently installed.)
Preparing to unpack dpkg_1.18.15+nmu1_amd64.deb ...
D000001: process_archive oldversionstatus=installed
D000001: cmpversions a='0:1.18.15+nmu1' b='0:1.16.1' r=2
D000001: cmpversions a='0:1.18.15+nmu1' b='0:1.16.2' r=2
D000001: ensure_diversions: same, skipping
Unpacking dpkg (1.18.15+nmu1) over (1.18.10) ...
D000001: cmpversions a='0:1.18.15+nmu1' b='0:1.16.2' r=2
D000001: ensure_diversions: same, skipping
D000001: process_archive updating info directory
D000001: generating infodb hashfile
Preparing to unpack dpkg-dev_1.18.15+nmu1_all.deb ...
D000001: process_archive oldversionstatus=unpacked but not configured
D000001: ensure_diversions: same, skipping
Unpacking dpkg-dev (1.18.15+nmu1) over (1.18.15+nmu1) ...
D000001: process_archive updating info directory
D000001: generating infodb hashfile
Preparing to unpack dpkg-dbgsym_1.18.15+nmu1_amd64.deb ...
D000001: process_archive oldversionstatus=unpacked but not configured
Unpacking dpkg-dbgsym (1.18.15+nmu1) over (1.18.15+nmu1) ...
D000001: process_archive updating info directory
D000001: generating infodb hashfile
Preparing to unpack dselect_1.18.15+nmu1_amd64.deb ...
D000001: process_archive oldversionstatus=installed
D000001: ensure_diversions: same, skipping
Unpacking dselect (1.18.15+nmu1) over (1.18.15+nmu1) ...
D000001: process_archive updating info directory
D000001: generating infodb hashfile
Preparing to unpack dselect-dbgsym_1.18.15+nmu1_amd64.deb ...
D000001: process_archive oldversionstatus=installed
Unpacking dselect-dbgsym (1.18.15+nmu1) over (1.18.15+nmu1) ...
D000001: process_archive updating info directory
D000001: generating infodb hashfile
Preparing to unpack libdpkg-perl_1.18.15+nmu1_all.deb ...
D000001: process_archive oldversionstatus=unpacked but not configured
Unpacking libdpkg-perl (1.18.15+nmu1) over (1.18.15+nmu1) ...
D000001: process_archive updating info directory
D000001: generating infodb hashfile
Preparing to unpack libdpkg-dev_1.18.15+nmu1_amd64.deb ...
D000001: process_archive oldversionstatus=installed
Unpacking libdpkg-dev:amd64 (1.18.15+nmu1) over (1.18.15+nmu1) ...
D000001: process_archive updating info directory
D000001: generating infodb hashfile
D000001: process queue pkg dpkg:amd64 queue.len 6 progress 1, try 1
Setting up dpkg (1.18.15+nmu1) ...
D000001: deferred_configure updating conffiles
D000001: ensure_diversions: same, skipping
D000001: process queue pkg dpkg-dev:all queue.len 5 progress 1, try 1
D000001: process queue pkg dpkg-dbgsym:amd64 queue.len 5 progress 2, try 1
Setting up dpkg-dbgsym (1.18.15+nmu1) ...
D000001: deferred_configure updating conffiles
D000001: process queue pkg dselect:amd64 queue.len 4 progress 1, try 1
Setting up dselect (1.18.15+nmu1) ...
D000001: deferred_configure updating conffiles
D000001: process queue pkg dselect-dbgsym:amd64 queue.len 3 progress 1, try 1
Setting up dselect-dbgsym (1.18.15+nmu1) ...
D000001: deferred_configure updating conffiles
D000001: process queue pkg libdpkg-perl:all queue.len 2 progress 1, try 1
Setting up libdpkg-perl (1.18.15+nmu1) ...
D000001: deferred_configure updating conffiles
D000001: process queue pkg libdpkg-dev:amd64 queue.len 1 progress 1, try 1
Setting up libdpkg-dev:amd64 (1.18.15+nmu1) ...
D000001: deferred_configure updating conffiles
D000001: process queue pkg dpkg-dev:all queue.len 0 progress 1, try 1
Setting up dpkg-dev (1.18.15+nmu1) ...
D000001: deferred_configure updating conffiles
Processing triggers for man-db (2.7.5-1) ...
D000001: ensure_diversions: same, skipping
D000001: cmpversions a='0:2016.03.30' b='0:2016.05.24' r=-2
D000001: cmpversions a='0:1.18.15+nmu1' b='0:1.16' r=2
D000001: cmpversions a='0:1.18.15+nmu1' b='0:1.16' r=2
D000001: cmpversions a='0:1.18.15+nmu1' b='0:1.16' r=2

And lastly –

┌─[shirish@debian] - [/usr/share/doc/dpkg] - [5815]
└─[$] zcat changelog.Debian.gz | less

dpkg (1.18.15+nmu1) UNRELEASED; urgency=medium

  * Non-maintainer upload.
  * Apply d-m-h fix from #844701

 -- shirish <shirish@debian>  Mon, 21 Nov 2016 01:04:02 +0530

dpkg (1.18.15) unstable; urgency=medium

This means that it got installed correctly.

[$] apt-show-versions dpkg dpkg-dbgsym dpkg-dev libdpkg-perl libdpkg-dev dselect dselect-dbgsym                                     
dpkg:amd64 1.18.15+nmu1 newer than version in archive
dpkg-dbgsym:amd64 1.18.15+nmu1 newer than version in archive
dpkg-dev:all 1.18.15+nmu1 newer than version in archive
dselect:amd64 1.18.15+nmu1 newer than version in archive
dselect-dbgsym:amd64 1.18.15+nmu1 newer than version in archive
libdpkg-dev:amd64 1.18.15+nmu1 newer than version in archive
libdpkg-perl:all 1.18.15+nmu1 newer than version in archive

Best Answer

Starting with the situation you have:

cd dpkg-1.18.15
patch -p1 < ../d-m-h-verbose-version-check.patch

will apply the patch. Before building, add a NMU changelog entry (this will avoid having your patched version of dpkg overwritten by apt & co., but will ensure your version is upgraded to the next dpkg release when that's available):

dch -n "Apply d-m-h fix from #844701."

This will rename the current directory (because dpkg is a native package), so you need to change directories again:

cd ../dpkg-1.18.15+nmu1

To build, I tend to use

dpkg-buildpackage -us -uc

That will produce the various .deb files in the parent directory; you can install them using dpkg as usual.

(Calling debian/rules targets explicitly works too; but you shouldn't use fakeroot for debian/rules build, just for debian/rules clean and debian/rules binary.)

Adding a NMU changelog entry also ensures that the source you've downloaded is left untouched, which addresses your backup concerns. It also means that reinstalling version 1.18.15 will restore the Debian version, without your patch.

Related Question