From fcc26371f9b38cda3ec5caf1abb6f960892e179d Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 9 Aug 2026 12:36:23 -0600 Subject: [PATCH] refac --- cptr/frontend/src/lib/apis/git.ts | 84 +++- .../src/lib/components/Admin/Git.svelte | 77 ---- .../frontend/src/lib/components/GitBar.svelte | 67 +-- .../src/lib/components/Settings/Git.svelte | 432 ++++++++++++++++++ .../src/lib/components/SettingsModal.svelte | 106 +++-- .../src/lib/components/Sidebar.svelte | 6 + cptr/frontend/src/lib/stores.ts | 6 + cptr/frontend/src/routes/+layout.svelte | 17 +- cptr/routers/git.py | 343 +++++++++++--- cptr/utils/audit.py | 13 +- cptr/utils/gh.py | 229 ++++++++++ cptr/utils/git.py | 262 ++++++++--- 12 files changed, 1382 insertions(+), 260 deletions(-) delete mode 100644 cptr/frontend/src/lib/components/Admin/Git.svelte create mode 100644 cptr/frontend/src/lib/components/Settings/Git.svelte create mode 100644 cptr/utils/gh.py diff --git a/cptr/frontend/src/lib/apis/git.ts b/cptr/frontend/src/lib/apis/git.ts index 1dd15d0..0a554fc 100644 --- a/cptr/frontend/src/lib/apis/git.ts +++ b/cptr/frontend/src/lib/apis/git.ts @@ -3,6 +3,59 @@ */ import { fetchJSON, jsonBody } from '$lib/apis'; +export interface GitIdentity { + name?: string; + email?: string; + name_source?: string; + email_source?: string; +} + +export interface GhAccount { + state?: string; + active?: boolean; + host?: string; + login?: string; + tokenSource?: string; + scopes?: string; + gitProtocol?: string; +} + +export interface GitSettingsConfig { + root: string; + git: { + installed: boolean; + version?: string | null; + is_repo: boolean; + identity?: GitIdentity; + credential_helpers?: string[]; + remote_url?: string; + }; + app_identity?: GitIdentity; + gh: { + installed: boolean; + version?: string | null; + hosts?: Record; + message?: string; + }; + permissions: { + can_manage_gh: boolean; + can_manage_commit_model: boolean; + }; +} + +export interface GhLoginStatus { + session_id: string; + status: 'pending' | 'complete' | 'failed' | 'expired'; + verification_uri?: string; + user_code?: string; + auth?: GitSettingsConfig['gh']; +} + +export interface GitOperationResult { + ok: boolean; + message: string; +} + // Deduplicate concurrent getGitStatus calls. // Multiple components (layout, GitBar, FileEditor × N) all fetch on mount; // this ensures they share a single in-flight request and a brief result cache. @@ -56,6 +109,29 @@ export const createGitWorktree = (root: string, branch: string, path?: string) = export const getGitStashes = (root: string) => fetchJSON(`/api/git/stashes?root=${encodeURIComponent(root)}`); +export const getGitConfig = (root?: string) => + fetchJSON( + `/api/git/config${root ? `?root=${encodeURIComponent(root)}` : ''}` + ); + +export const startGhLogin = (hostname = 'github.com', git_protocol = 'https') => + fetchJSON('/api/git/gh/login/start', jsonBody({ hostname, git_protocol })); + +export const getGhLoginStatus = (session_id: string) => + fetchJSON('/api/git/gh/login/status', jsonBody({ session_id })); + +export const cancelGhLogin = (session_id: string) => + fetchJSON('/api/git/gh/login/cancel', jsonBody({ session_id })); + +export const ghLogout = (hostname = 'github.com', user?: string) => + fetchJSON('/api/git/gh/logout', jsonBody({ hostname, user })); + +export const ghSwitch = (hostname: string, user: string) => + fetchJSON('/api/git/gh/switch', jsonBody({ hostname, user })); + +export const ghSetupGit = (hostname = 'github.com') => + fetchJSON('/api/git/gh/setup-git', jsonBody({ hostname })); + export const stageFiles = (root: string, files: string[]) => fetchJSON('/api/git/stage', jsonBody({ root, files })); @@ -74,9 +150,11 @@ export const generateGitCommitMessage = (root: string, modelId?: string) => jsonBody({ root, model_id: modelId || undefined }) ); -export const gitPull = (root: string) => fetchJSON('/api/git/pull', jsonBody({ root })); +export const gitPull = (root: string) => + fetchJSON('/api/git/pull', jsonBody({ root })); -export const gitFetch = (root: string) => fetchJSON('/api/git/fetch', jsonBody({ root })); +export const gitFetch = (root: string) => + fetchJSON('/api/git/fetch', jsonBody({ root })); export const gitPush = ( root: string, @@ -85,7 +163,7 @@ export const gitPush = ( set_upstream = false, branch }: { force?: boolean; set_upstream?: boolean; branch?: string } = {} -) => fetchJSON('/api/git/push', jsonBody({ root, force, set_upstream, branch })); +) => fetchJSON('/api/git/push', jsonBody({ root, force, set_upstream, branch })); export const gitUncommit = (root: string) => fetchJSON('/api/git/uncommit', jsonBody({ root })); diff --git a/cptr/frontend/src/lib/components/Admin/Git.svelte b/cptr/frontend/src/lib/components/Admin/Git.svelte deleted file mode 100644 index 3f3b954..0000000 --- a/cptr/frontend/src/lib/components/Admin/Git.svelte +++ /dev/null @@ -1,77 +0,0 @@ - - -
- {#if loading} -
- {:else} -
-

{$t('admin.git')}

- -
-
- - {$t('admin.gitCommitMessageModel')} - -
- -
-
-

- {$t('admin.gitCommitMessageModelHint')} -

-
-
- -
- -
- {/if} -
diff --git a/cptr/frontend/src/lib/components/GitBar.svelte b/cptr/frontend/src/lib/components/GitBar.svelte index 4ec5a61..a5302ae 100644 --- a/cptr/frontend/src/lib/components/GitBar.svelte +++ b/cptr/frontend/src/lib/components/GitBar.svelte @@ -361,21 +361,26 @@ async function doCommit() { if (!commitSummary.trim() || !stagedFiles.length) return; loading = true; - await stageFiles( - workspacePath, - stagedFiles.map((f) => f.path) - ); - const msg = commitDescription.trim() - ? `${commitSummary.trim()}\n\n${commitDescription.trim()}` - : commitSummary.trim(); - await gitCommit(workspacePath, msg); - commitSummary = ''; - commitDescription = ''; - flash($t('git.committed')); - selectedFile = null; - fileDiff = []; - loading = false; - await refresh(); + try { + await stageFiles( + workspacePath, + stagedFiles.map((f) => f.path) + ); + const msg = commitDescription.trim() + ? `${commitSummary.trim()}\n\n${commitDescription.trim()}` + : commitSummary.trim(); + await gitCommit(workspacePath, msg); + commitSummary = ''; + commitDescription = ''; + flash($t('git.committed')); + selectedFile = null; + fileDiff = []; + } catch (e) { + flash(e instanceof Error ? e.message : 'Commit failed'); + } finally { + loading = false; + await refresh(); + } } async function generateCommitMessage() { @@ -407,10 +412,15 @@ async function doPull() { loading = true; - const d = await gitPull(workspacePath); - flash(d.ok ? $t('git.pulled') : d.message); - loading = false; - await refresh({ force: true }); + try { + const d = await gitPull(workspacePath); + flash(d.ok ? $t('git.pulled') : d.message); + } catch (e) { + flash(e instanceof Error ? e.message : 'Pull failed'); + } finally { + loading = false; + await refresh({ force: true }); + } } async function doFetch() { @@ -431,13 +441,18 @@ async function doPush() { loading = true; - const d = await gitPush(workspacePath, { - set_upstream: needsPublish, - branch: needsPublish ? gitStatus?.branch : undefined - }); - flash(d.ok ? $t('git.pushed') : d.message); - loading = false; - await refresh({ force: true }); + try { + const d = await gitPush(workspacePath, { + set_upstream: needsPublish, + branch: needsPublish ? gitStatus?.branch : undefined + }); + flash(d.ok ? $t('git.pushed') : d.message); + } catch (e) { + flash(e instanceof Error ? e.message : 'Push failed'); + } finally { + loading = false; + await refresh({ force: true }); + } } async function switchBranch(branch: string) { diff --git a/cptr/frontend/src/lib/components/Settings/Git.svelte b/cptr/frontend/src/lib/components/Settings/Git.svelte new file mode 100644 index 0000000..d77f441 --- /dev/null +++ b/cptr/frontend/src/lib/components/Settings/Git.svelte @@ -0,0 +1,432 @@ + + +
+ {#if loading} +
+ {:else} +
+

{tr('admin.git')}

+ +

Git

+
+
+
+
Commit author
+
+ {identityLabel(detectedIdentity)} +
+
+ {identitySource(detectedIdentity)} +
+
+ + {identityComplete ? 'Git config' : 'Needs identity'} + +
+
+ +
+ +
+
+
+ + +
+
+ + +
+
+

+ Git config wins. This fallback only fills missing commit author details. +

+
+
+
+ +

GitHub

+
+
+
+
GitHub CLI
+
+ {ghSummary()} +
+
+ {config?.gh.message || config?.gh.version || 'GitHub CLI'} +
+
+ {#if config?.gh.installed && !ghHosts.length && config.permissions.can_manage_gh} + + {/if} +
+ + {#if login} +
+
+
+ {login.verification_uri || 'Waiting for GitHub CLI...'} +
+
+ {login.user_code} +
+
+
+ {#if login.verification_uri} + Open + {/if} + + +
+
+ {/if} +
+ +
+ +
+ {#if !config?.gh.installed} +
+ {config?.gh.message || 'GitHub CLI is not installed.'} +
+ {:else if ghHosts.length} + {#each ghHosts as [host, accounts]} + {@const active = activeAccount(accounts)} +
+
+
+ {active?.login || 'Not signed in'} +
+
+ {host} · {active?.state || 'unknown'} · {active?.gitProtocol || 'git'} +
+ {#if accounts.length > 1} +
+ {#each accounts as account, index (accountKey(account, index))} + + {account.login || 'unknown'} + {#if account.active} + (active) + {:else if config.permissions.can_manage_gh && account.login} + + {/if} + + {/each} +
+ {/if} +
+ {#if config.permissions.can_manage_gh} +
+ + + +
+ {/if} +
+ {/each} + {:else} +
+
+
+ {config.permissions.can_manage_gh ? 'Not signed in' : 'Managed by admin'} +
+
+ {config.gh.version} +
+
+ {#if config.permissions.can_manage_gh} + + {/if} +
+ {/if} +
+
+
+ + {#if isAdmin} +

+ {tr('sidebar.admin')} +

+
+
+
+
+ {tr('admin.gitCommitMessageModel')} +
+
+ {tr('admin.gitCommitMessageModelHint')} +
+
+
+ +
+
+
+ {/if} +
+ +
+ +
+ {/if} +
diff --git a/cptr/frontend/src/lib/components/SettingsModal.svelte b/cptr/frontend/src/lib/components/SettingsModal.svelte index b522de0..1d42ff5 100644 --- a/cptr/frontend/src/lib/components/SettingsModal.svelte +++ b/cptr/frontend/src/lib/components/SettingsModal.svelte @@ -1,5 +1,6 @@