Open History to see the last commit
1/**
2 * Pure parsing functions for git command output.
3 *
4 * Extracted from GitCore.ts for testability. These functions parse raw stdout
5 * from various git commands into structured data.
6 */
7
8export function parseBranchAb(value: string): { ahead: number; behind: number } {
9 const match = value.match(/^\+(\d+)\s+-(\d+)$/);
10 if (!match) return { ahead: 0, behind: 0 };
11 return {
12 ahead: Number(match[1] ?? "0"),
13 behind: Number(match[2] ?? "0"),
14 };
15}
16
17export interface NumstatEntry {
18 path: string;
19 insertions: number;
20 deletions: number;
21}
22
23export function parseNumstatEntries(stdout: string): NumstatEntry[] {
24 const entries: NumstatEntry[] = [];
25 for (const line of stdout.split(/\r?\n/g)) {
26 if (line.trim().length === 0) continue;
27 const [addedRaw, deletedRaw, ...pathParts] = line.split("\t");
28 const rawPath =
29 pathParts.length > 1 ? (pathParts.at(-1) ?? "").trim() : pathParts.join("\t").trim();
30 if (rawPath.length === 0) continue;
31 const added = Number.parseInt(addedRaw ?? "0", 10);
32 const deleted = Number.parseInt(deletedRaw ?? "0", 10);
33 const renameArrowIndex = rawPath.indexOf(" => ");
34 const normalizedPath =
35 renameArrowIndex >= 0 ? rawPath.slice(renameArrowIndex + " => ".length).trim() : rawPath;
36 entries.push({
37 path: normalizedPath.length > 0 ? normalizedPath : rawPath,
38 insertions: Number.isFinite(added) ? added : 0,
39 deletions: Number.isFinite(deleted) ? deleted : 0,
40 });
41 }
42 return entries;
43}
44
45export function parsePorcelainPath(line: string): string | null {
46 if (line.startsWith("? ") || line.startsWith("! ")) {
47 const simple = line.slice(2).trim();
48 return simple.length > 0 ? simple : null;
49 }
50
51 if (!(line.startsWith("1 ") || line.startsWith("2 ") || line.startsWith("u "))) {
52 return null;
53 }
54
55 const tabIndex = line.indexOf("\t");
56 if (tabIndex >= 0) {
57 const fromTab = line.slice(tabIndex + 1);
58 const [filePath] = fromTab.split("\t");
59 return filePath?.trim().length ? filePath.trim() : null;
60 }
61
62 const parts = line.trim().split(/\s+/g);
63 const filePath = parts.at(-1) ?? "";
64 return filePath.length > 0 ? filePath : null;
65}
66
67export function parseBranchLine(line: string): { name: string; current: boolean } | null {
68 const trimmed = line.trim();
69 if (trimmed.length === 0) return null;
70
71 const name = trimmed.replace(/^[*+]\s+/, "");
72 // Exclude symbolic refs like: "origin/HEAD -> origin/main".
73 // Exclude detached HEAD pseudo-refs like: "(HEAD detached at origin/main)".
74 if (name.includes(" -> ") || name.startsWith("(")) return null;
75
76 return {
77 name,
78 current: trimmed.startsWith("* "),
79 };
80}
81
82export function parseRemoteNames(stdout: string): ReadonlyArray<string> {
83 return stdout
84 .split("\n")
85 .map((line) => line.trim())
86 .filter((line) => line.length > 0)
87 .toSorted((a, b) => b.length - a.length);
88}
89
90export function sanitizeRemoteName(value: string): string {
91 const sanitized = value
92 .trim()
93 .replace(/[^A-Za-z0-9._-]+/g, "-")
94 .replace(/^-+|-+$/g, "");
95 return sanitized.length > 0 ? sanitized : "fork";
96}
97
98export function normalizeRemoteUrl(value: string): string {
99 return value
100 .trim()
101 .replace(/\/+$/g, "")
102 .replace(/\.git$/i, "")
103 .toLowerCase();
104}
105
106export function parseRemoteFetchUrls(stdout: string): Map<string, string> {
107 const remotes = new Map<string, string>();
108 for (const line of stdout.split("\n")) {
109 const trimmed = line.trim();
110 if (trimmed.length === 0) continue;
111 const match = /^(\S+)\s+(\S+)\s+\((fetch|push)\)$/.exec(trimmed);
112 if (!match) continue;
113 const [, remoteName = "", remoteUrl = "", direction = ""] = match;
114 if (direction !== "fetch" || remoteName.length === 0 || remoteUrl.length === 0) {
115 continue;
116 }
117 remotes.set(remoteName, remoteUrl);
118 }
119 return remotes;
120}
121
122export function parseRemoteRefWithRemoteNames(
123 branchName: string,
124 remoteNames: ReadonlyArray<string>,
125): { remoteRef: string; remoteName: string; localBranch: string } | null {
126 const trimmedBranchName = branchName.trim();
127 if (trimmedBranchName.length === 0) return null;
128
129 for (const remoteName of remoteNames) {
130 const remotePrefix = `${remoteName}/`;
131 if (!trimmedBranchName.startsWith(remotePrefix)) {
132 continue;
133 }
134 const localBranch = trimmedBranchName.slice(remotePrefix.length).trim();
135 if (localBranch.length === 0) {
136 return null;
137 }
138 return {
139 remoteRef: trimmedBranchName,
140 remoteName,
141 localBranch,
142 };
143 }
144
145 return null;
146}
147
148export function parseTrackingBranchByUpstreamRef(
149 stdout: string,
150 upstreamRef: string,
151): string | null {
152 for (const line of stdout.split("\n")) {
153 const trimmedLine = line.trim();
154 if (trimmedLine.length === 0) {
155 continue;
156 }
157 const [branchNameRaw, upstreamBranchRaw = ""] = trimmedLine.split("\t");
158 const branchName = branchNameRaw?.trim() ?? "";
159 const upstreamBranch = upstreamBranchRaw.trim();
160 if (branchName.length === 0 || upstreamBranch.length === 0) {
161 continue;
162 }
163 if (upstreamBranch === upstreamRef) {
164 return branchName;
165 }
166 }
167
168 return null;
169}
170
171export function deriveLocalBranchNameFromRemoteRef(branchName: string): string | null {
172 const separatorIndex = branchName.indexOf("/");
173 if (separatorIndex <= 0 || separatorIndex === branchName.length - 1) {
174 return null;
175 }
176 const localBranch = branchName.slice(separatorIndex + 1).trim();
177 return localBranch.length > 0 ? localBranch : null;
178}
179
180export function parseDefaultBranchFromRemoteHeadRef(
181 value: string,
182 remoteName: string,
183): string | null {
184 const trimmed = value.trim();
185 const prefix = `refs/remotes/${remoteName}/`;
186 if (!trimmed.startsWith(prefix)) {
187 return null;
188 }
189 const branch = trimmed.slice(prefix.length).trim();
190 return branch.length > 0 ? branch : null;
191}