mirror of
https://github.com/open-webui/computer.git
synced 2026-11-22 21:50:43 -05:00
refac
This commit is contained in:
1 parent
bcfac08952
commit
08d2afffc5
4 files changed
+80
-21
No files matched your search
@@ -45,6 +45,9 @@ export const getGitShow = (root: string, ref: string) =>
|
||||
export const getGitBranches = (root: string) =>
|
||||
fetchJSON(`/api/git/branches?root=${encodeURIComponent(root)}`);
|
||||
|
||||
export const getGitStashes = (root: string) =>
|
||||
fetchJSON(`/api/git/stashes?root=${encodeURIComponent(root)}`);
|
||||
|
||||
export const stageFiles = (root: string, files: string[]) =>
|
||||
fetchJSON('/api/git/stage', jsonBody({ root, files }));
|
||||
|
||||
@@ -75,6 +78,9 @@ export const gitUncommit = (root: string) => fetchJSON('/api/git/uncommit', json
|
||||
export const gitStash = (root: string, message?: string) =>
|
||||
fetchJSON('/api/git/stash', jsonBody({ root, message }));
|
||||
|
||||
export const gitUnstash = (root: string, index = 0) =>
|
||||
fetchJSON('/api/git/unstash', jsonBody({ root, index }));
|
||||
|
||||
export const createGitBranch = (root: string, name: string) =>
|
||||
fetchJSON('/api/git/branch', jsonBody({ root, name }));
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
getGitDiff,
|
||||
getGitShow,
|
||||
getGitBranches,
|
||||
getGitStatusFresh,
|
||||
getGitStashes,
|
||||
stageFiles,
|
||||
unstageFiles,
|
||||
discardChanges,
|
||||
@@ -14,6 +16,7 @@
|
||||
gitPush,
|
||||
gitUncommit,
|
||||
gitStash,
|
||||
gitUnstash,
|
||||
checkoutBranch,
|
||||
createGitBranch
|
||||
} from '$lib/apis/git';
|
||||
@@ -25,7 +28,14 @@
|
||||
import { t } from '$lib/i18n';
|
||||
import Spinner from '$lib/components/common/Spinner.svelte';
|
||||
|
||||
type GitFile = { path: string; status: string; staged: boolean };
|
||||
type GitFile = {
|
||||
path: string;
|
||||
status: string;
|
||||
staged: boolean;
|
||||
unstaged?: boolean;
|
||||
staged_status?: string;
|
||||
unstaged_status?: string;
|
||||
};
|
||||
type DiffHunk = { header: string; lines: { type: string; content: string }[] };
|
||||
type DiffFile = { path: string; hunks: DiffHunk[] };
|
||||
type Commit = { hash: string; short_hash: string; author: string; date: string; message: string };
|
||||
@@ -253,6 +263,10 @@
|
||||
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();
|
||||
@@ -326,12 +340,34 @@
|
||||
await performBranchCheckout(branch, 'bring');
|
||||
}
|
||||
|
||||
async function restoreStashedChangesForBranch(branch: string) {
|
||||
const stashes = (await getGitStashes(workspacePath)) as { message?: string }[];
|
||||
const index = stashes.findIndex((stash) => {
|
||||
const message = stash.message ?? '';
|
||||
return (
|
||||
message.includes(`cptr-branch-switch:${branch}`) ||
|
||||
message.includes(`Changes on ${branch} before checking out`)
|
||||
);
|
||||
});
|
||||
if (index < 0) return;
|
||||
|
||||
const freshStatus = (await getGitStatusFresh(workspacePath)) as { files?: GitFile[] };
|
||||
if ((freshStatus.files ?? []).length > 0) return;
|
||||
|
||||
const result = (await gitUnstash(workspacePath, index)) as { ok?: boolean; message?: string };
|
||||
if (result.ok) {
|
||||
flash(`Restored changes for ${branch}`);
|
||||
} else {
|
||||
flash(result.message || `Could not restore changes for ${branch}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function performBranchCheckout(branch: string, changeMode: 'bring' | 'leave') {
|
||||
loading = true;
|
||||
try {
|
||||
if (changeMode === 'leave') {
|
||||
const currentBranch = gitStatus?.branch || 'current branch';
|
||||
const message = `Changes on ${currentBranch} before checking out ${branch}`;
|
||||
const message = `cptr-branch-switch:${currentBranch}`;
|
||||
const stashResult = (await gitStash(workspacePath, message)) as {
|
||||
ok?: boolean;
|
||||
message?: string;
|
||||
@@ -343,6 +379,7 @@
|
||||
}
|
||||
await checkoutBranch(workspacePath, branch);
|
||||
pendingCheckoutBranch = null;
|
||||
await restoreStashedChangesForBranch(branch);
|
||||
} catch (e) {
|
||||
flash(e instanceof Error ? e.message : 'Failed to switch branch');
|
||||
} finally {
|
||||
@@ -806,7 +843,7 @@
|
||||
|
||||
<!-- File list -->
|
||||
<div class="flex-1 overflow-y-auto">
|
||||
{#each gitStatus?.files ?? [] as file (`${file.path}:${file.staged}`)}
|
||||
{#each gitStatus?.files ?? [] as file (file.path)}
|
||||
{@const fp = fPath(file.path)}
|
||||
{@const sc = statusChar(file.status)}
|
||||
<button
|
||||
|
||||
@@ -14,6 +14,9 @@ export interface GitFile {
|
||||
path: string;
|
||||
status: string;
|
||||
staged: boolean;
|
||||
unstaged?: boolean;
|
||||
staged_status?: string;
|
||||
unstaged_status?: string;
|
||||
}
|
||||
|
||||
export interface GitStatus {
|
||||
|
||||
+31
-18
@@ -68,7 +68,32 @@ async def status(root: str) -> dict[str, Any]:
|
||||
ahead = 0
|
||||
behind = 0
|
||||
has_ab = False
|
||||
files: list[dict] = []
|
||||
files_by_path: dict[str, dict[str, Any]] = {}
|
||||
|
||||
def add_file(
|
||||
path: str,
|
||||
status_text: str,
|
||||
*,
|
||||
staged: bool = False,
|
||||
unstaged: bool = False,
|
||||
) -> None:
|
||||
entry = files_by_path.setdefault(
|
||||
path,
|
||||
{
|
||||
"path": path,
|
||||
"status": status_text,
|
||||
"staged": False,
|
||||
"unstaged": False,
|
||||
},
|
||||
)
|
||||
if staged:
|
||||
entry["staged"] = True
|
||||
entry["staged_status"] = status_text
|
||||
if unstaged:
|
||||
entry["unstaged"] = True
|
||||
entry["unstaged_status"] = status_text
|
||||
# Prefer the working-tree status for the row label when both sides exist.
|
||||
entry["status"] = status_text
|
||||
|
||||
for line in out.splitlines():
|
||||
if line.startswith("# branch.head "):
|
||||
@@ -94,30 +119,18 @@ async def status(root: str) -> dict[str, Any]:
|
||||
staged_code = xy[0]
|
||||
unstaged_code = xy[1]
|
||||
if staged_code != ".":
|
||||
files.append(
|
||||
{
|
||||
"path": path,
|
||||
"status": _status_char(staged_code),
|
||||
"staged": True,
|
||||
}
|
||||
)
|
||||
add_file(path, _status_char(staged_code), staged=True)
|
||||
if unstaged_code != ".":
|
||||
files.append(
|
||||
{
|
||||
"path": path,
|
||||
"status": _status_char(unstaged_code),
|
||||
"staged": False,
|
||||
}
|
||||
)
|
||||
add_file(path, _status_char(unstaged_code), unstaged=True)
|
||||
elif line.startswith("? "):
|
||||
# Untracked
|
||||
path = line[2:]
|
||||
files.append({"path": path, "status": "untracked", "staged": False})
|
||||
add_file(path, "untracked", unstaged=True)
|
||||
elif line.startswith("u "):
|
||||
# Unmerged
|
||||
parts = line.split(" ", 10)
|
||||
path = parts[-1]
|
||||
files.append({"path": path, "status": "conflict", "staged": False})
|
||||
add_file(path, "conflict", unstaged=True)
|
||||
|
||||
# upstream is set but remote branch doesn't exist yet (no ab line)
|
||||
# — treat as unpublished so the frontend shows "Publish"
|
||||
@@ -136,7 +149,7 @@ async def status(root: str) -> dict[str, Any]:
|
||||
"remote_url": remote_url,
|
||||
"ahead": ahead,
|
||||
"behind": behind,
|
||||
"files": files,
|
||||
"files": list(files_by_path.values()),
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user