diff --git a/src/lib/context.ts b/src/lib/context.ts index f83c8d6..04f290a 100644 --- a/src/lib/context.ts +++ b/src/lib/context.ts @@ -246,6 +246,18 @@ export async function getCurrentRepoContext( if (override) { return parseRepositoryInput(override, (explicitRepo ?? runtime.repo) ? 'flag' : 'environment'); } + return getGitRepoContext(cwd); +} + +/** + * The repository this clone's Git remotes point to, whatever --repo/-R or + * TANG_REPO select: where a branch is pushed, so where a pull request from a + * fork takes its head from. + */ +export async function getGitRepoContext( + cwd: string = process.cwd() +): Promise { + const runtime = getRuntimeOptions(); // Get all tangled remotes const remotes = await getTangledRemotes(cwd); diff --git a/tests/lib/context.test.ts b/tests/lib/context.test.ts index c98bfa3..a09640b 100644 --- a/tests/lib/context.test.ts +++ b/tests/lib/context.test.ts @@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; import type { RepositoryContext } from '../../src/lib/context.js'; import { getCurrentRepoContext, + getGitRepoContext, getTangledRemotes, promptForRemote, } from '../../src/lib/context.js'; @@ -433,4 +434,32 @@ describe('Context Resolution', () => { consoleWarnSpy.mockRestore(); }); }); + + describe('getGitRepoContext', () => { + it('gives the repository the clone pushes to even when TANG_REPO selects another', async () => { + vi.mocked(simpleGit).mockReturnValue({ + checkIsRepo: vi.fn().mockResolvedValue(true), + getRemotes: vi + .fn() + .mockResolvedValue([ + { name: 'origin', refs: { fetch: 'git@tangled.org:fork.example/tangled-cli' } }, + ]), + } as never); + process.env.TANG_REPO = 'upstream.example/tangled-cli'; + try { + expect(await getCurrentRepoContext()).toMatchObject({ + owner: 'upstream.example', + selectionSource: 'environment', + }); + expect(await getGitRepoContext()).toMatchObject({ + owner: 'fork.example', + name: 'tangled-cli', + remoteName: 'origin', + selectionSource: 'git', + }); + } finally { + delete process.env.TANG_REPO; + } + }); + }); });