prepare-commit-msg
的文件,并将其复制到.git/hooks
文件夹中。#!/bin/bash
# Get the current branch name
current_branch=`git rev-parse --abbrev-ref HEAD`
# Search Jira ID in a pattern such a "feature/ABCD-123-my-feature"
id=$(echo $current_branch | sed -nE 's,[a-z]+/([A-Z]+-[0-9]+)-.+,\1,p')
# Only prepend if an ID was found in the current branch
if [[ ! -z $id ]]; then
# $1 is the name of the file containing the commit message
# Prepend "ABCD-123: "
sed -i.bak -E "1s/^/${id}: /" $1
fi
sed -nr
,不幸的是,这种方法在Sierra之后不再可用。请使用-E
。Flow / Jira Commit Prefix
的扩展程序,它几乎满足了我的需求。可以根据需要调整源代码。我建议您使用commit-msg
本地钩子和pre-receive
服务器钩子来实现您的目的。 Git钩子是验证和修改存储库写入操作的脚本 - 您可以在官方文档中了解它们:https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
本地commit-msg
钩子可以从分支中提取票据名称并将其附加到消息中,如果在那里没有找到,则可以甚至取消提交过程,如果分支名称不符合命名要求。
这种方法的主要缺点是任何开发人员都应该手动放置本地钩子,由于安全考虑,因此可能会忘记或忽略这一点。因此,您还需要一个服务器端pre-receive
钩子,以拒绝推送的分支,无论其中一个新提交是否包含无效消息。