diff --git a/src/extension.ts b/src/extension.ts index 10d4e0b..9bc3b29 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -3,10 +3,13 @@ import { promisify } from "util"; import * as vscode from "vscode"; import { + getCurrentBranch, getPullRequestDiff, getStagedDiff, getWorkingDiff, GitError, + listBaseBranchCandidates, + resolvePullRequestBaseBranch, } from "./git/diff"; import { Provider, @@ -476,6 +479,42 @@ async function runCommand(): Promise { } } +export function orderBaseBranches( + branches: string[], + defaultBranch?: string, +): string[] { + if (!defaultBranch || !branches.includes(defaultBranch)) return branches; + return [defaultBranch, ...branches.filter((b) => b !== defaultBranch)]; +} + +/** Asks for the PR target; the resolved default (setting → origin/HEAD → main → master) is listed first. */ +async function pickPullRequestBaseBranch( + cwd: string, + configured: string, +): Promise { + const current = await getCurrentBranch(cwd); + const defaultBranch = await resolvePullRequestBaseBranch( + cwd, + configured, + ).catch(() => undefined); + const branches = orderBaseBranches( + await listBaseBranchCandidates(cwd, current), + defaultBranch, + ); + if (branches.length === 0) { + throw new GitError("No branches to compare against"); + } + + const picked = await vscode.window.showQuickPick( + branches.map((name) => ({ + label: name, + description: name === defaultBranch ? "default" : undefined, + })), + { placeHolder: `Target branch for ${current}` }, + ); + return picked?.label; +} + async function runPullRequestContentCommand(): Promise { const cwd = getRepoRoot(); if (!cwd) { @@ -496,9 +535,14 @@ async function runPullRequestContentCommand(): Promise { let pullRequestDiff; try { - pullRequestDiff = await getPullRequestDiff( + const baseBranch = await pickPullRequestBaseBranch( cwd, settings.pullRequestBaseBranch, + ); + if (!baseBranch) return; + pullRequestDiff = await getPullRequestDiff( + cwd, + baseBranch, settings.pullRequestIncludeCommitList, ); } catch (err) { diff --git a/src/extension.unit.test.ts b/src/extension.unit.test.ts index b59097d..9392f3b 100644 --- a/src/extension.unit.test.ts +++ b/src/extension.unit.test.ts @@ -7,6 +7,7 @@ import { getApiKeySecret, getProviderSettingKey, openGitHubPullRequestCreate, + orderBaseBranches, readSettings, updateActiveProvider, updateProviderEndpoint, @@ -394,3 +395,18 @@ describe("extension helpers", () => { expect(vscode.commands.executeCommand).not.toHaveBeenCalled(); }); }); + +describe("orderBaseBranches", () => { + it("moves the default branch to the top", () => { + expect(orderBaseBranches(["develop", "master", "x"], "master")).toEqual([ + "master", + "develop", + "x", + ]); + }); + + it("keeps order when the default is missing or unknown", () => { + expect(orderBaseBranches(["a", "b"])).toEqual(["a", "b"]); + expect(orderBaseBranches(["a", "b"], "main")).toEqual(["a", "b"]); + }); +}); diff --git a/src/git/diff.test.ts b/src/git/diff.test.ts index 6fd6fd3..20a9411 100644 --- a/src/git/diff.test.ts +++ b/src/git/diff.test.ts @@ -4,6 +4,7 @@ import { getStagedDiff, getWorkingDiff, GitError, + listBaseBranchCandidates, resolvePullRequestBaseBranch, } from "./diff"; @@ -206,3 +207,32 @@ describe("getPullRequestDiff", () => { ); }); }); + +describe("listBaseBranchCandidates", () => { + beforeEach(() => jest.clearAllMocks()); + + it("lists local and remote branches without HEAD aliases or the current branch", async () => { + routeGit({ + "for-each-ref --format=%(refname) refs/heads refs/remotes": { + stdout: [ + "refs/heads/master", + "refs/heads/develop", + "refs/heads/feature/x", + "refs/heads/other/feature/x", + "refs/remotes/origin/HEAD", + "refs/remotes/origin/master", + "refs/remotes/origin/feature/x", + "refs/remotes/origin/release/1.0", + ].join("\n"), + }, + }); + + await expect(listBaseBranchCandidates(CWD, "feature/x")).resolves.toEqual([ + "master", + "develop", + "other/feature/x", + "origin/master", + "origin/release/1.0", + ]); + }); +}); diff --git a/src/git/diff.ts b/src/git/diff.ts index fef8d99..5d74fa8 100644 --- a/src/git/diff.ts +++ b/src/git/diff.ts @@ -144,6 +144,28 @@ export async function resolvePullRequestBaseBranch( throw new GitError("Could not resolve a pull request base branch"); } +/** Local then remote branches a PR could target, excluding `current` and remote HEAD aliases. */ +export async function listBaseBranchCandidates( + cwd: string, + current: string, +): Promise { + const out = await git( + ["for-each-ref", "--format=%(refname)", "refs/heads", "refs/remotes"], + cwd, + ); + return splitLines(out) + .filter((ref) => !ref.endsWith("/HEAD")) + .filter( + (ref) => + ref !== `refs/heads/${current}` && + !( + ref.startsWith("refs/remotes/") && + ref.split("/").slice(3).join("/") === current + ), // remote twin: refs/remotes// + ) + .map((ref) => ref.replace(/^refs\/(heads|remotes)\//, "")); +} + export async function getPullRequestDiff( cwd: string, configuredBaseBranch?: string,