diff --git a/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts b/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts index 537481999e..56ab64b54b 100644 --- a/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts +++ b/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts @@ -3,10 +3,7 @@ import { assert, it } from "@effect/vitest"; import { Effect, FileSystem, Layer } from "effect"; import { ChildProcessSpawner } from "effect/unstable/process"; -import { - GitCommandError, - type SourceControlProviderError, -} from "@t3tools/contracts"; +import { GitCommandError, type SourceControlProviderError } from "@t3tools/contracts"; import { ServerConfig } from "../config.ts"; import { @@ -156,7 +153,7 @@ it.effect("clones a looked-up repository into the requested destination", () => it.effect("publishes by creating the repository, adding a remote, and pushing upstream", () => { const createCalls: Array<{ cwd: string; repository: string; visibility: string }> = []; const remoteCalls: Array<{ cwd: string; preferredName: string; url: string }> = []; - const pushCalls: string[] = []; + const pushCalls: Array<{ cwd: string; remoteName: string | null | undefined }> = []; const provider = makeProvider({ createRepository: (input) => Effect.sync(() => { @@ -194,7 +191,7 @@ it.effect("publishes by creating the repository, adding a remote, and pushing up assert.deepStrictEqual(remoteCalls, [ { cwd: "/workspace", preferredName: "origin", url: CLONE_URLS.sshUrl }, ]); - assert.deepStrictEqual(pushCalls, ["/workspace"]); + assert.deepStrictEqual(pushCalls, [{ cwd: "/workspace", remoteName: "origin" }]); }).pipe( Effect.provide( makeLayer({ @@ -205,9 +202,9 @@ it.effect("publishes by creating the repository, adding a remote, and pushing up remoteCalls.push(input); return "origin"; }), - pushCurrentBranch: (cwd) => + pushCurrentBranch: (cwd, _fallbackBranch, options) => Effect.sync(() => { - pushCalls.push(cwd); + pushCalls.push({ cwd, remoteName: options?.remoteName }); return { status: "pushed" as const, branch: "feature/remote-v1", @@ -221,6 +218,43 @@ it.effect("publishes by creating the repository, adding a remote, and pushing up ); }); +it.effect("publishes to the remote name returned by ensureRemote", () => { + const pushCalls: Array<{ cwd: string; remoteName: string | null | undefined }> = []; + + return Effect.gen(function* () { + const service = yield* SourceControlRepositoryService; + const result = yield* service.publishRepository({ + cwd: "/workspace", + provider: "github", + repository: "octocat/t3code", + visibility: "private", + remoteName: "origin", + protocol: "ssh", + }); + + assert.equal(result.remoteName, "origin-1"); + assert.deepStrictEqual(pushCalls, [{ cwd: "/workspace", remoteName: "origin-1" }]); + }).pipe( + Effect.provide( + makeLayer({ + git: { + ensureRemote: () => Effect.succeed("origin-1"), + pushCurrentBranch: (cwd, _fallbackBranch, options) => + Effect.sync(() => { + pushCalls.push({ cwd, remoteName: options?.remoteName }); + return { + status: "pushed" as const, + branch: "feature/remote-v1", + upstreamBranch: `${options?.remoteName ?? "missing"}/feature/remote-v1`, + setUpstream: true, + }; + }), + }, + }), + ), + ); +}); + it.effect("publish succeeds with status remote_added when the local repo has no commits", () => { let pushCalls = 0; return Effect.gen(function* () { diff --git a/apps/server/src/sourceControl/SourceControlRepositoryService.ts b/apps/server/src/sourceControl/SourceControlRepositoryService.ts index acd69ea77a..bfd19f2cdf 100644 --- a/apps/server/src/sourceControl/SourceControlRepositoryService.ts +++ b/apps/server/src/sourceControl/SourceControlRepositoryService.ts @@ -292,7 +292,7 @@ export const make = Effect.fn("makeSourceControlRepositoryService")(function* () }; } - const pushResult = yield* git.pushCurrentBranch(input.cwd, null); + const pushResult = yield* git.pushCurrentBranch(input.cwd, null, { remoteName }); return { repository: toRepositoryInfo(providerKind, urls), diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts index 661490b71a..727f7e8650 100644 --- a/apps/server/src/vcs/GitVcsDriver.ts +++ b/apps/server/src/vcs/GitVcsDriver.ts @@ -165,6 +165,7 @@ export interface GitVcsDriverShape { readonly pushCurrentBranch: ( cwd: string, fallbackBranch: string | null, + options?: { readonly remoteName?: string | null }, ) => Effect.Effect; readonly readRangeContext: ( cwd: string, diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index aa15bff76b..25e9071b83 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -326,5 +326,41 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => { assert.notEqual(badBranch.exitCode, 0); }), ); + + it.effect("pushes to the requested remote instead of the primary remote", () => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const originRemote = yield* makeTmpDir("git-origin-remote-"); + const publishRemote = yield* makeTmpDir("git-publish-remote-"); + yield* initRepoWithCommit(cwd); + const driver = yield* GitVcsDriver.GitVcsDriver; + yield* git(cwd, ["branch", "-M", "main"]); + yield* git(originRemote, ["init", "--bare"]); + yield* git(publishRemote, ["init", "--bare"]); + yield* git(cwd, ["remote", "add", "origin", originRemote]); + yield* git(cwd, ["remote", "add", "origin-1", publishRemote]); + + const pushed = yield* driver.pushCurrentBranch(cwd, null, { remoteName: "origin-1" }); + + assert.deepInclude(pushed, { + status: "pushed", + branch: "main", + upstreamBranch: "origin-1/main", + setUpstream: true, + }); + assert.equal( + yield* git(publishRemote, ["log", "-1", "--pretty=%s", "main"]), + "initial commit", + ); + const originMain = yield* driver.execute({ + operation: "GitVcsDriver.test.originMainMissing", + cwd: originRemote, + args: ["show-ref", "--verify", "--quiet", "refs/heads/main"], + allowNonZeroExit: true, + timeoutMs: 10_000, + }); + assert.notEqual(originMain.exitCode, 0); + }), + ); }); }); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index dd71d53f90..707461cebc 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -1431,7 +1431,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* }); const pushCurrentBranch: GitVcsDriverShape["pushCurrentBranch"] = Effect.fn("pushCurrentBranch")( - function* (cwd, fallbackBranch) { + function* (cwd, fallbackBranch, options) { const details = yield* statusDetails(cwd); const branch = details.branch ?? fallbackBranch; if (!branch) { @@ -1443,6 +1443,23 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ); } + const requestedRemoteName = options?.remoteName?.trim() || null; + if (requestedRemoteName) { + const publishBranch = yield* resolvePublishBranchName(cwd, branch); + yield* runGit("GitVcsDriver.pushCurrentBranch.pushWithRequestedRemote", cwd, [ + "push", + "-u", + requestedRemoteName, + `HEAD:refs/heads/${publishBranch}`, + ]); + return { + status: "pushed" as const, + branch, + upstreamBranch: `${requestedRemoteName}/${publishBranch}`, + setUpstream: true, + }; + } + const hasNoLocalDelta = details.aheadCount === 0 && details.behindCount === 0; if (hasNoLocalDelta) { if (details.hasUpstream) { diff --git a/apps/web/src/components/GitActionsControl.tsx b/apps/web/src/components/GitActionsControl.tsx index 7c466a10ce..f2977d4c66 100644 --- a/apps/web/src/components/GitActionsControl.tsx +++ b/apps/web/src/components/GitActionsControl.tsx @@ -967,9 +967,11 @@ export default function GitActionsControl({ const canPublishRepository = isRepo && gitStatusForActions !== null && !hasPrimaryRemote; const canSubmitPublishRepository = (() => { if (publishRepositoryMutation.isPending) return false; - const [owner, ...rest] = publishRepository.trim().split("/"); + const repositoryParts = publishRepository.trim().split("/"); + const owner = repositoryParts[0]?.trim() ?? ""; + const rest = repositoryParts.slice(1); const name = rest.join("/").trim(); - return owner.trim().length > 0 && name.length > 0; + return owner.length > 0 && name.length > 0; })(); const publishHost = publishProvider === "github" ? "github.com" : "gitlab.com"; @@ -1413,22 +1415,20 @@ export default function GitActionsControl({ aria-labelledby="publish-provider-cards-label" className="grid grid-cols-2 gap-2.5" > - {( - [ - { - value: "github" as const, - label: "GitHub", - description: "github.com", - Icon: GitHubIcon, - }, - { - value: "gitlab" as const, - label: "GitLab", - description: "gitlab.com", - Icon: GitLabIcon, - }, - ] - ).map((option) => { + {[ + { + value: "github" as const, + label: "GitHub", + description: "github.com", + Icon: GitHubIcon, + }, + { + value: "gitlab" as const, + label: "GitLab", + description: "gitlab.com", + Icon: GitLabIcon, + }, + ].map((option) => { const isSelected = publishProvider === option.value; return ( )} {publishWizardStep === 1 && ( - -
- -
- - {publishProvider === "github" ? ( - - ) : ( - - )} - {publishHost}/ - - { - setPublishRepository(event.target.value); - setHasUserEditedPublishRepository(true); - }} - onKeyDown={(event) => { - if (event.key === "Enter") { - event.preventDefault(); - submitPublishRepository(); - } - }} - placeholder={publishPathPlaceholder} - disabled={publishRepositoryMutation.isPending} - className="w-full bg-transparent px-3 py-2 font-mono text-sm placeholder:text-muted-foreground/60 focus:outline-none" - /> -
-
-
- - Visibility - - - setPublishVisibility(value as SourceControlRepositoryVisibility) - } - aria-labelledby="publish-visibility-cards-label" - disabled={publishRepositoryMutation.isPending} - className="grid grid-cols-2 gap-2.5" - > - {( - [ - { - value: "private" as const, - label: "Private", - description: "Only invited people", - Icon: LockIcon, - }, - { - value: "public" as const, - label: "Public", - description: "Anyone on the web", - Icon: GlobeIcon, - }, - ] - ).map((option) => { - const isSelected = publishVisibility === option.value; - return ( - - - - - {option.label} - - - {option.description} - - - - ); - })} - -
-
- - {publishAdvancedOpen && ( -
- -
- - Protocol - - - setPublishProtocol(value as SourceControlCloneProtocol) - } - aria-labelledby="publish-protocol-label" - disabled={publishRepositoryMutation.isPending} - className="grid grid-cols-2 gap-2" - > - {(["ssh", "https"] as const).map((value) => { - const isSelected = publishProtocol === value; - return ( - - {value === "ssh" ? "SSH" : "HTTPS"} - - ); - })} - -
-
+ +
+ +
+ + {publishProvider === "github" ? ( + + ) : ( + )} + {publishHost}/ + + { + setPublishRepository(event.target.value); + setHasUserEditedPublishRepository(true); + }} + onKeyDown={(event) => { + if (event.key === "Enter") { + event.preventDefault(); + submitPublishRepository(); + } + }} + placeholder={publishPathPlaceholder} + disabled={publishRepositoryMutation.isPending} + className="w-full bg-transparent px-3 py-2 font-mono text-sm placeholder:text-muted-foreground/60 focus:outline-none" + /> +
+
+
+ + Visibility + + + setPublishVisibility(value as SourceControlRepositoryVisibility) + } + aria-labelledby="publish-visibility-cards-label" + disabled={publishRepositoryMutation.isPending} + className="grid grid-cols-2 gap-2.5" + > + {[ + { + value: "private" as const, + label: "Private", + description: "Only invited people", + Icon: LockIcon, + }, + { + value: "public" as const, + label: "Public", + description: "Anyone on the web", + Icon: GlobeIcon, + }, + ].map((option) => { + const isSelected = publishVisibility === option.value; + return ( + + + + + {option.label} + + + {option.description} + + + + ); + })} + +
+
+ + {publishAdvancedOpen && ( +
+ +
+ + Protocol + + + setPublishProtocol(value as SourceControlCloneProtocol) + } + aria-labelledby="publish-protocol-label" + disabled={publishRepositoryMutation.isPending} + className="grid grid-cols-2 gap-2" + > + {(["ssh", "https"] as const).map((value) => { + const isSelected = publishProtocol === value; + return ( + + {value === "ssh" ? "SSH" : "HTTPS"} + + ); + })} + +
- {publishRepositoryMutation.isPending && ( -
- - Publishing repository to {publishProviderLabel}... -
- )} - {publishError && !publishRepositoryMutation.isPending && ( -
-

Publish failed

-

{publishError}

-
- )} - + )} +
+ {publishRepositoryMutation.isPending && ( +
+ + Publishing repository to {publishProviderLabel}... +
+ )} + {publishError && !publishRepositoryMutation.isPending && ( +
+

Publish failed

+

{publishError}

+
+ )} +
)} {publishWizardStep === 2 && publishResult && (