graham-campbell / git-wrapper
Git 命令行工具的 PHP 封装。
Requires
- php: ^7.4.15 || ^8.0.2
- symfony/event-dispatcher: ^4.4 || ^5.4 || ^6.0 || ^7.0
- symfony/process: ^4.4 || ^5.4 || ^6.0 || ^7.0
Requires (Dev)
- ondram/ci-detector: ^4.1
- phpunit/phpunit: ^9.6.15 || ^10.5.1
- psr/log: ^1.1 || ^2.0 || ^3.0
- symfony/filesystem: ^4.4 || ^5.4 || ^6.0 || ^7.0
Suggests
- monolog/monolog: Enables logging of executed git commands
README
这是由 cpliakas/git-wrapper(作者:Chris Pliakas 和 Tomas Votruba)创建的分支。我们为您提供了一个可读的 API,用于抽象化在 PHP 进程中执行 Git 命令的挑战。
- 它基于
Symfony\Process
来执行 Git 命令,具有 跨平台支持,并使用 PHP 可用的最佳技术。 - 此外,此库还提供 SSH 封装脚本和 API 方法,让开发者可以通过 StackOverflow 中的技术轻松指定除默认以外的私钥。
- 最后,预期将在包含工作副本的目录中执行各种命令。库会 透明地处理 此过程,因此开发者无需考虑。
安装
$ composer require "graham-campbell/git-wrapper:^1.1"
使用方法
<?php use GrahamCampbell\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. $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 GrahamCampbell\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秒。在您使用大型项目的克隆功能或缓慢的互联网时,这可能会引起“问题”。
$this->gitWrapper = new GitWrapper(); $this->gitWrapper->setTimeout(120);
许可证
本项目遵循MIT许可证(MIT)进行授权。