sebastian/git-state
PHP library to describe the state of a Git checkout. Retrieve origin URL, current branch and commit hash, and determine whether the working directory is clean or get a git-style status output. Useful for build/test tooling and CI metadata.
php artisan git:validate).GitStateService for version tracking or feature flags).git to be installed on the server. Critical for production—must ensure Git is available in all deployment environments (e.g., Docker, CI/CD pipelines, shared hosting). This is a non-negotiable requirement for production use.false if not in a Git repository. Must design graceful fallbacks (e.g., custom exceptions, logging, or silent defaults) to avoid runtime errors in non-Git environments (e.g., Docker builds, CI pipelines).git status, git rev-parse) are synchronous and blocking. For high-frequency checks (e.g., per-request middleware), consider:
Process facade for custom Git parsing, GitHub API for remote repos)?php artisan git:check to validate repo state before migrations or deployments). Example:
php artisan make:command GitCheck
app/Services/GitStateService) to abstract Git logic from business code. Example:
namespace App\Services;
use SebastianBergmann\GitState\Builder;
class GitStateService {
public function getState(): ?array {
$builder = new Builder();
$state = $builder->build();
return $state ? [
'branch' => $state->branch(),
'commit' => $state->commit(),
'is_clean' => $state->isClean(),
'origin' => $state->originUrl(),
] : null;
}
}
migrate). Example:
namespace App\Http\Middleware;
use App\Services\GitStateService;
use Closure;
class EnsureCleanGitState {
public function __construct(protected GitStateService $git) {}
public function handle($request, Closure $next) {
$state = $this->git->getState();
if (!$state || !$state['is_clean']) {
abort(500, 'Git working directory must be clean for deployment.');
}
return $next($request);
}
}
job:finished).dev dependency:
composer require --dev sebastian/git-state
public function test_git_state_is_clean() {
$state = (new Builder())->build();
$this->assertTrue($state->isClean(), 'Working directory must be clean for tests.');
}
composer require sebastian/git-state
- name: Validate Git state
run: |
php artisan git:check || exit 1
PATH. Test on Windows if applicable (e.g., using Git Bash or WSL).RUN apt-get update && apt-get install -y git
composer.json (dev or main).How can I help you explore Laravel packages today?