bit3/git-php

PHP 的简单易用的 GIT 封装。

1.5.1 2022-12-28 05:46 UTC

README

Build Status Latest Version tagged Latest Version on Packagist Installations via composer per month

PHP 的简单易用 GIT 封装

这是一个轻量级的封装,提供 PHP 中的 git 命令。

使用示例

API 使用命令构建器,允许你构建一个命令并一次性执行。

主要用法是

$git->command()->option()->execute();

$git->command() 将创建一个新的命令,*->option() 将向命令中添加一个选项,*->execute() 将最终执行该命令。

命令和选项的命名遵循 git 命名。如果你想查找特定命令或选项的文档,只需查看 git 文档。你将在那里找到命令/选项。

初始化一个新的 git 仓库

use Bit3\GitPhp\GitRepository;

$directory = '/path/to/git/target/directory';

$git = new GitRepository($directory);
$git->init()->execute();

克隆一个 git 仓库

因为 clone 是 PHP 中的一个保留字,所以 clone 命令命名为 cloneRepository()

use Bit3\GitPhp\GitRepository;

$directory = '/path/to/git/target/directory';

$git = new GitRepository($directory);
$git->cloneRepository()->execute();

描述

$annotatedTag   = $git->describe()->execute();
$lightweightTag = $git->describe()->tags()->execute();
$recentRef      = $git->describe()->all()->execute();

设置远程获取 URL

$git->remote()
    ->setUrl('origin', 'git@github.com:bit3/git-php.git')
    ->execute();

设置远程推送 URL

$git->remote()
    ->setPushUrl('origin', 'git@github.com:bit3/git-php.git')
    ->execute();

添加新的远程

$git->remote()
    ->add('github', 'git@github.com:bit3/git-php.git')
    ->execute();

获取远程对象

$git->fetch()->execute('github');

检出

$git->checkout()->execute('hotfix/1.2.3');

检出特定路径

$git->checkout()->execute('hotfix/1.2.3', '/fileA', '/fileB', '/dir/fileC');

推送对象

$git->push()->execute('github', 'hotfix/1.2.3');

将文件添加到暂存索引

$git->add()->execute('file/to/add.ext');

移除文件

$git->rm()->execute('file/to/remove.ext');

提交更改

$git->commit()->message('Commit message')->execute();

创建标签

$git->tag()->execute('v1.2.3');

便利和快捷方法

列出远程

$remotes = $git->remote()->getNames();

// array(
//     'origin',
//     'composer',
// )

列出分支

$remotes = $git->branch()->getNames();

// array(
//     'master',
//     'hotfix/1.2.3',
// )

列出远程跟踪分支

$remotes = $git->branch()->remotes()->->getNames();

// array(
//     'origin/master',
//     'origin/hotfix/1.2.3',
//     'origin/release/4.5.6',
// )

列出包括远程跟踪分支的分支

$remotes = $git->branch()->all()->->getNames();

// array(
//     'master',
//     'hotfix/1.2.3',
//     'remotes/origin/master',
//     'remotes/origin/hotfix/1.2.3',
//     'remotes/origin/release/4.5.6',
// )

获取修改状态

$status = $git->status()->getStatus();

// array(
//     'existing-file.txt'      => array('index' => 'D',   'worktree' => false),
//     'removed-but-staged.txt' => array('index' => 'D',   'worktree' => 'A'),
//     'staged-file.txt'        => array('index' => false, 'worktree' => 'A'),
//     'unknown-file.txt'       => array('index' => '?',   'worktree' => '?'),
// )

获取索引修改状态

$status = $git->status()->getIndexStatus();

// array(
//     'existing-file.txt'      => 'D',
//     'removed-but-staged.txt' => 'D',
//     'staged-file.txt'        => false,
//     'unknown-file.txt'       => '?',
// )

获取工作树修改状态

$status = $git->status()->getWorkTreeStatus();

// array(
//     'existing-file.txt'      => 'worktree' => false,
//     'removed-but-staged.txt' => 'worktree' => 'A',
//     'staged-file.txt'        => 'worktree' => 'A',
//     'unknown-file.txt'       => 'worktree' => '?',
// )