3

我在 GIT 中遇到了这种情况:

我想在推送中更改特定文件时对它“做某事”。例如,如果 .sql 文件发生更改,则必须将其转储到数据库中。

我在 GIT 中使用 'post-receive' 钩子,声明如下:

DUMP=$(git diff-tree --name-only -r -z master dump.sql);

if [ -n "$DUMP" ]; then
  // using the new dump.sql
fi

如何访问刚刚从挂钩中推送的新 dump.sql?

4

1 回答 1

7

您可以使用以下方法从修订版 $rev 中检索文件 dump.sql:

git cat-file blob $rev:dump.sql

除了推送 master 之外,还调用 post-receive 钩子......希望您在某处检查您正在处理更新的 master ref。作为风格问题,我会使用传递给钩子的新修订值,而不是直接从钩子中引用 master 。

通常我会写一个这样的 post-receive 钩子:

while read oldrev newrev refname; do
    if [ "$refname" = "refs/heads/master" ]; then
        # definitely updating master; $oldrev and $newrev encompass the changes
        if git diff-tree --name-only -r -z $oldrev $newrev dump.sql; then
            # dump.sql changed...
        fi
    fi
done

Importantly, this also copes with a single push sending over several commits to master in one go--- the command you showed in the question only looked at the last commit on master.

于 2011-11-20T13:01:27.540 回答