pages tagged diffrohieb.namehttps://rohieb.name/blag/tag/diff/rohieb.nameikiwiki2013-10-24T16:30:29ZSplitting overly large hunks in patcheshttps://rohieb.name/blag/post/splitting-overly-large-hunks-in-patches/rohieb
CC-BY-SA 3.0
2013-10-24T16:30:29Z2013-10-24T16:30:29Z
<p>Today I stumbled over a lengthy patch on my harddisk. It was about half a year
old, and consisted of only one hunk, which was about 1000 lines in length. Most
of the contents were indentation changes from tabs to spaces, but I knew that
the patch contained a small useful portion, which I wanted to extract. What was
slightly more annoying was the fact the the patch did not apply cleanly to the
file it was supposed to change, and <code>patch</code> only applies hunks atomically, the
whole patch was rejected.</p>
<p>Since I did not want to compare each of the lines in the patch visually and
decide whether they changed only whitespace, I tried to look for a way to split
the patch into smaller hunks. My first try was looking at the useful tool in the
<a href="http://cyberelk.net/tim/software/patchutils/">patchutils</a> package, but none of
them did what I wanted, they only allowed me to split patches into single hunks
(but my patch already had only one hunk).</p>
<p>But after a bit of googling, I found out that Emacs has a
<a href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Diff-Mode.html"><code>diff-split-hunk</code></a> command, so I installed Emacs (for the first time
in my life), opened my patch, selected Emacs' Diff mode with <code>M-x diff-mode</code>,
and split the patch into smaller hunks by pressing <code>C-x C-s</code> on appropriate
context lines. After saving, the patch applied cleanly except for two smaller
hunks, which I could easily identify as containing only whitespace changes. Then
I could compare my patched file with the original file, this time ignoring
whitespace changes with <code>diff -w</code>, and, voilĂ , I got the seven useful lines I
wanted.</p>
<p>For illustration, see the different <a href="https://rohieb.name/blag/post/splitting-overly-large-hunks-in-patches/edit-stages/">edit stages of my patch</a> on a
separate page.</p>