Skip to content

Instantly share code, notes, and snippets.

@tobyS
Created June 1, 2018 08:37
Show Gist options
  • Save tobyS/7fc8002e786345af2aa8d13f74de35ce to your computer and use it in GitHub Desktop.
Save tobyS/7fc8002e786345af2aa8d13f74de35ce to your computer and use it in GitHub Desktop.
Ticket number prefix without branch (reads .ticket file)
#!/usr/bin/php
# FILE: .git/hooks/commit-msg
<?php
$commitMessages = file($argv[1]);
if (preg_match('(^[0-9]+:\s*$)', $commitMessages[0])) {
echo "Empty commit message, aborting.\n";
exit(1);
}
#!/usr/bin/php
# FILE: .git/hooks/prepare-commit-msg
<?php
if (isset($argv[2])) {
echo "Special commit type. Won't edit message.\n";
exit(0);
}
if (!file_exists('.ticket')) {
echo "No .ticket file found. Won't edit message.\n";
exit(0);
}
$commitFile = $argv[1];
$ticketNumber = trim(file_get_contents('.ticket'));
if ($ticketNumber === '') {
echo "No ticket number in ticket file. Won't edit message.\n";
exit(0);
}
file_put_contents(
$commitFile,
$ticketNumber . ': ' . file_get_contents($commitFile)
);
echo "Prefixed commit message with ticket number {$ticketNumber}.\n";
exit(0);
@tobyS
Copy link
Author

tobyS commented Jun 1, 2018

git config --global core.editor "vim '+ normal \$'" makes your VIM start at the end of the line (aka behind the ticket number) for editing the commit message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment