+ Managing Repository With SVNKit
- - Printing Out A Subversion Repository Tree
Printing out a Subversion repository tree
The first thing we will try - observing a Subversion repository. First we need to set up the library to use the protocol which we would like to access a repository through. In this example we will use the http:// protocol to observe a world-readable repository:
We need a URL of the repository and credentials (here we use some dummy ones since we think an Apache will let us read repository contents, but you may change them to some valid credentials to observe repositories closed to unregistered persons). Having these parameters we create an SVNRepository driver for the selected location:
1 ...
2 String url = "http://svn.svnkit.com/repos/svnkit/trunk/doc";
3 String name = "anonymous";
4 String password = "anonymous";
5
6 SVNRepository repository = null;
7 try {
8 repository = SVNRepositoryFactory.create( SVNURL.parseURIDecoded( url ) );
9 ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( name , password );
10 repository.setAuthenticationManager( authManager );
11 ...
We have got a repository access driver with a default authentication manager. Let's print out some information identifying the repository itself - its root location path and UUID:
If everything is Ok (we connected to our repository successfully and cached its UUID and root location), we are moving further. Now we will check whether the url we are going to use corresponds to a directory. If it does not, we are finished.
1 ...
2 SVNNodeKind nodeKind = repository.checkPath( "" , -1 );
3 if ( nodeKind == SVNNodeKind.NONE ) {
4 System.err.println( "There is no entry at '" + url + "'." );
5 System.exit( 1 );
6 } else if ( nodeKind == SVNNodeKind.FILE ) {
7 System.err.println( "The entry at '" + url + "' is a file while a directory was expected." );
8 System.exit( 1 );
9 }
10 ...
Repository nodes are represented by SVNNodeKind enum objects. Here and later we use an invalid revision number -1 to mean that the latest revision must be used. The next step - writing a function which will recursively call itself to traverse the repository tree starting at a particular path (parameter) and print out all contents:
1 public static void listEntries( SVNRepository repository, String path ) throws SVNException {
2 Collection entries = repository.getDir( path, -1 , null , (Collection) null );
3 Iterator iterator = entries.iterator( );
4 while ( iterator.hasNext( ) ) {
5 SVNDirEntry entry = ( SVNDirEntry ) iterator.next( );
6 System.out.println( "/" + (path.equals( "" ) ? "" : path + "/" ) + entry.getName( ) +
7 " ( author: '" + entry.getAuthor( ) + "'; revision: " + entry.getRevision( ) +
8 "; date: " + entry.getDate( ) + ")" );
9 if ( entry.getKind() == SVNNodeKind.DIR ) {
10 listEntries( repository, ( path.equals( "" ) ) ? entry.getName( ) : path + "/" + entry.getName( ) );
11 }
12 }
13 }
This function receives a path of a repository subtree which is to be printed out. Using a getDir(...) method of the repository access driver passed it reads the contents of the current path which are represented by SVNDirEntry objects. If an entry is a directory the function recursively calls itself with the path of that directory. And so on. Collected SVNDirEntry objects contain such information as the last change revision, author, and date-stamp.
From our main() program we call listEntries() passing our driver object and the current path that the driver is bound to:
At the end of the program we can finish with printing the latest revision of our repository:
And finally we run the program and have the following output in our console:
Repository Root: http://svn.svnkit.com/repos/svnkit Repository UUID: 0a862816-5deb-0310-9199-c792c6ae6c6e /examples (author: 'sa'; revision: 2794; date: Tue Nov 14 03:21:11 NOVT 2006) /examples/svnkit-examples.iml (author: 'alex'; revision: 2775; date: Fri Nov 10 02:08:45 NOVT 2006) /examples/src (author: 'sa'; revision: 2794; date: Tue Nov 14 03:21:11 NOVT 2006) /examples/src/org (author: 'sa'; revision: 2794; date: Tue Nov 14 03:21:11 NOVT 2006) /examples/src/org/tmatesoft (author: 'sa'; revision: 2794; date: Tue Nov 14 03:21:11 NOVT 2006) /examples/src/org/tmatesoft/svn (author: 'sa'; revision: 2794; date: Tue Nov 14 03:21:11 NOVT 2006) /examples/src/org/tmatesoft/svn/examples (author: 'sa'; revision: 2794; date: Tue Nov 14 03:21:11 NOVT 2006) /examples/src/org/tmatesoft/svn/examples/wc (author: 'alex'; revision: 2776; date: Fri Nov 10 02:25:08 NOVT 2006) ...................................................... --------------------------------------------- Repository latest revision: 2802
Download the example program source code.