phpcq/branch-alias-validation
Installation:
Add to composer.json under require-dev:
"phpcq/branch-alias-validation": "^1.0"
Run composer update.
First Run: Navigate to your project root and execute:
./vendor/bin/validate-branch-alias.php
Optionally specify a custom Git repo path:
./vendor/bin/validate-branch-alias.php /path/to/repo
First Use Case: Integrate into a pre-merge GitHub Actions workflow to block PRs where feature branches lag behind the latest tag:
- name: Validate branch against latest tag
run: ./vendor/bin/validate-branch-alias.php
CI/CD Integration:
feature/* and release/* branches.
jobs:
validate-branch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: ./vendor/bin/validate-branch-alias.php
before_script to validate branches pre-merge.
validate_branch:
script:
- ./vendor/bin/validate-branch-alias.php
rules:
- if: '$CI_MERGE_REQUEST_ID'
Local Development:
.git/hooks/pre-push (symlink to the binary).
#!/bin/bash
./vendor/bin/validate-branch-alias.php || exit 1
git push origin HEAD:refs/for/$CI_BRANCH
// app/Console/Commands/ValidateBranch.php
public function handle() {
$exitCode = shell_exec('php ' . base_path('vendor/bin/validate-branch-alias.php'));
if ($exitCode !== 0) {
$this->error('Branch validation failed!');
exit(1);
}
}
Tag-Based Workflows:
release/* branches against the latest minor/patch tag (e.g., v1.2.x must be ahead of v1.2.0).hotfix/* branches are ahead of the latest patch tag (e.g., v1.0.1).Monorepo Support:
./vendor/bin/validate-branch-alias.php packages/auth
./vendor/bin/validate-branch-alias.php packages/api
Feature Branch Validation:
feature/x branches from develop. Before merging to develop, the branch must be ahead of the latest tag (e.g., v1.0.0).# GitHub Actions example
jobs:
feature-validation:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: ./vendor/bin/validate-branch-alias.php
if: "startsWith(github.ref, 'refs/pull/') && contains(github.head_ref, 'feature/')"
Release Gatekeeping:
main if the release branch (e.g., release/1.2) isn’t ahead of the latest tag (v1.2.0).// app/Providers/AppServiceProvider.php
public function boot() {
if (app()->environment('production') && str_starts_with(request()->branch, 'release/')) {
$exitCode = shell_exec('php ' . base_path('vendor/bin/validate-branch-alias.php'));
if ($exitCode !== 0) {
abort(500, 'Release branch validation failed!');
}
}
}
Tagging Automation:
# After validation passes
git tag -a v1.2.0 -m "Release 1.2.0"
git push origin v1.2.0
Laravel-Specific:
php artisan vendor:publish --tag=branch-alias-validation
git:pre-push or deploy events:
// app/Providers/EventServiceProvider.php
protected $listen = [
'git.pre-push' => [
Commands\ValidateBranch::class,
],
];
Git Hooks:
.git/hooks/pre-push or .git/hooks/pre-commit:
ln -s ../../vendor/bin/validate-branch-alias.php .git/hooks/pre-push
chmod +x .git/hooks/pre-push
Custom Rules:
#!/bin/bash
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
if [[ "$BRANCH_NAME" != "main" && "$BRANCH_NAME" != "develop" ]]; then
./vendor/bin/validate-branch-alias.php
fi
CI Optimization:
- name: Cache Git tags
run: git fetch --tags
- name: Validate branch
run: ./vendor/bin/validate-branch-alias.php
False Positives:
release/ branch may legitimately lag behind tags during preparation (e.g., release/1.2 is behind v1.2.0 but will be updated before merging).if [[ "$(git rev-parse --abbrev-ref HEAD)" == release/* ]]; then
echo "Skipping validation for release branches..."
exit 0
else
./vendor/bin/validate-branch-alias.php
fi
Git Not Installed:
- name: Install Git
run: sudo apt-get install -y git
Permission Errors:
.git directory in shared environments (e.g., Docker, CI).chmod -R a+rX .git
Tag Naming Inconsistencies:
v1.0.0-rc1 may cause validation to fail if the package expects strict SemVer.Large Repositories:
git tag) may time out in CI for repos with >50k commits.git fetch --depth=1 --tags
./vendor/bin/validate-branch-alias.php
Exit Codes:
0: Validation passed (branch is ahead of the latest tag).1: Validation failed (branch is behind or no tags exist).- name: Validate and fail if needed
run: |
if ! ./vendor/bin/validate-branch-alias.php; then
echo "::error::Branch validation failed!"
exit 1
fi
Verbose Output:
./vendor/bin/validate-branch-alias.php > validation.log 2>&1
Dry Runs:
# Simulate a "bad" branch (detached HEAD)
git checkout v1.0.0
./vendor/bin/validate-branch-alias.php # Should fail
How can I help you explore Laravel packages today?