VersionControl_SVN
[ class tree: VersionControl_SVN ] [ index: VersionControl_SVN ] [ all elements ]

Introduction to using VersionControl_SVN

Using VersionControl_SVN to manage a Subversion repository

by Clay Loveless
clay@killersoft.com
Copyright 2004, Clay Loveless

Table of Contents

Introduction

VersionControl_SVN is a simple Object-Oriented interface for the svn command-line application that makes up the core of Subversion, a free/open-source version control system.

Subversion can be used to manage trees of source code, text files, image files -- just about any collection of files.

VersionControl_SVN's features include:

  • Full support of svn subcommands.

  • Flexible error reporting provided by PEAR_ErrorStack

  • Multi-object factory design.

  • Fully documented source code

  • The power of a version control system like Subversion, when accessed through VersionControl_SVN, can be extended far beyond typical "source code" repositories.

    For example, what content management system (CMS) couldn't benefit from version control functionality? For many non-programmers, version control is a confusing subject to get a firm grasp on. With VersionControl_SVN, developers are now able to customize the interface to Subversion with the ease-of-use goals of their particular audience in mind. VersionControl_SVN lets you leverage the strengths of version control without burdening end-users with the learning curve of change control fundamentals.

    A Simple Example

    So you've got Subversion repository set up somewhere, and you want to take a look at what's inside with a PHP script. With the VersionControl_SVN::VersionControl_SVN_List() command, you're just a few steps away.

    1. <?php
    2. require_once 'VersionControl/SVN.php';
    3.  
    4. // Setup error handling -- always a good idea!
    5. $svnstack &PEAR_ErrorStack::singleton('VersionControl_SVN');
    6.  
    7. // Set up runtime options. 
    8. $options = array('fetchmode' => VERSIONCONTROL_SVN_FETCHMODE_ARRAY);
    9. // Request list class from factory
    10. $svn VersionControl_SVN::factory('list'$options);
    11.  
    12. // Define any switches and aguments we may need
    13. $switches = array('username' => 'user''password' => 'pass');
    14. $args = array('svn://svn.example.com/repos/TestProject');
    15.  
    16. // Run command
    17. if ($output $svn->run($args$switches)) {
    18.     print_r($output);
    19. else {
    20.     if (count($errs $svnstack->getErrors())) 
    21.         foreach ($errs as $err{
    22.             echo '<br />'.$err['message']."<br />\n";
    23.             echo "Command used: " $err['params']['cmd'];
    24.         }}
    25. }
    26. ?>

    If your example repository above happened to have the VersionControl_SVN source in it, your output would be something like this:

    1. <?php
    2. Array
    3. (
    4.     [0=> Array
    5.         (
    6.             [name=> docs
    7.             [type=> D
    8.         )
    9.  
    10.     [1=> Array
    11.         (
    12.             [name=> package.xml
    13.             [type=> F
    14.         )
    15.  
    16.     [2=> Array
    17.         (
    18.             [name=> SVN.php
    19.             [type=> F
    20.         )
    21.  
    22.     [3=> Array
    23.         (
    24.             [name=> SVN
    25.             [type=> D
    26.         )
    27.  
    28.     [4=> Array
    29.         (
    30.             [name=> tests
    31.             [type=> D
    32.         )
    33.  
    34. )
    35. ?>

    Note that in the above output, directories are flagged as type D, and files are flagged as type F.

    Note: For additional information in the output, try setting verbose to true in your $options array.

    One Factory To Rule Them All

    Have a script that needs to utilize several VersionControl_SVN subclasses? At the expense of a little overhead, you can be sure your $svn objects are fully-loaded by using the VersionControl_SVN::factory() command keyword __ALL__.

    For example, in a basic script to get the list of current files in a repository, you just need the VersionControl_SVN::VersionControl_SVN_List() subclass.

    1. <?php
    2. require_once 'VersionControl/SVN.php';
    3.  
    4. // Setup error handling -- always a good idea!
    5. $svnstack &PEAR_ErrorStack::singleton('VersionControl_SVN');
    6.  
    7. // Set up runtime options. 
    8. $options = array('fetchmode' => VERSIONCONTROL_SVN_FETCHMODE_ARRAY);
    9.  
    10. // Request list class from factory
    11. $svn VersionControl_SVN::factory('list'$options);
    12.  
    13. // Define any switches and aguments we may need
    14. $switches = array('username' => 'user''password' => 'pass');
    15. $args = array('svn://svn.example.com/repos/TestProject');
    16.  
    17. // Run command
    18. if ($output $svn->run($args$switches)) {
    19.     print_r($output);
    20. else {
    21.     if (count($errs $svnstack->getErrors())) 
    22.         foreach ($errs as $err{
    23.             echo '<br />'.$err['message']."<br />\n";
    24.             echo "Command used: " $err['params']['cmd'];
    25.         }}
    26. }
    27. ?>

    However, if you need to get a recursive list of files in a repository, look up the recent log activity for those files, and view the annotated source for those files, you've got two options.

    1. <?php
    2. require_once 'VersionControl/SVN.php';
    3.  
    4. // Setup error handling -- always a good idea!
    5. $svnstack &PEAR_ErrorStack::singleton('VersionControl_SVN');
    6.  
    7. // Set up runtime options. 
    8. $options = array('fetchmode' => VERSIONCONTROL_SVN_FETCHMODE_RAW);
    9.  
    10. // METHOD ONE: Lowest Overhead
    11. // Create svn object with subcommands we need listed out individually
    12. $svn VersionControl_SVN::factory(array('list''log''blame')$options);
    13.  
    14. // Define any switches and aguments we may need
    15. $switches = array('username' => 'user''password' => 'pass');
    16. $args = array('svn://svn.example.com/repos/TestProject');
    17.  
    18. print_r($svn->list->run($args$switches));
    19.  
    20. // Pick files out of the above output, and see who did what
    21. $args = array('svn://svn.example.com/repos/TestProject/trunk/index.php');
    22.  
    23. echo "<pre>" $svn->blame->run($args"</pre>";
    24.  
    25. // METHOD TWO: Put all available commands at your disposal
    26. // Load up all subcommands - higher overhead, but convenient for certain occasions
    27. $svn VersionControl_SVN::factory('__ALL__'$options);
    28.  
    29. // Now you may run whatever you need to ...
    30. $svn->cat->run($args$switches);
    31. $svn->info->run($args$switches);
    32. // ... and so on.
    33. ?>

    Further Reading

    If you are interested in learning more about Subversion, see the following:

  • Version Control with Subversion - The primary reference manual for all things related to Subversion, from general use to repository administration.

  • Subversion Website - The official Subversion website offers a FAQ, mailing list, and of course, the Subversion source code. Also included are links to GUI Subversion applications.


  • Documentation generated on Sun, 09 Aug 2009 21:30:04 +0000 by phpDocumentor 1.4.2. PEAR Logo Copyright © PHP Group 2004.