首页 > 解决方案 > 访问 git pre-receive 钩子中的更改文件并搜索字符串模式

问题描述

我有一个预提交脚本/钩子可以很好地搜索文件中的特定字符串模式并拒绝提交。我不确定如何读取预接收脚本中的传入文件以搜索字符串模式。

我的预提交脚本如下所示:

#!/usr/bin/env bash

if git rev-parse --verify HEAD >/dev/null 2>&1
then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    EMPTY_TREE=$(git hash-object -t tree /dev/null)
    against=$EMPTY_TREE
fi

FILES=$(git diff --cached --name-only $against)

if [ -n "$FILES" ]; then

string1 = $(grep -rE --line-number 'access_key' $FILES)

   if [ -n "$string1" ] then
            echo "string1 there so reject it"
        while true; do
                exit 1;

         done
  fi
fi

我不确定如何将其转换为 git 服务器端的预接收挂钩脚本。我已经尝试了几个小时没有运气。有人可以帮我吗?

标签: bashgitunixgithub

解决方案


推荐阅读