diff --git a/cptr/frontend/src/lib/apis/git.ts b/cptr/frontend/src/lib/apis/git.ts
index 17e90c7..26edb93 100644
--- a/cptr/frontend/src/lib/apis/git.ts
+++ b/cptr/frontend/src/lib/apis/git.ts
@@ -72,6 +72,9 @@ export const gitPush = (
export const gitUncommit = (root: string) => fetchJSON('/api/git/uncommit', jsonBody({ root }));
+export const gitStash = (root: string, message?: string) =>
+ fetchJSON('/api/git/stash', jsonBody({ root, message }));
+
export const createGitBranch = (root: string, name: string) =>
fetchJSON('/api/git/branch', jsonBody({ root, name }));
diff --git a/cptr/frontend/src/lib/components/DropdownMenu.svelte b/cptr/frontend/src/lib/components/DropdownMenu.svelte
index 67d60bf..e3392f5 100644
--- a/cptr/frontend/src/lib/components/DropdownMenu.svelte
+++ b/cptr/frontend/src/lib/components/DropdownMenu.svelte
@@ -33,6 +33,12 @@
maxHeight?: string;
/** Optional header snippet rendered above items (e.g. search input). */
header?: Snippet;
+ /** Optional footer snippet rendered below items (e.g. pinned actions). */
+ footer?: Snippet;
+ /** Show a divider after the header. */
+ headerDivider?: boolean;
+ /** Show a divider before the footer. */
+ footerDivider?: boolean;
/** Optional snippet rendered when items array is empty. */
empty?: Snippet;
/** Additional CSS classes for the menu container. */
@@ -51,6 +57,9 @@
inlineAbove = false,
maxHeight,
header,
+ footer,
+ headerDivider = true,
+ footerDivider = true,
empty,
className = '',
align = 'start'
@@ -120,15 +129,7 @@
}
const rect = anchor.getBoundingClientRect();
- return [
- rect.left,
- rect.top,
- rect.right,
- rect.bottom,
- rect.width,
- rect.height,
- viewportState()
- ]
+ return [rect.left, rect.top, rect.right, rect.bottom, rect.width, rect.height, viewportState()]
.map((value) => (typeof value === 'number' ? value.toFixed(2) : value))
.join(':');
}
@@ -332,9 +333,9 @@
: 'fixed'} z-[1001] min-w-36 rounded-xl bg-white dark:bg-[#1a1a1a] border border-gray-150 dark:border-white/6 shadow-xl p-0.5 flex flex-col overflow-hidden {className}"
style="{inlineAbove
? ''
- : `left: ${pos.x}px; ${pos.bottom != null ? `bottom: ${pos.bottom}px;` : `top: ${pos.top ?? -9999}px;`} ${menuMaxHeight
- ? `max-height: ${menuMaxHeight}px;`
- : ''}`} {anchorWidth ? `width: ${anchorWidth}px;` : ''} opacity: {ready
+ : `left: ${pos.x}px; ${pos.bottom != null ? `bottom: ${pos.bottom}px;` : `top: ${pos.top ?? -9999}px;`} ${
+ menuMaxHeight ? `max-height: ${menuMaxHeight}px;` : ''
+ }`} {anchorWidth ? `width: ${anchorWidth}px;` : ''} opacity: {ready
? 1
: 0}; pointer-events: {ready ? 'auto' : 'none'};"
onclick={(e) => e.stopPropagation()}
@@ -343,7 +344,9 @@
{#if header}
{@render header()}
-
+ {#if headerDivider}
+
+ {/if}
{/if}
@@ -392,4 +395,13 @@
{/each}
{/if}
+
+ {#if footer}
+
+ {#if footerDivider}
+
+ {/if}
+ {@render footer()}
+
+ {/if}
diff --git a/cptr/frontend/src/lib/components/GitBar.svelte b/cptr/frontend/src/lib/components/GitBar.svelte
index 9282e74..6e5b960 100644
--- a/cptr/frontend/src/lib/components/GitBar.svelte
+++ b/cptr/frontend/src/lib/components/GitBar.svelte
@@ -13,12 +13,14 @@
gitPull,
gitPush,
gitUncommit,
+ gitStash,
checkoutBranch,
createGitBranch
} from '$lib/apis/git';
import { gitStatusStore } from '$lib/stores/gitStatus.svelte';
import Icon from './Icon.svelte';
import DropdownMenu from './DropdownMenu.svelte';
+ import Modal from './Modal.svelte';
import { tooltip } from '$lib/tooltip';
import { t } from '$lib/i18n';
import Spinner from '$lib/components/common/Spinner.svelte';
@@ -32,10 +34,20 @@
let view = $state<'changes' | 'history'>('changes');
let showDiff = $state(false);
let showBranches = $state(false);
+ let branchBtnEl = $state();
+ let branchSearchInputEl = $state();
+ let newBranchInputEl = $state();
let commits = $state([]);
- let branchData = $state<{ current: string; local: string[]; remote: string[] } | null>(null);
+ let branchData = $state<{
+ current: string;
+ local: string[];
+ remote: string[];
+ all?: { name: string; is_current?: boolean; is_local?: boolean; upstream?: string | null }[];
+ } | null>(null);
+ let branchSearch = $state('');
let newBranchName = $state('');
let creatingBranch = $state(false);
+ let pendingCheckoutBranch = $state(null);
let selectedFile = $state(null);
let selectedCommit = $state(null);
let fileDiff = $state([]);
@@ -71,6 +83,21 @@
// Branch has never been pushed to remote
const needsPublish = $derived(!gitStatus?.upstream);
const unpushedCount = $derived(needsPublish ? (commits?.length ?? 0) : (gitStatus?.ahead ?? 0));
+ const filteredBranches = $derived.by(() => {
+ const branches = branchData?.all ?? [];
+ const query = branchSearch.trim().toLowerCase();
+ if (!query) return branches;
+ return branches.filter((branch) => branch.name.toLowerCase().includes(query));
+ });
+ const branchMenuItems = $derived(
+ filteredBranches.map((branch) => ({
+ label: branch.name,
+ icon: 'git-branch',
+ active: Boolean(branch.is_current),
+ check: true,
+ onclick: () => switchBranch(branch.name)
+ }))
+ );
// Convert git remote URL to browser URL
const remoteWebUrl = $derived.by(() => {
@@ -175,6 +202,20 @@
}
}
+ async function toggleBranches(e: MouseEvent) {
+ e.stopPropagation();
+ if (showBranches) {
+ showBranches = false;
+ return;
+ }
+ showBranches = true;
+ branchSearch = '';
+ creatingBranch = false;
+ newBranchName = '';
+ await loadBranches();
+ setTimeout(() => branchSearchInputEl?.focus(), 0);
+ }
+
function switchView(v: 'changes' | 'history') {
view = v;
showDiff = false;
@@ -273,11 +314,41 @@
}
async function switchBranch(branch: string) {
+ if (branch === gitStatus?.branch) {
+ showBranches = false;
+ return;
+ }
+ if (totalChanges > 0) {
+ pendingCheckoutBranch = branch;
+ showBranches = false;
+ return;
+ }
+ await performBranchCheckout(branch, 'bring');
+ }
+
+ async function performBranchCheckout(branch: string, changeMode: 'bring' | 'leave') {
loading = true;
- await checkoutBranch(workspacePath, branch);
- showBranches = false;
- loading = false;
- await refresh();
+ try {
+ if (changeMode === 'leave') {
+ const currentBranch = gitStatus?.branch || 'current branch';
+ const message = `Changes on ${currentBranch} before checking out ${branch}`;
+ const stashResult = (await gitStash(workspacePath, message)) as {
+ ok?: boolean;
+ message?: string;
+ };
+ if (!stashResult.ok) {
+ flash(stashResult.message || 'No changes stashed');
+ return;
+ }
+ }
+ await checkoutBranch(workspacePath, branch);
+ pendingCheckoutBranch = null;
+ } catch (e) {
+ flash(e instanceof Error ? e.message : 'Failed to switch branch');
+ } finally {
+ loading = false;
+ await refresh();
+ }
}
async function createBranch() {
@@ -490,12 +561,9 @@
>