Home :: Documentation :: How to Use

Preface

Purpose of the JavaSVN library is to provide means to work with Subversion (SVN) repository and working copy files within java program without using native subversion executables or libraries. JavaSVN library is a complete replacement for svn javahl bindings and svn native client.

In general there are two kind of library users - first are using the API to access the repository or manage working copy files, second are extending the library to provide custom implementation for various library aspects, like connection protocols, merge implementation or working copy storage format. This page mainly describes what the first, more common type of users should know to work with the library.

Library features are divided into two major sets. First are repository access features, that could be considered as low-level ones and second are high-level operations that works both with repository and working copies. For instance, using the low-level API it is possible to implement a powerful repository browser and the high level API abstracts such operations as check out, update, commit - all that work both with repository information and local working copies. Needless to say that high-level API implementation uses low-level one.

In the sections below you will find more detailed information about those APIs

Setting up the library

JavaSVN is an extenable library, so it may use different implementations for different features or aspects. That is why it needs to be configured before first use. By configuring the library you describe which implementations should be used at run-time. If you're not writing eclipse plugin and using JavaSVN library as a standalone jar file, you have to set up the library to make it usable:

    // import neccessary files
    import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
    import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
    import org.tmatesoft.svn.core.internal.ws.fs.FSEntryFactory;

    // Set up connection protocols support:
    // for DAV (over http and https)
    DAVRepositoryFactory.setup();
    // for SVN (over svn and svn+ssh)
    SVNRepositoryFactoryImpl.setup();

    // Working copy storage (default is file system).
    FSEntryFactory.setup();	
Setting up default library implementations

Repository Access

Package org.tmatesoft.svn.core.io

Repository access API defines two basic notions: SVNRepository and SVNRepositoryLocation. Code below explains how to get an SVNRepository instance having a repository URL:

    String URL = "http://svn.collab.net/svn/trunk/";
    try { 
        SVNRepositoryLocation location = SVNRepositoryLocation.parseURL(URL);
        SVNRepository repository = SVNRepositoryFactory.create(location);

        // work with repository
    } catch (SVNException e) {
        e.printStackTrace();
    }
Obtaining SVNRepository instance

SVNRepository is an abstract class that declares all the available repository operations. For instance consider extending the example above to get latest repository revision and all the log messages in the repository:
   String URL = "http://svn.collab.net/svn/trunk/";
   try { 
       SVNRepositoryLocation location = SVNRepositoryLocation.parseURL(URL);
       SVNRepository repository = SVNRepositoryFactory.create(location);

       // get latest revisions
       int latestRevision = repository.getLatestRevision();
       System.out.println("[" + location.toString() + "] latest revision: " + latestRevision); 

       // log messages
       String[] targetPaths = new String[] {""};
       repository.log(targetPaths, 0, latestRevision, true, true, new ISVNLogEntryHandler() {
            public void handleLogEntry(SVNLogEntry logEntry) {
                System.out.println(logEntry.getRevision() + " : " + logEntry.getMessage());
            }
       });
    } catch (SVNException e) {
        e.printStackTrace();
    }
SVNRepository usage example

All the interfaces and classes referenced by SVNRepository class, like SVNLogEntry are located in the same package. For users familiar with the native subversion source code it may be useful to know that SVNRepository interface is more or less similar to the include/svn_ra.h declaration file.

Working Copies

Package org.tmatesoft.svn.core

JavaSVN library introduces notion of workspace that is represented by ISVNWorkspace interface. Workspace is a tree of checked out repository files, usually stored as a directory in the file system. To obtain ISVNWorkspace instance use the following code:

    try {
        File directory = new File(path); 
        ISVNWorkspace workspace = SVNWorkspaceManager.createWorkspace("file", directory.getAbsolutePath());
    } catch (SVNException e) {
        e.printStackTrace();
    }
Obtaining ISVNWorkspace instance

In the above example using file type to create workspace results in creating workspace that uses filesystem to store working copy files and administrative info. Now it is the only possible type to use, but eventually there could be other types, for instance db to use database as a storage media.

As soon as workspace is created you may perform various operations on this workspace. The following examples demonstrates how to check out files from repository into workspace, and how to commit modified workspace:

    try {
        File directory = new File(path); 
        ISVNWorkspace workspace = SVNWorkspaceManager.createWorkspace("file", directory.getAbsolutePath());

 	// checkout 
        SVNRepositoryLocation location = SVNRepositoryLocation.parseURL("http://svn.collab.net/repos/svn/);
        int revision = workspace.checkout(location, ISVNWorkspace.HEAD, false);
        System.out.println("Checkout out revision " + revision + ".");

	// modify working copy files
        // create new file.
        makeChanges();

        // add file, similar to "svn add path" command line.
        // note, path is relative to the workspace root, i.e. "directory".
        String newFilePath = "directory/newFile.txt";
	workspace.add(newFilePath);

        // commit changes
        int committedRevision = workspace.commit("change log message");
        System.out.println("committed revision: " + committedRevision);
    } catch (SVNException e) {
        e.printStackTrace();
    }
Working copy operations

Note that paths passed to the ISVNWorkspace methods should be relative to the workspace root - location used when creating workspace.

(c) 2004 TMate Software. All rights reserved.