The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 04:15 UTC
fix(server): a branch on the only remote is not a fork when there is no origin (#20412)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Victor Vogel committed
Oct 18, 2026 at 14:23 UTC
2c8f3a0cfcf3c986f7cb13f05da3663fbe7997e2
2 files changed
+447
-5
apps/server/src/git/GitManager.test.ts
+374
@@ -47,6 +47,7 @@ import {
47
import * as DateTime from "effect/DateTime";
48
import { decodeGitHubPullRequestListJson } from "@t3tools/source-control-github/server/gitHubPullRequests";
49
import * as GitHubChangeRequestTemplate from "@t3tools/source-control-github/server/gitHubChangeRequestTemplate";
50
+import * as HostProcess from "@t3tools/shared/HostProcess";
51
import * as GitLabCli from "@t3tools/source-control-gitlab/server/GitLabCli";
52
import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
53
import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
@@ -718,6 +719,7 @@ function makeManager(input?: {
719
setupScriptRunner?: ProjectSetupScriptRunner.ProjectSetupScriptRunner["Service"];
720
gitConfigReads?: string[];
721
gitCommands?: string[];
722
+ environment?: NodeJS.ProcessEnv;
723
gitSpawnGate?: {
724
readonly matches: (args: ReadonlyArray<string>) => boolean;
725
readonly reached: Deferred.Deferred<void>;
@@ -845,6 +847,7 @@ function makeManager(input?: {
847
yield* input.seed.pipe(Effect.provideContext(stores), Effect.orDie);
848
}
849
const manager = yield* GitManager.make.pipe(
850
+ Effect.provideService(HostProcess.Environment, input?.environment ?? {}),
851
Effect.provide(layerManager),
852
Effect.provideContext(stores),
853
);
@@ -2668,6 +2671,13 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
2671
yield* initRepo(repoDir);
2672
const forkDir = yield* createBareRemote();
2673
yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);
2674
+ // Origin is the base repository; a lone remote would be the base itself.
2675
+ yield* runGit(repoDir, [
2676
+ "remote",
2677
+ "add",
2678
+ "origin",
2679
+ "git@github.com:pingdotgg/codething-mvp.git",
2680
+ ]);
2681
yield* runGit(repoDir, ["checkout", "-b", "statemachine"]);
2682
NodeFS.writeFileSync(NodePath.join(repoDir, "fork-pr.txt"), "fork pr\n");
2683
yield* runGit(repoDir, ["add", "fork-pr.txt"]);
@@ -4413,6 +4423,13 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
4423
yield* runGit(repoDir, ["checkout", "-b", "statemachine"]);
4424
const forkDir = yield* createBareRemote();
4425
yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);
4426
+ // Origin is the base repository; a lone remote would be the base itself.
4427
+ yield* runGit(repoDir, [
4428
+ "remote",
4429
+ "add",
4430
+ "origin",
4431
+ "git@github.com:pingdotgg/codething-mvp.git",
4432
+ ]);
4433
yield* runGit(repoDir, ["push", "-u", "fork-seed", "statemachine"]);
4434
yield* configureVisibleRemoteUrlWithLocalRewrite(
4435
repoDir,
@@ -4462,6 +4479,335 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
4479
12_000,
4480
);
4481
4482
+ it.effect.each([
4483
+ { remoteUrl: "git@github.com:pingdotgg/codething-mvp.git", ghRepo: null, ghResolved: null },
4484
+ {
4485
+ remoteUrl: "git@github.com:pingdotgg/codething-mvp.git",
4486
+ ghRepo: "https://github.com/pingdotgg/codething-mvp?tab=readme#readme",
4487
+ ghResolved: null,
4488
+ },
4489
+ {
4490
+ remoteUrl: "https://github.example.com:8443/pingdotgg/codething-mvp.git",
4491
+ ghRepo: "https://github.example.com:8443/pingdotgg/codething-mvp",
4492
+ ghResolved: null,
4493
+ },
4494
+ // The GitHub resolver finds no repository for this GitLab remote, so the remote decides.
4495
+ {
4496
+ remoteUrl: "https://gitlab.example.com:8443/pingdotgg/codething-mvp.git",
4497
+ ghRepo: null,
4498
+ ghResolved: null,
4499
+ },
4500
+ // With a port on the remote's host the provider ignores gh-resolved and picks the remote.
4501
+ {
4502
+ remoteUrl: "https://github.example.com:8443/pingdotgg/codething-mvp.git",
4503
+ ghRepo: null,
4504
+ ghResolved: "acme/demo",
4505
+ },
4506
+ {
4507
+ remoteUrl: "https://github.example.com:8443/pingdotgg/codething-mvp.git",
4508
+ ghRepo: "base",
4509
+ ghResolved: "acme/demo",
4510
+ },
4511
+ ] as const)(
4512
+ "reuses the open same-repository PR when the only remote is not named origin (remote $remoteUrl, GH_REPO $ghRepo, gh-resolved $ghResolved)",
4513
+ ({ remoteUrl, ghRepo, ghResolved }) =>
4514
+ Effect.gen(function* () {
4515
+ const repoDir = yield* makeTempDir("t3code-git-manager-");
4516
+ yield* initRepo(repoDir);
4517
+ yield* runGit(repoDir, ["checkout", "-b", "dev"]);
4518
+ const remoteDir = yield* createBareRemote();
4519
+ yield* runGit(repoDir, ["remote", "add", "fork", remoteDir]);
4520
+ yield* runGit(repoDir, ["push", "-u", "fork", "dev"]);
4521
+ yield* configureVisibleRemoteUrlWithLocalRewrite(repoDir, "fork", remoteUrl, remoteDir);
4522
+ if (ghResolved !== null) {
4523
+ yield* runGit(repoDir, ["config", "remote.fork.gh-resolved", ghResolved]);
4524
+ }
4525
+ NodeFS.writeFileSync(NodePath.join(repoDir, "changes.txt"), "change\n");
4526
+
4527
+ const { manager, ghCalls } = yield* makeManager({
4528
+ environment: ghRepo === null ? {} : { GH_REPO: ghRepo },
4529
+ ghScenario: {
4530
+ prListByHeadSelector: {
4531
+ dev: encodeCliJson([
4532
+ {
4533
+ number: 3,
4534
+ title: "Dev",
4535
+ url: "https://github.com/pingdotgg/codething-mvp/pull/3",
4536
+ baseRefName: "main",
4537
+ headRefName: "dev",
4538
+ state: "OPEN",
4539
+ isCrossRepository: false,
4540
+ headRepository: { nameWithOwner: "pingdotgg/codething-mvp" },
4541
+ headRepositoryOwner: { login: "pingdotgg" },
4542
+ },
4543
+ ]),
4544
+ },
4545
+ },
4546
+ });
4547
+
4548
+ const result = yield* runStackedAction(manager, {
4549
+ cwd: repoDir,
4550
+ action: "commit_push_pr",
4551
+ });
4552
+
4553
+ expect(result.push.status).toBe("pushed");
4554
+ expect(result.pr.status).toBe("opened_existing");
4555
+ expect(result.pr.number).toBe(3);
4556
+ expect(ghCalls.some((call) => call.startsWith("pr create "))).toBe(false);
4557
+ }),
4558
+ 12_000,
4559
+ );
4560
+
4561
+ it.effect(
4562
+ "creates fork PRs with the owner head when gh's default repository is not the only remote",
4563
+ () =>
4564
+ Effect.gen(function* () {
4565
+ const repoDir = yield* makeTempDir("t3code-git-manager-");
4566
+ yield* initRepo(repoDir);
4567
+ yield* runGit(repoDir, ["checkout", "-b", "dev"]);
4568
+ const forkDir = yield* createBareRemote();
4569
+ yield* runGit(repoDir, ["remote", "add", "fork", forkDir]);
4570
+ yield* runGit(repoDir, ["push", "-u", "fork", "dev"]);
4571
+ yield* configureVisibleRemoteUrlWithLocalRewrite(
4572
+ repoDir,
4573
+ "fork",
4574
+ "https://github.com/contributor/demo.git",
4575
+ forkDir,
4576
+ );
4577
+ // What `gh repo set-default acme/demo` records when the default is not a remote.
4578
+ yield* runGit(repoDir, ["config", "remote.fork.gh-resolved", "acme/demo"]);
4579
+ NodeFS.writeFileSync(NodePath.join(repoDir, "changes.txt"), "change\n");
4580
+
4581
+ const { manager, ghCalls } = yield* makeManager();
4582
+
4583
+ const result = yield* runStackedAction(manager, {
4584
+ cwd: repoDir,
4585
+ action: "commit_push_pr",
4586
+ });
4587
+
4588
+ expect(result.pr.status).toBe("created");
4589
+ expect(
4590
+ ghCalls.some(
4591
+ (call) => call.startsWith("pr create ") && call.includes("--head contributor:dev"),
4592
+ ),
4593
+ ).toBe(true);
4594
+ }),
4595
+ 12_000,
4596
+ );
4597
+
4598
+ it.effect.each([
4599
+ { ghRepo: "acme/demo", ghResolved: null, head: "contributor:dev" },
4600
+ { ghRepo: "ghe.example.com/contributor/demo", ghResolved: null, head: "contributor:dev" },
4601
+ { ghRepo: "github.com/Contributor/demo", ghResolved: null, head: "dev" },
4602
+ {
4603
+ ghRepo: "https://github.com/contributor/demo?tab=readme#readme",
4604
+ ghResolved: null,
4605
+ head: "dev",
4606
+ },
4607
+ { ghRepo: "contributor/demo", ghResolved: "acme/demo", head: "dev" },
4608
+ { ghRepo: "acme/demo", ghResolved: "base", head: "contributor:dev" },
4609
+ // The provider ignores these GH_REPO values, so gh-resolved decides.
4610
+ { ghRepo: "junk/extra/contributor/demo", ghResolved: "acme/demo", head: "contributor:dev" },
4611
+ { ghRepo: "base", ghResolved: "acme/demo", head: "contributor:dev" },
4612
+ ] as const)(
4613
+ "creates the PR with head $head when GH_REPO is $ghRepo, gh-resolved is $ghResolved and the only remote is contributor/demo",
4614
+ ({ ghRepo, ghResolved, head }) =>
4615
+ Effect.gen(function* () {
4616
+ const repoDir = yield* makeTempDir("t3code-git-manager-");
4617
+ yield* initRepo(repoDir);
4618
+ yield* runGit(repoDir, ["checkout", "-b", "dev"]);
4619
+ const forkDir = yield* createBareRemote();
4620
+ yield* runGit(repoDir, ["remote", "add", "fork", forkDir]);
4621
+ yield* runGit(repoDir, ["push", "-u", "fork", "dev"]);
4622
+ yield* configureVisibleRemoteUrlWithLocalRewrite(
4623
+ repoDir,
4624
+ "fork",
4625
+ "https://github.com/contributor/demo.git",
4626
+ forkDir,
4627
+ );
4628
+ if (ghResolved !== null) {
4629
+ yield* runGit(repoDir, ["config", "remote.fork.gh-resolved", ghResolved]);
4630
+ }
4631
+ NodeFS.writeFileSync(NodePath.join(repoDir, "changes.txt"), "change\n");
4632
+
4633
+ const { manager, ghCalls } = yield* makeManager({ environment: { GH_REPO: ghRepo } });
4634
+
4635
+ const result = yield* runStackedAction(manager, {
4636
+ cwd: repoDir,
4637
+ action: "commit_push_pr",
4638
+ });
4639
+
4640
+ expect(result.pr.status).toBe("created");
4641
+ expect(
4642
+ ghCalls.some((call) => call.startsWith("pr create ") && call.includes(`--head ${head} `)),
4643
+ ).toBe(true);
4644
+ }),
4645
+ 12_000,
4646
+ );
4647
+
4648
+ it.effect.each([
4649
+ {
4650
+ remoteUrl: "https://github.example.com:8443/contributor/demo.git",
4651
+ ghRepo: "https://github.example.com:9443/contributor/demo",
4652
+ ghResolved: null,
4653
+ head: "contributor:dev",
4654
+ },
4655
+ {
4656
+ remoteUrl: "https://github.example.com:8443/contributor/demo.git",
4657
+ ghRepo: "github.example.com/contributor/demo",
4658
+ ghResolved: null,
4659
+ head: "contributor:dev",
4660
+ },
4661
+ {
4662
+ remoteUrl: "https://github.example.com:8443/contributor/demo.git",
4663
+ ghRepo: "github.example.com:8443/contributor/demo",
4664
+ ghResolved: null,
4665
+ head: "dev",
4666
+ },
4667
+ {
4668
+ remoteUrl: "https://github.example.com/contributor/demo.git",
4669
+ ghRepo: "github.example.com:443/contributor/demo",
4670
+ ghResolved: null,
4671
+ head: "dev",
4672
+ },
4673
+ // An SSH port is not part of the host the provider uses.
4674
+ {
4675
+ remoteUrl: "ssh://git@github.example.com:2222/contributor/demo.git",
4676
+ ghRepo: "github.example.com/contributor/demo",
4677
+ ghResolved: null,
4678
+ head: "dev",
4679
+ },
4680
+ // The provider ignores gh-resolved only when the remote's host carries a port.
4681
+ {
4682
+ remoteUrl: "https://github.example.com:8443/contributor/demo.git",
4683
+ ghRepo: null,
4684
+ ghResolved: "acme/demo",
4685
+ head: "dev",
4686
+ },
4687
+ {
4688
+ remoteUrl: "https://github.example.com/contributor/demo.git",
4689
+ ghRepo: null,
4690
+ ghResolved: "acme/demo",
4691
+ head: "contributor:dev",
4692
+ },
4693
+ {
4694
+ remoteUrl: "https://github.example.com:443/contributor/demo.git",
4695
+ ghRepo: null,
4696
+ ghResolved: "acme/demo",
4697
+ head: "contributor:dev",
4698
+ },
4699
+ {
4700
+ remoteUrl: "git@github.com:contributor/demo.git",
4701
+ ghRepo: null,
4702
+ ghResolved: "acme/demo",
4703
+ head: "contributor:dev",
4704
+ },
4705
+ {
4706
+ remoteUrl: "ssh://git@github.example.com:2222/contributor/demo.git",
4707
+ ghRepo: null,
4708
+ ghResolved: "acme/demo",
4709
+ head: "contributor:dev",
4710
+ },
4711
+ ] as const)(
4712
+ "creates the PR with head $head when the only remote is $remoteUrl, GH_REPO is $ghRepo and gh-resolved is $ghResolved",
4713
+ ({ remoteUrl, ghRepo, ghResolved, head }) =>
4714
+ Effect.gen(function* () {
4715
+ const repoDir = yield* makeTempDir("t3code-git-manager-");
4716
+ yield* initRepo(repoDir);
4717
+ yield* runGit(repoDir, ["checkout", "-b", "dev"]);
4718
+ const forkDir = yield* createBareRemote();
4719
+ yield* runGit(repoDir, ["remote", "add", "fork", forkDir]);
4720
+ yield* runGit(repoDir, ["push", "-u", "fork", "dev"]);
4721
+ yield* configureVisibleRemoteUrlWithLocalRewrite(repoDir, "fork", remoteUrl, forkDir);
4722
+ if (ghResolved !== null) {
4723
+ yield* runGit(repoDir, ["config", "remote.fork.gh-resolved", ghResolved]);
4724
+ }
4725
+ NodeFS.writeFileSync(NodePath.join(repoDir, "changes.txt"), "change\n");
4726
+
4727
+ const { manager, ghCalls } = yield* makeManager({
4728
+ environment: ghRepo === null ? {} : { GH_REPO: ghRepo },
4729
+ });
4730
+
4731
+ const result = yield* runStackedAction(manager, {
4732
+ cwd: repoDir,
4733
+ action: "commit_push_pr",
4734
+ });
4735
+
4736
+ expect(result.pr.status).toBe("created");
4737
+ expect(
4738
+ ghCalls.some((call) => call.startsWith("pr create ") && call.includes(`--head ${head} `)),
4739
+ ).toBe(true);
4740
+ }),
4741
+ 12_000,
4742
+ );
4743
+
4744
+ it.effect(
4745
+ "still treats a fork remote as cross-repository with an upstream remote but no origin",
4746
+ () =>
4747
+ Effect.gen(function* () {
4748
+ const repoDir = yield* makeTempDir("t3code-git-manager-");
4749
+ yield* initRepo(repoDir);
4750
+ yield* runGit(repoDir, ["checkout", "-b", "dev"]);
4751
+ const forkDir = yield* createBareRemote();
4752
+ yield* runGit(repoDir, ["remote", "add", "fork", forkDir]);
4753
+ yield* runGit(repoDir, ["push", "-u", "fork", "dev"]);
4754
+ yield* configureVisibleRemoteUrlWithLocalRewrite(
4755
+ repoDir,
4756
+ "fork",
4757
+ "https://github.com/contributor/demo.git",
4758
+ forkDir,
4759
+ );
4760
+ const upstreamDir = yield* createBareRemote();
4761
+ yield* runGit(repoDir, ["remote", "add", "upstream", upstreamDir]);
4762
+ yield* runGit(repoDir, ["push", "upstream", "main"]);
4763
+ yield* configureVisibleRemoteUrlWithLocalRewrite(
4764
+ repoDir,
4765
+ "upstream",
4766
+ "https://github.com/acme/demo.git",
4767
+ upstreamDir,
4768
+ );
4769
+ NodeFS.writeFileSync(NodePath.join(repoDir, "changes.txt"), "change\n");
4770
+
4771
+ const sameRepositoryPr = {
4772
+ number: 3,
4773
+ title: "Same-repository decoy",
4774
+ url: "https://github.com/contributor/demo/pull/3",
4775
+ baseRefName: "main",
4776
+ headRefName: "dev",
4777
+ state: "OPEN",
4778
+ isCrossRepository: false,
4779
+ headRepository: { nameWithOwner: "contributor/demo" },
4780
+ headRepositoryOwner: { login: "contributor" },
4781
+ };
4782
+ const { manager, ghCalls } = yield* makeManager({
4783
+ ghScenario: {
4784
+ prListByHeadSelector: {
4785
+ dev: encodeCliJson([
4786
+ sameRepositoryPr,
4787
+ {
4788
+ ...sameRepositoryPr,
4789
+ number: 4,
4790
+ title: "Fork PR",
4791
+ url: "https://github.com/acme/demo/pull/4",
4792
+ isCrossRepository: true,
4793
+ },
4794
+ ]),
4795
+ },
4796
+ },
4797
+ });
4798
+
4799
+ const result = yield* runStackedAction(manager, {
4800
+ cwd: repoDir,
4801
+ action: "commit_push_pr",
4802
+ });
4803
+
4804
+ expect(result.pr.status).toBe("opened_existing");
4805
+ expect(result.pr.number).toBe(4);
4806
+ expect(ghCalls.some((call) => call.startsWith("pr create "))).toBe(false);
4807
+ }),
4808
+ 12_000,
4809
+ );
4810
+
4811
it.effect(
4812
"returns the correct existing PR when a slash remote checks out to a synthetic local alias",
4813
() =>
@@ -4545,6 +4891,13 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
4891
yield* runGit(repoDir, ["checkout", "-b", "statemachine"]);
4892
const forkDir = yield* createBareRemote();
4893
yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);
4894
+ // Origin is the base repository; a lone remote would be the base itself.
4895
+ yield* runGit(repoDir, [
4896
+ "remote",
4897
+ "add",
4898
+ "origin",
4899
+ "git@github.com:pingdotgg/codething-mvp.git",
4900
+ ]);
4901
yield* runGit(repoDir, ["push", "-u", "fork-seed", "statemachine"]);
4902
yield* runGit(repoDir, ["checkout", "-b", "t3code/pr-142/statemachine"]);
4903
yield* runGit(repoDir, ["branch", "--set-upstream-to", "fork-seed/statemachine"]);
@@ -4611,6 +4964,13 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
4964
yield* runGit(repoDir, ["checkout", "-b", "statemachine"]);
4965
const forkDir = yield* createBareRemote();
4966
yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);
4967
+ // Origin is the base repository; a lone remote would be the base itself.
4968
+ yield* runGit(repoDir, [
4969
+ "remote",
4970
+ "add",
4971
+ "origin",
4972
+ "git@github.com:pingdotgg/codething-mvp.git",
4973
+ ]);
4974
yield* runGit(repoDir, ["push", "-u", "fork-seed", "statemachine"]);
4975
yield* runGit(repoDir, ["checkout", "-b", "t3code/pr-142/statemachine"]);
4976
yield* runGit(repoDir, ["branch", "--set-upstream-to", "fork-seed/statemachine"]);
@@ -4672,6 +5032,13 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
5032
yield* runGit(repoDir, ["checkout", "-b", "statemachine"]);
5033
const forkDir = yield* createBareRemote();
5034
yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);
5035
+ // Origin is the base repository; a lone remote would be the base itself.
5036
+ yield* runGit(repoDir, [
5037
+ "remote",
5038
+ "add",
5039
+ "origin",
5040
+ "git@github.com:pingdotgg/codething-mvp.git",
5041
+ ]);
5042
yield* runGit(repoDir, ["push", "-u", "fork-seed", "statemachine"]);
5043
yield* runGit(repoDir, [
5044
"config",
@@ -5159,6 +5526,13 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
5526
yield* initRepo(repoDir);
5527
const forkDir = yield* createBareRemote();
5528
yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);
5529
+ // Origin is the base repository; a lone remote would be the base itself.
5530
+ yield* runGit(repoDir, [
5531
+ "remote",
5532
+ "add",
5533
+ "origin",
5534
+ "git@github.com:pingdotgg/codething-mvp.git",
5535
+ ]);
5536
yield* runGit(repoDir, ["checkout", "-b", "statemachine"]);
5537
NodeFS.writeFileSync(NodePath.join(repoDir, "changes.txt"), "change\n");
5538
yield* runGit(repoDir, ["add", "changes.txt"]);
apps/server/src/git/GitManager.ts
+73
-5
@@ -62,6 +62,11 @@ import {
62
type ChangeRequestTerminology,
63
} from "@t3tools/shared/sourceControl";
64
import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
65
+import * as HostProcess from "@t3tools/shared/HostProcess";
66
+import {
67
+ parseGitHubRepositorySelector,
68
+ resolveGitHubRepository,
69
+} from "@t3tools/source-control-github/server/gitHubRepositoryResolution";
70
import {
71
normalizeThreadPullRequestKey,
72
threadPullRequestKeyOf,
@@ -616,6 +621,11 @@ function headBranchProbe(
621
);
622
}
623
624
+/** A URL's host as `new URL` reads it: lowercased, any non-default port kept, `:443` dropped. */
625
+function urlHost(url: string): string | null {
626
+ return URL.canParse(url) ? new URL(url).host : null;
627
+}
628
+
629
function appendUnique(values: string[], next: string | null | undefined): void {
630
const trimmed = next?.trim() ?? "";
631
if (trimmed.length === 0 || values.includes(trimmed)) {
@@ -703,6 +713,7 @@ export const make = Effect.gen(function* () {
713
const crypto = yield* Crypto.Crypto;
714
const fileSystem = yield* FileSystem.FileSystem;
715
const path = yield* Path.Path;
716
+ const environment = yield* HostProcess.Environment;
717
718
const sourceControlProvider = (cwd: string) => sourceControlProviders.resolve({ cwd });
719
const serverSettingsService = yield* ServerSettings.ServerSettingsService;
@@ -1373,6 +1384,41 @@ export const make = Effect.gen(function* () {
1384
const readConfigValueNullable = (cwd: string, key: string) =>
1385
gitCore.readConfigValue(cwd, key).pipe(Effect.orElseSucceed(() => null));
1386
1387
+ // Whether the GitHub provider creates PRs in the only remote. It picks GH_REPO when that
1388
+ // parses, else what resolveGitHubRepository makes of the remote and its gh-resolved mark from
1389
+ // `gh repo set-default`, given the host the provider's context gets from the remote URL. An
1390
+ // HTTPS remote keeps a non-default port there, and the provider then ignores the mark. With no
1391
+ // answer at all, the provider falls back to the remote itself.
1392
+ const targetsOnlyRemote = Effect.fn("targetsOnlyRemote")(function* (
1393
+ cwd: string,
1394
+ remoteName: string,
1395
+ remote: { readonly remoteUrl: string | null; readonly repositoryNameWithOwner: string | null },
1396
+ ) {
1397
+ const baseUrl =
1398
+ remote.remoteUrl === null
1399
+ ? undefined
1400
+ : detectSourceControlProviderFromGitRemoteUrl(remote.remoteUrl)?.baseUrl;
1401
+ const host = baseUrl === undefined ? null : urlHost(baseUrl);
1402
+ const repository = remote.repositoryNameWithOwner?.toLowerCase();
1403
+ const fromEnvironment = parseGitHubRepositorySelector(environment.GH_REPO ?? "", host ?? "");
1404
+ if (fromEnvironment !== null) {
1405
+ const { host: targetHost, owner, name } = fromEnvironment;
1406
+ return (
1407
+ host !== null &&
1408
+ urlHost(`https://${targetHost}`) === host &&
1409
+ `${owner}/${name}`.toLowerCase() === repository
1410
+ );
1411
+ }
1412
+ const mark = yield* readConfigValueNullable(cwd, `remote.${remoteName}.gh-resolved`);
1413
+ const { locator } = resolveGitHubRepository({
1414
+ remotes: remote.remoteUrl === null ? "" : `${remoteName}\t${remote.remoteUrl} (fetch)`,
1415
+ resolved: mark === null ? "" : `remote.${remoteName}.gh-resolved ${mark}`,
1416
+ hostHint: host ?? undefined,
1417
+ defaultHost: host ?? "github.com",
1418
+ });
1419
+ return locator === null || `${locator.owner}/${locator.name}`.toLowerCase() === repository;
1420
+ });
1421
+
1422
const resolveHostingProvider = Effect.fn("resolveHostingProvider")(function* (
1423
cwd: string,
1424
branch: string | null,
@@ -1402,6 +1448,7 @@ export const make = Effect.gen(function* () {
1448
) {
1449
if (!remoteName) {
1450
return {
1451
+ remoteUrl: null,
1452
remoteUrlKey: null,
1453
repositoryNameWithOwner: null,
1454
ownerLogin: null,
@@ -1425,6 +1472,7 @@ export const make = Effect.gen(function* () {
1472
repositoryNameWithOwner = parseRepositoryNameWithOwnerFromRemoteUrl(remoteUrl, provider);
1473
}
1474
return {
1475
+ remoteUrl,
1476
remoteUrlKey: remoteUrl ? normalizeGitRemoteUrl(remoteUrl) : null,
1477
repositoryNameWithOwner,
1478
ownerLogin: parseRepositoryOwnerLogin(repositoryNameWithOwner),
@@ -1463,14 +1511,35 @@ export const make = Effect.gen(function* () {
1511
remoteName,
1512
);
1513
1514
+ // With no origin to compare against, the only remote is the repository gh targets
1515
+ // unless GH_REPO or `gh repo set-default` (gh-resolved) names another, so a branch on it
1516
+ // is not a fork. With several remotes, or with that other target, it may still be a fork.
1517
+ const isOnlyRemote =
1518
+ remoteName !== null &&
1519
+ remoteName !== "origin" &&
1520
+ originRepository.remoteUrlKey === null &&
1521
+ (yield* gitCore
1522
+ .execute({
1523
+ operation: "GitManager.resolveBranchHeadContext.remotes",
1524
+ cwd,
1525
+ args: ["remote"],
1526
+ timeoutMs: 5_000,
1527
+ })
1528
+ .pipe(
1529
+ Effect.map(
1530
+ (result) => result.stdout.split("\n").filter((name) => name.trim().length > 0).length,
1531
+ ),
1532
+ Effect.orElseSucceed(() => 0),
1533
+ )) === 1 &&
1534
+ (yield* targetsOnlyRemote(cwd, remoteName, remoteRepository));
1535
+ const isForeignRemote = remoteName !== null && remoteName !== "origin" && !isOnlyRemote;
1536
+
1537
const isCrossRepository =
1538
remoteRepository.repositoryNameWithOwner !== null &&
1539
originRepository.repositoryNameWithOwner !== null
1540
? remoteRepository.repositoryNameWithOwner.toLowerCase() !==
1541
originRepository.repositoryNameWithOwner.toLowerCase()
1471
- : remoteName !== null &&
1472
- remoteName !== "origin" &&
1473
- remoteRepository.repositoryNameWithOwner !== null;
1542
+ : isForeignRemote && remoteRepository.repositoryNameWithOwner !== null;
1543
1544
const ownerHeadSelector =
1545
remoteRepository.ownerLogin && headBranch.length > 0
@@ -1478,8 +1547,7 @@ export const make = Effect.gen(function* () {
1547
: null;
1548
const remoteAliasHeadSelector =
1549
remoteName && headBranch.length > 0 ? `${remoteName}:${headBranch}` : null;
1481
- const shouldProbeRemoteOwnedSelectors =
1482
- isCrossRepository || (remoteName !== null && remoteName !== "origin");
1550
+ const shouldProbeRemoteOwnedSelectors = isCrossRepository || isForeignRemote;
1551
1552
const headSelectors: string[] = [];
1553
if (isCrossRepository && shouldProbeRemoteOwnedSelectors) {