3

我使用来自 CollabNET 的 SharpSvn 库。我想在提交时设置修订作者,但我总是以我的 Windows 用户名提交。

这对我不起作用:

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

我也试过:

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

但我得到一个错误,即目标(第一个参数)不好。

那么你能告诉我如何在c#中设置提交到颠覆存储库的用户(作者)吗?

4

1 回答 1

7

这一切都取决于您如何连接到存储库,因为存储库负责将用户名添加到修订版。(它通常会复制连接的凭据,但不必这样做)。

当您使用 file:/// 存储库(通常不推荐使用 - 请参阅The Subversion Book)时,您可以直接在提交时解决此问题。

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);
}

如果您连接到远程存储库,则可以在存储库中安装 pre-revprop-change 挂钩时更改修订的作者(请参阅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] 最近的 SharpSvn 版本也允许这样做:

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

最后一个示例假设直接文件访问存储库,但它绕过存储库挂钩以获得最佳性能。

于 2009-05-05T13:32:48.097 回答