Skip to content

Commit

Permalink
Merge pull request #43 from veewee/feature/commit-msg
Browse files Browse the repository at this point in the history
Commit message task
  • Loading branch information
veewee committed Sep 22, 2015
2 parents f7c5f94 + 052cb48 commit f8b0127
Show file tree
Hide file tree
Showing 14 changed files with 467 additions and 9 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ parameters:
tasks:
behat: ~
blacklist: ~
git_commit_message: ~
phpcsfixer: ~
phpcs:
standard: "PSR2"
Expand Down Expand Up @@ -218,6 +219,38 @@ parameters:
- "exit;"
```

#### Git commit message (git_commit_message)

The git comit message can be used in combination with the git hook `git:commit-msg`.
It can be used to enforce patterns in a commit message.
For example: if you are working with JIRA, it is possible to add a pattern for the JIRA issue number.

**matchers**

*Default: []*

Use this parameter to specify one or multiple patterns. The value can be in regex or glob style.
Here are some example matchers:

- /JIRA-([0-9]*)/
- pre-fix*
- *suffix
- ...

**case_insensitive**

*Default: true*

Mark the matchers as case sensitive.

**multiline**

*Default:true*

Markt he matchers as multiline.



#### PHP-CS-Fixer

The PHP-CS-Fixer task will run codestyle checks.
Expand Down
8 changes: 8 additions & 0 deletions resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,11 @@ services:
- @process_builder
tags:
- {name: grumphp.task, config: blacklist}

task.git.commitmessage:
class: GrumPHP\Task\Git\CommitMessage
arguments:
- @config
- "@=parameter('tasks')['git_commit_message'] ? parameter('tasks')['git_commit_message'] : []"
tags:
- {name: grumphp.task, config: git_commit_message}
21 changes: 21 additions & 0 deletions resources/hooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

#
# Run the hook command.
# Note: this will be replaced by the real command during copy.
#

GIT_USER=$(git config user.name)
GIT_EMAIL=$(git config user.email)
COMMIT_MSG_FILE=$1

(cd "${HOOK_EXEC_PATH}" && exec $(HOOK_COMMAND) "--git-user=$GIT_USER" "--git-email=$GIT_EMAIL" "$COMMIT_MSG_FILE")

# Validate exit code of above command
RC=$?
if [ "$RC" != 0 ]; then
exit $RC;
fi

# Clean exit:
exit 0;
2 changes: 1 addition & 1 deletion resources/hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Run the hook command.
# Note: this will be replaced by the real command during copy.
#
(cd "${HOOK_EXEC_PATH}" && exec $(HOOK_COMMAND))
(cd "${HOOK_EXEC_PATH}" && exec $(HOOK_COMMAND) '--skip-success-output')

# Validate exit code of above command
RC=$?
Expand Down
45 changes: 45 additions & 0 deletions spec/GrumPHP/Task/Context/GitCommitMsgContextSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace spec\GrumPHP\Task\Context;

use GrumPHP\Collection\FilesCollection;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class GitCommitMsgContextSpec extends ObjectBehavior
{
/**
* @var string
*/
protected $tempFile;

function let(FilesCollection $files, \SplFileInfo $fileInfo)
{
$this->beConstructedWith($files, $fileInfo, 'user', '[email protected]');
}

function it_is_initializable()
{
$this->shouldHaveType('GrumPHP\Task\Context\GitCommitMsgContext');
}

function it_should_be_a_task_context()
{
$this->shouldImplement('GrumPHP\Task\Context\ContextInterface');
}

function it_should_have_files(FilesCollection $files)
{
$this->getFiles()->shouldBe($files);
}

function it_should_know_the_git_user()
{
$this->getUserName()->shouldBe('user');
}

function it_should_know_the_git_email()
{
$this->getUserEmail()->shouldBe('[email protected]');
}
}
45 changes: 45 additions & 0 deletions spec/GrumPHP/Task/Git/CommitMessageSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace spec\GrumPHP\Task\Git;

use GrumPHP\Configuration\GrumPHP;
use GrumPHP\Task\Context\GitCommitMsgContext;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class CommitMessageSpec extends ObjectBehavior
{
function let(GrumPHP $grumPHP)
{
$this->beConstructedWith($grumPHP, array(
'matchers' => array('test', '*es*', 'te[s][t]', '/^te(.*)/', '/(.*)st$/', '/t(e|a)st/', 'TEST')
));
}

function it_is_initializable()
{
$this->shouldHaveType('GrumPHP\Task\Git\CommitMessage');
}

function it_is_a_grumphp_task()
{
$this->shouldImplement('GrumPHP\Task\TaskInterface');
}

function it_should_run_in_git_commit_msg_context(GitCommitMsgContext $context)
{
$this->canRunInContext($context)->shouldReturn(true);
}

function it_runs_the_suite(GitCommitMsgContext $context)
{
$context->getCommitMessage()->willReturn('test');

$this->run($context);
}

function it_throws_exception_if_the_process_fails(GitCommitMsgContext $context) {
$context->getCommitMessage()->willReturn('invalid');
$this->shouldThrow('GrumPHP\Exception\RuntimeException')->duringRun($context);
}
}
24 changes: 18 additions & 6 deletions src/GrumPHP/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class Application extends SymfonyConsole
*/
protected $container;

/**
* @var string
*/
protected $configDefaultPath;

/**
* Set up application:
*/
Expand Down Expand Up @@ -57,19 +62,23 @@ protected function getDefaultInputDefinition()
*/
protected function getConfigDefaultPath()
{
$defaultConfigName = getcwd() . DIRECTORY_SEPARATOR . self::APP_CONFIG_FILE;
if ($this->configDefaultPath) {
return $this->configDefaultPath;
}

$this->configDefaultPath = getcwd() . DIRECTORY_SEPARATOR . self::APP_CONFIG_FILE;
$composerFile = 'composer.json';

if (!file_exists($composerFile)) {
return $defaultConfigName;
return $this->configDefaultPath;
}

$composer = json_decode(file_get_contents($composerFile), true);
if (isset($composer['extra']['grumphp']['config-default-path'])) {
return $composer['extra']['grumphp']['config-default-path'];
$this->configDefaultPath = $composer['extra']['grumphp']['config-default-path'];
}

return $defaultConfigName;
return $this->configDefaultPath;
}

/**
Expand All @@ -87,6 +96,10 @@ protected function getDefaultCommands()
$container->get('task_runner')
);

$commands[] = new Command\Git\CommitMsgCommand(
$container->get('config'),
$container->get('locator.changed_files')
);
$commands[] = new Command\Git\DeInitCommand(
$container->get('config'),
$container->get('filesystem')
Expand Down Expand Up @@ -131,8 +144,7 @@ protected function getContainer()

// Load cli options:
$input = new ArgvInput();
$input->bind($this->getDefaultInputDefinition());
$configPath = $input->getOption('config');
$configPath = $input->getParameterOption(array('--config', '-c'), $this->getConfigDefaultPath());

// Make sure to set the full path when it is declared relative
// This will fix some issues in windows.
Expand Down
99 changes: 99 additions & 0 deletions src/GrumPHP/Console/Command/Git/CommitMsgCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace GrumPHP\Console\Command\Git;

use GrumPHP\Collection\FilesCollection;
use GrumPHP\Configuration\GrumPHP;
use GrumPHP\Console\Helper\PathsHelper;
use GrumPHP\Console\Helper\TaskRunnerHelper;
use GrumPHP\Locator\LocatorInterface;
use GrumPHP\Task\Context\GitCommitMsgContext;
use SplFileInfo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* This command runs the git commit-msg hook.
*/
class CommitMsgCommand extends Command
{
const COMMAND_NAME = 'git:commit-msg';

/**
* @var GrumPHP
*/
protected $grumPHP;

/**
* @var LocatorInterface
*/
protected $changedFilesLocator;

/**
* @param GrumPHP $grumPHP
* @param LocatorInterface $changedFilesLocator
*/
public function __construct(GrumPHP $grumPHP, LocatorInterface $changedFilesLocator)
{
parent::__construct();

$this->grumPHP = $grumPHP;
$this->changedFilesLocator = $changedFilesLocator;
}

/**
* Configure command
*/
protected function configure()
{
$this->setName(self::COMMAND_NAME);
$this->addOption('git-user', null, InputOption::VALUE_REQUIRED, 'The configured git user name.', '');
$this->addOption('git-email', null, InputOption::VALUE_REQUIRED, 'The configured git email.', '');
$this->addArgument('commit-msg-file', InputArgument::REQUIRED, 'The configured commit message file.');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int|void
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$files = $this->getCommittedFiles();
$gitUser = $input->getOption('git-user');
$gitEmail = $input->getOption('git-email');
$commitMsgPath = $input->getArgument('commit-msg-file');
$commitMsgFile = new SplFileInfo($commitMsgPath);

$context = new GitCommitMsgContext($files, $commitMsgFile, $gitUser, $gitEmail);
return $this->taskRunner()->run($output, $context);
}

/**
* @return FilesCollection
*/
protected function getCommittedFiles()
{
return $this->changedFilesLocator->locate();
}

/**
* @return TaskRunnerHelper
*/
protected function taskRunner()
{
return $this->getHelper(TaskRunnerHelper::HELPER_NAME);
}

/**
* @return PathsHelper
*/
protected function paths()
{
return $this->getHelper(PathsHelper::HELPER_NAME);
}
}
1 change: 1 addition & 0 deletions src/GrumPHP/Console/Command/Git/DeInitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class DeInitCommand extends Command
*/
protected static $hooks = array(
'pre-commit',
'commit-msg',
);

/**
Expand Down
1 change: 1 addition & 0 deletions src/GrumPHP/Console/Command/Git/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class InitCommand extends Command
*/
public static $hooks = array(
'pre-commit',
'commit-msg',
);

/**
Expand Down
10 changes: 9 additions & 1 deletion src/GrumPHP/Console/Command/Git/PreCommitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use GrumPHP\Task\Context\GitPreCommitContext;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand Down Expand Up @@ -47,6 +48,12 @@ public function __construct(GrumPHP $grumPHP, LocatorInterface $changedFilesLoca
protected function configure()
{
$this->setName(self::COMMAND_NAME);
$this->addOption(
'skip-success-output',
null,
InputOption::VALUE_NONE,
'Skips the success output. This will be shown by another command in the git commit hook chain.'
);
}

/**
Expand All @@ -59,8 +66,9 @@ public function execute(InputInterface $input, OutputInterface $output)
{
$files = $this->getCommittedFiles();
$context = new GitPreCommitContext($files);
$skipSuccessOutput = (bool) $input->getOption('skip-success-output');

return $this->taskRunner()->run($output, $context);
return $this->taskRunner()->run($output, $context, $skipSuccessOutput);
}

/**
Expand Down
Loading

0 comments on commit f8b0127

Please sign in to comment.