rodziu / php-git
纯PHP Git客户端(只读)。
2.1.2
2024-05-23 11:40 UTC
Requires
- php: >=8.2
- ext-curl: *
- ext-zlib: *
- guzzlehttp/guzzle: ^7.8
Requires (Dev)
- ext-zip: *
- friendsofphp/php-cs-fixer: ^3.53
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5.9
This package is not auto-updated.
Last update: 2024-09-26 13:14:51 UTC
README
只读Git客户端实现,允许用户在不安装本地Git客户端的情况下克隆或读取Git仓库数据。
先决条件
- PHP >= 8.2,
- ext-zlib - 正确解压缩git对象。
用法
您可以通过实例化 GitRepository
类与Git仓库进行交互。
$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
获取当前HEAD
$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git'); $head = $gitRepository->getHead(); $head->getCommitHash(); // commit hash that current head points to $head->getBranch(); // current branch, null if head is detached
获取可用的本地或远程分支列表
$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git'); $gitRepository->getBranches(); // returns an array of local branch names $gitRepository->getBranches(remote: true); // returns an array of remote branch names
获取可用的标签列表
$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git'); foreach ($gitRepository->getTags() as $tag) { $tag; // \Rodziu\Git\Objects\Tag or \Rodziu\Git\Objects\AnnotatedTag $tag->getName(); $tag->getTaggedObjectHash(); ... }
从给定的commit-ish开始迭代git log
GitRepository->getLog(?string $commitIsh = null): \Generator
如果省略参数,日志将从当前HEAD开始。
$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git'); foreach ($gitRepository->getLog() as $commit) { $commit; // \Rodziu\Git\Objects\Commit object $commit->getMessage(); $commit->getCommitDate(); ... } // get origin/master branch log $gitRepository->getLog('origin/master');
通过hash获取提交对象
$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git'); $gitRepository->getCommit('commit-hash'); // \Rodziu\Git\Objects\Commit object ...
git describe
基于可用的ref给对象一个可读名称
$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git'); $gitRepository->describe(); // describe current HEAD with annotated tags $gitRepository->describe('commit-ish', all: true); // describe given ref as in git describe --all $gitRepository->describe('commit-ish', tags: true); // describe given ref as in git describe --tags
git clone
获取仓库信息以及所有对象(直到当前HEAD),然后将工作树检出至 /destination/path/repository-name
。
\Rodziu\Git\GitRepository::cloneRepository( 'https://your.repository.url/repository-name.git', '/destination/path/' );
git fetch
从远程更新仓库数据
$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git'); $gitRepository->fetch('origin');
git checkout
检出给定分支、标签或提交的工作树
$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git'); $gitRepository->checkout('commit-ish');