在 Git 钩子中获取提交消息

Posted

技术标签:

【中文标题】在 Git 钩子中获取提交消息【英文标题】:Get commit message in Git hook 【发布时间】:2011-07-20 13:46:56 【问题描述】:

我想在 Git 提交前检查提交信息。

我使用预提交挂钩来执行此操作,但我找不到在 .git/pre-commit 脚本中获取提交消息的方法。 我怎么能得到它?

【问题讨论】:

【参考方案1】:

pre-commit 挂钩中,提交消息通常尚未创建1。您可能想改用prepare-commit-msgcommit-msg 钩子之一。 nice section in Pro Git 说明了这些钩子的运行顺序,以及您通常可以对它们执行的操作。

1. 例外是提交者可能提供了带有-m 的提交消息,但pre-commit 钩子仍然无法访问该消息,而prepare-commit-msgcommit-msg 可以访问该消息

【讨论】:

请注意,这些是客户端脚本,对于服务器端脚本,可能需要使用pre-receive `提交消息还没有被创建`是的,它有..当用户输入git commit -m "foobar" @OlegzandrDenman - 好的,很公平 - 我已经改写了答案并添加了一个脚注。【参考方案2】:

您可以使用 Python 在 pre-receive 挂钩(用于服务器端)中执行以下操作,这将显示修订信息。

import sys
import subprocess
old, new, branch = sys.stdin.read().split()
proc = subprocess.Popen(["git", "rev-list", "--oneline","--first-parent" , "%s..%s" %(old, new)], stdout=subprocess.PIPE)
commitMessage=str(proc.stdout.readlines()[0])  

【讨论】:

【参考方案3】:

我在 commit-msg 挂钩中实现了这一点。见the documentation。

commit-msg

This hook is invoked by git commit, and can be bypassed with the --no-verify option.
It takes a single parameter, the name of the file that holds the proposed commit log message.
Exiting with a non-zero status causes the git commit to abort.

my_git_project/.git/hooks 下,我添加了文件commit.msg(必须是这个名称)。我在这个文件中添加了以下 Bash 内容来进行验证。

#!/usr/bin/env bash
INPUT_FILE=$1
START_LINE=`head -n1 $INPUT_FILE`
PATTERN="^(MYPROJ)-[[:digit:]]+: "
if ! [[ "$START_LINE" =~ $PATTERN ]]; then
  echo "Bad commit message, see example: MYPROJ-123: commit message"
  exit 1
fi

【讨论】:

它不工作。我无法在 commit-msg 挂钩中获得提交消息。 COMMIT_FILE=$1 COMMIT_MSG=$(cat $1) 不应该是 commit-msg 而不是 commit.msg 吗? 文件必须命名为 commit-msg,而不是 commit.msg。【参考方案4】:

钩子名称应该是:

commit-msg ,否则不会被调用:

【讨论】:

是的,显然提交消息是传递给commit-msg的第一个参数,这也与文件.git/COMMIT_EDITMSG的内容相同 是的,在(示例)文件commit-msg.sample第9行中也是这么说的:'要启用此挂钩,请将此文件重命名为“提交消息”。'【参考方案5】:

我在 Bash 中创建了一个 commit-msg 脚本,其提交语法为 --。此语法可用于 Azure DevOps 开发人员基于票证 ID 的提交。

#!/bin/sh

# The below input_file is file ".git/COMMIT_EDITMSG" where commits are stored
INPUT_FILE=$1

# It will copy the commit string from ".git/COMMIT_EDITMSG"
START_LINE=`head -n1 $INPUT_FILE`

# Initial index value
sum=0

# Add commit in an array variable separated by -
IFS='- ' read -r -a array_value <<< "$START_LINE"

# Count array index
for i in $!array_value[@]
do
    sum=`expr $sum + $i`
done

# Verify commit
if [ $sum == 3 ]; then

BRANCH_NAME=`git branch | awk '/\*/  print $2; '`
TICKET_DIGIT=`awk -F '[0-9]' 'print NF-1' <<< "$array_value[1]"`

   if [ $array_value[0] != $BRANCH_NAME ];  then
        echo "please enter current branch name"
        exit 1
   fi

   if [ "$TICKET_DIGIT" != "4" ];  then
        echo "INVALID TICKET ID"
        exit 1
   else
      echo "verify ticket ID $array_value[1]"
   fi

else
   echo "pattern must be <CURRENT_BRANCH_NAME>-<4_DIGIT_TICKETID>-<COMMIT_DECRIPTION> without space and don't use - in commit_description"
   exit 1
fi

【讨论】:

以上是关于在 Git 钩子中获取提交消息的主要内容,如果未能解决你的问题,请参考以下文章

[转] 利用git钩子,使用python语言获取提交的文件列表

在git中实施提交消息格式

用于自动化的 Git 钩子

Git 子模块提交钩子

Git远程/共享预提交钩子

git钩子