Created
January 20, 2022 11:53
-
-
Save bserem/f38975401348c0aaa17b82a51cbbe85a to your computer and use it in GitHub Desktop.
Commit msg hook in PHP that follow https://cbea.ms/git-commit
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
#!/usr/bin/php | |
<?php | |
$message = file_get_contents($argv[1]); | |
checkMessage($message); | |
exit(0); | |
function checkMessage($message) { | |
if (strlen($message) < 10) { | |
echo "A commit must be annotated by a prefixed message of at least ten characters\n"; | |
exit(1); | |
} | |
foreach (preg_split('/\v/', $message, -1) as $key => $line) { | |
switch($key) { | |
case 0: | |
// Check the Subject line. | |
if (strlen($line) > 50) { | |
echo "Limit the commit subject line to 50 characters\n"; | |
exit(1); | |
} | |
if (!ctype_upper($line[0])) { | |
echo "Capitalize the commit subject line\n"; | |
exit(1); | |
} | |
if (substr($line, -1) == '.') { | |
echo "Do not end the commit subject line with a period\n"; | |
exit(1); | |
} | |
break; | |
case 1: | |
if (strlen($line) > 0) { | |
echo "Separate commit subject from body with a blank line\n"; | |
exit(1); | |
} | |
break; | |
default: | |
// If line is a comment ignore it. | |
if ($line[0] == '#') { | |
continue 2; | |
} | |
if (strlen($line) > 72) { | |
echo "Wrap the commit body at 72 characters\n"; | |
exit(1); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment