From 3e24a08f8ec8472dcbbc46e7e39ab35aaa4a6f4f Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 1 Nov 2026 22:41:22 +0900 Subject: [PATCH] refac --- src/lib/apis/terminal/git.ts | 28 ++- src/lib/components/chat/ChatTabs.svelte | 1 + .../components/chat/ChatTabs/GitBar.svelte | 196 +++++++++++------- .../components/chat/ChatTabs/GitDiff.svelte | 59 +++++- .../chat/ChatTabs/GitPullRequests.svelte | 16 +- 5 files changed, 211 insertions(+), 89 deletions(-) diff --git a/src/lib/apis/terminal/git.ts b/src/lib/apis/terminal/git.ts index a7ae504b25..e8b62e50fd 100644 --- a/src/lib/apis/terminal/git.ts +++ b/src/lib/apis/terminal/git.ts @@ -18,6 +18,7 @@ export type GitStatus = { branch: string; head?: string; upstream?: string; + remote_url?: string; ahead: number; behind: number; files: GitFile[]; @@ -31,6 +32,22 @@ export type GitCommit = { }; export type GitBranch = { name: string; current: boolean; upstream: string; is_local?: boolean }; +export function gitRemoteWebUrl(remote = ''): string { + const scp = remote.match(/^[^/@:\s]+@([^/:\s]+):(.+)$/); + try { + const url = new URL(scp ? `https://${scp[1]}/${scp[2]}` : remote); + if (url.protocol === 'ssh:') { + return new URL(`https://${url.hostname}${url.pathname.replace(/\.git\/?$/, '')}`).href; + } + if (!['http:', 'https:'].includes(url.protocol)) return ''; + url.username = url.password = url.search = url.hash = ''; + url.pathname = url.pathname.replace(/\.git\/?$/, ''); + return url.href; + } catch { + return ''; + } +} + export async function gitRequest( connection: TerminalConnection, chatId: string | null, @@ -53,8 +70,17 @@ export async function gitRequest( }); if (!response.ok) { const error = await response.json().catch(() => null); + if (response.status === 404 && error?.detail === 'Not Found') { + throw new Error( + 'Git support is unavailable on this connection. Update and restart Open Terminal.' + ); + } throw new Error( - typeof error?.detail === 'string' ? error.detail : `Git request failed (${response.status})` + typeof error?.detail === 'string' + ? error.detail + : typeof error?.error === 'string' + ? error.error + : `Git request failed (${response.status})` ); } return response.json(); diff --git a/src/lib/components/chat/ChatTabs.svelte b/src/lib/components/chat/ChatTabs.svelte index 4403aff7aa..be0e1c5928 100644 --- a/src/lib/components/chat/ChatTabs.svelte +++ b/src/lib/components/chat/ChatTabs.svelte @@ -684,6 +684,7 @@ class:hidden={!$showTabs || !panelTarget} use:placePanel={$showTabs ? panelTarget : null} class="h-full w-full min-h-0 flex flex-col" + style:--chat-tabs-dark-background={largeScreen ? 'var(--color-gray-900)' : 'var(--color-gray-850)'} id="controls-container" >
diff --git a/src/lib/components/chat/ChatTabs/GitBar.svelte b/src/lib/components/chat/ChatTabs/GitBar.svelte index 58ed7c06f8..4f4a934b6a 100644 --- a/src/lib/components/chat/ChatTabs/GitBar.svelte +++ b/src/lib/components/chat/ChatTabs/GitBar.svelte @@ -4,6 +4,7 @@ import { gitRequest, gitAction, + gitRemoteWebUrl, type GitStatus, type GitFile, type GitCommit, @@ -11,6 +12,7 @@ } from '$lib/apis/terminal/git'; import Icon from '../FileNav/Icon.svelte'; import GitDiff from './GitDiff.svelte'; + import { parseDiffRows } from '../Messages/diff'; import Dropdown from '../../common/Dropdown.svelte'; import DropdownMenu from '../../common/DropdownMenu.svelte'; import ConfirmDialog from '../../common/ConfirmDialog.svelte'; @@ -34,6 +36,14 @@ let selected: { path: string; staged: boolean; untracked: boolean } | null = null; let selectedCommit: GitCommit | null = null; let diff = ''; + $: diffStats = parseDiffRows(diff).reduce( + (counts, row) => { + if (row.type === 'addition') counts.additions++; + if (row.type === 'deletion') counts.deletions++; + return counts; + }, + { additions: 0, deletions: 0 } + ); let commits: GitCommit[] = []; let branches: GitBranch[] = []; let branchSearch = ''; @@ -54,6 +64,7 @@ let summary = ''; let description = ''; let error = ''; + let detectionError = ''; let busy = false; let loadingDiff = false; let confirmDiscard = false; @@ -76,6 +87,16 @@ $: additions = status?.files.reduce((sum, file) => sum + file.additions, 0) ?? 0; $: deletions = status?.files.reduce((sum, file) => sum + file.deletions, 0) ?? 0; $: branchLabel = status?.branch === '(detached)' ? status.head?.slice(0, 7) : status?.branch; + $: remoteWebUrl = gitRemoteWebUrl(status?.remote_url); + $: remoteHost = remoteWebUrl ? new URL(remoteWebUrl).hostname : ''; + $: remotePlatform = + remoteHost === 'github.com' + ? 'GitHub' + : remoteHost.includes('gitlab') + ? 'GitLab' + : remoteHost === 'bitbucket.org' + ? 'Bitbucket' + : remoteHost; $: syncAction = !status?.upstream ? 'publish' : status.behind > 0 @@ -110,6 +131,7 @@ const task = gitRequest(connection, chatId, directory, 'status') .then((next) => { if (current !== generation) return; + detectionError = ''; if (status?.root && status.root !== next.root) resetSelection(); status = next.is_repo ? next : null; if (next.root && next.root !== prRoot) { @@ -131,8 +153,11 @@ ) resetSelection(); }) - .catch(() => { - if (current === generation) status = null; + .catch((reason) => { + if (current === generation) { + status = null; + detectionError = String(reason instanceof Error ? reason.message : reason); + } }) .finally(() => { if (current === generation) pending = null; @@ -155,6 +180,7 @@ pending = null; status = null; error = ''; + detectionError = ''; expanded = false; showBranches = showWorktrees = @@ -278,9 +304,6 @@ files.flatMap((file) => [file.path, ...(file.original_path ? [file.original_path] : [])]) ) ]; - const openFile = () => { - if (selected && status?.root) onOpenFile(`${status.root}/${selected.path}`); - }; const toggle = () => { expanded = !expanded; if (expanded) { @@ -343,14 +366,14 @@ }} >
{/if} -
+
@@ -454,12 +477,12 @@ @@ -545,22 +568,18 @@ >{/if} + + + -
{#if error}{/if} {#if expanded}
{#each ['changes', 'history', ...(prCapabilities?.available ? ['pull requests'] : [])] as section}{/each}
+ {#if remoteWebUrl} + + + + {/if}
act(allStaged ? 'unstage' : 'stage', { paths: paths(status?.files ?? []) })} > - {/if} - {file.path.split('/').pop()} { + if (status?.root) + onOpenFile(`${status.root.replace(/\/$/, '')}/${file.path}`); + }}>{file.path.split('/').pop()} - + +
{#if file.binary}{/if}
act('commit', { message: [summary.trim(), description.trim()].filter(Boolean).join('\n\n') @@ -789,7 +827,7 @@ bind:value={summary} />