&1'; exec($cmd, $lines, $code); return ['code' => $code, 'output' => trim(implode("\n", $lines))]; } function git_output(array $args): string { return git_run($args)['output']; } function git_local_branches(): array { $out = git_output(['branch', '--format=%(refname:short)']); $branches = array_values(array_filter(array_map('trim', explode("\n", $out)), fn($v) => $v !== '')); sort($branches, SORT_NATURAL | SORT_FLAG_CASE); return $branches; } $message = null; $result = null; if ($allowed && $_SERVER['REQUEST_METHOD'] === 'POST') { if (!hash_equals($csrf, $_POST['csrf'] ?? '')) { http_response_code(400); $message = 'Invalid CSRF token.'; } else { $action = $_POST['action'] ?? ''; if ($action === 'fetch') { $result = git_run(['fetch', '--prune']); $message = 'git fetch --prune'; } elseif ($action === 'pull') { $result = git_run(['pull', '--ff-only']); $message = 'git pull --ff-only'; } elseif ($action === 'switch') { $branch = trim((string)($_POST['branch'] ?? '')); $branches = git_local_branches(); if (!in_array($branch, $branches, true)) { $result = ['code' => 1, 'output' => 'Unknown or non-local branch: ' . $branch]; } else { $result = git_run(['switch', $branch]); $message = 'git switch ' . $branch; } } } } $currentBranch = ''; $status = ''; $branches = []; $lastCommit = ''; if ($allowed) { $currentBranch = git_output(['rev-parse', '--abbrev-ref', 'HEAD']); $status = git_output(['status', '-sb']); $branches = git_local_branches(); $lastCommit = git_output(['log', '-1', '--pretty=%h %s (%cr)']); } ?>
= admin_h($status ?: 'Clean') ?>
= admin_h($result['output'] ?: '(no output)') ?>