Best Practices for Creating Patch with Diff?

bobross419

Golden Member
Oct 25, 2007
1,981
1
0
I've made some changes to some source code on a *nix system and was wondering what the best practices are for creating patch files using diff. I've done some googling, but all I've really found is folks saying "use the arguments". Man page for diff doesn't help me much either: "-c Use the context output format". Unfortunately it doesn't tell me what the differences are between the different output formats.

I would like for my patch to work against different versions of the same source, and since the function I'm modifying is the same I figured it would work, but apparently it fails since the line numbers are different.

tl;dr
Anyone have any info on what are considered best practices for using diff to create patches?

Also, what options can I use so that I don't need to worry about line numbers?
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
I usually see unified format as described here:

http://en.wikipedia.org/wiki/Diff#Unified_format

Yup, do this. "diff -u9 -p oldfile newfile". If you find 9 lines too long, 3 (the default) is ok, but using more context sometimes allows a code reviewer to understand the exact change even without opening up the whole file (i.e. just by reading the diff). You can omit "-p" if diff doesn't understand the programming language you're using...but if you're using C or a syntactically-similar language it's convenient to have each part of the diff show what function it's operating on.

Context (in both "unified" and "context" diffs) also helps when you're dealing with patches that are slightly out of date - they'll work if line numbers change, and they can also detect cases where code has changed near the lines you want to patch (look at the stuff about "fuzz" in the patch man page). When the context changes, it's worth taking an extra look to make sure the patch will still do what you intend.
 

Elixer

Lifer
May 7, 2002
10,371
762
126
Isn't the source under version control, like with svn or git ?
If so, you can create patches easily with either of those...
Patch also works, but I find it better to use the one that the source's repo was made with.