11

I can't see on the wiki where checking out is documented. Ideally, I would like to check out a file "example/folder/file.xml", if not just the folder... and then when the application closes down or otherwise, be able to commit back in changes to this file. How do I do this?

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Federer
  • 33,677
  • 39
  • 93
  • 121

3 Answers3

21

As SVNKit developer, I would recommend you to prefer new API based on SvnOperationFactory. The old API (based on SVNClientManager) will be operational still but all new SVN features will come only to the new API.

final SvnOperationFactory svnOperationFactory = new SvnOperationFactory();
try {
    final SvnCheckout checkout = svnOperationFactory.createCheckout();
    checkout.setSingleTarget(SvnTarget.fromFile(workingCopyDirectory));
    checkout.setSource(SvnTarget.fromURL(url));
    //... other options
    checkout.run();
} finally {
    svnOperationFactory.dispose();
}
Dmitry Pavlenko
  • 8,530
  • 3
  • 30
  • 38
  • so we have to declare workingCopyDirectory as "File workingCopyDirectory=new File("/home/lucy");" ??? – Lucy Oct 09 '14 at 11:35
  • ok..I have another doubt..Will SvnOperationFactory copy a folder also along with files stored at a particukar SVN path or just the files?? – Lucy Oct 10 '14 at 12:16
  • 1
    It will behave in the same manner as `svn co ` does: `workingCopyDirectory` will contain the same files and directories as `url` does. – Dmitry Pavlenko Oct 10 '14 at 16:11
  • I have used the above code twice to checkout 2 directories from svn. Is there a better way of doing it?? – Lucy Nov 19 '14 at 12:37
  • No, there's no way to checkout several working copies in one operation. – Dmitry Pavlenko Nov 19 '14 at 16:55
  • So what i have done here http://stackoverflow.com/questions/27099430/checkout-multiple-directories-from-svn-using-svn-kit is correct?? – Lucy Nov 24 '14 at 07:00
  • Yes, this is the best way to do that. – Dmitry Pavlenko Nov 24 '14 at 11:40
  • @DmitryPavlenko I'm trying to checkout from a repository (dump | load) in my machine and the URL is :- SVNURL secondUrl = SVNURL.fromFile(new File("/home/vsharma/svn/API_Work/hotcopy/test")); but i'm getting exceptions:- org.tmatesoft.svn.core.SVNException: svn: E180001: Unable to open an ra_local session to URL svn: E180001: Unable to open repository 'file:///home/vsharma/svn/API_Work/hotcopy/test' svn: E125006: '/home/vsharma/svn/API_Work/hotcopy/test/db/format' contains invalid filesystem format option 'addressing logical' Any way to make it work? – Varun Sharma Apr 17 '17 at 12:32
  • 'addressing logical' feature was added in later SVNKit versions (>=1.8.12), so retry with the latest version. Also I would like to note that "checkout" operation has nothing to do with "svnadmin dump/load". For "svnadmin" commands look at SVNAdminClient class. – Dmitry Pavlenko Apr 17 '17 at 13:28
  • @DmitryPavlenko Anything over 1.8.7 is crashing JRE even in debugger. – Varun Sharma Apr 18 '17 at 05:57
  • That is probably related to Gnome Keyring: http://stackoverflow.com/questions/39342816/jre-getting-crashed-when-using-svnkit-inside-a-web-app-on-tomcat-server/43208098#43208098 I've fixed the issue in SVNKit repository but we didn't publish a release yet. For now you can either turn Gnome Keyring off, or wait for the next release (it will be soon), or build SVNKit from sources. – Dmitry Pavlenko Apr 18 '17 at 09:59
9

You cannot check out a file in Subversion. You have to check out a folder.

To check out a folder with one or more files:

SVNClientManager ourClientManager = SVNClientManager.newInstance(null, 
            repository.getAuthenticationManager());
SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
updateClient.setIgnoreExternals(false);
updateClient.doCheckout(url, destPath, revision, revision,
            isRecursive);

To commit a previously checked out folder:

SVNClientManager ourClientManager = SVNClientManager.newInstance(null, 
            repository.getAuthenticationManager());
ourClientManager.getWCClient().doInfo(wcPath, SVNRevision.HEAD);
ourClientManager.getCommitClient().doCommit
        (new File[] { wcPath }, keepLocks, commitMessage, false, true);
Jasper
  • 2,166
  • 4
  • 30
  • 50
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • 1
    +1 very useful answer. In addition, if you want to **export** a file then you can do this using [`updateClient.doExport(url, destPath, revision, revision, eolStyle, force, isRecursive)`](http://svnkit.com/javadoc/org/tmatesoft/svn/core/wc/SVNUpdateClient.html#doExport%28org.tmatesoft.svn.core.SVNURL,%20java.io.File,%20org.tmatesoft.svn.core.wc.SVNRevision,%20org.tmatesoft.svn.core.wc.SVNRevision,%20java.lang.String,%20boolean,%20boolean%29) – My Head Hurts Jun 27 '12 at 16:18
  • The listed overload of doExport is deprecated in SVNKit 1.7.5-v1. Instead I used the following overload: `updateClient.doCheckout(url, destPath, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, true);` – jbandi Sep 20 '12 at 19:37
  • On using the above code I'm getting a warning "The method doCheckout(SVNURL, File, SVNRevision, SVNRevision, boolean) from the type SVNUpdateClient is deprecated"...Any idea which method to use instead of doCheckout?? – Lucy Oct 09 '14 at 11:29
0

I also used the code snippet proposed by Dmitry Pavlenko and I had no problems. But it took nearly 30 minutes to checkout or update a repo struture of 35 MB. It's not useable in my usecase (simply checking out a directory structure as part of the content/documents/media of a web application). Or have I made some errors?

final ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
final SVNURL svnUrl = SVNURL.create(url.getProtocol(), name, url.getHost(), 443, url.getPath(), true);

SVNRepository svnRepo= SVNRepositoryFactory.create(svnUrl);
svnRepo.setAuthenticationManager(authManager);
svnOperationFactory.setAuthenticationManager(authManager);

SVNDirEntry entry = svnRepo.info(".", -1);
long remoteRevision = entry.getRevision();

if (!workingCopyDirectory.exists()) {
    workingCopyDirectory.mkdirs();
}

final SvnCheckout checkout = svnOperationFactory.createCheckout();
checkout.setSource(SvnTarget.fromURL(svnUrl));
checkout.setSingleTarget(SvnTarget.fromFile(workingCopyDirectory));
remoteRevision = checkout.run();