git - 在 VS Code 中自动将 Jira 问题编号添加到 git commit 消息

标签 git visual-studio-code jira

我们将 Jira Issue # 添加到提交消息以将更改与 Jira Issue 相关联。由于问题编号是手动添加的,因此容易出现人为错误。由于我们在 git 分支中总是有这个数字,提交消息可以有这个分支名称的默认值吗?可以通过 vs 代码扩展或 git 模板来完成吗?

enter image description here

最佳答案

我创建了这个名为 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

Gist 如下:https://gist.github.com/raduchiriac/36b6269bebb75cfac7371acb1459fdcc

注意:您会发现许多使用 sed -nr 的解决方案,不幸的是,这在塞 pull 之后不再适用了。使用 -E

关于git - 在 VS Code 中自动将 Jira 问题编号添加到 git commit 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53579441/

相关文章:

git interactive rebase 压缩到下一次提交

macos - Homebrew 软件更新失败 : "Please, commit your changes or stash them before you can merge"

git - 使无法分支 merge [Lock for merge?]

node-webkit - Visual Studio Code 编辑器是基于什么构建的

python - 是否可以使用 python-jira 更改 jira 问题状态?

git - 你如何删除远程 git branch "properly",也就是为所有用户更新远程分支列表?

java - Visual Studio Code 运行时检索 UnsupportedClassVersion 错误

sql-server - 使用 VS Code mssql 扩展时找不到命令 'extension.addObjectExplorer'

java - 如何使用 Java 读取 BitBucket/Stash 分支及其链接的 JIRA 票据

tomcat - JIRA、第三方 SSL、在 CentOS 上重定向