- How do I integrate sebastian/git-state into a Laravel Artisan command for CI/CD checks?
- Inject the `Builder` class into your Artisan command via Laravel’s service container. Use the `build()` method to fetch Git state, then validate the working directory with `isClean()` or `status()`. For example, throw an exception if `isClean()` returns false to block deployments. Cache the result if performance is critical, as repeated Git checks can slow down CI pipelines.
- Will sebastian/git-state work in Laravel Forge or shared hosting where Git CLI isn’t available?
- No, this package relies on Git CLI commands, so it will fail silently (return `false`) if Git isn’t installed. For shared hosting or serverless environments, consider wrapping the package in a fallback mechanism using `symfony/process` or document the requirement explicitly. Test edge cases like missing `.git` directories in your deployment pipeline.
- Can I use sebastian/git-state to dynamically enable Laravel feature flags based on Git tags?
- Yes, you can retrieve the current branch or tag using `$state->branch()` or `$state->tag()`. Compare these values in your feature flag logic (e.g., `if ($state->tag() === 'v2.0')`). Cache the Git state in Laravel’s cache layer or queue workers to avoid repeated CLI calls. For production, ensure Git is available and handle failures gracefully.
- How do I cache the Git state to avoid spawning Git processes on every request in Laravel?
- Cache the Git state using Laravel’s cache system (e.g., `Cache::remember()`) with a long TTL (e.g., 5 minutes) since Git state rarely changes mid-request. Alternatively, fetch the state once during Laravel’s `booted` event and store it in a singleton service. For high-traffic apps, defer the check to a queue worker if real-time Git state isn’t critical.
- Does sebastian/git-state support Laravel 10+ and PHP 8.2+?
- The package itself doesn’t enforce Laravel-specific dependencies, but it requires PHP 7.4+. Test compatibility by checking if the `Builder` class works with Laravel’s service container and PHP 8.2’s type system. If issues arise, wrap the package in a Laravel-specific service class to handle edge cases like detached HEAD states or cross-platform path issues.
- How can I log Git commit hashes to Laravel Telescope for debugging?
- Inject the `Builder` into a Telescope service provider or middleware. Use `$state->commit()` to append the hash to exception logs or requests. For performance, cache the Git state at the application level (e.g., via `app()->singleton`) and avoid per-request Git checks. Document the requirement for Git CLI in your deployment environment.
- What’s the best alternative to sebastian/git-state if I need Git state in serverless or air-gapped environments?
- Consider `symfony/process` to execute Git commands manually, or use a custom wrapper that falls back to hardcoded defaults (e.g., `unknown` branch) if Git fails. For air-gapped systems, pre-compute Git state during build time and embed it in your Laravel app as a config value. Libraries like `ruflin/elib` also offer Git parsing but may not be Laravel-specific.
- How do I handle cases where the Git repository is missing or the origin URL is not configured?
- The package returns `false` if no `.git` directory or origin remote exists. Validate this in your code with `if ($state === false)`. Throw a custom exception (e.g., `GitRepositoryException`) or log a warning for debugging. For critical paths (e.g., compliance logging), treat missing Git as a fatal error and document the requirement in your deployment checklist.
- Can I use sebastian/git-state to validate a clean working directory before Laravel migrations?
- Yes, use `$state->isClean()` to check the working directory before running migrations. Wrap the check in a Laravel service provider or middleware to block migrations if the directory is dirty. Cache the result if performance is a concern, but ensure the cache invalidates when Git state changes (e.g., via file events or manual cache clearing).
- How do I test sebastian/git-state in a Laravel CI pipeline to ensure Git is available?
- Add a CI step that runs `composer require sebastian/git-state` and a test script (e.g., `php artisan git:state:validate`) to verify Git CLI is installed. Use `GitHub Actions` or `GitLab CI` to mock Git environments. Test edge cases like detached HEAD states or submodules by creating temporary repos in your CI pipeline. Document failures as build errors.