How to create and apply a patch with Subversion

It’s been a while since I posted something new on the use of Subversion. I’ve been working with the tool a lot, and I’ve found that patches are a great way to communicate code changes.

For those of you who are still learning, let me first explain what a patch is. A patch is a text file that contains the alteration that were made to a specific file. It includes the lines that have been removed and the lines that have been added. In short, if you have a ruby script and edited it, you could create a patch file, containing the changes you’ve made.

Why is this useful? You could check in your changes to your repository directly. True, but there are cases that you don’t have write access to the repository. For example, if you wanted to contribute code changes to Acts As Exportable, you should create a new ticket and attach a patch file. I will then review your changes before I apply them to the code and commit them to the repository.

So, how do you go about creating a patch file and how do you later apply it to your source?

Creating a patch file

Creating a patch file is really easy. First, check out the most recent version of the code from Subversion using the ‘checkout’ command.

Make your changes.

Then, in the root the project run the following command. It will store the patch file in your home directory. Make sure to give it meaningful filename.

svn diff > ~/fix_ugly_bug.diff

The file has the .diff extention, which stands for differences. This extension is recognized by many text editors and enables ’syntax highlighting’ automatically. (Give it a try with TextMate and you’ll know what I mean.)

You can send the diff-file to the author of the project by email, or you can create a ticket in Trac and add it as an attachment. The author will review the changes you made and possibly apply them to the source.

Applying a patch

You should never apply patches from any person other than your development team without first reading through the changes, apply them locally and test your application and then commit them. Patches can not only include bug fixes, but also alterations to create back doors or add other exploits to your code.

Always read through a patch before applying it!

When you are sure the patch will bring no harm to you, your application or your customers, go ahead an apply it to your working copy. Here, I assume that you downloaded the patch file we previously generated, and placed it in your home directory. In the root of your application now run:

patch -p0 -i ~/fix_ugly_bug.diff

This will apply all the changes in the patch to your source. The -p0 option makes sure that all files can be found correctly (this has to do with something called ‘zero directories’, I won’t get into that right now). The -i option tells ‘patch’ what to use as input, in this case the ‘fix_ugly_bug.diff’ file in your home directory.

With the code changes in place, run your tests and make sure everything works as expected. If it does, commit your changes and celebrate with a cup of coffee.

  • Twitter
  • Digg
  • del.icio.us
  • DZone
  • Reddit
  • email

14 Responses to “How to create and apply a patch with Subversion”

  1. There’s one thing to especially watch for: file renames. If there’s a renamed file, svn diff will not include the rename in the patch.

    Plain diff would represent the file move by removing the whole of the old file and adding the whole content under the new name. Subversion doesn’t do that.

  2. Karl Fogel says:

    Maciej, work is under way to fix this, by developing an extended diff format that can handle renames. Charles Acknin, a Google Summer of Code student working with the Subversion project is doing the work on a branch:

    http://svn.collab.net/repos/svn/branches/svnpatch-diff/

    Search for his recent threads in the dev@subversion.tigris.org mailing list archive for more.

    -Karl Fogel

  3. Sur says:

    Hi Ariejan,
    Thanks for this useful information.

  4. How To Run A Coffee Shop says:

    More info please…. Your topic about How to create and apply a patch with Subversion needs more comments. I\’d like to spend me Saturday nights reading about how to run a coffee shop

  5. [...] more rare: creating patches: see this post Published [...]

  6. alex says:

    how to deal with situation when “patch process” includes inserting new binary files(like images) to repositary?

  7. @alex I haven’t tried, but I think this will also be included one way or another in the .diff file generated. Just give it a try ;-)

  8. Soumen (rimbik) says:

    Hi,
    Lets say I have a project called ‘Proj-A’ located at :

    https://scmt-ccnet.company.com/svn/repos/Proj-A/Source,

    and I make changes here (Adding both new files and updating existing for fix).
    Assume I committed 10 files. Now I need to apply the same changes in the actual project base lets say in

    https://scmt-ccnet.company.com/svn/Final-repos/Proj-A/Source

    Question: Is it possible to use a patch using the said technique to apply all the changes in the new svn source?

    Regards,
    Soumen Dey (Rimbik), India

  9. Ian says:

    Hi there – its a pretty simple question (im new to svn) but i can seem to find the “patch” command.

    Ive tried “patch” and “svn patch” in a command window but neither exist. Any ideas what im doing wrong? Is this a seperate tool?

    Thanks in advance,
    Ian

  10. Thank you for this article. It really helped me out. Another tip is to use specific revision numbers like so:

    svn diff -r OLD:NEW > changes_file.diff

    And I just did a bunch of them by adding the file, then appending to the diff file on subsequent iterations.

    svn diff path/to/local/file.php -r OLD:NEW >> changes_file.diff

  11. @ian patch is not an SVN command. It’s a separate program. Not sure how you would obtain it depending on your OS, but I’m sure you could try yum or apt-get or aptitude install…

  12. Me says:

    Great writeup exactly what I was looking for. Thanks!

Leave a Reply