One API to handle adding and updating files

233 views Asked by At

I'm using SVNKIT 1.8 with SVN 1.8.5 and the SVN protocol to attempt to add files in bulk to my SVN repository. I would like to have one method for adding and updating files and the below code successfully handles both when using the FILE protocol since the editor.addFile(file, null, -1) throws an SVNException. When I switch to the SVN protocol (desired protocol), the editor.addFile(file, null, -1); doesn't throw an exception. Instead the editor.closeEdit(); throws an exception which is not desired. Any ideas on how to use one API for both adding and updating files?

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);
    }
  }
1

There are 1 answers

0
Ben Reser On BEST ANSWER

This is a side effect of an optimization in the svn:// protocol. During an editor drive the server does not send any response unless there is an error and as such the client can't tell that a specific action succeeded. I haven't looked at SVNKit's code but I'd bet that you could potentially get the exception from any of the editor methods since the error will be detected in the next editor drive call after the server responds. In this case your changes are so small that the editor drive sending happens before the response from the server can be detected and so you end up seeing the error when you do closeEdit().

The svnmucc command in Subversion has a similar problem as what you're trying to solve. It has a put operation that adds or updates a file. It uses the same technique that Dmitry advised you to use on the svnkit-users mailing list (link1, link2). Specifically running a check_path before determining to add or create the file.

You're not going to be able to do anything better than this because of the way the protocol works.