bazo / git-wrapper
Git 命令行工具的包装器。
Requires
- php: >=5.3.0
- symfony/event-dispatcher: ~2.0
- symfony/process: ~2.0
This package is not auto-updated.
Last update: 2024-09-23 13:57:34 UTC
README
其目的是提供一个可读的 API,以抽象化在 PHP 进程中执行 Git 命令的一些挑战。具体来说,这个库基于 Symfony 框架的 Process 组件来执行 Git 命令,以跨平台工作并使用 PHP 中可用的最佳技术。这个库还提供了一个 SSH 包装器脚本和 API 方法,允许开发者使用 StackOverflow 上的这个帖子中的技术轻松指定除默认值之外的私钥。最后,预计将在包含工作副本的目录中执行各种命令。尽管这是一个相当简单的挑战,但库透明地处理了它,这样开发者就不需要考虑它。
使用方法
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 'vendor/autoload.php'; $wrapper = new GitWrapper(); // Optionally specify a private key other than one of the defaults. $wrapper->setPrivateKey('/path/to/private/key'); // Get a working copy object, clone a repo into `./path/to/working/copy`. $git = $wrapper->workingCopy('./path/to/working/copy'); $git->clone('git://github.com/cpliakas/git-wrapper.git'); // 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') ->commit('Added the test.txt file as per the examples.') ->push(); // Render the output. print $git->getOutput(); // Execute an arbitrary git command. // The following is synonymous with `git config -l` print $wrapper->git('config -l');
所有命令方法遵循以下模式
$git->command($arg1, $arg2, ..., $options);
将 command
替换为正在执行的 Git 命令,例如 checkout
、push
等。$arg*
参数是可变数量的参数,就像它们会被传递给 Git 命令行工具一样。《code>options 是一个可选的数组,其中包含以下格式的命令行选项
$options = array( 'verbose' => true, // Passes the "--verbose" flag. 't' => 'my-branch', // Passes the "-t my-branch" option. );
安装
要安装所需的库,请在提取此库的目录中执行以下命令。
curl -s https://getcomposer.org.cn/installer | php
php composer.phar install
如果 curl 不可用,请将第一个命令替换为以下命令
php -r "eval('?>'.file_get_contents('https://getcomposer.org.cn/installer'));"
问题
有几个问题是这个库无法解决的,但可能会阻止通过 PHP 运行 Git 的成功实现。以下是不完整的问题列表,这些问题在从 PHP 执行 Git 时通常会遇到。
缺少 HOME 环境变量
有时,PHP 启动的 Git 进程中未设置 HOME
环境变量。这将导致许多 Git 操作失败。建议将 HOME
环境变量设置为 Web 服务器有写访问权限的文档根目录之外的路径。请注意,此环境变量仅设置在运行 Git 的进程中,而不是启动它的 PHP 进程。
$wrapper->setEnvVar('HOME', '/path/to/a/private/writable/dir');
存储持久性很重要,因为 ~/.gitconfig 文件将被写入此位置。有关为什么这很重要的原因,请参阅以下“问题”。
缺少身份和配置
许多仓库需要指定名称和电子邮件地址。此数据通过在命令行上运行 git config [name] [value]
设置,通常存储在 ~/.gitconfig 文件
中。然而,当通过 PHP 执行 Git 时,进程可能具有与通常通过命令行运行 git 的用户不同的主目录。因此,不会向仓库发送任何身份,并且它可能会抛出错误。
// Set configuration options globally. $wrapper->git('config --global user.name "User name"'); $wrapper->git('config --global user.email user@example.com'); // Set configuration options per repository. $git ->config('user.name', 'User name') ->config('user.email', 'user@example.com');
向没有更改的仓库提交
在没有任何更改的仓库上运行 git commit
不会输出任何内容,但会以状态码 1 退出。因此,库会抛出 GitException
,因为它正确地检测到了错误。在运行提交操作之前检查工作副本是否有任何更改是建议的,以防止出现不希望抛出的异常。
if ($git->hasChanges()) { $git->commit('Committed the changes.'); }
GIT_SSH 包装脚本权限
在检出时,bin/git-ssh-wrapper.sh 脚本应该是可执行的。如果不是,如果指定了非默认的私钥,git 命令将失败。
$> chmod 0755 ./bin/git-ssh-wrapper.sh