我想提交一个修改过的单个文件。根据 http://wiki.svnkit.com/Committing_To_A_Repository 我使用以下代码:
public static SVNCommitInfo modifyFile(ISVNEditor editor, String dirPath, String filePath, InputStream is, long size) throws SVNException {
try {
SVNDeltaGenerator deltaGenerator = new SVNDeltaGenerator();
editor.openRoot(-1);
editor.openDir(dirPath, -1);
editor.openFile(filePath, -1);
editor.applyTextDelta(filePath, null);
String chksm = deltaGenerator.sendDelta(filePath, is, editor, true);
editor.textDeltaEnd(filePath);
editor.closeFile(filePath, chksm);
/*
* Closes the directory.
*/
editor.closeDir();
/*
* Closes the root directory.
*/
editor.closeDir();
return editor.closeEdit();
} catch (SVNException e) {
if (editor != null) {
try {
editor.abortEdit();
} catch (Exception ex) {
}
}
throw e;
}
}
但不幸的是,尽管提交是由拥有外观的用户完成的,但我还是得到了一个例外:
org.tmatesoft.svn.core.SVNException: svn: E175002: PUT of '/spielwiese/!svn/wrk/e9019037-4201-0010-b534-277444c0b279/postcommittesten.txt': 423 Locked (http://localhost:8081)
svn: E175002: PUT request failed on '/spielwiese/!svn/wrk/e9019037-4201-0010-b534-277444c0b279/postcommittesten.txt'
at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.error(SVNErrorManager.java:106)
at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.error(SVNErrorManager.java:90)
at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(HTTPConnection.java:739)
at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(HTTPConnection.java:369)
at org.tmatesoft.svn.core.internal.io.dav.DAVConnection.performHttpRequest(DAVConnection.java:728)
at org.tmatesoft.svn.core.internal.io.dav.DAVConnection.doPutDiff(DAVConnection.java:514)
at org.tmatesoft.svn.core.internal.io.dav.DAVCommitEditor.closeFile(DAVCommitEditor.java:335)
我做错了什么?正确的方法是什么?
我尝试使用SVNCommitClient。但是SVNCommitClient需要一个lokal工作副本来提交单个文件,我不想创建一个lokal工作副本。所以我想直接将文件提交到给定位置的存储库中。
如何提交被当前用户锁定的文件?
Subversion 和 DAV 需要的不仅仅是拥有锁的用户,以便对锁定的文件进行更改。 您还必须拥有该文件的锁定令牌。 这样做的原因是防止当一个程序持有锁而另一个程序修改由同一用户运行的文件时出现问题。 通常,锁定令牌存储在创建锁定的工作副本上,代码将在那里检索它。 但是,您似乎正在尝试在没有工作副本的情况下提交对文件的更改。
如果要提交锁定的文件,则需要在获取未包含在您提供的代码中的ISVNEditor
时提供锁定令牌。 在你提供的链接上的示例代码中,它是通过在 SVNRepository
类上调用 getCommitEditor
方法来完成的。 getCommitEditor
有几个签名可以Map
您可以使用的锁定令牌。
为了获得锁令牌,您必须从创建锁时存储它们。 如果您没有锁令牌,您也许可以简单地窃取锁。 为此,可以在SVNRepository
类上调用 lock
方法,并将 force
参数设置为 True(假设您有权窃取服务器上的锁)。 SVNRepository
上有一个getLock
getLocks
的方法,但我不记得您是否可以以这种方式检索锁定令牌,可能值得一试。
感谢@Ben Reser。你击中了靶心。我使用了SVNRepository#getCommitEditor(String, ISVNWorkspaceMediator)。但确实是签名错误的方法。
现在我的代码读取并且一切正常:
// Discover lock on the file
SVNLock lockedItem = this.repository.getLock(fileUrl);
Map<String, String> locks = new HashMap<String, String>();
if (lockedItem != null) {
locks.put(lockedItem.getPath(), lockedItem.getID());
}
// Retrieve a commiteditor and provide the lock tokens
ISVNEditor editor = this.repository.getCommitEditor(
comment, locks, true, new WorkspaceMediator());
SVNCommitInfo commitInfo = SVNUtils.modifyFile(editor,
dirUrl, fileUrl, fileReader, size);