能否对过去的提交运行 git commit-msg 钩子?

5

我有一个commit-msg钩子,用于我所有的本地提交。对于某个项目,我需要运行这个钩子。问题是,当我从其他分支合并时,我的一些同事没有运行这个commit-msg钩子。到目前为止,我一直在做

$ git rebase --interactive $commit_parent

这里描述的那样。选择那些未正确完成的提交,重新编辑等。所有这些都是可行的,但手动进行非常繁琐。

我该如何自动化这个过程?挂钩不需要监督。


你的commit-msg钩子具体是做什么的?它是从提交消息中提取信息,还是以某种方式修改提交消息?根据你想要达到的目标,解决方案会有所不同。 - Anti Veeranna
具体来说,我正在使用的commit-msg钩子是gerrit的ChangeID [hook](http://code.google.com/p/gerrit/source/browse/gerrit-server/src/main/resources/com/google/gerrit/server/tools/root/hooks/commit-msg)。有哪些考虑因素会改变答案的性质? - troutwine
修改已经与其他人共享的提交可能会引起很多问题。仅提取信息是可以的。 - Alex Jasmin
是的,我知道。我不会将修改后的提交推回同一个代码库。 - troutwine
2个回答

3

您可以使用自定义编辑器以自动方式实现这一目标:

#!/bin/bash
# Save to rebase_editor.sh, chmod +x it
file="$1"
if head -n1 "$file" | egrep -q '^pick' "$file" ; then
  perl -pi -e 's/^pick/r/' "$file"
fi

然后运行:

GIT_EDITOR=./rebase_editor.sh git rebase -i <some-rev>

在第一次调用时,我们的“编辑器”将从rebase -i获取提交列表,并将每个pick更改为r(即reword)。在这种模式下,git会立即使用每个重定位提交的消息调用我们的“编辑器”(并且没有任何暂停,通常在pick模式中可以更改提交本身)。

0

根据“hooks”手册中对钩子的描述,我认为这很容易。

It takes a single parameter, the name of the file
that holds the proposed commit log message.

对于每个提交,只需运行钩子脚本:

git rev-list HEAD |while read h; do
    git show --format='%B' $h > tmp && sh .git/hooks/commit-msg.sample tmp
done
rm tmp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接