You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SVN服务器上部署C#编写的pre-commit钩子?

SVN服务器端Pre-Commit钩子集成keys.txt空文件校验

步骤1:适配C#程序到服务器端逻辑

原客户端钩子依赖本地文件路径列表,服务器端需通过SVN事务直接校验文件内容。修改C#程序如下,使其接收仓库路径和事务ID作为参数:

using System;
using System.Diagnostics;
using System.IO;

class Program
{
    static int Main(string[] args)
    {
        if (args.Length != 2)
        {
            Console.Error.WriteLine("Usage: KeysFileCheck.exe <repo-path> <txn-id>");
            return 1;
        }

        string repoPath = args[0];
        string txnId = args[1];

        // 获取事务中变更的所有文件
        var changeProcess = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "svnlook",
                Arguments = $"changed -t {txnId} {repoPath}",
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = true
            }
        };
        changeProcess.Start();
        string changedFiles = changeProcess.StandardOutput.ReadToEnd();
        changeProcess.WaitForExit();

        int result = 0;
        foreach (var line in changedFiles.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
        {
            string filePath = line.Substring(4).Trim();
            if (Path.GetFileName(filePath).Equals("keys.txt", StringComparison.OrdinalIgnoreCase))
            {
                // 导出文件内容
                var catProcess = new Process
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = "svnlook",
                        Arguments = $"cat -t {txnId} {repoPath} {filePath}",
                        RedirectStandardOutput = true,
                        UseShellExecute = false,
                        CreateNoWindow = true
                    }
                };
                catProcess.Start();
                string fileContent = catProcess.StandardOutput.ReadToEnd();
                catProcess.WaitForExit();

                if (!string.IsNullOrEmpty(fileContent.Trim()))
                {
                    Console.Error.WriteLine($"The keys file {filePath} cannot be saved with data in it as the build server will fail to build the solution");
                    result = 1;
                }
            }
        }

        return result;
    }
}

编译程序:

  • Windows服务器:用Visual Studio编译为Release版exe,或执行命令:dotnet publish -c Release -r win-x64 --self-contained true
  • Linux服务器:执行命令编译为自包含可执行文件:dotnet publish -c Release -r linux-x64 --self-contained true

步骤2:修改服务器端pre-commit脚本

根据服务器操作系统选择对应脚本修改:

Windows环境(pre-commit为bat脚本)

找到仓库下的hooks/pre-commit.bat,添加以下内容:

@echo off

:: 原有提交消息校验逻辑
findstr /r "[a-zA-Z0-9]" %1\hooks\commit-msg.tmpl > nul || (
    echo "A commit message is required" 1>&2
    exit /b 1
)

:: 调用C#校验程序
"C:\full\path\to\KeysFileCheck.exe" %1 %2

:: 校验失败则阻止提交
if %errorlevel% neq 0 exit /b %errorlevel%

exit /b 0

Linux环境(pre-commit为shell脚本)

找到仓库下的hooks/pre-commit,添加以下内容:

#!/bin/sh

# 原有提交消息校验逻辑
grep "[a-zA-Z0-9]" "$1/hooks/commit-msg.tmpl" > /dev/null || {
    echo "A commit message is required" >&2
    exit 1
}

# 调用C#校验程序
/full/path/to/KeysFileCheck "$1" "$2"

# 校验失败则阻止提交
if [ $? -ne 0 ]; then
    exit $?
fi

exit 0

步骤3:配置权限(仅Linux需要)

确保pre-commit脚本具备可执行权限:

chmod +x /path/to/your/repo/hooks/pre-commit

验证测试

提交一个包含非空keys.txt的变更,确认钩子会阻止提交并输出指定错误信息;同时测试空keys.txt和正常提交消息的情况,确保不影响合法提交。

内容的提问来源于stack exchange,提问作者Veronica

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 17:01:09