Search code examples
c#svnsharpsvn

How to set author of SVN commit using SharpSVN library in c#


I use SharpSvn library from CollabNET. I would like to set revision author while commiting, but I always end up with a commit with my Windows user name.

This does not work for me:

System.Net.NetworkCredential oCred = new
    System.Net.NetworkCredential("user"​, "pass");
client.Authentication.DefaultCredentials = oCred;

I've tried also:

client.SetProperty("", "svn:author", "user");

But I get an error that target (first argument) is bad.

So could you please tell me how to set user (author) of commit to subversion repository in c#?


Solution

  • This all depends on how you connect to your repository, as the repository is responsible for adding a username to the revision. (It usually copies the credentials for the connections but it doesn't have to do that).

    When you use a file:/// repository (which is usually not recommended - See The Subversion Book) you can work around this directly on the commit.

    using (SvnClient client = new SvnClient())
    {
        client.Authentication.Clear(); // Clear predefined handlers
    
        // Install a custom username handler
        client.Authentication.UserNameHandlers +=
            delegate(object sender, SvnUserNameEventArgs e)
            {
                e.UserName = "MyName";
            };
    
        SvnCommitArgs ca = new SvnCommitArgs { LogMessage = "Hello" }
        client.Commit(dir, ca);
    }
    

    If you connect to a remote repository you can change the author of a revision when a pre-revprop-change hook is installed in the repository (See The Subversion Book)

    using (SvnClient client = new SvnClient())
    {
        client.SetRevisionProperty(new Uri("http://my/repository"), 12345,
                                  SvnPropertyNames.SvnAuthor,
                                  "MyName");
    
        // Older SharpSvn releases allowed only the now obsolete syntax
        client.SetRevisionProperty(
            new SvnUriTarget(new Uri("http://my/repository"), 12345),
            SvnPropertyNames.SvnAuthor,
            "MyName");
    
    }
    

    [2009-08-14] More recent SharpSvn releases also allow this:

    using (SvnRepositoryClient rc = new SvnRepositoryClient())
    {
       SvnSetRevisionPropertyRepositoryArgs ra;
       ra.CallPreRevPropChangeHook = false;
       ra.CallPostRevPropChangeHook = false;
       rc.SetRevisionProperty(@"C:\Path\To\Repository", 12345,
                             SvnPropertyNames.SvnAuthor, "MyName", ra);
    }
    

    This last example assumes direct file access to the repository, but it bypasses repository hooks for optimal performance.