-
Notifications
You must be signed in to change notification settings - Fork 8
/
prepare-commit-msg
executable file
·34 lines (31 loc) · 1.12 KB
/
prepare-commit-msg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/php
<?php
# Git Prepare Commit Message Hook Script
#
# Location: <repository>/.git/hooks/prepare-commit-msg
#
# This script will automatically add the correct
# JIRA ISSUE ID to the beginning of each commit message
# When the branch ID is starts with the JIRA ISSUE ID.
# It can be overridden if specified in the message.
#
# Example:
#
# jira-123_branch_name => 'JIRA-123 commit message'
#
# @author Tamas Kalman <ktamas77@gmail.com>
$messageFile = $argv[1];
$message = file_get_contents($messageFile);
$messageJiraPattern = '/^([A-Z]{1,32}-[0-9]{1,32})\ /';
preg_match($messagePivotalPattern, $message, $matches);
$messageJiraId = (isset($matches[1])) ? $matches[1] : null;
if ($messageJiraId === null) {
$currentBranchName = exec('git rev-parse --abbrev-ref HEAD');
$branchJiraPattern = '/^([a-zA-Z]{1,32}-[0-9]{1,32})[-_]?/';
preg_match($branchJiraPattern, $currentBranchName, $matches);
$branchJiraId = (isset($matches[1])) ? $matches[1] : null;
if ($branchJiraId !== null) {
$message = sprintf('%s %s', strtoupper($branchJiraId), $message);
file_put_contents($messageFile, $message);
}
}