diff --git a/admin.php b/admin.php index ccf9856..82b25e3 100644 --- a/admin.php +++ b/admin.php @@ -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) { + +
+ + +
+ + +
+ +