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

3

5

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.

Cyclostyle answered 8/3, 2010 at 18:4 Comment(0)
S
13

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.

Slavophile answered 21/12, 2010 at 14:10 Comment(0)
C
2

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
Comeon answered 8/3, 2010 at 20:50 Comment(1)
Im currently testing with local filesystem repositories (ie the url is "file:///<somepath>"), and in this case SharpSvn.SvnClient.List gives me a "SharpSvn.SvnRepositoryIOException: Unable to open an ra_local session to URL" when i feed it the url of the repository appended with a bogus relative path. Exactly the same exception is thrown when giving a completely bogus url.Cyclostyle
S
1

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);
}
Sherburn answered 20/1, 2015 at 1:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.