Thursday, January 19, 2012

Patches from SVN

Creating and Applying Patches for Subversion


Subversion is a source code versioning system that allows developers to concurrently make changes to source code and reconcile any differences before a release is deployed.

To check out a copy of Habari using subversion to the current directory using the command line:
svn checkout http://svn.habariproject.org/habari/trunk/htdocs .

If you want to check out tests as well, leave the htdocs off the directory path above.

To update an existing working copy to the latest code in the repository, execute this from the command line in the directory containing the working copy:

svn update

There are a number of graphical interfaces to interact with subversion for each operating system. These may be easier to use if you are not as familiar with running commands from the command line.

Contents

[hide]

Common SVN Tasks

Merge Trunk Into a Branch

When working on a branch, the branch may become out of sync with trunk. To get your branch up to date with trunk, you must merge trunk into the branch. Doing so is a four step process.

  1. First, you must find the last revision that trunk was merged into the branch. To do so, hunt back through the commit logs.
    svn log --stop-on-copy
  2. Next, you merge all the changes that have happened on trunk since that time into your branch. To do so, execute the following command, substituting "A" with the revision from step 1.
    svn merge -r:HEAD http://svn.habariproject.org/habari/trunk/ .
  3. When merging, some conflicts may be created. These must be resolved before you can commit again. Information on resolving conflicts is available from the svn documentation.
  4. Once all conflicts have been resolved, just commit your changes to the branch.
    svn ci -m "merge trunk changes from rA:HEAD into branch"

Testing patches that are not yet released

What are diff files ?

A diff file is the difference between a version of the source code in the repository and a working copy. It represents any changes, such as additional features or bug fixes, that have been made by a developer in a working copy. A diff file is generated using the svn diff command.

svn diff > descriptive_name_of_patch.diff

Applying diff files to your copy of habari

You can use the patch command to apply the diff file.

For windows systems, you can obtain a copy of patch from the gnuwin32 project

The common execution is patch -p0 < /path/to/file. This command will apply the differences that have been recorded in file to the appropriate file(s) in the current directory. As one diff file can contain changes for many files, and it preserves relative paths to sub-directories, the format of the diff file may affect where you need to invoke patch.

A practical example of diff and patch

predominantly from Scott Merrill's answer to Michael Bishop's question....

I modified a whole bunch of files for the Site class. When I was done editing, I changed to the htdocs directory of my checkout:

$ cd /home/skippy/code/habari/trunk/htdocs

I then executed svn status to see (and review) a list of all the files in my local copy that I have changed:

$ svn status

After double-checking the list of files, I executed svn diff to output a list of all the changes to all the files. I piped this output into a file:

$ svn diff > /tmp/site.diff

I attached the diff file to an email, or upload it to the Google issue tracker.

You read my email, and save my diff file. You transfer this file to your web host, which is running an SVN checkout of Habari. Because my diff was made from /trunk/htdocs, you should change to that directory in your local copy. (You may need to discern this from the contents of the diff file, if you don't know otherwise.)

Once in the target directory, you execute:

$ patch -p0 < /path/to/site.diff

The output of patch should report what it's doing:

skippy@skippy:~/code/habari/trunk/htdocs$ patch -p0 < /tmp/site.diff patching file system/admin/footer.php patching file system/admin/content.php patching file system/admin/dashboard.php patching file system/admin/header.php patching file system/classes/url.php patching file system/classes/site.php patching file system/classes/plugins.php patching file system/classes/feedbackhandler.php patching file system/classes/post.php patching file system/classes/options.php patching file system/classes/installhandler.php patching file system/classes/user.php patching file system/classes/controller.php patching file system/installer/db_setup.php patching file index.php patching file user/themes/k2/comments.php patching file user/themes/k2/header.php

If there are any problems, patch will report which file(s) failed. Figuring out what failed for each file, and why, can sometimes be challenging.

When you've determined that the patch works (or not!), and you want to go back to your vanilla Habari, you use the Subversion revertcommand. Using the "-R" (recursive) flag lets you easily revert your entire checkout to a clean state:

skippy@skippy:~/code/habari/trunk/htdocs$ svn -R revert * Skipped 'config.php' Reverted 'index.php' Reverted 'system/admin/footer.php' Reverted 'system/admin/content.php' Reverted 'system/admin/dashboard.php' Reverted 'system/admin/header.php' Reverted 'system/classes/url.php' Reverted 'system/classes/site.php' Reverted 'system/classes/plugins.php' Reverted 'system/classes/feedbackhandler.php' Reverted 'system/classes/post.php' Reverted 'system/classes/options.php' Reverted 'system/classes/installhandler.php' Reverted 'system/classes/user.php' Reverted 'system/classes/controller.php' Reverted 'system/installer/db_setup.php' Reverted 'user/themes/k2/comments.php' Reverted 'user/themes/k2/header.php' 

Scripts to Semi-Automate SVN usage

#1173 contains a few scripts to semi-automate patch generation and testing. They were written for use on a Mac. Savvy Linux users will know how to change hdiff as needed.

hreset

hreset

is shorthand for:

svn revert -R *

and proceeding with a mass clean-up of .orig, .rej, .diff and .patch files that might be around.

hdiff

hdiff hdiff 123

are shorthand for:

svn diff svn diff > ~/Desktop/123.diff

hpatch and htest

htest is shorthand for calling hreset and hpatch immediately after it.

hpatch https://trac.habariproject.org/habari/attachment/ticket/132/132.diff hpatch 123 hpatch 123.2.diff hpatch 123 456.diff 

are shorthand for:

curl -s --connect-timeout 3 https://trac.habariproject.org/habari/raw-attachment/ticket/123/123.diff -o 123.diff &&   patch -p0 -l -i 123.diff && rm 123.diff curl -s --connect-timeout 3 https://trac.habariproject.org/habari/raw-attachment/ticket/123/123.diff -o 123.diff &&   patch -p0 -l -i 123.diff && rm 123.diff curl -s --connect-timeout 3 https://trac.habariproject.org/habari/raw-attachment/ticket/123/123.2.diff -o 123.2.diff &&   patch -p0 -l -i 123.2.diff && rm 123.2.diff curl -s --connect-timeout 3 https://trac.habariproject.org/habari/raw-attachment/ticket/123/456.diff -o 456.diff &&   patch -p0 -l -i 456.diff && rm 456.diff

No comments:

Post a Comment