symplify/git-wrapper

该包已被弃用,不再维护。作者建议使用gitonomy/gitlib包。

PHP包装Git命令行工具。

This package is auto-updated.

Last update: 2022-05-29 14:23:41 UTC


README

Total Downloads

Git Wrapper提供了可读的API,用于抽象PHP进程中执行Git命令的挑战。

  • 它基于Symfony\Process来执行Git命令,具有跨平台支持和PHP的最佳实践技术。
  • 该库还提供了一个SSH包装脚本和API方法,允许开发者通过使用StackOverflow中的技术轻松指定除默认密钥之外的密钥。
  • 最后,预计将在包含工作副本的目录中执行各种命令。该库将此处理得非常透明,因此开发者无需考虑。

安装

composer require symplify/git-wrapper

使用

use Symplify\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(__DIR__ . '/path/to/private/key');

// Clone a repo into `/path/to/working/copy`, get a working copy object
$git = $gitWrapper->cloneRepository('git://github.com/symplify/git-wrapper.git', __DIR__ . '/path/to/working/copy');

// Create a file in the working copy
touch(__DIR__ . '/path/to/working/copy/text.txt');

// Add it, commit it, and push the change
$git->add(__DIR__ . '/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命令,例如checkoutpush等。$arg*参数是可变数量的参数,就像传递给Git命令行工具一样。$options是可选的命令行选项数组,格式如下

$options = [
    'verbose' => true,
    // Passes the "--verbose" flag.
    't' => 'my-branch',
    // Passes the "-t my-branch" option.
];

日志记录

使用与PSR-3兼容的记录器,如Monolog,来记录执行的命令。

use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Symplify\GitWrapper\EventSubscriber\GitLoggerEventSubscriber;

// 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/symplify/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', __DIR__ . '/path/to/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);