Git钩子脚本介绍
Git Hooks
钩子(hooks)是一些在"$GIT-DIR/hooks"目录的脚本, 在被特定的事件(certain points)触发后被调用。当"git init"命令被调用后, 一些非常有用的示例钩子文件(hooks)被拷到新仓库的hooks目录中; 但是在默认情况下这些钩子(hooks)是不生效的。 把这些钩子文件(hooks)的".sample"文件名后缀去掉就可以使它们生效了。
applypatch-msg
GIT_DIR/hooks/applypatch-msg
当'git-am'命令执行时,这个钩子就被调用。它只有一个参数:就是存有提交消息(commit log message)的文件的名字。如果钩子的执行结果是非零,那么补丁(patch)就不会被应用(apply)。
The hook is allowed to edit the message file in place, and can be ud to normalize the me
ssage into some project standard format (if the project has one). It can also be ud to refu the commit after inspecting the message file. The default applypatch-msg hook, when enabled, runs the commit-msg hook, if the latter is enabled.
这个钩子用于在其它地方编辑提交消息,并且可以把这些消息规范成项目的标准格式(如果项目些类的标准的话)。它也可以在分析(inspect)完消息文件后拒绝此次提交(commit)。在默认情况下,当 applypatch-msg 钩子被启用时。。。。
()
pre-applypatch
GIT_DIR/hooks/pre-applypatch
minos当'git-am'命令执行时,这个钩子就被调用。它没有参数,并且是在一个补丁(patch)被应用后还未提交(commit)前被调用。如果钩子的执行结果是非零,那么刚才应用的补丁(patch)就不会被提交。
It can be ud to inspect the current working tree and refu to make a commit if it does not pass certain test. The default pre-applypatch hook, when enabled, runs the pre-commit hook, if the latter is enabled.
它用于检查当前的工作树,当提交的补丁不能通过特定的测试就拒绝将它提交(commit)进仓库。 ()
post-applypatch
GIT_DIR/hooks/post-applypatch
canton fair
This hook is invoked by 'git-am'. It takes no parameter, and is invoked after the patch is applied and a commit is made.
当'git-am'命令执行时,这个钩子就被调用。它没有参数,并且是在一个补丁(patch)被应用且在完成提交(commit)情况下被调用。
This hook is meant primarily for notification, and cannot affect the outcome of 'git-am'.
这个钩子的主要用途是通知提示(notification),它并不会影响'git-am'的执行和输出。
pre-commit
GIT_DIR/hooks/pre-commit
这个钩子被 'git-commit' 命令调用, 而且可以通过在命令中添加\--no-verify 参数来跳过。这个钩子没有参数,在得到提交消息和开始提交(commit)前被调用。如果钩子执行结果是非零,那么 'git-commit' 命令就会中止执行。
译注:此钩子可以用来在提交前检查代码错误(运行类似lint的程序)。
当默认的'pre-commit'钩子开启时,如果它发现文件尾部有空白行,那么就会中止此次提交。
译注:新版的默认钩子和这里所说有所有不同。
All the 'git-commit' hooks are invoked with the environment variable GIT_EDITOR=: if the command will not bring up an editor to modify the commit message.
高一课本
下面是一个运行 Rspec 测试的 Ruby 脚本,如果没有通过这个测试,那么不允许提交(commit)。
html_path = "spec_results.html"
`spec -f h:#{html_path} -f p spec`
html monochrome= open(html_path).read
examples = html.match(/(\d+) examples/)[0].to_i rescue 0
failures =more than html.match(/(\d+) failures/)[0].to_i rescue 0
pending = html.match(/(\d+) pending/)[0].to_i rescue 0
ifreferences failures.zero?
puts "0 failures! #{examples} run, #{pending} pending"
el
uhmwpe
puts "\aDID NOT COMMIT YOUR FILES!"
puts "View spec results at #{File.reliable是什么意思expand_path(html_path)}"
puts
puts "#{failures} failures! #{examples} run, #{pending} pending"
exit 1
end
prepare-commit-msg
GIT_DIR/hooks/prepare-commit-msg
当'git-commit'命令执行时:在编辑器(editor)启动前,默认提交消息准备好后,这个钩子就被调用。
It takes one to three parameters. The first is the name of the file that the commit log message. The cond is the source of the commit message, and can be: message (if a -m or -F option was given); template (if a -t option was given or the configuration option plate is t); merge (if the commit is a merge or a .git/MERGE_MSG file exists); squash (if a 中文日文翻译.git/SQUASH_MSG file exists); or commit, followed by a commit SHA1 (if a -c, -C or \--amend option was given).
它有三个参数。第一个是提交消息文件的名字。第二个是提交消息的来源,它可以是:().
如果钩子的执行結果是非零的话,那么'git-commit'命令就会被中止执行。
The purpo of the hook is to edit the message file in place, and it is not suppresd by the \--no-verify option. A non-zero exit means a failure of the hook and aborts the commit. It should not be ud as replacement for pre-commit hook.
The sample prepare-commit-msg hook that comes with git comments out the Conflicts: part of a merge's commit message.
commit-msg
GIT_DIR/hooks/commit-msg
当'git-commit'命令执行时,这个钩子被调用;也可以在命令中添加term\--no-verify参数来跳过。这个钩子有一个参数:就是被选定的提交消息文件的名字。如这个钩子的执行結果是非零,那么'git-commit'命令就会中止执行。
The hook is allowed to edit the message file in place, and can be ud to normalize the message into some project standard format (if the project has one). It can also be ud to refu the commit after inspecting the message file.