Checking Out Directory / File with SVNKit
Asked Answered
C

3

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?

Cabinetwork answered 3/8, 2010 at 9:46 Comment(0)
P
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);
Peacoat answered 3/8, 2010 at 17:47 Comment(3)
+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)Splenic
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);Audiphone
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??Detrusion
H
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();
}
Heavyhanded answered 17/1, 2013 at 19:45 Comment(11)
so we have to declare workingCopyDirectory as "File workingCopyDirectory=new File("/home/lucy");" ???Detrusion
ok..I have another doubt..Will SvnOperationFactory copy a folder also along with files stored at a particukar SVN path or just the files??Detrusion
It will behave in the same manner as svn co <url> <workingCopyDirectory> does: workingCopyDirectory will contain the same files and directories as url does.Heavyhanded
I have used the above code twice to checkout 2 directories from svn. Is there a better way of doing it??Detrusion
No, there's no way to checkout several working copies in one operation.Heavyhanded
So what i have done here #27099930 is correct??Detrusion
Yes, this is the best way to do that.Heavyhanded
@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?Faustena
'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.Heavyhanded
@DmitryPavlenko Anything over 1.8.7 is crashing JRE even in debugger.Faustena
That is probably related to Gnome Keyring: #39343316 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.Heavyhanded
P
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);
Peacoat answered 3/8, 2010 at 17:47 Comment(3)
+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)Splenic
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);Audiphone
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??Detrusion
D
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();
Day answered 15/7, 2017 at 11:50 Comment(1)
I have the same problem, it take forever to finish... There must a parameter missing. Did you ever get it solved?Endoergic

© 2022 - 2024 — McMap. All rights reserved.