简体   繁体   English

是否可以在过去的提交中运行git commit-msg钩子?

[英]Is it possible to run a git commit-msg hook over past commits?

I have a commit-msg hook that I run for all of my local commits and, for a certain project, I require to have been run. 我有一个commit-msg钩子,我为所有本地提交运行,对于某个项目,我需要运行。 Problem is, when I pull in from other forks some of my compatriots have not run this commit-msg hook. 问题是,当我从其他叉子拉进来时,我的一些同胞没有运行这个commit-msg钩子。 To date I've been doing 到目前为止,我一直在做

$ git rebase --interactive $commit_parent

as outlined very similarly here . 作为概括非常相似这里 Pick the commits that haven't been done properly, re-edit and so on. 选择未正确完成的提交,重新编辑等。 All very workable, but also tedious as I'm doing it by hand. 一切都非常可行,但我手工做的也很乏味。

How can I automate this? 我该如何自动化? The hook requires no oversight. 钩子不需要疏忽。

This can be achieved in automatic fashion using custom editor: 这可以使用自定义编辑器以自动方式实现:

#!/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

and then running: 然后运行:

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

On the first invocation, our "editor" will get a list of commits from rebase -i , where it will change every pick to r (which is reword ). 在第一次调用时,我们的“编辑器”将获得一个来自rebase -i的提交列表,它将把每个pick改为r (这是reword )。 In this mode git will start immediately calling our "editor" with a message of every rebased commit (and without any pauses that it usually makes in pick mode where you can change your commit itself). 在这种模式下, git将立即开始调用我们的“编辑器”,其中包含每个重新提交的提交消息(并且没有任何暂停,它通常在pick模式下进行,您可以在其中更改提交本身)。

I suppose it is very easy, given the description of the hook in "hooks" manpage: 我想这很简单,给出了“hooks”手册页中钩子的描述:

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

Just run the hooks script for every commit: 只需为每次提交运行hooks脚本:

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

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

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