Last active
November 27, 2023 19:59
-
-
Save d1vanloon/57c84e25b21ef222a5702bdd750f145a to your computer and use it in GitHub Desktop.
Git commit hook that prepends the active branch's JIRA issue number to the commit message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param ( | |
[Parameter()] | |
[string] | |
$CommitFile | |
) | |
$CommitMessage = Get-Content -Path $CommitFile | |
$CurrentBranch = (git rev-parse --abbrev-ref HEAD) | |
$HasMatch = $CurrentBranch -match "[A-Z0-9]{1,10}-?[A-Z0-9]+-\d+" | |
if ($HasMatch) { | |
$IssueNumber = $Matches[0] | |
$AlreadyStartsWith = $CommitMessage -like "$($IssueNumber):*" | |
if ($AlreadyStartsWith) { | |
Write-Host "Nice job including the issue number." | |
} else { | |
$CommitMessage = "${IssueNumber}:", ($CommitMessage -join "`r`n") -join " " | |
Set-Content -Path $CommitFile -Value $CommitMessage | |
Write-Host "Prepended issue number $IssueNumber to the commit message. (Use --no-verify to skip)" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment