29 Jul 2022

In my new team, we’re using Conventional Commits, a specification for Git commit messages. Due to the used conventions it is possible khổng lồ generate e. G. Release notes directly from the Git history.

Bạn đang xem: Mu88

Unfortunately, I sometimes mess up with the different types of commit or simply forget it, which, as a consequence, breaks the release chú ý generation. That’s why I’ve set up a Git hook which triggers a PowerShell script on every git commit và checks the commit message.

The PowerShell script looks like this:

12345678910111213141516171819202122# The commit message has lớn be loaded from a Git-internal file$commitMessage = Get-Content $args<0># Attention: contains a filter for Jira!$conventionalCommitRegex = "(?-i)^(build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert|BREAKING CHANGE)!?((+))?:s(w3-d4)?s?1.*"# Without Jira filter:# $conventionalCommitRegex = "(?-i)^(build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert|BREAKING CHANGE)!?((+))?:s1.*"$nuKeeperRegex = "(?-i)^:package: .*"if ($commitMessage -match $conventionalCommitRegex) exit 0if ($commitMessage -match $nuKeeperRegex) exit 0Write-Host "The commit message does not match the Conventional Commit rules"exit 1
Git’s current commit message is stored in the tệp tin .git/COMMIT_EDITMSG which gets read on the first line of the script. Then a pretty long RegEx defines all the different Conventional Commit varieties. This RegEx is executed against the current commit message in line 12. If it matches, exit 0; indicates success lớn the Git hook.

To enable this Git hook for a repository, the tệp tin .git/commit-msg has khổng lồ be created with the following content:

#!/bin/shpwsh -noprofile C:/source/aryannations88.com/config/hooks/Git_EnsureConventionalCommitMessage.ps1 $1It is a default Shell script which runs pwsh (the PowerShell executable) and the formerly explained PowerShell script. The -noprofile flag skips loading the current user’s PowerShell profile which is unwanted in my case (as I have several things lượt thích Oh My Posh configured). Finally, the $1 argument is provided by Git và contains the path of the Git commit message tệp tin (remember: .git/COMMIT_EDITMSG).

Xem thêm: Cơ Quan Lãnh Đạo Cao Nhất Của Chi Đoàn Là Gì? ? Cơ Quan Lãnh Đạo Cao Nhất Của Chi Đoàn Là Gì

And that’s all. Now when changing a file & trying to lớn commit it via git commit -a -m "some arbitrary commit message", the command fails with The commit message does not match the Conventional Commit rules.But when using git commit -a -m "feat: my fancy new feature", everything works fine as before.

This is great so far. But the setting has the disadvantage that the Git hook has to lớn be configured for each và every repository. But with Git, there is a solution for everything