new blag post: Splitting overly large hunks in patches
[www-rohieb-name.git] / blag / post / splitting-overly-large-hunks-in-patches.mdwn
1 [[!meta title="Splitting overly large hunks in patches"]]
2 [[!meta author="rohieb"]]
3 [[!meta license="CC-BY-SA 3.0"]]
4 [[!img defaults size=x200]]
6 Today I stumbled over a lengthy patch on my harddisk. It was about half a year
7 old, and consisted of only one hunk, which was about 1000 lines in length. Most
8 of the contents were indentation changes from tabs to spaces, but I knew that
9 the patch contained a small useful portion, which I wanted to extract. What was
10 slightly more annoying was the fact the the patch did not apply cleanly to the
11 file it was supposed to change, and `patch` only applies hunks atomically, the
12 whole patch was rejected.
14 Since I did not want to compare each of the lines in the patch visually and
15 decide whether they changed only whitespace, I tried to look for a way to split
16 the patch into smaller hunks. My first try was looking at the useful tool in the
17 [patchutils]( package, but none of
18 them did what I wanted, they only allowed me to split patches into single hunks
19 (but my patch already had only one hunk).
21 But after a bit of googling, I found out that Emacs has a
22 [`diff-split-hunk`][emacs-dsh] command, so I installed Emacs (for the first time
23 in my life), opened my patch, selected Emacs' Diff mode with `M-x diff-mode`,
24 and split the patch into smaller hunks by pressing `C-x C-s` on appropriate
25 context lines. After saving, the patch applied cleanly except for two smaller
26 hunks, which I could easily identify as containing only whitespace changes. Then
27 I could compare my patched file with the original file, this time ignoring
28 whitespace changes with `diff -w`, and, voilĂ , I got the seven useful lines I
29 wanted.
31 [emacs-dsh]:
33 For illustration, see the different [[edit stages of my patch|edit-stages]] on a
34 separate page.
36 [[!tag patch patchutils diff GNU Emacs QDjango C++ howto]]
This page took 0.051621 seconds and 5 git commands to generate.