Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -476,6 +479,42 @@ async function runCommand(): Promise<void> {
}
}

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<string | undefined> {
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<void> {
const cwd = getRepoRoot();
if (!cwd) {
Expand All @@ -496,9 +535,14 @@ async function runPullRequestContentCommand(): Promise<void> {

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) {
Expand Down
16 changes: 16 additions & 0 deletions src/extension.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getApiKeySecret,
getProviderSettingKey,
openGitHubPullRequestCreate,
orderBaseBranches,
readSettings,
updateActiveProvider,
updateProviderEndpoint,
Expand Down Expand Up @@ -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"]);
});
});
30 changes: 30 additions & 0 deletions src/git/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getStagedDiff,
getWorkingDiff,
GitError,
listBaseBranchCandidates,
resolvePullRequestBaseBranch,
} from "./diff";

Expand Down Expand Up @@ -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",
]);
});
});
22 changes: 22 additions & 0 deletions src/git/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> {
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/<remote>/<current>
)
.map((ref) => ref.replace(/^refs\/(heads|remotes)\//, ""));
}

export async function getPullRequestDiff(
cwd: string,
configuredBaseBranch?: string,
Expand Down
Loading