51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Git;
|
|
|
|
class ValidateGitBranchName
|
|
{
|
|
public function message(string $branchName): ?string
|
|
{
|
|
$trimmedBranchName = trim($branchName);
|
|
|
|
if ($trimmedBranchName === '') {
|
|
return 'Branch name is required.';
|
|
}
|
|
|
|
if (preg_match('/\s/', $trimmedBranchName) === 1) {
|
|
return 'Branch names must not contain whitespace.';
|
|
}
|
|
|
|
if (
|
|
str_starts_with($trimmedBranchName, '/') ||
|
|
str_ends_with($trimmedBranchName, '/') ||
|
|
str_starts_with($trimmedBranchName, '.') ||
|
|
str_ends_with($trimmedBranchName, '.')
|
|
) {
|
|
return 'Branch names must not start or end with "/" or ".".';
|
|
}
|
|
|
|
if (
|
|
str_contains($trimmedBranchName, '..') ||
|
|
str_contains($trimmedBranchName, '//') ||
|
|
str_contains($trimmedBranchName, '@{')
|
|
) {
|
|
return 'Branch names contain an unsupported ref pattern.';
|
|
}
|
|
|
|
if (preg_match('/[~^:?*\[\\\\]/', $trimmedBranchName) === 1) {
|
|
return 'Branch names contain unsupported characters.';
|
|
}
|
|
|
|
if (str_ends_with($trimmedBranchName, '.lock')) {
|
|
return 'Branch names must not end with ".lock".';
|
|
}
|
|
|
|
if (preg_match('/[\x00-\x1F\x7F]/', $trimmedBranchName) === 1) {
|
|
return 'Branch names contain unsupported control characters.';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|