使用 git commit hook 附加票号?

发布于 2024-11-03 22:35:16 字数 475 浏览 7 评论 0 原文

因此,我的分支以 bugtracker 票号命名,例如“issue-1234”,并且我们有一个约定,即始终在提交消息中写下票号。我想知道当我在 issues-* 分支上工作时是否可以自动将票号附加到提交消息中,而无需我显式键入它。

我查看了 git commit hooks,即预提交、准备消息和后提交,它们似乎都不能做我想要的事情。提交后挂钩已接近,但您无法修改使用 -m 提交的消息。

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

在分支上:issue-1234

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

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

fixed this pesky issue. ticket number: #1234

So my branch is named after bugtracker ticket number, something like "issue-1234", and we have a convention to always write down ticket number in commit message. I'm wondering if it's possible to append the ticket number in commit message automatically when I'm working on an issue-* branch without me explicitly typing it.

I looked at git commit hooks, namely pre-commit, prepare-message, and post-commit, and none of them seem to be able to do what I wanted. Post-commit hook comes close, but you cannot modify the message that's committed with -m.

To reiterate, I'm wondering if this is possible:

On branch: issue-1234

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

After the commit, in git log, it shows the message as:

fixed this pesky issue. ticket number: #1234

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

成熟稳重的好男人 2024-11-10 22:35:16

你错过了一个钩子。您想要的是 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)。您实际上可以在单行提交消息中写入足够的信息,这是非常非常罕见的。此外,这将让您在提交之前看到消息,以防您的钩子没有达到您想要的效果。

You missed a hook. The one you want is commit-msg:

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

So for example:

#!/bin/sh

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

That's a very naive parsing of your branch name, and it's simply appended to the commit message on its own line. Modify it if that's not good enough for you.

Of course, I'd actually recommend doing this in prepare-commit-msg, and committing with git commit (without -m). It's very, very rare that you can actually write sufficient information in a single-line commit message. Further, that will let you see the message before the commit is made, in case your hook doesn't do quite what you want.

开始看清了 2024-11-10 22:35:16

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

当您在名为 foo-123 的功能分支中工作时,使用 .git/hooks/prepare-commit-msg 中的以下脚本,然后 [#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

You can as well use prepare-commit-msg hook, which accepts more parameters than commit-msg. Then you can check if the message is coming from a file, a template, etc to avoid appending the issue numbers when you don't want it.

With the following script in .git/hooks/prepare-commit-msg when you are working in a feature branch named foo-123, then [#123] will be added to the third line of every commit you make.

More information in this post I wrote

#!/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
蒲公英的约定 2024-11-10 22:35:16

这样您就可以将分支名称添加到提交消息的开头。这是准备提交消息钩子。
适用于“git commit -m”和“git commit”命令。该选项是文件 .git/hooks/pre-commit.skip,其中包含您不想自动添加的分支列表。

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

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
倒数 2024-11-10 22:35:16

使用 预提交 以及 预提交 。 com/milin/giticket" rel="nofollow noreferrer">giticket 钩子,可以很好地自动在提交中包含票号。

Using pre-commit along with the giticket hook, works pretty well to have the ticket number in the commit automatically.

私野 2024-11-10 22:35:16

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

prepare-commit-msg

#!/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 目录中以仅应用于存储库,或设置 ~/.gitconfig 中的“https://git-scm.com/docs/git-config#git-config-corehooksPath” rel="nofollow noreferrer">core.hooksPath 和复制到该目录以应用到您的所有存储库。

请参阅 我的配置文件存储库< /a> 除了其他有用的脚本之外。

Here's a complete solution for any kind of issue/ticket numbering commit messages:

prepare-commit-msg

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

Put it into repository's .git/hooks directory to apply only to the repo, or set up core.hooksPath in ~/.gitconfig and copy to that directory to apply to all of your repositories.

See in my config files repository besides other useful scripts.

方觉久 2024-11-10 22:35:16

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

Another option would be to use git notes to add the ticket number information to the commit, using one of the hooks you mention.
(See "Notes to self" blog post entry for more on the notes mechanism)

清引 2024-11-10 22:35:16

因为这对于寻求快速解决方案的人来说可能很有用 - 具有改进的可能性和相当好的可移植性(将其添加到新框中只需简单的 bash source git-tricks.sh 即可)

我们的分支名称通常位于形式:
/-

就像: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-描述-提交用于快速添加对存储库的更改。

As this maybe useful for somebody looking for quick solution - with possibility of improvement and quite good portability (adding this to a new box is matter of simple bash source git-tricks.sh)

Our branchnames are usually in the form:
<work-category>/<ticket-id>-<short-description>

Like: bug/ID-1234-bad-button-color

I then have following aliases:

  • alias git-branch-name='git rev-parse --abbrev-ref HEAD'
    Output: bug/ID-1234-bad-button-color
  • alias git-branch-ticket='git-branch-name | grep -oP "^[^/]*/\K[^-]*-[0-9]+"'
    Output: ID-1234
    (In case of author of the question it should be:'git-branch-name | grep -oP "^issue-\K[0-9]+"' )

And final one:

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

Which allows me to use git-describe-commit for quickly adding changes to the repo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文