forked from phpro/grumphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommitMsgCommand.php
126 lines (107 loc) · 3.55 KB
/
CommitMsgCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?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\IO\ConsoleIO;
use GrumPHP\Locator\ChangedFiles;
use GrumPHP\Runner\TaskRunnerContext;
use GrumPHP\Task\Context\GitCommitMsgContext;
use GrumPHP\Util\Filesystem;
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 ChangedFiles
*/
protected $changedFilesLocator;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @param GrumPHP $grumPHP
* @param ChangedFiles $changedFilesLocator
* @param Filesystem $filesystem
*/
public function __construct(GrumPHP $grumPHP, ChangedFiles $changedFilesLocator, Filesystem $filesystem)
{
parent::__construct();
$this->grumPHP = $grumPHP;
$this->changedFilesLocator = $changedFilesLocator;
$this->filesystem = $filesystem;
}
/**
* 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)
{
$io = new ConsoleIO($input, $output);
$files = $this->getCommittedFiles($io);
$gitUser = $input->getOption('git-user');
$gitEmail = $input->getOption('git-email');
$commitMsgPath = $input->getArgument('commit-msg-file');
if (!$this->filesystem->isAbsolutePath($commitMsgPath)) {
$commitMsgPath = $this->paths()->getGitDir() . $commitMsgPath;
}
$commitMsgFile = new SplFileInfo($commitMsgPath);
$commitMsg = $this->filesystem->readFromFileInfo($commitMsgFile);
$output->writeln('<fg=yellow>GrumPHP detected a commit-msg command.</fg=yellow>');
$context = new TaskRunnerContext(
new GitCommitMsgContext($files, $commitMsg, $gitUser, $gitEmail),
$this->grumPHP->getTestSuites()->getOptional('git_commit_msg')
);
return $this->taskRunner()->run($output, $context);
}
/**
* @return FilesCollection
*/
protected function getCommittedFiles(ConsoleIO $io)
{
if ($stdin = $io->readCommandInput(STDIN)) {
return $this->changedFilesLocator->locateFromRawDiffInput($stdin);
}
return $this->changedFilesLocator->locateFromGitRepository();
}
/**
* @return TaskRunnerHelper
*/
protected function taskRunner()
{
return $this->getHelper(TaskRunnerHelper::HELPER_NAME);
}
/**
* @return PathsHelper
*/
protected function paths()
{
return $this->getHelper(PathsHelper::HELPER_NAME);
}
}