innmind/git

Git抽象层

3.2.0 2023-09-23 12:52 UTC

This package is auto-updated.

Last update: 2024-09-23 15:30:52 UTC


README

Build Status codecov Type Coverage

本地Git仓库操作抽象层。

请随意提交PR来添加其他Git功能。

安装

composer require innmind/git

使用

use Innmind\Git\{
    Git,
    Repository\Remote\Name,
    Repository\Remote\Url,
    Revision\Branch,
};
use Innmind\OperatingSystem\Factory;
use Innmind\Url\Path;

$os = Factory::build();
$git = Git::of($os->control(), $os->clock());
$repository = $git->repository(Path::of('/somewhere/on/the/local/machine'))->match(
    static fn($repository) => $repository,
    static fn() => throw new \RuntimeException('The path does not exist'),
);
$_ = $repository->init()->match(
    static fn() => null, // pass
    static fn() => throw new \RuntimeException('Failed to init the repository'),
);
$remotes = $repository->remotes();
$remotes->add(Name::of('origin'), Url::of('git@github.com:Vendor/Repo.git'))
$remotes->push(Branch::of('master'));
$repository
    ->branches()
    ->new(Branch::of('develop'));
$repository
    ->checkout()
    ->revision(Branch::of('develop'));

以下示例初始化本地git仓库,声明GitHub仓库为其远程仓库,并最终检出新的分支develop

提供的功能远不止此单一示例,请查看类的接口以发现所有功能。