forked from xziino/ff14-mitigator
fix branch names
This commit is contained in:
parent
0f4d5a98d4
commit
c2cf4db458
65
admin.php
65
admin.php
@ -41,12 +41,28 @@ function git_output(array $args): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function git_local_branches(): array {
|
function git_local_branches(): array {
|
||||||
$out = git_output(['branch', '--format=%(refname:short)']);
|
$out = git_output(['branch', '--list', '--no-color']);
|
||||||
$branches = array_values(array_filter(array_map('trim', explode("\n", $out)), fn($v) => $v !== ''));
|
$branches = array_values(array_filter(array_map(function ($line) {
|
||||||
|
return trim(ltrim(trim($line), '*'));
|
||||||
|
}, explode("\n", $out)), fn($v) => $v !== ''));
|
||||||
sort($branches, SORT_NATURAL | SORT_FLAG_CASE);
|
sort($branches, SORT_NATURAL | SORT_FLAG_CASE);
|
||||||
return $branches;
|
return $branches;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function git_remote_branches(): array {
|
||||||
|
$out = git_output(['branch', '-r', '--no-color']);
|
||||||
|
$branches = array_values(array_filter(array_map('trim', explode("\n", $out)), function ($branch) {
|
||||||
|
return $branch !== '' && !str_contains($branch, ' -> ') && !str_ends_with($branch, '/HEAD');
|
||||||
|
}));
|
||||||
|
sort($branches, SORT_NATURAL | SORT_FLAG_CASE);
|
||||||
|
return $branches;
|
||||||
|
}
|
||||||
|
|
||||||
|
function branch_from_remote(string $remoteBranch): string {
|
||||||
|
$parts = explode('/', $remoteBranch, 2);
|
||||||
|
return $parts[1] ?? $remoteBranch;
|
||||||
|
}
|
||||||
|
|
||||||
$message = null;
|
$message = null;
|
||||||
$result = null;
|
$result = null;
|
||||||
|
|
||||||
@ -63,13 +79,33 @@ if ($allowed && $_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$result = git_run(['pull', '--ff-only']);
|
$result = git_run(['pull', '--ff-only']);
|
||||||
$message = 'git pull --ff-only';
|
$message = 'git pull --ff-only';
|
||||||
} elseif ($action === 'switch') {
|
} elseif ($action === 'switch') {
|
||||||
$branch = trim((string)($_POST['branch'] ?? ''));
|
$ref = trim((string)($_POST['branch'] ?? ''));
|
||||||
$branches = git_local_branches();
|
$localBranches = git_local_branches();
|
||||||
if (!in_array($branch, $branches, true)) {
|
$remoteBranches = git_remote_branches();
|
||||||
$result = ['code' => 1, 'output' => 'Unknown or non-local branch: ' . $branch];
|
if (str_starts_with($ref, 'local:')) {
|
||||||
|
$branch = substr($ref, 6);
|
||||||
|
if (!in_array($branch, $localBranches, true)) {
|
||||||
|
$result = ['code' => 1, 'output' => 'Unknown local branch: ' . $branch];
|
||||||
|
} else {
|
||||||
|
$result = git_run(['switch', $branch]);
|
||||||
|
$message = 'git switch ' . $branch;
|
||||||
|
}
|
||||||
|
} elseif (str_starts_with($ref, 'remote:')) {
|
||||||
|
$remoteBranch = substr($ref, 7);
|
||||||
|
if (!in_array($remoteBranch, $remoteBranches, true)) {
|
||||||
|
$result = ['code' => 1, 'output' => 'Unknown remote branch: ' . $remoteBranch];
|
||||||
|
} else {
|
||||||
|
$branch = branch_from_remote($remoteBranch);
|
||||||
|
if (in_array($branch, $localBranches, true)) {
|
||||||
|
$result = git_run(['switch', $branch]);
|
||||||
|
$message = 'git switch ' . $branch;
|
||||||
|
} else {
|
||||||
|
$result = git_run(['switch', '--track', $remoteBranch]);
|
||||||
|
$message = 'git switch --track ' . $remoteBranch;
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$result = git_run(['switch', $branch]);
|
$result = ['code' => 1, 'output' => 'Unknown branch selection.'];
|
||||||
$message = 'git switch ' . $branch;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,11 +114,13 @@ if ($allowed && $_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$currentBranch = '';
|
$currentBranch = '';
|
||||||
$status = '';
|
$status = '';
|
||||||
$branches = [];
|
$branches = [];
|
||||||
|
$remoteBranches = [];
|
||||||
$lastCommit = '';
|
$lastCommit = '';
|
||||||
if ($allowed) {
|
if ($allowed) {
|
||||||
$currentBranch = git_output(['rev-parse', '--abbrev-ref', 'HEAD']);
|
$currentBranch = git_output(['rev-parse', '--abbrev-ref', 'HEAD']);
|
||||||
$status = git_output(['status', '-sb']);
|
$status = git_output(['status', '-sb']);
|
||||||
$branches = git_local_branches();
|
$branches = git_local_branches();
|
||||||
|
$remoteBranches = git_remote_branches();
|
||||||
$lastCommit = git_output(['log', '-1', '--pretty=%h %s (%cr)']);
|
$lastCommit = git_output(['log', '-1', '--pretty=%h %s (%cr)']);
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
@ -170,10 +208,19 @@ if ($allowed) {
|
|||||||
<label for="branch">Branch</label>
|
<label for="branch">Branch</label>
|
||||||
<select id="branch" name="branch">
|
<select id="branch" name="branch">
|
||||||
<?php foreach ($branches as $branch): ?>
|
<?php foreach ($branches as $branch): ?>
|
||||||
<option value="<?= admin_h($branch) ?>" <?= $branch === $currentBranch ? 'selected' : '' ?>>
|
<option value="local:<?= admin_h($branch) ?>" <?= $branch === $currentBranch ? 'selected' : '' ?>>
|
||||||
<?= admin_h($branch) ?>
|
<?= admin_h($branch) ?>
|
||||||
</option>
|
</option>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
|
<?php if ($remoteBranches): ?>
|
||||||
|
<optgroup label="Remote branches">
|
||||||
|
<?php foreach ($remoteBranches as $branch): ?>
|
||||||
|
<option value="remote:<?= admin_h($branch) ?>">
|
||||||
|
<?= admin_h($branch) ?>
|
||||||
|
</option>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</optgroup>
|
||||||
|
<?php endif; ?>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<button class="btn" type="submit">Switch</button>
|
<button class="btn" type="submit">Switch</button>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user