2015/06/25: Fixing MUA-broken patches

Patches are a useful format for suggesting changes: at a glance one can see the changes, and one can apply them locally to have the suggested new version. And they can easily be sent via email—provided no one uses a broken mail client, that is. Unfortunately, some mail user agents "helpfully" break lines on behalf of the user, even without asking; and, even more unfortunately, people use those, e.g., because they have to do so for work email.

The good news is that unified diffs have a quite rigid format which restricts the symbols that might occur at the beginning of a line to a small set. This gives a simple heuristic to fix it: if a line doesn't start with one those symbols, it is the continuation of a line that was broken, probably dropping a space character. Of course, there is no guarantee that a line isn't broken just before, e.g., a plus symbol, and there is no guarantee either that a program that changes the mail content without the user's permission only replaces spaces by newlines, but in practise it works good enough. And it's just the following simple sequence of ed commands.


1
/^diff
.,$g/^[^-+@ ]/-1s/$/ /\
j
w
q
download