Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Git State Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Library for describing the state of a Git checkout.

Frequently asked questions about Git State
How do I integrate sebastian/git-state into a Laravel Artisan command?
Use Laravel’s service container to bind the `Builder` class and inject it into your Artisan command. For example, register it in `AppServiceProvider` and then use dependency injection in your command’s `handle()` method. This keeps your code clean and testable. The package’s `build()` method returns a state object with methods like `branch()`, `commit()`, and `isClean()` for validation.
Will sebastian/git-state work in Laravel Forge or shared hosting environments where Git isn’t always available?
No, this package requires Git to be installed and available in the system’s PATH. If your environment lacks Git (e.g., some shared hosting or serverless setups), you’ll need to either enforce Git as a requirement or implement fallback logic, such as returning a default state or throwing a descriptive exception. Document this dependency clearly in your project’s setup instructions.
Can I use sebastian/git-state to block deployments if the working directory has uncommitted changes?
Yes, you can integrate this package into Laravel middleware or a custom Artisan command to check the Git state before proceeding with deployments. For example, use the `isClean()` method to validate the working directory. If dirty, abort the request or log a warning. This is useful for enforcing clean codebases in CI/CD pipelines or deployment scripts.
Does sebastian/git-state support Laravel’s Process facade for better error handling and timeouts?
Yes, while the package uses PHP’s native `exec()`, you can wrap Git commands with Laravel’s `Process` facade for enhanced control, such as timeouts, logging, or error handling. For example, replace direct Git calls with `Process::run('git status')` to leverage Laravel’s built-in features. This is especially useful in unreliable environments or for long-running operations.
How do I handle cases where the repository has no origin or is not a Git repo at all?
The package returns `false` if the directory isn’t a Git repository or lacks an origin. Wrap the `Builder` in a Laravel service to customize error handling, such as throwing a `RuntimeException` with a helpful message or logging the issue. For example, check the return value of `build()` and handle it gracefully in your application logic.
Is sebastian/git-state compatible with Laravel 10+ and PHP 8.2+?
Yes, the package has no strict Laravel version requirements and works with modern PHP versions (8.0+). However, always verify compatibility with your specific Laravel version by checking the package’s dependencies and testing in your environment. Since it’s dependency-free, integration should be seamless as long as Git is available.
Can I cache the Git state to avoid repeated I/O calls in high-traffic Laravel applications?
Yes, caching is recommended for performance-critical applications where Git state checks are frequent (e.g., per API request). Use Laravel’s cache system to store the state object (e.g., `Cache::remember()`) with a short TTL, such as 5 minutes. This reduces I/O overhead and improves response times in high-throughput environments.
Are there alternatives to sebastian/git-state for Laravel that offer more features (e.g., submodule support)?
If you need advanced Git features like submodule handling or deeper Git operations, consider alternatives like `php-git` or `league/git`. However, for simple state inspection (branch, commit, origin, cleanliness), `sebastian/git-state` is lightweight and focused. It’s ideal for Laravel applications where minimalism and performance are priorities over extensive Git functionality.
How do I test sebastian/git-state in a Laravel application’s CI/CD pipeline?
Add the package as a dev dependency (`composer require --dev sebastian/git-state`) and include tests in your CI pipeline to validate Git state during builds. For example, create a PHPUnit test that checks the `isClean()` method before running migrations or deployments. Ensure your CI environment has Git installed and configured to avoid false negatives.
What edge cases should I consider when using sebastian/git-state in Laravel, such as detached HEAD or submodules?
The package handles basic Git states well, but edge cases like detached HEAD or submodules may require custom logic. For detached HEAD, check the `branch()` method for `null` or a special value. For submodules, extend the package or use Laravel’s `Process` facade to run `git submodule status`. Document these edge cases in your project’s README and handle them explicitly in your application code.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
boundwize/jsonrecast
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata