Last active
December 28, 2018 08:32
-
-
Save smotesko/2bdb73d66a7e92d46f6c to your computer and use it in GitHub Desktop.
git commit message helper
This file contains hidden or 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/env php | |
<?php | |
// filename: featcommit | |
/** | |
* | |
* This scripts prepends task number to the commit message if current git branch is | |
* "FOO-xxxx". | |
* So it helps you by inserting your feature number into the commit message. | |
* Use it as `featcommit.php "commit message"`. | |
*/ | |
function getAsteriskLine($command) { | |
$output = array(); | |
$status = 0; | |
exec($command, $output, $status); | |
if ($status > 0) | |
throw new Exception('Command `'.$command.'` returned status '.$status, 1); | |
foreach ($output as $line) { | |
if (preg_match('/^\*/', $line)) { | |
return $line; | |
} | |
} | |
throw new Exception('Cannot find line starting with * in the output of '.$command); | |
} | |
function getFeatureNumber() { | |
$line = getAsteriskLine('git branch'); | |
$matches = array(); | |
if (!preg_match('/^\*\s(\w+-\d+).+$/', $line, $matches)) | |
throw new Exception("Can't find the feature number", 1); | |
return $matches[1]; | |
} | |
if ($argc < 2) { | |
echo(sprintf("Usage: %s \"commit message\"\n", $argv[0])); | |
exit(1); | |
} | |
$featureNumber = getFeatureNumber(); | |
$commitCommand = sprintf("git commit -m \"%s: %s\"", $featureNumber, $argv[1]); | |
echo "Executing `$commitCommand`\n"; | |
passthru($commitCommand); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment