|
| 1 | +import { GitExecutor, parseGitHubRemoteUrl } from './gitExecutor'; |
| 2 | + |
| 3 | +export interface IGitRemoteInfo { |
| 4 | + name: string; |
| 5 | + fetchUrl: string; |
| 6 | + pushUrl: string; |
| 7 | +} |
| 8 | + |
| 9 | +export type RemoteResolution = { remote: string } | { needsPick: IGitRemoteInfo[] }; |
| 10 | + |
| 11 | +/** |
| 12 | + * Per-repository session memory: once a user has resolved an ambiguous |
| 13 | + * remote pick (via the caller-layer QuickPick), we remember it for the |
| 14 | + * rest of the VS Code session so we don't ask again for the same repo. |
| 15 | + */ |
| 16 | +const remembered = new Map<string, string>(); |
| 17 | + |
| 18 | +export interface ResolveRemoteOptions { |
| 19 | + /** Branch whose configured upstream remote should be preferred, if any. */ |
| 20 | + branch?: string; |
| 21 | + /** Value of the `git-smart-checkout.defaultRemote` setting. */ |
| 22 | + defaultRemote?: string; |
| 23 | + purpose: 'fetch' | 'push'; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Safely lists remotes for a repository. Falls back to an empty list if the |
| 28 | + * underlying git call fails (e.g. not a git repo yet) or if `git` is a |
| 29 | + * lightweight test double that doesn't implement `listRemotes` — callers |
| 30 | + * treat an empty list the same as "nothing to resolve, use 'origin'". |
| 31 | + */ |
| 32 | +async function safeListRemotes(git: GitExecutor): Promise<IGitRemoteInfo[]> { |
| 33 | + try { |
| 34 | + return (await git.listRemotes?.()) ?? []; |
| 35 | + } catch { |
| 36 | + return []; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +async function safeGetUpstreamRemote(git: GitExecutor, branch: string): Promise<string | undefined> { |
| 41 | + try { |
| 42 | + return await git.getUpstreamRemote?.(branch); |
| 43 | + } catch { |
| 44 | + return undefined; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Resolves which remote should be used for a git operation, following the |
| 50 | + * order documented in the multi-remote support spec: |
| 51 | + * 1. The branch's configured upstream remote. |
| 52 | + * 2. The `defaultRemote` setting, if set and it exists in the repo. |
| 53 | + * 3. If the repo has exactly one remote, that remote. |
| 54 | + * 4. A remembered pick from earlier in this session (for this repo). |
| 55 | + * 5. Otherwise, the caller must prompt the user (`needsPick`). |
| 56 | + * |
| 57 | + * This function is UI-free by design; callers that receive `needsPick` |
| 58 | + * are responsible for prompting the user and calling `rememberRemote`. |
| 59 | + * If remote discovery is unavailable (e.g. `listRemotes` fails or isn't |
| 60 | + * implemented by the caller's `GitExecutor`), it falls back to `'origin'` |
| 61 | + * so existing single-remote-repo behavior is unchanged. |
| 62 | + */ |
| 63 | +export async function resolveRemote(git: GitExecutor, opts: ResolveRemoteOptions): Promise<RemoteResolution> { |
| 64 | + const remotes = await safeListRemotes(git); |
| 65 | + if (remotes.length === 0) { |
| 66 | + return { remote: 'origin' }; |
| 67 | + } |
| 68 | + |
| 69 | + const available = new Set(remotes.map((remote) => remote.name)); |
| 70 | + |
| 71 | + const upstream = opts.branch ? await safeGetUpstreamRemote(git, opts.branch) : undefined; |
| 72 | + if (upstream && available.has(upstream)) { |
| 73 | + return { remote: upstream }; |
| 74 | + } |
| 75 | + |
| 76 | + if (opts.defaultRemote && available.has(opts.defaultRemote)) { |
| 77 | + return { remote: opts.defaultRemote }; |
| 78 | + } |
| 79 | + |
| 80 | + if (remotes.length === 1) { |
| 81 | + return { remote: remotes[0].name }; |
| 82 | + } |
| 83 | + |
| 84 | + const cached = remembered.get(git.repositoryPath); |
| 85 | + if (cached && available.has(cached)) { |
| 86 | + return { remote: cached }; |
| 87 | + } |
| 88 | + |
| 89 | + return { needsPick: remotes }; |
| 90 | +} |
| 91 | + |
| 92 | +/** Records the user's answer to an ambiguous remote pick for this repo/session. */ |
| 93 | +export function rememberRemote(repositoryPath: string, remote: string): void { |
| 94 | + remembered.set(repositoryPath, remote); |
| 95 | +} |
| 96 | + |
| 97 | +/** Test-only helper to reset session memory between test cases. */ |
| 98 | +export function clearRememberedRemotes(): void { |
| 99 | + remembered.clear(); |
| 100 | +} |
| 101 | + |
| 102 | +export interface ResolveGitHubRemoteOptions extends ResolveRemoteOptions { |
| 103 | + /** `owner/repo` full name of the GitHub repository the operation targets (e.g. a PR's base repo). */ |
| 104 | + githubRepo?: string; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Resolves the remote to use for GitHub-specific flows (PR clone, |
| 109 | + * checkout-by-PR, PR review). When `githubRepo` is provided, remotes whose |
| 110 | + * fetch URL parses (via `parseGitHubRemoteUrl`) to that `owner/repo` are |
| 111 | + * preferred over the generic resolution order — this is what lets a fork |
| 112 | + * setup (`origin` = fork, `upstream` = canonical repo) pick the correct |
| 113 | + * remote for a given PR's base repository. |
| 114 | + */ |
| 115 | +export async function resolveGitHubRemote(git: GitExecutor, opts: ResolveGitHubRemoteOptions): Promise<RemoteResolution> { |
| 116 | + if (!opts.githubRepo) { |
| 117 | + return resolveRemote(git, opts); |
| 118 | + } |
| 119 | + |
| 120 | + const remotes = await safeListRemotes(git); |
| 121 | + if (remotes.length === 0) { |
| 122 | + return { remote: 'origin' }; |
| 123 | + } |
| 124 | + |
| 125 | + const matches = remotes.filter((remote) => { |
| 126 | + const parsed = parseGitHubRemoteUrl(remote.fetchUrl); |
| 127 | + return parsed ? `${parsed.owner}/${parsed.repo}`.toLowerCase() === opts.githubRepo!.toLowerCase() : false; |
| 128 | + }); |
| 129 | + |
| 130 | + if (matches.length === 1) { |
| 131 | + return { remote: matches[0].name }; |
| 132 | + } |
| 133 | + |
| 134 | + if (matches.length > 1) { |
| 135 | + // Multiple remotes point at the same GitHub repo (rare) — fall back to |
| 136 | + // the generic resolution order, restricted to the matching remotes. |
| 137 | + const available = new Set(matches.map((remote) => remote.name)); |
| 138 | + const upstream = opts.branch ? await safeGetUpstreamRemote(git, opts.branch) : undefined; |
| 139 | + if (upstream && available.has(upstream)) return { remote: upstream }; |
| 140 | + if (opts.defaultRemote && available.has(opts.defaultRemote)) return { remote: opts.defaultRemote }; |
| 141 | + const cached = remembered.get(git.repositoryPath); |
| 142 | + if (cached && available.has(cached)) return { remote: cached }; |
| 143 | + return { needsPick: matches }; |
| 144 | + } |
| 145 | + |
| 146 | + // No remote matches the PR's GitHub repo (e.g. shallow/renamed remotes) — |
| 147 | + // fall back to the generic resolution order across all remotes. |
| 148 | + return resolveRemote(git, opts); |
| 149 | +} |
0 commit comments