cpliakas / git-wrapper
Requires
- php: >=7.3
- nette/utils: ^3.0
- symfony/event-dispatcher: ^4.4|^5.1
- symfony/process: ^4.4|^5.1
Requires (Dev)
- ondram/ci-detector: ^3.5
- phpstan/phpstan: ^0.12.64
- phpstan/phpstan-phpunit: ^0.12
- phpunit/phpunit: ^9.5
- psr/log: ^1.1
- rector/rector: ^0.9
- symfony/filesystem: ^4.4|^5.1
- symplify/changelog-linker: ^9.0
- symplify/easy-coding-standard: ^9.0
- symplify/phpstan-extensions: ^9.0
- symplify/phpstan-rules: ^9.0
- tracy/tracy: ^2.7
Suggests
- monolog/monolog: Enables logging of executed git commands
This package is auto-updated.
Last update: 2021-02-11 11:47:37 UTC
README
Git Wrapper提供了一种可读的API,它抽象了在PHP进程中执行Git命令的挑战。
- 它基于
Symfony\Process
执行Git命令,具有跨平台支持,并使用PHP中可用的最佳技术。 - 此库还提供了一个SSH包装脚本和API方法,使开发者可以使用StackOverflow中的技术轻松指定除默认以外的私钥。
- 最后,预计将在包含工作副本的目录中执行各种命令。此库可以透明地处理,因此开发者无需考虑。
安装
composer require cpliakas/git-wrapper
用法
use GitWrapper\GitWrapper; // Initialize the library. If the path to the Git binary is not passed as // the first argument when instantiating GitWrapper, it is auto-discovered. require_once __DIR__ . '/vendor/autoload.php'; $gitWrapper = new GitWrapper(); // Optionally specify a private key other than one of the defaults $gitWrapper->setPrivateKey('/path/to/private/key'); // Clone a repo into `/path/to/working/copy`, get a working copy object $git = $gitWrapper->cloneRepository('git://github.com/cpliakas/git-wrapper.git', '/path/to/working/copy'); // Create a file in the working copy touch('/path/to/working/copy/text.txt'); // Add it, commit it, and push the change $git->add('test.txt'); $git->commit('Added the test.txt file as per the examples.'); $git->push(); // Render the output for operation echo $git->push(); // Stream output of subsequent Git commands in real time to STDOUT and STDERR. $gitWrapper->streamOutput(); // Execute an arbitrary git command. // The following is synonymous with `git config -l` $gitWrapper->git('config -l');
所有命令方法遵循以下范例
$git->command($arg1, $arg2, ..., $options);
将command
替换为正在执行的Git命令,例如checkout
、push
等。 $arg*
参数是作为Git命令行工具传递的参数的可变数量的参数。 $options
是可选的命令行选项数组,其格式如下
$options = [ 'verbose' => true, // Passes the "--verbose" flag. 't' => 'my-branch', // Passes the "-t my-branch" option. ];
日志记录
使用与PSR-3兼容的记录器(如Monolog)一起使用的记录器监听器来记录执行的命令。
<?php use GitWrapper\EventSubscriber\GitLoggerEventSubscriber; use Monolog\Logger; use Monolog\Handler\StreamHandler; // Log to a file named "git.log" $logger = new Logger('git'); $logger->pushHandler(new StreamHandler('git.log', Logger::DEBUG)); // Instantiate the subscriber, add the logger to it, and register it. $gitWrapper->addLoggerEventSubscriber(new GitLoggerEventSubscriber($logger)); $git = $gitWrapper->cloneRepository('git://github.com/cpliakas/git-wrapper.git', '/path/to/working/copy'); // The "git.log" file now has info about the command that was executed above.
注意事项
有几个此库无法解决的问题,但可能会阻止通过PHP运行Git的成功实施。
缺失的HOME环境变量
有时,由PHP生成的Git进程未设置HOME
环境变量。这将导致许多Git操作失败。建议将HOME
环境变量设置在Web服务器具有写入访问权限的文档根之外的路径。请注意,此环境变量仅针对运行Git的进程设置,而不是生成它的PHP进程。
$gitWrapper->setEnvVar('HOME', '/path/to/a/private/writable/dir');
存储必须是持久的,因为~/.gitconfig
文件将写入此位置。有关为什么这很重要的原因,请参阅以下“注意事项”。
缺失的标识符和配置
许多仓库要求指定名称和电子邮件地址。这些数据可以通过在命令行中运行git config [name] [value]
来设置,配置通常存储在~/.gitconfig文件
中。然而,当通过PHP执行Git时,进程可能具有与通过命令行运行Git的用户不同的主目录。因此,不会向仓库发送任何身份信息,它可能会抛出错误。
// Set configuration options globally. $gitWrapper->git('config --global user.name "User name"'); $gitWrapper->git('config --global user.email user@example.com'); // Set configuration options per repository. $git->config('user.name', 'User name'); $git->config('user.email', 'user@example.com');
对无变更仓库的提交
在没有任何变更的仓库上运行git commit
会失败并抛出异常。为了防止这种情况,请检查如下变更:
if ($git->hasChanges()) { $git->commit('Committed the changes.'); }
Git_SSH包装脚本的权限
在检出时,bin/git-ssh-wrapper.sh脚本应该是可执行的。如果不是,如果指定了非默认的私钥,git命令将失败。
$ chmod +x ./bin/git-ssh-wrapper.sh
超时
默认超时时间为60秒。在使用大项目的clone功能或缓慢的互联网时,这可能会引起“问题”。
$this->gitWrapper = new GitWrapper(); $this->gitWrapper->setTimeout(120);