This repository was archived by the owner on Jun 28, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathgit-status-handler.ts
More file actions
66 lines (58 loc) · 1.77 KB
/
Copy pathgit-status-handler.ts
File metadata and controls
66 lines (58 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { WebviewMessageHandler, HandlerContext } from "./types";
import { GitActions, BranchInfo } from "../../services/git-actions";
const GIT_COMMANDS = ["git-status-request"] as const;
function isGitMessage(
msg: unknown,
): msg is { command: (typeof GIT_COMMANDS)[number] } {
return (
typeof msg === "object" &&
msg !== null &&
"command" in msg &&
typeof (msg as Record<string, unknown>).command === "string" &&
GIT_COMMANDS.includes(
(msg as Record<string, unknown>).command as (typeof GIT_COMMANDS)[number],
)
);
}
export class GitStatusHandler implements WebviewMessageHandler {
readonly commands = [...GIT_COMMANDS];
async handle(
message: Record<string, unknown>,
ctx: HandlerContext,
): Promise<void> {
if (!isGitMessage(message)) return;
try {
const git = new GitActions();
const [branchInfo, status] = await Promise.all([
git.getCurrentBranchInfo(),
git.getRepositoryStatus(),
]);
const changedFiles: number =
(status.modified?.length ?? 0) +
(status.not_added?.length ?? 0) +
(status.created?.length ?? 0) +
(status.deleted?.length ?? 0) +
(status.renamed?.length ?? 0);
const staged: number = status.staged?.length ?? 0;
await ctx.webview.webview.postMessage({
type: "git-status-result",
branch: branchInfo.current,
upstream: branchInfo.upstream ?? null,
changedFiles,
staged,
ahead: status.ahead ?? 0,
behind: status.behind ?? 0,
});
} catch {
await ctx.webview.webview.postMessage({
type: "git-status-result",
branch: null,
upstream: null,
changedFiles: 0,
staged: 0,
ahead: 0,
behind: 0,
});
}
}
}