SVN & Subclipse, Patching and Reverse Patching

You probably know that you can get diff report between two revisions using the SVN diff command, but subclipse has a feature built into it to help with patching and unpatching which is a pretty powerful tool I didn’t take note of until recently.

 Patching is useful in the following scenarios :

  • You want to add changes from one branch to another that are out of sync without doing a full merge (I’m not always in favor of merge as it doesn’t tell you where errors are). This is particularly usefull when merging changes into the trunk as you can get a complete over site of the changes being applied.
  • Something breaks between commits, a good example is when things get moved to the wrong place, or files get accidently deleted.
  • You want to bring a specific change from one branch to another. With a project I was recently working on the trunk version isn’t merged until it goes to production and has been approved by testing and the client, which means there can be two branches being worked on at once due to time commitments (the first being the current version being tested and the second with new fixes that aren’t applicable to the version being tested - which I’m working on in advance of the next release).
  • Assert a specific change that caused a bug with your app.
  • You want to merge specific parts of a branch with another, or your trunk.
To get the diff report use the following :
  • svn diff -r [old revision]:[comparison]  {location - optional, if not it uses the current repo location} or
     
  • svn diff {old location}@[revision] {new location}@[revision]

adding ‘ > [file name]‘ to windows and macs outputs this to a nice file, which is useful later.

In subclipse there’s also a useful feature to let you get a diff report from the history to a file :
  1. Right click the file / directory
  2. Goto Team > Show History
  3. Right Click the revision and then select ‘create unified diff‘.
I don’t use this feature that much, but from what I understand this will give you a diff report of a specific revision commit (but not between two revisions).
Applying the patch with Subclipse
Once you have created the diff file you can then apply it as a patch to another branch.
First you will need to SVN switch to the brach/tag you want to apply the patch to; now right click your project, goto Team > Apply Patch and you’ll be given a wizard for applying the patch :
  1. Select the diff file you created above, click next.
  2. Select the project you want to apply the patch to.
  3. You’ll see a cool interface where you can see visually the differences between the patch you created and select the differences you want to apply.
  4. Exclude differences by right clicking the change(s) and selecting exclude, or move the changes to another file (if a file has changed location or name).
A cool feature in this is the reverse patch option. This essentially allows you to remove changes from a diff file in reverse. For example :

The current head is at revision 185, but there were changes made between 176 and 179 you want to reverse because they caused a problem.

  1. switch to the branch you want to apply the patch to
  2. svn diff for the specified revision range (svn diff -r 176:179 > 176-179.diff)
  3. Go through the above to point 3
  4. Select the reverse patch option
You’ll now see a visual representation, much like the SVN sync feature showing you the changes that are going to be made and conflicts. Remember, you can exclude patch changes by right clicking the file and selecting exclude.
Note: By selecting ‘generate .rej file for un-merged hunks‘ the patcher will create a neat little file in the same location of the relevant file if an un-matched diff is found. Also (as with merge) SVN will not commit your changes to the repository until you select commit, therefore all your changes can be undone by simply selecting the revert option and reverting to the HEAD revision.
SVN Diff / Patch Vs Merge
As a policy, I only use SVN merge once I have a version I want to commit to the trunk (normally once this has been tested and checked as relatively bug free). SVN merge is relatively useful if you are in a scenario where there are few differences between your branch(es) and trunk and (or) there is only one active branch to be merged - your trunk should never be changed directly and therefore you don’t need to worry about conflicts in changes between these branches, you are simply taking the changes from one branch and adding them to the another.
If however you find yourself in a scenario where there are two or more active branches, or you want to apply a patch to your tagged version from an amended branch this tool becomes pretty powerful.
For more information regarding branching and merging approaches I would suggest reading the following:

 

0 Responses to “SVN & Subclipse, Patching and Reverse Patching”


  1. No Comments

Leave a Reply

You must login to post a comment.