├── src ├── Providers │ ├── CommandServiceProvider.php │ └── GitCommitCheckerServiceProvider.php └── Commands │ ├── PreCommitHookCommand.php │ └── InstallCommand.php ├── CHANGELOG.md ├── LICENSE ├── composer.json ├── config └── git-commit-checker.php ├── README.md ├── resources └── views │ └── summary.blade.php └── CONTRIBUTING.md /src/Providers/CommandServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 14 | return; 15 | } 16 | 17 | $this->commands([ 18 | InstallCommand::class, 19 | PreCommitHookCommand::class, 20 | ]); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Providers/GitCommitCheckerServiceProvider.php: -------------------------------------------------------------------------------- 1 | mergeConfigFrom( 12 | __DIR__ . '/../../config/git-commit-checker.php', 13 | 'git-commit-checker' 14 | ); 15 | } 16 | public function boot() 17 | { 18 | $this->app->register(CommandServiceProvider::class); 19 | 20 | $this->loadViewsFrom(__DIR__ . '/../../resources/views', 'git-commit-checker'); 21 | 22 | if ($this->app->runningInConsole()) { 23 | $this->publishes([ 24 | __DIR__ . '/../../config/git-commit-checker.php' => config_path('git-commit-checker.php'), 25 | ], 'git-commit-checker-config'); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `botble/git-commit-checker` will be documented in this file. 4 | 5 | ## 2.1.2 - 2023-01-11 6 | 7 | - Bump version `laravel/pint` 8 | 9 | ## 2.1.1 - 2023-01-06 10 | 11 | - Fix various bugs 12 | 13 | ## 2.1.0 - 2023-01-06 14 | 15 | - Fix output render on Windows platform (https://github.com/botble/git-commit-checker/pull/10) 16 | 17 | ## 2.0.1 - 2023-01-05 18 | 19 | - Add warning message "Run command to see coding standard detail issues" 20 | - Update version icon from README.md 21 | - Remove verbose when run `pint --test` 22 | 23 | ## 2.0.0 - 2023-01-05 24 | 25 | - Drop support PHP 7.x, Laravel <= 8.x 26 | - Replace PHP Code Sniffer, PHPLint to Laravel Pint 27 | - Rename command `git:install-hooks` to `git-commit-checker:install` 28 | - Rename command `git:pre-commit-hook` to `git-commit-checker:pre-commit-hook` 29 | - Remove command `git:create-phpcs` 30 | - Remove TravisCI, StyleCI, Scrutinizer 31 | - Remove unnecessary files 32 | 33 | ## 1.0.0 - 2019-08-26 34 | 35 | - First release. 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 Sang Nguyen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "botble/git-commit-checker", 3 | "description": "Check coding standard & code syntax with Git pre-commit hook.", 4 | "type": "package", 5 | "license": "MIT", 6 | "keywords": [ 7 | "botble", 8 | "botble cms", 9 | "botble platform", 10 | "botble git commit checker" 11 | ], 12 | "homepage": "https://botble.com", 13 | "authors": [ 14 | { 15 | "name": "Sang Nguyen", 16 | "email": "sangnguyenplus@gmail.com" 17 | } 18 | ], 19 | "autoload": { 20 | "psr-4": { 21 | "Botble\\GitCommitChecker\\": "src" 22 | } 23 | }, 24 | "autoload-dev": { 25 | "psr-4": { 26 | "Botble\\GitCommitChecker\\Tests\\": "tests" 27 | } 28 | }, 29 | "require": { 30 | "php": "^8.0|^8.1|^8.2|^8.3", 31 | "laravel/framework": ">=9.32", 32 | "laravel/pint": "^1.5" 33 | }, 34 | "require-dev": { 35 | "phpunit/phpunit": "^9.0|^10.0|^11.0" 36 | }, 37 | "extra": { 38 | "laravel": { 39 | "providers": [ 40 | "Botble\\GitCommitChecker\\Providers\\GitCommitCheckerServiceProvider" 41 | ] 42 | } 43 | }, 44 | "config": { 45 | "sort-packages": true 46 | }, 47 | "minimum-stability": "stable" 48 | } 49 | -------------------------------------------------------------------------------- /config/git-commit-checker.php: -------------------------------------------------------------------------------- 1 | env('GIT_COMMIT_CHECKER_ENABLED', true), 7 | 8 | 'hooks' => [ 9 | 'pre-commit' => PreCommitHookCommand::class, 10 | ], 11 | 12 | 'pint' => [ 13 | 'presets' => [ 14 | 'laravel' => 'Laravel (Default)', 15 | 'symfony' => 'Symfony', 16 | 'psr12' => 'PSR-12', 17 | 'recommended' => 'Recommended (PSR-12 Extended)', 18 | ], 19 | 20 | 'recommended_preset' => [ 21 | 'preset' => 'psr12', 22 | 'rules' => [ 23 | 'array_syntax' => ['syntax' => 'short'], 24 | 'binary_operator_spaces' => [ 25 | 'default' => 'single_space', 26 | 'operators' => [ 27 | '=' => null, 28 | ], 29 | ], 30 | 'blank_line_before_statement' => [ 31 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 32 | ], 33 | 'concat_space' => [ 34 | 'spacing' => 'one', 35 | ], 36 | 'function_typehint_space' => true, 37 | 'native_function_casing' => true, 38 | 'no_extra_blank_lines' => true, 39 | 'no_leading_namespace_whitespace' => true, 40 | 'no_spaces_around_offset' => true, 41 | 'no_unused_imports' => true, 42 | 'not_operator_with_successor_space' => true, 43 | 'object_operator_without_whitespace' => true, 44 | 'single_quote' => true, 45 | 'trailing_comma_in_multiline' => true, 46 | 'unary_operator_spaces' => true, 47 | 'whitespace_after_comma_in_array' => true, 48 | ], 49 | ], 50 | ], 51 | ]; 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel GIT Commit Checker 2 | 3 |

4 | Overview Git Commit Checker 5 |

6 | 7 |

8 | Latest Version 9 | Software License 10 | Total Downloads 11 | Maintainability 12 |

13 | 14 | ## Requirement 15 | 16 | - Laravel 9.32 or later 17 | - If you're using Laravel 8.0 or earlier, please use version 1.x 18 | 19 | ## Installation 20 | 21 | You can install the package via composer: 22 | 23 | ```shell 24 | composer require botble/git-commit-checker 25 | ``` 26 | 27 | Publish the configuration: 28 | 29 | ```bash 30 | php artisan vendor:publish --tag=git-commit-checker-config 31 | ``` 32 | 33 | ### Install GIT hooks 34 | 35 | Run this command to install: 36 | 37 | ```shell 38 | php artisan git-commit-checker:install 39 | ``` 40 | 41 | Run test manually (made sure you've added all changed files to git stage): 42 | 43 | ```shell 44 | php artisan git-commit-checker:pre-commit-hook 45 | ``` 46 | 47 | ### Changelog 48 | 49 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 50 | 51 | ## Contributing 52 | 53 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 54 | 55 | ### Security 56 | 57 | If you discover any security related issues, please email contact@botble.com instead of using the issue tracker. 58 | 59 | ## Credits 60 | 61 | - [Sang Nguyen](https://github.com/sangnguyenplus) 62 | - [All Contributors](../../contributors) 63 | 64 | ## License 65 | 66 | The MIT License (MIT). Please see [License File](LICENSE) for more information. 67 | -------------------------------------------------------------------------------- /resources/views/summary.blade.php: -------------------------------------------------------------------------------- 1 |
2 | @if(! $isSuccessful) 3 | @foreach($result['files'] as $file) 4 |
5 | File 6 | {{ ltrim(str_replace(base_path(), '', $file['name']), DIRECTORY_SEPARATOR) }} 7 |
8 | 9 |
10 | Rules 11 | 12 | 13 | @foreach($file['appliedFixers'] as $fixer) 14 | {{ $fixer . ($loop->last ? '' : ', ') }} 15 | @endforeach 16 | 17 |
18 | 19 |
20 | 21 |
22 | @endforeach 23 | 24 |
25 | @endif 26 | 27 |
28 | 29 | Checked {{ count($result['files']) }} files in {{ $result['time']['total'] ?? 0 }} seconds. 30 | Using memory {{ $result['memory'] }} MB. 31 | 32 |
33 | 34 | @if(! $isSuccessful) 35 |
36 | Warn 37 | 38 | Run ./vendor/bin/pint --dirty --test -v to see coding standard detail issues. 39 | 40 |
41 |
42 | Warn 43 | 44 | Run ./vendor/bin/pint --dirty to fix coding standard issues. 45 | 46 |
47 | @else 48 |
49 | Success 50 | Your code is perfect, no syntax error found! 51 |
52 | @endif 53 |
54 | -------------------------------------------------------------------------------- /src/Commands/PreCommitHookCommand.php: -------------------------------------------------------------------------------- 1 | laravel['config']->get('git-commit-checker.enabled')) { 18 | $this->components->info('git-commit-hook is disabled. Skipped.'); 19 | 20 | return self::SUCCESS; 21 | } 22 | 23 | $uncommittedFiles = $this->uncommittedFiles(); 24 | 25 | if (! count($uncommittedFiles)) { 26 | $this->components->info('No files to check coding standard. Skipped.'); 27 | 28 | return self::SUCCESS; 29 | } 30 | 31 | $this->components->info('Running Laravel Pint...'); 32 | 33 | $command = [ 34 | $this->laravel->basePath('vendor/bin/pint'), 35 | '--test', 36 | '--format=json', 37 | '-v', 38 | ]; 39 | 40 | $command = array_merge($command, $uncommittedFiles); 41 | 42 | $process = $this->getProcess($command); 43 | 44 | $process->run(); 45 | 46 | if ($process->getOutput()) { 47 | $result = json_decode($process->getOutput(), true); 48 | 49 | render( 50 | view('git-commit-checker::summary', [ 51 | 'result' => $result, 52 | 'isSuccessful' => $process->isSuccessful(), 53 | ]) 54 | ); 55 | 56 | if (! $process->isSuccessful()) { 57 | return self::FAILURE; 58 | } 59 | } 60 | 61 | return self::SUCCESS; 62 | } 63 | 64 | protected function getProcess(array $command): Process 65 | { 66 | return (new Process($command, $this->laravel->basePath()))->setTimeout(null); 67 | } 68 | 69 | protected function uncommittedFiles(): array 70 | { 71 | $process = tap(new Process(['git', 'status', '--short', '--', '*.php']))->run(); 72 | 73 | if (! $process->isSuccessful()) { 74 | $this->components->error('git-commit-checker is only available when using Git.'); 75 | abort(1); 76 | } 77 | 78 | return collect(preg_split('/\R+/', $process->getOutput(), flags: PREG_SPLIT_NO_EMPTY)) 79 | ->mapWithKeys(fn ($file) => [substr($file, 3) => trim(substr($file, 0, 3))]) 80 | ->reject(fn ($status) => $status === 'D') 81 | ->map(fn ($status, $file) => $status === 'R' ? Str::after($file, ' -> ') : $file) 82 | ->map(fn ($file) => $this->laravel->basePath($file)) 83 | ->values() 84 | ->all(); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /src/Commands/InstallCommand.php: -------------------------------------------------------------------------------- 1 | laravel->isLocal()) { 19 | $this->components->error('git-commit-checker is only available in local environment.'); 20 | 21 | return self::FAILURE; 22 | } 23 | 24 | if (! $this->laravel['files']->isDirectory($this->laravel->basePath('.git'))) { 25 | $this->components->error('git-commit-checker is only available when using Git.'); 26 | 27 | return self::FAILURE; 28 | } 29 | 30 | foreach ($this->laravel['config']->get('git-commit-checker.hooks') as $hook => $command) { 31 | $this->install($hook, $command) 32 | ? $this->components->info("Hook [$hook] is installed successfully.") 33 | : $this->components->error("Unable to install hook [$hook]."); 34 | } 35 | 36 | $pintConfigFilePath = $this->laravel->basePath('pint.json'); 37 | 38 | if ($this->laravel['files']->exists($pintConfigFilePath)) { 39 | if ($this->components->confirm('A pint.json exists. Do you want to overwrite this file?')) { 40 | $this->generatePintConfiguration($pintConfigFilePath); 41 | } 42 | 43 | return self::SUCCESS; 44 | } 45 | 46 | if ($this->components->confirm('A pint.json does not exists. Do you want to create this file?')) { 47 | $this->generatePintConfiguration($pintConfigFilePath); 48 | } 49 | 50 | return self::SUCCESS; 51 | } 52 | 53 | protected function install(string $hook, string $class): bool 54 | { 55 | if (! class_exists($class)) { 56 | $this->components->error("Class [$class] not found."); 57 | abort(1); 58 | } 59 | 60 | $command = new $class(); 61 | 62 | if ($command instanceof Command === false) { 63 | $this->components->error("Class [$class] is not instance of " . Command::class . '.'); 64 | abort(1); 65 | } 66 | 67 | $script = $this->generateHookScript($command->getName()); 68 | 69 | $path = $this->laravel->basePath('.git/hooks/' . $hook); 70 | $relativePath = ltrim(str_replace($this->laravel->basePath(), '', $path), DIRECTORY_SEPARATOR); 71 | 72 | if ( 73 | $this->laravel['files']->exists($path) && 74 | ! $this->confirmToProceed($relativePath . ' already exists, do you want to overwrite it?', true) 75 | ) { 76 | return false; 77 | } 78 | 79 | return $this->writeHookScript($path, $script); 80 | } 81 | 82 | protected function generateHookScript(string $signature): string 83 | { 84 | return sprintf("#!/bin/sh\n\n%s\n", Application::formatCommandString($signature)); 85 | } 86 | 87 | protected function generatePintConfiguration(string $path): void 88 | { 89 | $presets = $this->laravel['config']->get('git-commit-checker.pint.presets', []); 90 | 91 | if (empty($presets)) { 92 | $this->components->error('Do not found a list of supported presets'); 93 | abort(1); 94 | } 95 | 96 | $standard = $this->components->choice('Which standard you want to use?', array_values($presets), 0); 97 | 98 | $preset = array_flip($presets)[$standard]; 99 | 100 | if (! $this->laravel['files']->put( 101 | $path, 102 | json_encode( 103 | $preset !== 'recommended' 104 | ? ['preset' => $preset] 105 | : $this->laravel['config']->get('git-commit-checker.pint.recommended_preset'), 106 | JSON_PRETTY_PRINT 107 | ) . PHP_EOL 108 | )) { 109 | $this->components->error('Unable to write ' . $path); 110 | abort(1); 111 | } 112 | 113 | $this->components->info( 114 | "Created $path using $presets[$preset] preset successfully" 115 | ); 116 | } 117 | 118 | protected function writeHookScript(string $path, string $script): bool 119 | { 120 | if (! $this->laravel['files']->put($path, $script)) { 121 | return false; 122 | } 123 | 124 | if (! $this->laravel['files']->chmod($path, 0755)) { 125 | return false; 126 | } 127 | 128 | return true; 129 | } 130 | 131 | protected function configure(): void 132 | { 133 | $this->addOption('force', 'f', InputOption::VALUE_NONE, 'Force the operation to overwrite existing git hook files'); 134 | } 135 | } 136 | --------------------------------------------------------------------------------