mcampbell508 / git-review
一个可扩展的版本控制钩子框架。
6.0.0
2018-03-24 17:45 UTC
Requires
- php: ^7.1
- danielstjules/stringy: ~3.1.0
- league/climate: ^2.0 || ^3.0
- pimple/pimple: ~3.0
- spatie/regex: ^1.2
- symfony/console: ^2.0 || ^3.2
- symfony/process: ^2.1 || ^3.1
- tightenco/collect: ^5.5 || ^5.6
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.11
- fzaninotto/faker: ^1.7
- mockery/mockery: ^0.9
- phpunit/phpunit: ^4.6 || ^5.0
- sensiolabs/security-checker: ^3.0
- squizlabs/php_codesniffer: ^2.2
Suggests
- sensiolabs/security-checker: required by ComposerSecurityReview.
- squizlabs/php_codesniffer: required by PhpCodeSnifferReview.
This package is auto-updated.
Last update: 2024-09-28 02:14:52 UTC
README
此包是从已被放弃的 Static Review 分支出来的。我将它用作个人项目,并更名为 git-review
一个可扩展的版本控制钩子框架。
要求
- PHP 7.1 及以上版本
用法
对于由 composer 管理的项目,您可以简单地运行以下命令...
$ composer require mcampbell508/git-review
钩子可以按照以下方式安装...
$ vendor/bin/git-review.php hook:install vendor/mcampbell508/git-review/hooks/example-pre-commit.php .git/hooks/pre-commit
否则,如果您不使用 composer...
$ git clone https://github.com/mcampbell508/git-review.git $ cd git-review/ $ composer install --no-dev --optimize-autoloader $ bin/git-review.php hook:install hooks/example-pre-commit.php ~/.../.git/hooks/pre-commit
全局安装和使用
如果您全局安装了 git-review,则钩子也可以用于任何项目
$ composer g require mcampbell508/git-review
然后,就像您通常安装钩子一样安装,但引用全局安装路径
$ git-review.php hook:install ~/.composer/vendor/mcampbell508/git-review/hooks/git-review-commit-msg.php .git/hooks/commit-msg
这假设您已设置 全局 composer 路径。
示例钩子
Static Review 可以用于文件和提交信息审查。以下是每个的基本钩子。
对于文件
#!/usr/bin/env php <?php include __DIR__ . '/../../../autoload.php'; // Reference the required classes. use GitReview\GitReview; use GitReview\Review\General\LineEndingsReview; [...] $reporter = new Reporter(); $review = new GitReview($reporter); // Add any reviews to the GitReview instance, supports a fluent interface. $review->addReview(new LineEndingsReview()); $git = new GitVersionControl(); // Review the staged files. $review->files($git->getStagedFiles()); // Check if any issues were found. // Exit with a non-zero status to block the commit. ($reporter->hasIssues()) ? exit(1) : exit(0);
对于提交信息
#!/usr/bin/env php <?php include __DIR__ . '/../../../autoload.php'; // Reference the required classes. use GitReview\GitReview; use GitReview\Review\Message\BodyLineLengthReview; [...] $reporter = new Reporter(); $review = new GitReview($reporter); // Add any reviews to the GitReview instance, supports a fluent interface. $review->addReview(new BodyLineLengthReview()); $git = new GitVersionControl(); // Review the current commit message. // The hook is passed the file holding the commit message as the first argument. $review->message($git->getCommitMessage($argv[1])); // Check if any issues were found. // Exit with a non-zero status to block the commit. ($reporter->hasIssues()) ? exit(1) : exit(0);
文件审查示例
class NoCommitTagReview extends AbstractFileReview { // Review any text based file. public function canReviewFile(FileInterface $file) { $mime = $file->getMimeType(); // check to see if the mime-type starts with 'text' return (substr($mime, 0, 4) === 'text'); } // Checks if the file contains `NOCOMMIT`. public function review(ReporterInterface $reporter, ReviewableInterface $file) { $cmd = sprintf('grep --fixed-strings --ignore-case --quiet "NOCOMMIT" %s', $file->getFullPath()); $process = $this->getProcess($cmd); $process->run(); if ($process->isSuccessful()) { $message = 'A NOCOMMIT tag was found'; $reporter->error($message, $this, $file); } } }
信息审查示例
class WorkInProgressReview extends AbstractMessageReview { // Check if the commit message contains "wip" public function review(ReporterInterface $reporter, ReviewableInterface $commit) { $fulltext = $commit->getSubject() . PHP_EOL . $commit->getBody(); if (preg_match('/\bwip\b/i', $fulltext)) { $message = 'Do not commit WIP to shared branches'; $reporter->error($message, $this, $commit); } } }
单元测试
请参阅 vagrantup.com 和 phpunit.de。
$ git clone https://github.com/mcampbell508/git-review.git $ cd git-review/ $ vagrant up $ vagrant ssh ... $ cd /srv $ composer update $ composer test
许可证
此库的内容由 MIT 许可证 发布,由 Samuel Parkinson 发布。