35.4.6 提交过滤器

参数—commit-filter用于设置提交过滤器。提交过滤器所给出的脚本,在版本库重整过程的每次提交时运行,取代要默认执行的git commit-tree命令。不过一般情况下会在脚本中调用git commit-tree命令。传递给脚本的参数格式为"<TREE_ID>[(-p<PARENT_COMMIT_ID>)……]",提交日志以标准输入的方式传递给脚本。脚本的输出是新提交的提交ID。作为扩展,如果脚本输出了多个提交ID,则这些提交ID作为子提交的多个父节点。

使用下面的命令,可以过滤掉空提交(合并提交除外)。


$git filter-branch—commit-filter'

git_commit_non_empty_tree "$@" '


函数git_commit_non_empty_tree是在脚本git-filter-branch中已经定义过的函数。可以打开文件$(git—exec-path)/git-filter-branch查看。


if you run 'git_commit_non_empty_tree "$@" ' in a commit filter,

it will skip commits that leave the tree untouched,commit the other.

git_commit_non_empty_tree()

{

if test$#=3&&test "$1"=$(git rev-parse "$3^{tree}");then

map "$3"

else

git commit-tree "$@"

fi

}


如果只想跳过某个用户(如badboy)的提交,而无论该提交是否为空,可以使用下面的命令:


$git filter-branch—commit-filter'

if["$GIT_AUTHOR_NAME"="badboy"];

then

skip_commit "$@";

else

git commit-tree "$@";

f'HEAD


其中,函数skip_commit也是在脚本git-filter-branch中已经定义好了的。该函数的作用就是处理传递给提交过滤器脚本的参数"<tree_id>-p parent1-p parent2……",形成"parent1 parent2"的输出。参见Git命令脚本$(git—exec-path)/git-filter-branch中相关的函数。


if you run 'skip_commit "$@" ' in a commit filter,it will print the(mapped)

parents,effectively skipping the commit.

skip_commit()

{

shift;

while[-n "$1"];

do

shift;

map "$1";

shift;

done;

}