symplify / git-wrapper
Requires
- php: >=8.0
- nette/utils: ^3.2
- symfony/event-dispatcher: ^6.0
- symfony/process: ^6.0
Requires (Dev)
- ondram/ci-detector: ^4.1
- phpunit/phpunit: ^9.5
- psr/log: ^1.1
- symplify/package-builder: ^10.2.7
- symplify/smart-file-system: ^10.2.7
Conflicts
- symplify/amnesia: <10.2.7
- symplify/astral: <10.2.7
- symplify/autowire-array-parameter: <10.2.7
- symplify/coding-standard: <10.2.7
- symplify/composer-json-manipulator: <10.2.7
- symplify/config-transformer: <10.2.7
- symplify/easy-ci: <10.2.7
- symplify/easy-coding-standard: <10.2.7
- symplify/easy-parallel: <10.2.7
- symplify/easy-testing: <10.2.7
- symplify/latte-phpstan-compiler: <10.2.2
- symplify/monorepo-builder: <10.2.7
- symplify/neon-config-dumper: <10.2.7
- symplify/package-builder: <10.2.7
- symplify/php-config-printer: <10.2.7
- symplify/phpstan-extensions: <10.2.7
- symplify/phpstan-latte-rules: <10.2.2
- symplify/phpstan-rules: <10.2.7
- symplify/rule-doc-generator: <10.2.7
- symplify/rule-doc-generator-contracts: <10.2.7
- symplify/skipper: <10.2.7
- symplify/smart-file-system: <10.2.7
- symplify/symfony-static-dumper: <10.2.7
- symplify/symplify-kernel: <10.2.7
- symplify/template-phpstan-compiler: <10.2.2
- symplify/vendor-patches: <10.2.7
- dev-main / 10.3.x-dev
- 10.2.7
- 10.2.6
- 10.2.5
- 10.2.4
- 10.2.3
- 10.2.2
- 10.2.1
- 10.2.0
- 10.1.4
- 10.1.3
- 10.1.2
- 10.1.1
- 10.1.0
- 10.0.25
- 10.0.24
- 10.0.23
- 10.0.22
- 10.0.21
- 10.0.20
- 10.0.19
- 10.0.18
- 10.0.17
- 10.0.16
- 10.0.15
- 10.0.14
- 10.0.13
- 10.0.12
- 10.0.11
- 10.0.10
- 10.0.9
- 10.0.8
- 10.0.7
- 10.0.6
- 10.0.5
- 10.0.4
- 10.0.3
- 10.0.2
- 10.0.1
- 10.0.0
- 10.0.0-beta17
- 10.0.0-beta16
- 10.0.0-beta15
- 10.0.0-beta14
- 10.0.0-beta13
- 10.0.0-beta12
- 10.0.0-beta11
- 10.0.0-beta10
- 10.0.0-beta9
- 10.0.0-beta8
- 10.0.0-beta7
- 10.0.0-beta6
- 10.0.0-beta5
- 10.0.0-beta4
- 10.0.0-beta3
- 10.0.0-beta2
- 10.0.0-beta1
- 9.4.70
- 9.4.69
- 9.4.68
- 9.4.67
- 9.4.66
- 9.4.65
- 9.4.64
- 9.4.63
- 9.4.62
- 9.4.61
- 9.4.60
- 9.4.59
- 9.4.58
- 9.4.57
- 9.4.56
- 9.4.55
- 9.4.54
- 9.4.53
- 9.4.52
- 9.4.51
- 9.4.50
- 9.4.49
- 9.4.48
- 9.4.47
- 9.4.46
- 9.4.45
- 9.4.44
- 9.4.43
- 9.4.42
- 9.4.41
- 9.4.40
- 9.4.39
- 9.4.38
- 9.4.37
- 9.4.36
- 9.4.35
- 9.4.34
- 9.4.33
- 9.4.32
- 9.4.31
- 9.4.30
- 9.4.29
- 9.4.28
- 9.4.27
- 9.4.26
- 9.4.25
- 9.4.24
- 9.4.23
- 9.4.22
- 9.4.21
- 9.4.20
- 9.4.19
- 9.4.18
- 9.4.17
- 9.4.16
- 9.4.15
- 9.4.14
- 9.4.13
- 9.4.12
- 9.4.11
- 9.4.10
- 9.4.9
- 9.4.8
- 9.4.7
- 9.4.6
- 9.4.5
- 9.4.4
- 9.4.3
- 9.4.2
- v9.4.1
- v9.4.0
- v9.3.27
- v9.3.26
- v9.3.25
- v9.3.24
- v9.3.23
- v9.3.22
- v9.3.21
- v9.3.20
- v9.3.19
- v9.3.18
- v9.3.17
- v9.3.16
- v9.3.15
- v9.3.14
- v9.3.13
- v9.3.12
- v9.3.11
- v9.3.10
- v9.3.8
- v9.3.6
- v9.3.5
- v9.3.4
- v9.3.3
- v9.3.1
- v9.3.0
- v9.2.24
- v9.2.23
- v9.2.22
- v9.2.21
- v9.2.20
- v9.2.19
- v9.2.18
- v9.2.17
- v9.2.16
- v9.2.15
- v9.2.14
- v9.2.13
- v9.2.12
- v9.2.11
- v9.2.10
- v9.2.9
- v9.2.8
- v9.2.7
- v9.2.6
- v9.2.5
- v9.2.4
- v9.2.3
- v9.2.2
- 9.2.1
- 9.2.0
- 9.1.9
- 9.1.8
- 9.1.7
- 9.1.6
- 9.1.5
- 9.1.4
- 9.1.3
- 9.1.1
- 9.1.0
This package is auto-updated.
Last update: 2022-05-29 14:23:41 UTC
README
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命令,例如checkout
、push
等。$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);