我目前正在尝试获取所有 SVN 修订版,然后逐一检查它们并对其进行一些处理。
与
svn checkout http://hamcrest.googlecode.com/svn/trunk/ hamcrest-read-only
(使用 SVN 1.8.8)我得到了代码(我只是使用 hamcrest 作为示例)。
得到它后,我正在尝试执行以下操作:
FSRepositoryFactory.setup();
SVNURL localRepo = FSRepositoryFactory.createLocalRepository(f, true,
true);
System.out.println("Repo: " + localRepo);
SVNRepository repo = FSRepositoryFactory.create(localRepo);
log.info("Revisions: " + repo.getLatestRevision() + " Path: "
+ f.getAbsolutePath());
List<SVNLogEntry> col = (List<SVNLogEntry>) repo.log(
new String[] { "" }, null, 0, repo.getLatestRevision(), true,
true);
for (Object o : col) {
System.out.println("O: " + o + " " + o.getClass());
}
System.out.println("Col: " + col.getClass());
不幸的是,这会创建一个空存储库,并删除其中的所有内容。这对应于 FSRepositoryFactory.create 的文档。
因此,我搜索了一种以另一种方式打开现有存储库的方法,并找到了SVNURL localRepo = SVNURL.fromFile(f);
。不幸的是,这会产生错误:
Exception in thread "main" org.tmatesoft.svn.core.SVNException: svn: E180001: Unable to open an ra_local session to URL
svn: E180001: Unable to open repository 'file:///home/reichelt/workspace/hamcrest/hamcrest-read-only'
svn: E180001: Unable to open repository 'file:///home/reichelt/workspace/hamcrest/hamcrest-read-only'
at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.error(SVNErrorManager.java:64)
at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.error(SVNErrorManager.java:51)
at org.tmatesoft.svn.core.internal.io.fs.FSRepository.openRepository(FSRepository.java:796)
at org.tmatesoft.svn.core.internal.io.fs.FSRepository.getLatestRevision(FSRepository.java:121)
at de.performanzanalysator.vcs.SVNUtils.getVersions(SVNUtils.java:70)
at de.performanzanalysator.vcs.SVNUtils.main(SVNUtils.java:90)
有谁知道是否有办法使用 SVNKit 打开现有的、已下载的存储库? SVNWCClient
似乎是正确的,但我找不到任何使用 SVNWCClient
打开存储库的方法。
我使用的是 SVNKit 1.8.5。
最佳答案
问题中提到的异常发生是因为 SVNRepository
类预期用于存储库,但它用于工作副本。
SVN的服务器端部分称为存储库,客户端部分称为工作副本。 svn checkout 命令用于从(通常是远程)存储库创建(通常是本地)工作副本。工作副本仅包含单个修订版,因此某些操作(例如日志)将需要访问远程存储库。
SvnOperationFactory
类提供了一个 API,用于针对工作副本和存储库执行所有“通用”SVN 任务。它可用于获取完整的更改日志,但请记住,即使针对本地工作副本启动日志操作,它也会联系存储库。
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 );
您可以使用相同的代码直接从存储库获取更改日志,而无需通过将 setSingleTarget
初始化更改为指向远程存储库来 check out 工作副本:
String repoLocation = "http://hamcrest.googlecode.com/svn/trunk";
logOperation.setSingleTarget(
SvnTarget.fromURL(
SVNURL.parseURIEncoded( repoLocation )
)
);
自 SVNKit 1.7 起,SvnOperationFactory
是使用 SVNKit 的首选方式。 SVNKit 仍然包含 SVNClientManager,它提供类似的功能,但它被认为已过时。
SVNRepository
类及其子类旨在与存储库交互,如果您正在编写创建、配置或服务 SVN 存储库的软件,这将非常有用。例如,您可以使用该 API 直接从存储库获取更改日志,但除非您正在编写一些执行其他存储库相关任务的服务器端软件,否则您应该使用 SvnOperationFactory
方法。
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 );
}
}
);
关于java - 使用 SVNKit 获取所有修订,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23611255/