Skip to content

Instantly share code, notes, and snippets.

@tommy-muehle
Created June 30, 2015 10:31
Show Gist options
  • Save tommy-muehle/260dbbe0c30947b990df to your computer and use it in GitHub Desktop.
Save tommy-muehle/260dbbe0c30947b990df to your computer and use it in GitHub Desktop.
Git hook to add JIRA issue key from branch-name (if it exists) and add it before the commit message
#!/usr/bin/env php
<?php
/**
* Add this file as "prepare-commit-msg" under /my-project/.git/hooks directory
* and make it executable. (chmod +x)
*
* Example:
* If Branchname was: "release/MYPRO-1-awesome-feature"
* and the commit message you typed in are: "Take this"
* this hooks make the message: "MYPRO-1: Take this"
*/
$messageFile = $argv[1];
$message = file_get_contents($messageFile);
if (false === function_exists('exec')) {
exit(0);
}
$branchName = exec('git symbolic-ref --short HEAD');
preg_match('/[A-Z]{3,}-[0-9]{1,}/', $branchName, $matches);
if (true === is_array($matches) && 1 === count($matches)) {
$message = sprintf('%s: %s', current($matches), $message);
}
file_put_contents($messageFile, $message);
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment