Source Code Changes Exchange Using Patches



The current process of coding – in my company – is to make peer review before committing changes to subversion – our version control system – I want to enforce code reviews, usually developers have permissions to update svn – subversion – directly, but we request them to not commit until the peer review. I think we need a better tool support to make exchanging changes, reviewing and testing it easier as we have many developers running in parallel on the same code base.

After some research I found the open source community already use the concept of patchs. developer A could make a bug fix or a new feature, generate a patch and send it to code committer – the person who have authority to commit changes to version control -, this person apply the patch to his current code base, try it, discuss it and finally commit the patch to the code base if OK.

I found the patch technique is useful to use it, I applied it in a pilot project and after being familiar with it, we applied it in all the projects, here I will specify the tools needed to do so in Windows XP, but the tools is open source and available in many other platforms, so the same tools and commands is usable across platforms.

1) You need of Cygwin if your using Windows.
2) You need subversion.
3) Generate Patch:
– Change directory to where your local working directory exist:
– $ svn diff > patch1.patch
– If you added or deleted files, you have to execute these commands before
– $ svn add file1.c
– $ svn delete file1.c

The add and delete will mark the files, so the svn diff command can generate the correct patch file.

4) Apply Patch:
– if you are using Linux, you have to install patch command if not found.
– if you are using Windows, use cygwin to install it.
– $ patch -p0 <>Note the -p parameter enables you to skip parts of the file path, you can read about through “man patch” manual page.

if you are using Windows, you may have a problem with CR LF – carriage return line feed -, the patch command may replace all CR LF with LF only, if so you have to convert LF back to CR LF, so use the following command on the shell prompt
$ unix2dos file1.c file2.c file3.c


How could you know if the CR LF is replaced? You can use Programmer’s Notepad to know about it


From ahm507.blogspot.com