Skip to content

Instantly share code, notes, and snippets.

@d1vanloon
Last active November 27, 2023 19:59
Show Gist options
  • Save d1vanloon/57c84e25b21ef222a5702bdd750f145a to your computer and use it in GitHub Desktop.
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
[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