29

所以我的分支以 bugtracker 票号命名,类似于“issue-1234”,我们有一个约定,总是在提交消息中写下票号。我想知道是否可以在我处理 issue-* 分支时自动在提交消息中附加票号,而无需我明确输入。

我查看了 git commit 钩子,即 pre-commit、prepare-message 和 post-commit,它们似乎都不能做我想做的事。提交后挂钩接近,但您无法修改使用 -m 提交的消息。

重申一下,我想知道这是否可能:

在分支上:issue-1234

git commit -a -m"fixed this pesky issue"

提交后,在 git log 中,它显示消息为:

fixed this pesky issue. ticket number: #1234
4

7 回答 7

22

你错过了一个钩子。你想要的是commit-msg

这个钩子由 git commit 调用,可以用 --no-verify 选项绕过。它采用单个参数,即保存建议的提交日志消息的文件的名称。以非零状态退出会导致 git 提交中止。

例如:

#!/bin/sh

ticket=$(git symbolic-ref HEAD | awk -F- '/^issue-/ {print $2}')
if [ -n "$ticket" ]; then
    echo "ticket #$ticket" >> $1
fi

这是对您的分支名称的非常​​幼稚的解析,它只是附加到提交消息的单独行中。如果这对您来说还不够好,请修改它。

当然,我实际上建议在 中执行此操作,并使用(不使用)prepare-commit-msg提交。您实际上可以在单行提交消息中写入足够的信息是非常非常罕见的。此外,这将让您在提交之前看到消息,以防您的钩子不能完全满足您的要求。git commit-m

于 2011-04-28T20:53:55.233 回答
8

你也可以使用prepare-commit-msghook,它接受的参数比commit-msg. 然后,您可以检查消息是否来自文件、模板等,以避免在不需要时附加问题编号。

.git/hooks/prepare-commit-msg当您在名为 的功能分支中工作时,使用以下脚本foo-123,然后[#123]将添加到您所做的每个提交的第三行。

我写的这篇文章中有更多信息

#!/bin/sh

if [ x = x${2} ]; then
  BRANCH_NAME=$(git symbolic-ref --short HEAD)
  STORY_NUMBER=$(echo $BRANCH_NAME | sed -n 's/.*-\([0-9]\)/\1/p')
  if [ x != x${STORY_NUMBER} ]; then
    sed -i.back "1s/^/\n\n[#$STORY_NUMBER]/" "$1"
  fi
fi
于 2014-07-19T14:20:54.683 回答
3

This way you can add branch name to the start of commit message. It's prepare-commit-msg hook. Work both for "git commit -m" and "git commit" commands. The option is file .git/hooks/pre-commit.skip which contains a list of branches you don't want to auto-prepend.

BRANCH="$(git rev-parse --abbrev-ref HEAD)"
FILE_CONTENT="$(cat $1)"
skip_list=`git rev-parse --git-dir`"/hooks/pre-commit.skip"
if grep -E "^$BRANCH$" $skip_list; then
  exit
fi
if [ $2 = "message" ]; then
  echo $BRANCH: $FILE_CONTENT > $1
else
  echo $BRANCH: > $1
  echo $FILE_CONTENT >> $1
fi
于 2014-10-23T18:44:39.347 回答
2

另一种选择是使用git notes您提到的一个钩子将票号信息添加到提交中。
(有关笔记机制的更多信息,请参阅“自我笔记”博客文章条目)

于 2011-04-28T19:20:55.287 回答
2

使用pre-commitgitcket钩子,可以很好地自动在提交中包含票号。

于 2019-01-04T03:51:22.730 回答
1

这是针对任何类型的问题/票证编号提交消息的完整解决方案:

准备提交消息

#!/bin/bash
# Append issue number / bug tracking URL to commit.
#
# If the branch name contains the issue number, it will append it to the
# commit message. Example:
#
#   BRANCH NAME            LINE TO APPEND
#   feature/GH-123-emoji   GitHub: #123
#   WRIKE-123-add-payment  Wrike: https://www.wrike.com/open.htm?id=123
#   UNKNOWN-123            Issue: #123

branchName=`git rev-parse --abbrev-ref HEAD`

IFS=- read issueTracker issueNumber <<< $(echo $branchName | sed -nr 's,([a-z-]+/)?([A-Z]+-[0-9]+)-.+,\2,p')

if [[ -z $issueNumber ]]; then
  exit 0
fi

case "$issueTracker" in
  WRIKE)
    line="Wrike: https://www.wrike.com/open.htm?id=$issueNumber"
    ;;
  GH)
    line="GitHub: #$issueNumber"
    ;;
  GL)
    line="GitLab: #$issueNumber"
    ;;
  *)
    line="Issue: #$issueNumber"
    ;;
esac

# If the commit message already contains the line (`--amend`), then do
# not add it again.
if ! ( grep "$line" "$1" > /dev/null ); then
  sed -i.bak -e "/# Please enter the commit message for your changes./ s,^,$line\n\n," $1
fi

将其放入存储库的目录以仅应用于存储库,或者在其中.git/hooks设置core.hooksPath~/.gitconfig并复制到该目录以应用于所有存储库。

除了其他有用的脚本外,请参阅我的配置文件存储库。

于 2018-06-08T05:53:51.803 回答
0

因为这可能对寻求快速解决方案的人有用 - 具有改进的可能性和相当好的可移植性(将它添加到新盒子是简单的 bash 问题source git-tricks.sh

我们的分支名称通常采用以下形式: <work-category>/<ticket-id>-<short-description>

喜欢:bug/ID-1234-bad-button-color

然后我有以下别名:

  • alias git-branch-name='git rev-parse --abbrev-ref HEAD'
    输出:bug/ID-1234-bad-button-color
  • alias git-branch-ticket='git-branch-name | grep -oP "^[^/]*/\K[^-]*-[0-9]+"'
    输出:(ID-1234 如果是问题的作者,它应该是'git-branch-name | grep -oP "^issue-\K[0-9]+"':)

最后一个:

alias git-describe-commit='git commit --all --edit --message "[$(git-branch-ticket)] --edit this--"'

这使我可以使用它git-describe-commit来快速添加对 repo 的更改。

于 2021-01-12T15:41:08.403 回答