Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai c08664d2d3 feat(vcs): Checkout and Update for a local branch with an upstream
The branch popup and the Branches pane offer Checkout and Update next to
Checkout for a local branch that is not checked out: it checks the branch out,
then updates it with the configured update method, in each repository where
the checkout took place (the common-branch menu of a multi-repository project
acts in all of them). Disabled without a tracked branch.

Fixes #6222
2026-11-27 03:06:10 +08:00
6 змінених файлів з 81 додано та 2 видалено

No files matched your search

+38 -2
Переглянути файл
@@ -6,6 +6,7 @@ const mocks = vi.hoisted(() => ({
update: vi.fn(),
updateCurrent: vi.fn(),
head: vi.fn(),
checkout: vi.fn(),
notify: vi.fn(),
openWorktree: vi.fn(),
}));
@@ -22,7 +23,7 @@ vi.mock("../ops", async (original) => ({
vi.mock("../openWorktree", () => ({ openWorktree: mocks.openWorktree }));
vi.mock("@/platform/ipc", async (original) => {
const real = await original<typeof import("@/platform/ipc")>();
return { ...real, commands: { ...real.commands, vcsBranchInfo: mocks.head } };
return { ...real, commands: { ...real.commands, vcsBranchInfo: mocks.head, vcsCheckout: mocks.checkout } };
});
vi.mock("@/workbench/contributions", async (original) => ({
...(await original<typeof import("@/workbench/contributions")>()),
@@ -30,7 +31,7 @@ vi.mock("@/workbench/contributions", async (original) => ({
}));
import { branchMenu, commonBranchMenu } from "../branchMenu";
import { branchUpdateSummary, updateBranches } from "../branchOps";
import { branchUpdateSummary, checkoutAndUpdate, updateBranches } from "../branchOps";
import { getAction } from "@/platform/actions";
import { useProject } from "@/platform/project";
import type { Settings } from "@/bindings";
@@ -209,3 +210,38 @@ describe("branch common to several repositories", () => {
]);
});
});
describe("Checkout and Update", () => {
const item = (items: ReturnType<typeof branchMenu>) =>
items.find((i) => "label" in i && i.label === "Checkout and Update") as { disabled?: boolean } | undefined;
it("is offered for a local branch that is not checked out, and needs a tracked branch", () => {
expect(item(branchMenu(branch("topic"), "main"))?.disabled).toBe(false);
expect(item(branchMenu(branch("topic", { upstream: null }), "main"))?.disabled).toBe(true);
expect(item(branchMenu(branch("origin/topic", { remote: true, upstream: null }), "main"))).toBeUndefined();
expect(item(branchMenu(branch("main", { current: true }), "main"))).toBeUndefined();
const common = { name: "feature", remote: false, current: false, favorite: false, upstream: true };
expect(item(commonBranchMenu(common, ["/p", "/p/lib"], null))?.disabled).toBe(false);
});
it("checks the branch out, then pulls it with the configured method", async () => {
let head = "main";
mocks.checkout.mockImplementation(async () => {
head = "topic";
return { status: "ok", data: { kind: "done" } };
});
mocks.head.mockImplementation(async () => ({ status: "ok", data: { head, upstream: `origin/${head}` } }));
await checkoutAndUpdate(branch("topic"));
expect(mocks.checkout).toHaveBeenCalledTimes(1);
expect(mocks.updateCurrent).toHaveBeenCalledWith([
{ repo: "/p", op: { op: "pull", expectedBranch: "topic", rebase: false } },
]);
});
it("updates nothing when the checkout did not happen", async () => {
mocks.checkout.mockResolvedValue({ status: "error", error: { kind: "failed", message: "nope" } });
await checkoutAndUpdate(branch("topic"));
expect(mocks.updateCurrent).not.toHaveBeenCalled();
expect(mocks.update).not.toHaveBeenCalled();
});
});
+19
Переглянути файл
@@ -5,6 +5,7 @@ import { showContextMenu, useContextMenuStore, type MenuItem } from "@/ui";
import { isFavoriteBranch } from "./branches";
import {
branchRef,
checkoutAndUpdate,
checkoutBranch,
checkoutDetached,
checkoutInRepos,
@@ -158,6 +159,15 @@ export function branchMenu(
}
const items: MenuItem[] = [
{ label: "Checkout", run: run(() => checkoutBranch(b)) },
...(b.remote
? []
: [
{
label: "Checkout and Update",
disabled: !b.upstream || updating || !!existingWorktree,
run: run(() => checkoutAndUpdate(b)),
},
]),
...(existingWorktree && !b.remote
? [{ label: "Open Existing Worktree", icon: "folder", run: run(() => openWorktree(existingWorktree, true)) }]
: []),
@@ -327,6 +337,15 @@ export function commonBranchMenu(
const base = bases[0];
const items: MenuItem[] = [
{ label: "Checkout", run: run(() => checkoutInRepos(b, roots)) },
...(b.remote
? []
: [
{
label: "Checkout and Update",
disabled: !b.upstream || !!useVcs.getState().remoteBusy || checkedOutInWorktree,
run: run(() => checkoutAndUpdate(b, roots)),
},
]),
newFrom,
worktree,
...(bases.every(Boolean)
+21
Переглянути файл
@@ -1376,6 +1376,27 @@ export async function checkoutAndRebase({ branch, repos }: CheckoutRebaseTarget)
});
}
/**
* Checkout and Update on a local branch: checks it out (in `roots`, or the
* current repository), then updates it like Update does, in each repository
* where the checkout took place.
*/
export async function checkoutAndUpdate(branch: NamedBranch, roots?: readonly string[]) {
const project = useProject.getState().project;
if (roots) await checkoutInRepos(branch, roots);
else await checkoutBranch(branch);
if (useProject.getState().project !== project) return;
const repos: readonly (string | null)[] = roots ?? [currentRepo()];
const heads = await Promise.all(
repos.map(async (repo) => [repo, await call(commands.vcsBranchInfo(repo)).catch(() => null)] as const),
);
if (useProject.getState().project !== project) return;
const targets = heads
.filter(([, info]) => info?.head === branch.name)
.map(([repo]) => ({ repo: repo ?? undefined, branch }));
if (targets.length) await updateBranches(targets);
}
export type BranchUpdateTarget = { repo?: string; branch?: Pick<Branch, "name" | "remote"> };
/** Update the named branches. Checked-out branches follow the configured update method. */
+1
Переглянути файл
@@ -3659,6 +3659,7 @@
"Stopped highlighting unpicked commits from '{source}' after the branch filter changed": "Stopped highlighting unpicked commits from '{source}' after the branch filter changed",
"Checkout and Rebase": "Checkout and Rebase",
"Checkout and Rebase onto Current": "Checkout and Rebase onto Current",
"Checkout and Update": "Checkout and Update",
"Checkout and Rebase failed": "Checkout and Rebase failed",
"Checkout and Rebase requires an existing commit": "Checkout and Rebase requires an existing commit",
"Checked out and rebased '{branchName}'": "Checked out and rebased '{branchName}'",
+1
Переглянути файл
@@ -5616,6 +5616,7 @@
"Stopped highlighting unpicked commits from '{source}' after the branch filter changed": "Подсветка неперенесённых коммитов из '{source}' остановлена после изменения фильтра веток",
"Checkout and Rebase": "Переключиться и перебазировать",
"Checkout and Rebase onto Current": "Переключиться и перебазировать на текущую",
"Checkout and Update": "Переключиться и обновить",
"Checkout and Rebase failed": "Не удалось переключиться и перебазировать",
"Checkout and Rebase requires an existing commit": "Для переключения и перебазирования требуется существующий коммит",
"Checked out and rebased '{branchName}'": "Выполнено переключение и перебазирование '{branchName}'",
+1
Переглянути файл
@@ -5760,6 +5760,7 @@
"Stopped highlighting unpicked commits from '{source}' after the branch filter changed": "分支筛选已更改,停止高亮 '{source}' 中未拣选的提交",
"Checkout and Rebase": "签出并变基",
"Checkout and Rebase onto Current": "签出并变基到当前分支",
"Checkout and Update": "签出并更新",
"Checkout and Rebase failed": "签出并变基失败",
"Checkout and Rebase requires an existing commit": "签出并变基需要已有提交",
"Checked out and rebased '{branchName}'": "已签出并变基 '{branchName}'",