简体   繁体   English

git:如何在预接收挂钩中 grep 接收到的文件

[英]git: how to grep received files in pre-receive hook

I created the following pre-push script in my local repository .git/hooks/ to disallow pushing files with unresolved git conflicts to server:我在本地存储库.git/hooks/创建了以下预推送脚本,以禁止将具有未解决的 git 冲突的文件推送到服务器:

#!/bin/bash
top_dir=$(git rev-parse --show-toplevel)
conflicts=$(grep -r -n -s --include=\*.{pm,pl,mc,mi,js,css} "<<<<<<< HEAD" "$top_dir")

if [ "$conflicts" = "" ]; then
    exit 0
else
    printf "Unresolved git conflict found, commit rejected.\n\n"
    echo "$conflicts"
    exit 1
fi

It works.有用。 But how can we reject all the commits with unresolved conflicts on server side pre-receive ?但是我们如何才能拒绝所有在服务器端pre-receive上存在未解决冲突的提交呢? Here I know top_dir - the dir, where to grep.在这里,我知道top_dir - 目录,grep 的位置。 But on server there are no saved files saved.但是在服务器上没有保存已保存的文件。 I need to grep among the files that the client sends me.我需要在客户端发送给我的文件中进行 grep。

How to do it?怎么做?

As torek documents in " pre-receive hook unable to read the committed file to push into remote master ":正如“ pre-receive钩子无法读取提交的文件以推送到远程master ”中的torek文档:

Pre-receive hooks are generally more difficult to write as you must handle many cases:预接收钩子通常更难编写,因为您必须处理许多情况:

  • multiple commits多次提交
  • references that are not branches (tags)不是分支的引用(标签)
  • objects that are not commits (annotated tags)未提交的对象(带注释的标签)
  • branch creations and deletions as well as updates分支的创建和删除以及更新

That kind of hook works well for commit messages .这种钩子适用于提交消息
That being said, you can see an example here , in the nominal case:话虽如此,你可以在这里看到一个例子,在名义情况下:

# <oldrev> <newrev> <refname>
while  read oldrev newrev ref ;
do
    list = $ ( git show --pretty = " format: " --name-only $ {newrev}  | grep -e ' .php ' -e ' .phtml ' )
    for  file  in  $ {list} ;  do
        git show $ {newrev} : $ {file}  >  $ TMP_FILE
        OUTPUT = $ ( $ PHPCS_BIN -s --standard = $ PHPCS_CODING_STANDARD  $ TMP_FILE )
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM