43 lines
1.1 KiB
PHP
Executable file
43 lines
1.1 KiB
PHP
Executable file
#!/usr/bin/env php
|
|
<?php
|
|
|
|
function extractBranchNamesFromInfo(string $branchInfo): array
|
|
{
|
|
$branchNames = array_map(function (string $branchInfo): string {
|
|
preg_match('/\s*((\w|-|\/)+)\s*/', $branchInfo, $matches);
|
|
|
|
return $matches[1] ?? '';
|
|
}, explode(PHP_EOL, $branchInfo));
|
|
|
|
return array_filter($branchNames);
|
|
}
|
|
|
|
function filterIgnoredBranches(array $branchNames): array
|
|
{
|
|
return array_filter($branchNames, function (string $branchName): bool {
|
|
return !in_array($branchName, ['develop', 'master', 'staging', 'production']);
|
|
});
|
|
}
|
|
|
|
$branchInfo = shell_exec('git branch -vv | grep ": gone]"');
|
|
|
|
# Return early if there are no branches to delete.
|
|
if ($branchInfo === NULL) {
|
|
return;
|
|
}
|
|
|
|
$branchNames = extractBranchNamesFromInfo($branchInfo);
|
|
$filteredBranchNames = filterIgnoredBranches($branchNames);
|
|
|
|
$currentBranch = exec('git rev-parse --abbrev-ref HEAD');
|
|
|
|
foreach ($filteredBranchNames as $branchName) {
|
|
if ($branchName == $currentBranch) {
|
|
echo "Cannot delete {$branchName} as it is the current branch.";
|
|
continue;
|
|
}
|
|
|
|
echo "Deleting {$branchName}...";
|
|
exec("git branch -D ${branchName}");
|
|
}
|