Git 预接收钩子启动 PHP CodeSniffer

发布于 2024-08-26 19:45:01 字数 1536 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寂寞花火° 2024-09-02 19:45:01

也许这为您指明了正确的方向:(原文来自:http://www.squatlabs .de/versionierung/arbeiten-git-hooks(德语)

#!/usr/bin/php
<?php

$output = array();
$rc     = 0;
exec('git rev-parse --verify HEAD 2> /dev/null', $output, $rc);
if ($rc == 0)  $against = 'HEAD';
else           $against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';

exec('git diff-index --cached --name-only '. $against, $output);

$needle            = '/(\.php|\.module|\.install)$/';
$exit_status = 0;

foreach ($output as $file) {
        if (!preg_match($needle, $file)) {
                // only check php files
                continue;
        }

        $lint_output = array();
        $rc              = 0;
        exec('php -l '. escapeshellarg($file), $lint_output, $rc);
        if ($rc == 0) {
                continue;
        }
        # echo implode("\n", $lint_output), "\n";
        $exit_status = 1;
}

exit($exit_status);

您必须编辑 exec 行 exec('php -l... 以指向您的codesniffer 安装。

Maybe this point you in the right direction: (Orginal from: http://www.squatlabs.de/versionierung/arbeiten-git-hooks in German)

#!/usr/bin/php
<?php

$output = array();
$rc     = 0;
exec('git rev-parse --verify HEAD 2> /dev/null', $output, $rc);
if ($rc == 0)  $against = 'HEAD';
else           $against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';

exec('git diff-index --cached --name-only '. $against, $output);

$needle            = '/(\.php|\.module|\.install)$/';
$exit_status = 0;

foreach ($output as $file) {
        if (!preg_match($needle, $file)) {
                // only check php files
                continue;
        }

        $lint_output = array();
        $rc              = 0;
        exec('php -l '. escapeshellarg($file), $lint_output, $rc);
        if ($rc == 0) {
                continue;
        }
        # echo implode("\n", $lint_output), "\n";
        $exit_status = 1;
}

exit($exit_status);

You will have to edit the exec line exec('php -l... to point to your codesniffer installation.

五里雾 2024-09-02 19:45:01

好的,我找到了解决方案:)

这是概念验证代码:)用于预接收挂钩:

#!/bin/bash

while read old_sha1 new_sha1 refname; do
    echo "ns: " $new_sha1;
    echo "os: " $old_sha1;

    echo "----"

    git ls-tree -r $new_sha1 | cut -f 3 -d ' ' | cut -f 1 | while read file; do
        git cat-file blob $file
    done; 

    echo "----"

done

exit 1

此示例代码只会打印远程存储库收到的 blob,但这足以让需要类似内容的人继续(我希望)。

您可以将每个 blob 放入某个临时文件中,在该文件上运行您需要的任何内容,删除该文件等等...

Ok I found the solution :)

This is proof of concept code :) for pre-receive hook:

#!/bin/bash

while read old_sha1 new_sha1 refname; do
    echo "ns: " $new_sha1;
    echo "os: " $old_sha1;

    echo "----"

    git ls-tree -r $new_sha1 | cut -f 3 -d ' ' | cut -f 1 | while read file; do
        git cat-file blob $file
    done; 

    echo "----"

done

exit 1

This example code will only print blobs received by remote repository but it's enough to get someone needing something like that going (I hope).

You can put every blob in some temporary file run whatever you need on this file delete the file and so on...

源来凯始玺欢你 2024-09-02 19:45:01

我开发了一个基于 PHPCodeSniffer 的预接收 git hook,用于检查 PHP、JavaScript 和 CSS 文件的代码样式。

我的脚本可以从 Github 获取:
https://github.com/blueicefield/PHP_CodeSniffer_GIT_Hook

I developed a pre-receive git hook based on PHPCodeSniffer to check the code styling of PHP, JavaScript and CSS files.

My script is available from Github :
https://github.com/blueicefield/PHP_CodeSniffer_GIT_Hook

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文