sebastian/git-state
sebastian/git-state is a small PHP library that inspects a Git checkout and reports its state: origin URL, current branch, commit hash, and whether the working directory is clean (or the current git status). Useful for build/test tooling.
exec() under the hood, which is compatible with Laravel’s Process facade for more robust command execution if needed.Artisan commands, middleware, or service containers.// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton(GitStateService::class, function ($app) {
return new GitStateService(new Builder());
});
}
Process facade for more controlled Git command execution (e.g., timeouts, logging). Example:
use Illuminate\Support\Facades\Process;
$output = Process::run('git status')->output();
// app/Http/Middleware/CheckGitState.php
public function handle($request, Closure $next) {
$state = (new Builder())->build();
if (!$state || !$state->isClean()) {
abort(500, 'Git state validation failed: Working directory is dirty or invalid.');
}
return $next($request);
}
PATH. This introduces a risk if the environment lacks Git (e.g., some shared hosting or serverless environments). Mitigation: Document Git as a requirement and provide fallback logic.git status) are I/O-bound and may introduce latency in high-throughput applications. Mitigation: Cache Git state or offload checks to background jobs.false for non-Git repositories or missing origins, which may not be sufficient for all use cases. Mitigation: Extend the package or wrap it in a Laravel service with custom error handling.Environment Requirements:
Use Case Scope:
Error Handling Strategy:
Performance:
Testing:
Maintenance:
Builder as a singleton or bind it to a custom service for dependency injection.php artisan git:check).exec() with Laravel’s Process facade for better control over Git commands (e.g., timeouts, logging).Assessment Phase:
sebastian/git-state can replace custom logic or heavier libraries.main branch").Pilot Integration:
// app/Console/Commands/DeployCheck.php
use SebastianBergmann\GitState\Builder;
protected function handle() {
$state = (new Builder())->build();
if (!$state || $state->branch() !== 'main') {
$this->error('Deployment aborted: Must deploy from `main` branch.');
return 1;
}
$this->info("Deploying commit: {$state->commit()}");
return 0;
}
Gradual Rollout:
Deprecation:
phpgit) where sebastian/git-state provides sufficient functionality.Process facade for cross-platform command execution.Prerequisites:
RUN apt-get update && apt-get install -y git
Core Integration:
composer.json (or require-dev for testing).// app/Services/GitStateService.php
namespace App\Services;
use SebastianBergmann\GitState\Builder;
class GitStateService {
public function __construct(private Builder $builder) {}
public function getState() {
return $this->builder->build();
}
public function isDeployable(): bool {
$state = $this->getState();
return $state && $state->isClean() && in_array($state->branch(), ['main', 'release/*']);
}
}
Use Case Implementation:
!isDeployable()).// app/Http/Kernel.php
protected $routeMiddleware = [
'check.git.state' => \App\Http\Middleware\CheckGitState::class,
];
How can I help you explore Laravel packages today?