The exception mentioned in the questions happens because the SVNRepository
class expects to be used against a repository but it is used against a working copy.
The server-side part of the SVN is called repository and the client-side part is called working copy. The svn checkout
command is used the create a (usually local) working copy from a (usually remote) repository. The working copy contains a single revision only, and because of that some operations (log, for example) will need access to the remote repository.
The SvnOperationFactory
class provides an API to perform all "generic" SVN tasks both against working copies and repositories. It can be used to get the full change log but remember that it will contact the repository even if the log operation is initiated against the local working copy.
File workingCopyLocation = new File( ... );
SvnOperationFactory operationFactory = new SvnOperationFactory();
SvnLog logOperation = operationFactory.createLog();
logOperation.setSingleTarget(
SvnTarget.fromFile( workingCopyLocation )
);
logOperation.setRevisionRanges( Collections.singleton(
SvnRevisionRange.create(
SVNRevision.create( 1 ),
SVNRevision.HEAD
)
) );
Collection<SVNLogEntry> logEntries = logOperation.run( null );
System.out.println( "logEntries = " + logEntries );
You can use the same code to obtain the change log directly from the repository without checking out the working copy by changing the setSingleTarget
initialization to point to the remote repository:
String repoLocation = "http://hamcrest.googlecode.com/svn/trunk";
logOperation.setSingleTarget(
SvnTarget.fromURL(
SVNURL.parseURIEncoded( repoLocation )
)
);
Since SVNKit 1.7 the SvnOperationFactory
is the preferred way of using SVNKit. The SVNKit still contains SVNClientManager
which provides similar capabilities but it is considered obsolete.
The SVNRepository
class and its subclasses are intended to interact with repositories, which is useful if you are writing software that creates, configures or serves SVN repositories. For example, you can use that API to get the change log directly from the repository but unless you are writing some server-side software that performs other repository-related tasks you should use SvnOperationFactory
approach.
String repoLocation = "http://hamcrest.googlecode.com/svn/trunk";
// DAV allows access via http and https protocols
// use FSRepositoryFactory to access local repositories
// use SVNRepositoryFactory to access remote repositories via svn protocol
DAVRepositoryFactory.setup();
SVNRepository repository = DAVRepositoryFactory.create(
SVNURL.parseURIEncoded( repoLocation )
);
repository.log(
new String[]{ },
0, -1,
false, false,
new ISVNLogEntryHandler()
{
@Override
public void handleLogEntry( SVNLogEntry logEntry )
{
System.out.println( "logEntry = " + logEntry );
}
}
);