How to access file information in a pre-commit hook using SharpSVN

我的梦境 提交于 2019-12-19 04:22:07

问题


I am new to SharpSVN and SVN in general. I am trying to implement a pre-commit hook that when the user commits a certain type of XML file; I am going to need to intercept the file and analyze it to ensure they included certain elements before I allow the file to be committed.

Since it seems that SVN submits two arguments; the repository path and the transaction; I will need to use these two items to intercept the file. Does anyone know what I need to use in SharpSVN to get file information based on these two parameters?

Thanks, Flea#


回答1:


You can do this by using the builtin SvnLookClient.

To use this, first of all you need a SvnLookOrigin. SharpSvn contains standard argument parsing that 'knows' what arguments are passed to each type of hook. This gives you access to this SvnLookOrigin:

SvnHookArguments ha; 
if (!SvnHookArguments.ParseHookArguments(args, SvnHookType.PreCommit, false, out ha))
{
    Console.Error.WriteLine("Invalid arguments");
    Environment.Exit(1);  
}

Getting the changed files and contents of those files based on the parsed arguments

using (SvnLookClient cl = new SvnLookClient())
{
    Collection<SvnChangedEventArgs> changedItems;
    cl.GetChanged(ha.LookOrigin, out changedItems);

    foreach(var item in changedItems)
    {
        if(!IsXmlFile(item)) continue;

        using(MemoryStream ms = new MemoryStream())
        {
            cl.Write(ha.LookOrigin, item.Path, stream);

            VerifyXMLStream(stream);
        }
    }
}

Edit: Write to Console.Error and Environment.Exit(1) to report errors (exit non-null).



来源:https://stackoverflow.com/questions/2074891/how-to-access-file-information-in-a-pre-commit-hook-using-sharpsvn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!