add also merge branch function to admin

This commit is contained in:
Akurosia Kamo 2026-05-27 14:44:05 +02:00
parent e18252b491
commit 1e881be482

View File

@ -68,6 +68,22 @@ function git_last_commit(): string {
return $out !== '' ? $out : '(unknown)';
}
function resolve_selected_branch_ref(string $ref, array $localBranches, array $remoteBranches, bool $preferLocalForRemote): ?string {
if (str_starts_with($ref, 'local:')) {
$branch = substr($ref, 6);
return in_array($branch, $localBranches, true) ? $branch : null;
}
if (str_starts_with($ref, 'remote:')) {
$remoteBranch = substr($ref, 7);
if (!in_array($remoteBranch, $remoteBranches, true)) return null;
$localBranch = branch_from_remote($remoteBranch);
return $preferLocalForRemote && in_array($localBranch, $localBranches, true) ? $localBranch : $remoteBranch;
}
return null;
}
$message = null;
$result = null;
@ -100,19 +116,22 @@ if ($allowed && $_SERVER['REQUEST_METHOD'] === 'POST') {
$message = 'git commit';
}
}
} elseif ($action === 'merge') {
$ref = trim((string)($_POST['merge_branch'] ?? ''));
$localBranches = git_local_branches();
$remoteBranches = git_remote_branches();
$branch = resolve_selected_branch_ref($ref, $localBranches, $remoteBranches, false);
if ($branch === null) {
$result = ['code' => 1, 'output' => 'Unknown branch selection.'];
} else {
$result = git_run(['merge', '--no-edit', $branch]);
$message = 'git merge --no-edit ' . $branch;
}
} elseif ($action === 'switch') {
$ref = trim((string)($_POST['branch'] ?? ''));
$localBranches = git_local_branches();
$remoteBranches = git_remote_branches();
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:')) {
if (str_starts_with($ref, 'remote:')) {
$remoteBranch = substr($ref, 7);
if (!in_array($remoteBranch, $remoteBranches, true)) {
$result = ['code' => 1, 'output' => 'Unknown remote branch: ' . $remoteBranch];
@ -127,7 +146,13 @@ if ($allowed && $_SERVER['REQUEST_METHOD'] === 'POST') {
}
}
} else {
$result = ['code' => 1, 'output' => 'Unknown branch selection.'];
$branch = resolve_selected_branch_ref($ref, $localBranches, $remoteBranches, true);
if ($branch === null) {
$result = ['code' => 1, 'output' => 'Unknown branch selection.'];
} else {
$result = git_run(['switch', $branch]);
$message = 'git switch ' . $branch;
}
}
}
}
@ -258,6 +283,32 @@ if ($allowed) {
</div>
<button class="btn btn-gold section-gap" type="submit">Commit all changes</button>
</form>
<form method="post" class="admin-actions section-gap">
<input type="hidden" name="csrf" value="<?= admin_h($csrf) ?>">
<input type="hidden" name="action" value="merge">
<div class="fg">
<label for="merge-branch">Merge from branch</label>
<select id="merge-branch" name="merge_branch">
<?php foreach ($branches as $branch): ?>
<?php if ($branch === $currentBranch) continue; ?>
<option value="local:<?= admin_h($branch) ?>">
<?= admin_h($branch) ?>
</option>
<?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>
</div>
<button class="btn" type="submit">Merge</button>
</form>
</div>
<div class="card">