java - 使用 svnkit 获取文件的先前修改版本

标签 java svn svnkit

我正在使用 SVN 工具包从 SVN 存储库收集一些指标。我被这个简单的问题困扰,无法解决。

我有一个 SVNRepository 对象,它为我提供存储库中所有文件的路径及其不同版本。我想为每个修订设置先前的修订值。 我怎样才能在不检查该文件的情况下做到这一点?或者更确切地说,为了让我的代码获得最佳性能,最好的方法是什么?

在分配先前的修订版本时,还应该考虑复制/粘贴和移动 Activity 。

最佳答案

以下技术对我有用。

//This is where I query SVN server
if (entryPath.getType() != SVNLogEntryPath.TYPE_ADDED) {
    LogEntryPreviousRevFinder handler = new LogEntryPreviousRevFinder(workingFileName.substring(interestingPath.length()), thisRevision);

    //Start checking for previous modified revision for this file in the previous 3 revision.
    //If not found try for previous 5 and then 7. If not found till then, put in a warning and continue.
    //This is necessary because in SVN a branch operation is performed at the folder level and not file 
    //hence we need to go further back and find out the revision than was last modified.
    for (int i = 3; i <= 7; i+=2) {
        repository.log(new String[] {workingFileName}, thisRevision, 0l, true, false, i, handler);
        if (handler.isSuccess()) {
            prevPath = handler.getPreviousPath();
            prevRevision = handler.getPreviousRevision();
            break;
        } else {
            continue;
        }
    }

    if (!handler.isSuccess()) {
        log.warn("Failed to find previous revision for file: " + workingFileName 
                + " Will contine considering this one as added in this revision");
    }
} else {
    //Files with SVNLogEntryPath.TYPE_ADDED are either added in this revision
    //or are copied or renamed from an existing file. If later, we need to identify the Copy from path
    //as that will be the file which will be used for calculating relative metrics and later Flux values
    if (entryPath.getCopyPath() != null && entryPath.getCopyPath().trim().length() > 0) {
        prevPath = entryPath.getCopyPath();
        prevRevision = entryPath.getCopyRevision();
    } else {
        log.debug("File: " + workingFileName + " added in this revision");
    }
}

ISVNLogEntryHandler 实现:

//ISVNLogEntryHandler implementation for identifying previous revision
private class LogEntryPreviousRevFinder implements ISVNLogEntryHandler {
    private String interestingFile;
    private String previousPath;
    private long thisRevision;
    private long previousRevision;
    private boolean isSuccess;

    public LogEntryPreviousRevFinder(String interestingFile, long revision) {
        this.interestingFile = interestingFile;
        this.thisRevision = revision;
        isSuccess = false;
    }

    @Override
    public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
        if (isSuccess)
            return;

        if (thisRevision == logEntry.getRevision())
            return;

        Set changedPathsSet = logEntry.getChangedPaths().keySet();
        for (Iterator changedPaths = changedPathsSet.iterator(); changedPaths.hasNext();) {
            SVNLogEntryPath entryPath = (SVNLogEntryPath) logEntry.getChangedPaths().get(changedPaths.next());
            String workingFileName = entryPath.getPath();

            if (workingFileName.endsWith(interestingFile)) {
                previousRevision = logEntry.getRevision();
                previousPath = workingFileName;
                isSuccess = true;
            }
        }
    }

    public long getPreviousRevision() {
        return previousRevision;
    }
    public String getPreviousPath() {
        return previousPath;
    }
    public boolean isSuccess() {
        return isSuccess;
    }
}

我想知道是否有更好、更有效的方法来做到这一点。

关于java - 使用 svnkit 获取文件的先前修改版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5462384/

相关文章:

java - 将rabbitMQ消息日志分离到不同的日志文件中

java - firebase android开发中通过循环从子节点获取特定数据

java - 打乱数组时出现问题 - 返回 null 而不是随机整数

java - Subversion 中提取接口(interface)重构的最佳体现

java - svnkit:如何以编程方式将 'svn status' 的精确结果作为字符串获取?

java - 调用另一个构造函数时有什么方法可以访问 this.toString() 的值吗?

svn - 将 SVN 仓库作为分支导入到现有的 Git 仓库中

eclipse - 如何在工作空间中同时使用SVN和Eclipse?

java - svnkit:如何从 SVN DB 获取最新的修订号?

java - 最后使用 SVNkit 修改文件的详细信息