Skip to content
TypeScript

fix(server): read continued core.bare values during onboarding (#30174)

Harjot Singh8bc344bd9c31
diff --git a/packages/shared/src/git.test.ts b/packages/shared/src/git.test.ts
index ede9f059c57..a788abb1d1d 100644
--- a/packages/shared/src/git.test.ts
+++ b/packages/shared/src/git.test.ts
@@ -7,6 +7,7 @@ import {
formatGeneratedBranchName,
buildTemporaryWorktreeBranchName,
flattenTemporaryWorktreeBranchName,
+ isBareGitConfig,
isTemporaryWorktreeBranch,
normalizeGitRemoteUrl,
parseOriginUrlFromGitConfig,
@@ -64,6 +65,15 @@ describe("normalizeGitRemoteUrl", () => {
});
});
+describe("isBareGitConfig", () => {
+ it("reads continued bare values after comments and at end of input", () => {
+ const config = ["[core] bare = false # ignored backslash \\", 'bare = "tr\\', 'ue"'].join("\n");
+
+ expect(isBareGitConfig(config)).toBe(true);
+ expect(isBareGitConfig("[core]\nbare = tr\\\nue\\")).toBe(true);
+ });
+});
+
describe("parseOriginUrlFromGitConfig", () => {
it("reads the origin url and ignores other remotes", () => {
const config = [
diff --git a/packages/shared/src/git.ts b/packages/shared/src/git.ts
index 4ac89394c21..ba7c9f0a97b 100644
--- a/packages/shared/src/git.ts
+++ b/packages/shared/src/git.ts
@@ -170,6 +170,29 @@ export function normalizeGitRemoteUrl(value: string): string {
return repository === null ? lowerCaseRemote(value) : `${repository.host}/${repository.path}`;
}
+function* gitConfigLines(configText: string) {
+ let pending = "";
+ let quoted = false;
+ for (const line of configText.split(/\r?\n/)) {
+ let continued = false;
+ if (line.endsWith("\\")) {
+ for (const [token] of line.matchAll(/\\[\s\S]?|["#;]/g)) {
+ if (token === '"') quoted = !quoted;
+ else if (!quoted && (token === "#" || token === ";")) break;
+ else if (token === "\\") continued = true;
+ }
+ }
+ if (continued) {
+ pending += line.slice(0, -1);
+ continue;
+ }
+ yield pending + line;
+ pending = "";
+ quoted = false;
+ }
+ if (pending.length > 0) yield pending;
+}
+
function parseGitConfigValue(raw: string): string {
let out = "";
let quoted = false;
@@ -193,7 +216,7 @@ function parseGitConfigValue(raw: string): string {
export function isBareGitConfig(configText: string): boolean {
let inCore = false;
let bare = false;
- for (const rawLine of configText.split(/\r?\n/)) {
+ for (const rawLine of gitConfigLines(configText)) {
let line = rawLine.trim();
// Git accepts the first setting on the section header's own line.
const header = /^\[\s*([^\]]*?)\s*\](.*)$/.exec(line);