2017 © Pedro Peláez
 

library git-review

An extendable framework for version control hooks.

image

mcampbell508/git-review

An extendable framework for version control hooks.

  • Saturday, June 9, 2018
  • by mcampbell508
  • Repository
  • 2 Watchers
  • 1 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 9 Open issues
  • 30 Versions
  • 0 % Grown

The README.md

git-review

Build Status Total Downloads Latest Stable Version License, (*1)

This package was forked from Static Review which has been abandoned. I am using this as a personal project and have renamed to git-review, (*2)


An extendable framework for version control hooks., (*3)

GitReview Success Demo, (*4)

Requirements

  • PHP 7.1 and greater

Usage

For a composer managed project you can simply run the following ..., (*5)

$ composer require mcampbell508/git-review

Hooks can then be installed like so ..., (*6)

$ vendor/bin/git-review.php hook:install vendor/mcampbell508/git-review/hooks/example-pre-commit.php .git/hooks/pre-commit

Otherwise, if you don't use composer ..., (*7)

$ 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

Global Installation and Usage

The hooks can also be used for any project if you install git-review globally:, (*8)

$ composer g require mcampbell508/git-review

Then, just install the hooks as you would normally but reference the global installation path:, (*9)

$ git-review.php hook:install ~/.composer/vendor/mcampbell508/git-review/hooks/git-review-commit-msg.php .git/hooks/commit-msg

This assumes you have set up global composer paths., (*10)

Example Hooks

Static Review can be used for both files and commit message review. Below are basic hooks for each., (*11)

For Files

#!/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);

For Commit Messages

#!/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);

Example Review For Files

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);
        }
    }
}

Example Review For Messages

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);
        }
    }
}

Unit Tests

See vagrantup.com and phpunit.de., (*12)

$ git clone https://github.com/mcampbell508/git-review.git
$ cd git-review/
$ vagrant up
$ vagrant ssh
...
$ cd /srv
$ composer update
$ composer test

Licence

The content of this library is released under the MIT License by Samuel Parkinson., (*13)

The Versions

17/02 2017

dev-upgrade-dependency-versions

dev-upgrade-dependency-versions

An extendable framework for version control hooks.

  Sources   Download

MIT

The Requires

 

The Development Requires

11/02 2017

dev-issue-tracking-review

dev-issue-tracking-review

An extendable framework for version control hooks.

  Sources   Download

MIT

The Requires

 

The Development Requires

06/08 2014

1.1.4

1.1.4.0

An extendable framework for version control hooks.

  Sources   Download

MIT

The Requires

 

The Development Requires

04/08 2014

1.1.3

1.1.3.0

An extendable framework for version control hooks.

  Sources   Download

MIT

The Requires

 

The Development Requires

30/07 2014

1.1.2

1.1.2.0

An extendable framework for version control hooks.

  Sources   Download

MIT

The Requires

 

The Development Requires

26/07 2014

1.1.1

1.1.1.0

An extendable framework for version control hooks.

  Sources   Download

MIT

The Requires

 

The Development Requires

26/07 2014
20/07 2014

1.0.1

1.0.1.0

Modular pre-commit hook for static analysis of modified files.

  Sources   Download

MIT

The Requires

 

The Development Requires