gpt4 book ai didi

java - 一个 API 来处理添加和更新文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:48 25 4
gpt4 key购买 nike

我将 SVNKIT 1.8 与 SVN 1.8.5 和 SVN 协议(protocol)一起使用,以尝试将文件批量添加到我的 SVN 存储库中。我想要一种添加和更新文件的方法,并且在使用 FILE 协议(protocol)时下面的代码成功地处理了这两种方法,因为 editor.addFile(file, null, -1) 抛出了 SVNException。当我切换到 SVN 协议(protocol)(所需协议(protocol))时,editor.addFile(file, null, -1);不会抛出异常。取而代之的是 editor.closeEdit();抛出一个不需要的异常。关于如何使用一个 API 来添加和更新文件有什么想法吗?

public void addFiles(Map<String, String> data) throws Exception {
TreeSet<String> filesToCreate = new TreeSet<String>(data.keySet());

SVNRepository repo = null;
ISVNEditor editor = null;
try {
repo = openSession();
editor = repo.getCommitEditor("Adding files.", null);
editor.openRoot(-1);
for (String file : filesToCreate) {
try {
editor.addFile(file, null, -1);
} catch (SVNException e) {
editor.openFile(file, -1);
}
editor.applyTextDelta(file, null);
SVNDeltaGenerator gen = new SVNDeltaGenerator();
String checksum = gen.sendDelta(file, new ByteArrayInputStream(data.get(file).getBytes()), editor, true);
editor.closeFile(file, checksum);
}
editor.closeEdit();
} catch (Exception ex) {
abort(editor);
throw new Exception(ex.toString(), ex);
} finally {
closeSession(repo);
}
}

最佳答案

这是 svn://协议(protocol)优化的副作用。在编辑器驱动期间,服务器不会发送任何响应,除非出现错误,因此客户端无法判断特定操作是否成功。我没有查看 SVNKit 的代码,但我敢打赌,您可能会从任何编辑器方法中获得异常,因为在服务器响应后,将在下一个编辑器驱动器调用中检测到该错误。在这种情况下,您的更改非常小,以至于编辑器驱动器发送发生在可以检测到服务器响应之前,因此您在执行 closeEdit() 时最终会看到错误。

Subversion 中的 svnmucc 命令与您要解决的问题有类似的问题。它有一个添加或更新文件的放置操作。它使用 Dmitry 建议您在 svnkit-users 邮件列表(link1link2)上使用的相同技术。在确定添加或创建文件之前专门运行一个 check_path。

由于协议(protocol)的工作方式,您将无法做比这更好的事情。

关于java - 一个 API 来处理添加和更新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20369860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com