aybarsm/laravel-git

为Laravel和Laravel Zero提供直观的服务提供商,简化了Git仓库管理,无论是在应用程序内部还是外部。

v1.0.3 2023-09-09 17:15 UTC

This package is auto-updated.

Last update: 2024-09-09 19:34:25 UTC


README

本包专为Laravel和Laravel Zero开发者设计,旨在无缝地将Git仓库集成和管理到他们的应用程序中。无论是想要控制应用程序内的仓库还是与外部仓库交互,此服务提供商都简化了这些交互。建立在Laravel服务容器的基础上,它提供了一个简单高效的方法来管理Git操作,使您的应用程序与版本控制需求保持连接和同步。

安装

您可以通过composer安装此包

composer require aybarsm/laravel-git

您可以通过以下方式发布配置文件

php artisan vendor:publish --provider="Aybarsm\Laravel\Git\GitServiceProvider" --tag=config

配置Git提供者

您可以通过扩展类来更改具体实现,移除或添加新的仓库,并使用子命令修改命令。

注意:命令列表仅包括带有子命令的命令。几乎所有git命令都已实现为包中的具体实现和接口。
return [
    'repos' => [
        'default' => base_path(),
    ],
    'concretes' => [
        'Git' => \Aybarsm\Laravel\Git\Git::class,
        'GitRepo' => \Aybarsm\Laravel\Git\GitRepo::class,
    ],
    'commands' => [
        'bisect' => [
            'subcommands' => ['start', 'bad', 'new', 'good', 'old', 'terms', 'skip', 'reset', 'visualize', 'view', 'replay', 'log', 'run'],
        ],
        'bundle' => [
            'subcommands' => ['create', 'verify', 'list-heads', 'unbundle'],
        ],
        'maintenance' => [
            'subcommands' => ['run', 'start', 'stop', 'register', 'unregister'],
        ],
        'notes' => [
            'subcommands' => ['list', 'add', 'copy', 'append', 'edit', 'show', 'merge', 'remove', 'prune', 'get-ref'],
        ],
        'sparse-checkout' => [
            'subcommands' => ['init', 'list', 'set', 'add', 'reapply', 'disable', 'check-rules'],
        ],
        'stash' => [
            'subcommands' => ['list', 'show', 'drop', 'pop', 'apply', 'branch', 'push', 'save', 'clear', 'create', 'store'],
        ],
        'submodule' => [
            'subcommand_prefixes' => ['--quiet'],
            'subcommands' => ['add', 'status', 'init', 'deinit', 'update', 'set-branch', 'set-url', 'summary', 'foreach', 'sync', 'absorbgitdirs'],
        ],
        'worktree' => [
            'subcommands' => ['add', 'list', 'lock', 'move', 'prune', 'remove', 'repair', 'unlock'],
        ],
        'reflog' => [
            'subcommands' => ['show', 'expire', 'delete', 'exists'],
        ],
        'remote' => [
            'subcommands' => ['add', 'rename', 'remove', 'set-head', 'set-branches', 'get-url', 'set-url', 'show', 'prune', 'update'],
        ],
        'p4' => [
            'subcommands' => ['clone', 'sync', 'rebase', 'submit'],
        ],
        'commit-graph' => [
            'subcommands' => ['verify', 'write'],
        ],
        'credential' => [
            'subcommands' => ['fill', 'approve', 'reject'],
        ],
        'hook' => [
            'subcommands' => ['run'],
        ],
    ],
];

用法

您可以通过Git::class Facade或辅助函数git()来调用具体的Git。gitRepo($repoName)的辅助函数也实现了直接调用预定义的Git仓库。

示例

use Aybarsm\Laravel\Support\Enums\ProcessReturnType;

$git = git();
$repo = $git->repo(); // Returns the default pre-defined repo
// or you can directly reach the repo
// $repo = gitRepo('default');

if ($repo->isReady() && $repo->isDirty()){
    // arguments accepts strings or cli type arrays like arg, --arg=value, -arg value or -arg
    $repo->commit(
    args: [
        '-a',
        '-m' => '"v1.0.0"' 
        ]
    )
    // Git and GitRepo concretes are already uses Laravel's Conditionable trait however chaining made easier with pre-defined whenSuccessful and whenFailed methods.
    ->whenSuccessful(
        callback: fn ($repoInstance) => $repoInstance->tag('v1.0.0'),
        default: function ($repoInstance) {
                Log::info('Git Repo Command Error', (array)$repoInstance->result(ProcessReturnType::ALL_OUTPUT));
                return $repoInstance;
            }
    )
    ->whenSuccessful(
        callback: fn ($repoInstance) => $repoInstance->push('origin v1.0.0'),
        default: function ($repoInstance) {
                Log::info('Git Repo Command Error', (array)$repoInstance->result(ProcessReturnType::ALL_OUTPUT));
                return $repoInstance;
            }
    );
}

// You can easily create individual GitRepo instances by
$newRepo = GitRepo::make('someName', '/some/path');