Say i have a svn repository at https://www.mysvn.com/svn/. How can i use SharpSVN to figure out whether the remote folder https://www.mysvn.com/svn/a/b/c exists on the server?
I need to do it an a way that allows me to tell the difference between a failed connection (ie server down) and the folder simply not having been created yet.
Calling info on the full https://www.mysvn.com/svn/a/b/c path does not seem to give an exception that enables me to tell the difference between no repository at all and just a missing folder.
I could list all files for https://www.mysvn.com/svn/ but the repository can easily be so big that this can take too long.
Right now im doing an info on first the root url and then on the full u开发者_开发技巧rl. If the root url fails i treat it as a server problem, but if it succeeds and the full url fails i assume its because part of the path hasnt been created on the server. This could however give the wrong answer if the internet connection was lost between the two checks or similar.
Is is possible to use GetInfo and set the SvnInfoArgs.ThrowOnError to false.
Using SharpSVN syntax:
using (SvnClient sc = new SvnClient())
{
Uri targetUri = new Uri(RemoteUriTrunk, relPath);
var target = SvnTarget.FromUri(targetUri);
Collection<SvnInfoEventArgs> info;
bool result = sc.GetInfo(target, new SvnInfoArgs {ThrowOnError = false}, out info);
Assert.That(result, Is.False);
Assert.That(info, Is.Empty);
}
This assumes though that all exceptions that are thrown means that the remote file does not exist.
Maybe SvnRemoteSession.GetStat is a better way to solve this. I is a quite new feature, as it seems. I do not have it in my version of the library. http://sharpsvn.open.collab.net/source/browse/sharpsvn/trunk/src/SharpSvn/SvnRemoteSession.h?view=log
It is very irritating that the library throws different exceptions depending on what sort of repository it is.
Wouldn't svn list https://www.mysvn.com/svn/a/b/c
(or rather, its equivalent binding) do it? It should return something different if the path isn't found. For example, the command line client returns
svn: URL 'https://www.mysvn.com/svn/a/b/c' non-existent in that revision
Following up with Stefan's answer for new readers, SvnRemoteSession.GetStat seems to do the trick:
using (var session = new SharpSvn.Remote.SvnRemoteSession())
{
SvnUI.Bind(session, new SvnUIBindArgs());
// Throws 'SharpSvn.SvnRepositoryIOForbiddenException' if the server
// exists but the repo does not, or the user does not have read access.
// (SVN_ERR_RA_DAV_FORBIDDEN)
//
// Throws 'SharpSvn.SvnSystemException' if the server does not exist
// or could not be reached. (WSAHOST_NOT_FOUND)
session.Open(remoteUriTrunk);
SharpSvn.Remote.SvnRemoteStatEventArgs result;
// True if the file or folder exists, false if it does not exist.
bool exists = session.GetStat(relPath, out result);
}
精彩评论