From 0a47b126cf140a4a7d06ff9eae94e517ea36ad88 Mon Sep 17 00:00:00 2001 From: Akurosia Kamo Date: Fri, 29 May 2026 10:48:12 +0200 Subject: [PATCH] fix admin page --- admin.php | 84 +++++++++++++++++++++++++++++++++++++------- templates/topbar.php | 2 +- 2 files changed, 73 insertions(+), 13 deletions(-) diff --git a/admin.php b/admin.php index 82b25e3..99f3eaa 100644 --- a/admin.php +++ b/admin.php @@ -21,6 +21,8 @@ if (empty($_SESSION['admin_csrf'])) { $_SESSION['admin_csrf'] = bin2hex(random_bytes(16)); } $csrf = $_SESSION['admin_csrf']; +$returnPath = safe_return_path($_GET['return'] ?? null); +$adminAction = 'admin.php?return=' . rawurlencode($returnPath); function admin_h(?string $value): string { return htmlspecialchars((string)$value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); @@ -68,6 +70,63 @@ function git_last_commit(): string { return $out !== '' ? $out : '(unknown)'; } +function git_current_branch(): string { + return git_output(['rev-parse', '--abbrev-ref', 'HEAD']); +} + +function git_upstream_for_branch(string $branch): ?string { + $result = git_run(['rev-parse', '--abbrev-ref', $branch . '@{upstream}']); + return $result['code'] === 0 && $result['output'] !== '' ? $result['output'] : null; +} + +function git_is_ancestor(string $ancestor, string $descendant): bool { + return git_run(['merge-base', '--is-ancestor', $ancestor, $descendant])['code'] === 0; +} + +function git_pull_all_tracked_branches(): array { + $lines = []; + $exitCode = 0; + + $fetch = git_run(['fetch', '--all', '--prune']); + $lines[] = '$ git fetch --all --prune'; + $lines[] = $fetch['output'] ?: '(no output)'; + if ($fetch['code'] !== 0) return ['code' => $fetch['code'], 'output' => implode("\n", $lines)]; + + $current = git_current_branch(); + foreach (git_local_branches() as $branch) { + $upstream = git_upstream_for_branch($branch); + if ($upstream === null) { + $lines[] = ''; + $lines[] = $branch . ': no upstream configured, skipped'; + continue; + } + + $lines[] = ''; + $lines[] = $branch . ' <- ' . $upstream; + if ($branch === $current) { + $pull = git_run(['merge', '--ff-only', $upstream]); + $lines[] = '$ git merge --ff-only ' . $upstream; + $lines[] = $pull['output'] ?: '(no output)'; + if ($pull['code'] !== 0) $exitCode = $pull['code']; + continue; + } + + if (git_is_ancestor($branch, $upstream)) { + $update = git_run(['branch', '-f', $branch, $upstream]); + $lines[] = '$ git branch -f ' . $branch . ' ' . $upstream; + $lines[] = $update['output'] ?: '(fast-forwarded)'; + if ($update['code'] !== 0) $exitCode = $update['code']; + } elseif (git_is_ancestor($upstream, $branch)) { + $lines[] = 'already up to date or ahead'; + } else { + $lines[] = 'diverged, skipped'; + if ($exitCode === 0) $exitCode = 1; + } + } + + return ['code' => $exitCode, 'output' => implode("\n", $lines)]; +} + function resolve_selected_branch_ref(string $ref, array $localBranches, array $remoteBranches, bool $preferLocalForRemote): ?string { if (str_starts_with($ref, 'local:')) { $branch = substr($ref, 6); @@ -94,11 +153,11 @@ if ($allowed && $_SERVER['REQUEST_METHOD'] === 'POST') { } else { $action = $_POST['action'] ?? ''; if ($action === 'fetch') { - $result = git_run(['fetch', '--prune']); - $message = 'git fetch --prune'; + $result = git_run(['fetch', '--all', '--prune']); + $message = 'git fetch --all --prune'; } elseif ($action === 'pull') { - $result = git_run(['pull', '--ff-only']); - $message = 'git pull --ff-only'; + $result = git_pull_all_tracked_branches(); + $message = 'pull all tracked branches'; } elseif ($action === 'push') { $result = git_run(['push']); $message = 'git push'; @@ -118,6 +177,7 @@ if ($allowed && $_SERVER['REQUEST_METHOD'] === 'POST') { } } elseif ($action === 'merge') { $ref = trim((string)($_POST['merge_branch'] ?? '')); + git_run(['fetch', '--all', '--prune']); $localBranches = git_local_branches(); $remoteBranches = git_remote_branches(); $branch = resolve_selected_branch_ref($ref, $localBranches, $remoteBranches, false); @@ -164,7 +224,7 @@ $branches = []; $remoteBranches = []; $lastCommit = ''; if ($allowed) { - $currentBranch = git_output(['rev-parse', '--abbrev-ref', 'HEAD']); + $currentBranch = git_current_branch(); $status = git_output(['status', '-sb']); $branches = git_local_branches(); $remoteBranches = git_remote_branches(); @@ -203,9 +263,9 @@ if ($allowed) {
- +
- Back + Back
@@ -242,14 +302,14 @@ if ($allowed) {
Aktionen
-
+ - +
-
+
@@ -274,7 +334,7 @@ if ($allowed) { -
+
@@ -284,7 +344,7 @@ if ($allowed) { -
+
diff --git a/templates/topbar.php b/templates/topbar.php index b9ecde8..bb2aab1 100644 --- a/templates/topbar.php +++ b/templates/topbar.php @@ -8,7 +8,7 @@
- Admin + Admin