Apache Subversion is an open source version control system, popular among many developers. Creating and applying patches for your source code is a significant task you might need to do at some point of coding, specially when working with large scale enterprise product source codes. This post is about how to use subversion to create a patch or apply a patch after doing some modifications into the source code.
First let's see how to see the differences made into our code. 'diff' command is used to view the all the uncommited changes done into a certain project folder.
To view these uncommited changes goto the relevant directory in the local project repository and type;
svn diff
This will list all the modifications done into the source code with the specific class names.If we want to view the changes done to a single class or limited set of classes that can be viewed by adding those file names into our command as,
svn diff File1 File2
But note that we have to be in the correct directory where the files are located, before entering the command.
Another option is view the differences between two revisions of the project.This is done by giving the specific version numbers with the 'diff' command.
svn diff -r revision1:revision2
As an example let's say we need to view the changes between revision 112 and 113. Then use svn diff -r 112:113, this will list the changes between the two given revisions only now.
As we can see the changes now next step is creating the patch file which contains these changes. The changes can be applied to the another source code using a .diff or a .patch file. To create a patch for the modified contents in a whole directory, following command can be used.
svn diff > bug_fix.patch
or
svn diff > bug_fix.diff
If the patch is for some changes in a single file this can be,
svn diff File1 > bug_fix.patch
There may be times that a patch is needed for the changes between two revisions. If so we need to give the necessary revisions with the command.
svn diff -r revision1:revision2 > bug_fix.patch
All the changes we did are safely written into a patch file now and they can be applied to someone else's code by applying this patch in the correct way.