Skip to content

Commit 1b42b16

Browse files
authored
fix(server): preserve spaces in Git remote URLs (pingdotgg#34324)
1 parent b9381c2 commit 1b42b16

4 files changed

Lines changed: 109 additions & 40 deletions

File tree

‎apps/server/src/vcs/GitVcsDriver.test.ts‎

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -102,35 +102,54 @@ runVcsDriverContractSuite<GitVcsDriver.GitVcsDriver, GitContractError>({
102102
},
103103
});
104104

105-
it.effect("lists the fetch and push URLs of a partial clone remote", () =>
106-
Effect.gen(function* () {
107-
const fileSystem = yield* FileSystem.FileSystem;
108-
const cwd = yield* fileSystem.makeTempDirectoryScoped({
109-
prefix: "t3-partial-clone-remotes-",
110-
});
111-
const driver = yield* GitVcsDriver.makeVcsDriverShape();
112-
yield* runGit(cwd, ["init"]);
113-
yield* runGit(cwd, ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"]);
114-
yield* runGit(cwd, [
115-
"remote",
116-
"set-url",
117-
"--push",
118-
"origin",
119-
"[email protected]:octocat/t3code.git",
120-
]);
121-
yield* runGit(cwd, ["config", "remote.origin.promisor", "true"]);
122-
yield* runGit(cwd, ["config", "remote.origin.partialclonefilter", "blob:none"]);
123-
124-
const result = yield* driver.listRemotes(cwd);
125-
assert.deepEqual(result.remotes, [
126-
{
127-
name: "origin",
128-
url: "https://github.com/pingdotgg/t3code.git",
129-
pushUrl: Option.some("[email protected]:octocat/t3code.git"),
130-
isPrimary: true,
131-
},
132-
]);
133-
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
105+
it.effect.each([
106+
{
107+
fetchUrl: "https://github.com/pingdotgg/t3code.git",
108+
pushUrl: "[email protected]:octocat/t3code.git",
109+
lineEnding: "\n",
110+
},
111+
{ fetchUrl: "../team repos/shared.git", pushUrl: "../team repos/push.git", lineEnding: "\n" },
112+
{
113+
fetchUrl: " ../team repos/shared (fetch) [blob:none].git ",
114+
pushUrl: "../team repos/push (push).git\t",
115+
lineEnding: "\r\n",
116+
},
117+
])(
118+
"lists the fetch and push URLs of a partial clone remote (%j)",
119+
({ fetchUrl, pushUrl, lineEnding }) =>
120+
Effect.gen(function* () {
121+
const fileSystem = yield* FileSystem.FileSystem;
122+
const cwd = yield* fileSystem.makeTempDirectoryScoped({
123+
prefix: "t3-partial-clone-remotes-",
124+
});
125+
const process = yield* VcsProcess.VcsProcess;
126+
const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
127+
Effect.provideService(VcsProcess.VcsProcess, {
128+
run: (input) =>
129+
process.run(input).pipe(
130+
Effect.map((result) => ({
131+
...result,
132+
stdout: result.stdout.replace(/\r?\n/g, lineEnding),
133+
})),
134+
),
135+
}),
136+
);
137+
yield* runGit(cwd, ["init"]);
138+
yield* runGit(cwd, ["remote", "add", "origin", fetchUrl]);
139+
yield* runGit(cwd, ["remote", "set-url", "--push", "origin", pushUrl]);
140+
yield* runGit(cwd, ["config", "remote.origin.promisor", "true"]);
141+
yield* runGit(cwd, ["config", "remote.origin.partialclonefilter", "blob:none"]);
142+
143+
const result = yield* driver.listRemotes(cwd);
144+
assert.deepEqual(result.remotes, [
145+
{
146+
name: "origin",
147+
url: fetchUrl,
148+
pushUrl: Option.some(pushUrl),
149+
isPrimary: true,
150+
},
151+
]);
152+
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
134153
);
135154

136155
const makeCheckpointFixture = Effect.fn("makeCheckpointFixture")(function* (

‎apps/server/src/vcs/GitVcsDriver.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,10 @@ function parseGitRemoteVerboseOutput(
517517
output: string,
518518
): Map<string, { url?: string; pushUrl?: string }> {
519519
const remotes = new Map<string, { url?: string; pushUrl?: string }>();
520-
for (const line of output.split("\n")) {
521-
const trimmed = line.trim();
522-
if (trimmed.length === 0) {
523-
continue;
524-
}
525-
520+
for (const line of output.split(/\r?\n/)) {
521+
// Git uses a tab before the URL and one space before the direction; both can occur in URLs.
526522
// Partial clones append the promisor filter, e.g. `(fetch) [blob:none]`.
527-
const match = /^(\S+)\s+(\S+)\s+\((fetch|push)\)(?:\s+\[[^\]]+\])?$/.exec(trimmed);
523+
const match = /^(\S+)\t(.+) \((fetch|push)\)(?:\s+\[[^\]]+\])?$/.exec(line);
528524
if (!match) {
529525
continue;
530526
}

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5724,6 +5724,61 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
57245724
}),
57255725
);
57265726

5727+
it.effect.each(["\n", "\r\n"])(
5728+
"ensureRemote preserves spaces in fetch URLs and reserves their names (%j)",
5729+
(lineEnding) =>
5730+
Effect.gen(function* () {
5731+
const cwd = yield* makeTmpDir();
5732+
const fetchUrl = " ../team repos/shared (fetch) [blob:none].git ";
5733+
const pushUrl = "../team repos/push (push).git";
5734+
yield* git(cwd, ["init"]);
5735+
yield* git(cwd, ["remote", "add", "origin", fetchUrl]);
5736+
yield* git(cwd, ["remote", "set-url", "--push", "origin", pushUrl]);
5737+
yield* git(cwd, ["config", "remote.origin.promisor", "true"]);
5738+
yield* git(cwd, ["config", "remote.origin.partialclonefilter", "blob:none"]);
5739+
const liveDriver = yield* GitVcsDriver.GitVcsDriver;
5740+
const listing = yield* liveDriver.execute({
5741+
cwd,
5742+
args: ["remote", "-v"],
5743+
operation: "test",
5744+
});
5745+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
5746+
const spawner = ChildProcessSpawner.make((command) =>
5747+
ChildProcess.isStandardCommand(command) &&
5748+
command.args[0] === "remote" &&
5749+
command.args[1] === "-v"
5750+
? Effect.succeed(makeSuccessfulHandle(listing.stdout.replace(/\r?\n/g, lineEnding)))
5751+
: delegate.spawn(command),
5752+
);
5753+
const driver = yield* makeGitVcsDriverCore().pipe(
5754+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
5755+
Effect.provide(layerServerConfig),
5756+
);
5757+
5758+
assert.equal(
5759+
yield* driver.ensureRemote({ cwd, preferredName: "origin", url: fetchUrl }),
5760+
"origin",
5761+
);
5762+
assert.equal(
5763+
yield* driver.ensureRemote({ cwd, preferredName: "origin", url: pushUrl }),
5764+
"origin-1",
5765+
);
5766+
const fetch = yield* liveDriver.execute({
5767+
cwd,
5768+
args: ["config", "--get", "remote.origin.url"],
5769+
operation: "test",
5770+
});
5771+
const push = yield* liveDriver.execute({
5772+
cwd,
5773+
args: ["config", "--get", "remote.origin.pushurl"],
5774+
operation: "test",
5775+
});
5776+
assert.equal(fetch.stdout, `${fetchUrl}\n`);
5777+
assert.equal(push.stdout, `${pushUrl}\n`);
5778+
assert.equal(yield* git(cwd, ["config", "--get", "remote.origin-1.url"]), pushUrl);
5779+
}),
5780+
);
5781+
57275782
it.effect("ensureRemote returns the remote a parallel call added for the same URL", () =>
57285783
Effect.gen(function* () {
57295784
const cwd = yield* makeTmpDir();

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,10 @@ function sanitizeRemoteName(value: string): string {
353353

354354
function parseRemoteFetchUrls(stdout: string): Map<string, string> {
355355
const remotes = new Map<string, string>();
356-
for (const line of stdout.split("\n")) {
357-
const trimmed = line.trim();
358-
if (trimmed.length === 0) continue;
356+
for (const line of stdout.split(/\r?\n/)) {
357+
// Git uses a tab before the URL and one space before the direction; both can occur in URLs.
359358
// Partial clones append the promisor filter, e.g. `(fetch) [blob:none]`.
360-
const match = /^(\S+)\s+(\S+)\s+\((fetch|push)\)(?:\s+\[[^\]]+\])?$/.exec(trimmed);
359+
const match = /^(\S+)\t(.+) \((fetch|push)\)(?:\s+\[[^\]]+\])?$/.exec(line);
361360
if (!match) continue;
362361
const [, remoteName = "", remoteUrl = "", direction = ""] = match;
363362
if (direction !== "fetch" || remoteName.length === 0 || remoteUrl.length === 0) {

0 commit comments

Comments
 (0)