[[Managing repository With SVNKit|List]] | [[Printing Out File Contents|>>]]
== 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:
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
...
public class DisplayRepositoryTree {
public static void main(String[] args) {
DAVRepositoryFactory.setup();
...
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 [http://svnkit.com/kb/javadoc/org/tmatesoft/svn/core/io/SVNRepository.html SVNRepository] driver for the selected location:
...
String url = "http://svn.svnkit.com/repos/svnkit/trunk/doc";
String name = "anonymous";
String password = "anonymous";
SVNRepository repository = null;
try {
repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url));
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
repository.setAuthenticationManager(authManager);
...
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:
...
System.out.println("Repository Root: " + repository.getRepositoryRoot(true));
System.out.println("Repository UUID: " + repository.getRepositoryUUID(true));
...
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.
...
SVNNodeKind nodeKind = repository.checkPath("", -1);
if (nodeKind == SVNNodeKind.NONE) {
System.err.println("There is no entry at '" + url + "'.");
System.exit(1);
} else if (nodeKind == SVNNodeKind.FILE) {
System.err.println("The entry at '" + url + "' is a file while a directory was expected.");
System.exit(1);
}
...
Repository nodes are represented by [http://svnkit.com/kb/javadoc/org/tmatesoft/svn/core/SVNNodeKind.html 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:
public static void listEntries(SVNRepository repository, String path) throws SVNException {
Collection entries = repository.getDir(path, -1, null, (Collection) null);
Iterator iterator = entries.iterator();
while (iterator.hasNext()) {
SVNDirEntry entry = (SVNDirEntry) iterator.next();
System.out.println("/" + (path.equals("") ? "" : path + "/") + entry.getName() +
" (author: '" + entry.getAuthor() + "'; revision: " + entry.getRevision() +
"; date: " + entry.getDate() + ")");
if (entry.getKind() == SVNNodeKind.DIR) {
listEntries(repository, (path.equals("")) ? entry.getName() : path + "/" + entry.getName());
}
}
}
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
[http://svnkit.com/kb/javadoc/org/tmatesoft/svn/core/SVNDirEntry.html 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:
...
listEntries(repository, "");
...
At the end of the program we can finish with printing the latest revision of our repository:
...
latestRevision = repository.getLatestRevision();
System.out.println("Repository latest revision: " + latestRevision);
...
} catch (SVNException svne) {
//handle exception
}
}
}
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 [http://svn.svnkit.com/repos/svnkit/trunk/doc/examples/src/org/tmatesoft/svn/examples/repository/DisplayRepositoryTree.java example program source code].
[[Managing repository With SVNKit|List]] | [[Printing Out File Contents|>>]]