How to use SharpSVN to (quickly) check if a remote folder/file exists on the server

Ziphnor picture Ziphnor · Mar 8, 2010 · Viewed 8.5k times · Source

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 url. 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.

Answer

Stefan picture Stefan · Dec 21, 2010

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.