定制 wcm/git-php-hooks 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wcm/git-php-hooks

Composer 安装命令:

composer require wcm/git-php-hooks

包简介

Write git hooks with PHP, organize them on a per-project base and automatically add them

README 文档

README

Latest Stable Version Latest Git Release Total Downloads License GitHub Stars

Git Logo

Git PHP Hooks

Write your git hooks in PHP, organize them on a per project base and attach them automatically.

Git Hooks

Hooks are little scripts you can place in $GIT_DIR/hooks directory to trigger action at certain points.

There're two types of git hooks:

  1. pre-push (runs client side)
  2. post-push (runs server side)

For more info on Git Hooks, please take a look at the official docs - they are quite good.

How to

It's really easy:

  1. Add a folder to your project/repository. The name doesn't matter, as you have to specify it when triggering GitPHPHooks. The name in the following example is 'project-hooks'. (Hint: Not having a name allows you to customize and organize it as you like. It also allows git cloneing into a project specific directory.)
  2. Open your .git/hooks directory and add a new Git hook file. For example: pre-commit (without file extension).
  3. Add a new PHP file to the newly created custom Git hooks folder (again, 'project-hooks' in the example) that performs the task you want.

That's it.

All your Git hooks (inside .git/hooks) will have the same contents - only the target folder ('project-hooks') name will (maybe) differ.

#!/usr/bin/env php
<?php

include 'vendor/wcm/git-php-hooks/GitHooksLoader.php';
new \GitHooksLoader( __FILE__, 'project-hooks' );

Explanation:

  • The first line is a hashbang to specify that we actually have a PHP file on the Command Line.
  • The 1st argument for \GitHooksLoader() is the name of the current file to make the current hook identifyable for GitPHPHooks.
  • The 2nd argument is the target location where your custom, pre-project Git PHP hook files for the current task are located.

Naming convention

There's a naming convention that you must follow to properly attach PHP files to Git hooks. Sorting files is also done by file name.

  1. If a Git hook name is found in the file name, it will get attached to this specific hook and executed automatically. Example: pre-commit_
  2. If one of your hooking PHP files has a number attached, it will get added with this priority. Example: _10 If it ain't got any int in the file name, it will get skipped. This is useful to temporarily disable files if you are testing the order or a new hook.
  3. The name in between the Git hook name and the priority is just an identifiyer for yourself. Example: PHPUnit

Examples (and ready-to-use tasks)

Before jumping on examples, I suggest that you simply take a look at the GitPHPHooks Library repo. You will find a PHPLint and a PHP Mess Detector task and some others (hint: I happily accept pull requests!).

A real world scenario (simplified version of the task that is available in the linked library)

We want to run PHPLint before we commit

Add a new file named pre-commit in your .git/hooks directory. Then add a new directory in the root folder of your project/repository, named i.e. project-hooks. In there, add a new PHP file named pre-commit_lint_10.php. This file will automatically get added to your pre-commit hook where you called the \GitHooksLoader() like shown above. It will get added with a priority of 10. Then just put the following contents in your new file:

#!/usr/bin/env php
<?php
$output = shell_exec( 'php -l' );
echo $output;
if ( $output === 1 )
	exit 1;

Of course, above code is a very poor example. For a more detailed one, please refer to the library linked above. The GitPHPHooks Library runs two real world examples. To use PHP Mess Detector and PHPLint, I can just suggest using the library as those are currently built in. Again: If you have a custom one and want to share, just send a Pull Request.

Grunt integration

It can easily be integrated with grunt via grunt-githooks, originally written by @rhumaric.

Setup your grunt-githooks task like this:

php : {
	options      : {
		hashbang    : '#!/usr/bin/env php',
		startMarker : '\n<?php',
		template    : './templates/git-php-hooks.tmpl.hb'
	},
	'pre-push'   : 'none'
}

Then just add your hooked tasks to your project and use the following template:

include 'vendor/wcm/git-php-hooks/GitHooksLoader.php';
new \GitHooksLoader( __FILE__, 'vendor/wcm/git-php-hooks-library/src' );

This example is assuming that you are using the GitPHPHooksLibrary. The template in this case would be located inside a templates directory in the root folder of your project and be named git-php-hooks.tmpl.hb. It's important to set the hooks names value to none as GitPHPHooks doesn't need a task name as it identifies tasks by the filename by itself.

Install

Add the repo to your stack. You can use Composer (w/o Satis as it's added to Packagist). Simply add

"wcm/git-php-hooks": "^1.0"

to your composer.json file. GitHub has a service hook added to this repo to auto-update whenever this repo is updated. The ^1.0 version number will bring you all patches without breaking anything.

To add the repository to the dev-part of the stack in composer.json, the following command can be typed in the prompt (assuming composer is in your PATH or aliased ).

composer require --dev --prefer-dist -- wcm/git-php-hooks

wcm/git-php-hooks suggests installing a library of pre made hooks and tasks, wcm/git-php-hooks-library . To add this with composer the following can be used:

composer require --dev --prefer-dist -- wcm/git-php-hooks-library

wcm/git-php-hooks 适用场景与选型建议

wcm/git-php-hooks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 42.38k 次下载、GitHub Stars 达 64, 最近一次更新时间为 2014 年 07 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「git」 「hooks」 「git hook」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 wcm/git-php-hooks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 wcm/git-php-hooks 我们能提供哪些服务?
定制开发 / 二次开发

基于 wcm/git-php-hooks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 42.38k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 64
  • 点击次数: 18
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 64
  • Watchers: 5
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-07-18