获取与git push相关的所有文件

时间:2016-03-24 11:58:04

标签: git githooks

有没有办法获得(可能是服务器端挂钩)所有与推送相关的文件?喜欢:

当用户执行:$ git push origin master

如果我们使用$ git log那么钩子必须有SHA-1才能获得与推送相关的文件?

1 个答案:

答案 0 :(得分:1)

在服务器端,这通常在a pre-receive or post-receive hook中进行(取决于您是否允许推送或只是后处理)。
两个钩子都有一个从stdin推送的引用列表。

例如参见this gist

#!/bin/bash

echo "### Attempting to validate puppet files... ####"

# See https://www.kernel.org/pub/software/scm/git/docs/githooks.html#pre-receive    
oldrev=$1
newrev=$2
refname=$3

while read oldrev newrev refname; do

    # Get the file names, without directory, of the files that have been modified
    # between the new revision and the old revision
    files=`git diff --name-only ${oldrev} ${newrev}`

    # Get a list of all objects in the new revision
    objects=`git ls-tree --full-name -r ${newrev}`

    # Iterate over each of these files
    for file in ${files}; do

        # Search for the file name in the list of all objects
        object=`echo -e "${objects}" | egrep "(\s)${file}\$" | awk '{ print $3 }'`

        # If it's not present, then continue to the the next itteration
        if [ -z ${object} ]; then 
            continue; 
        fi
        ...

在上面的示例中,它不仅获取推送列表(提交后提交),还获取其内容(使用git ls-tree ...|grep file