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

...
Автор SHA1 Повідомлення Дата
l0ng-ai 35ad40483a fix(vcs): open the Log User filter without waiting for every author 2026-12-01 01:44:03 +08:00
3 змінених файлів з 103 додано та 7 видалено

No files matched your search

+51
Переглянути файл
@@ -0,0 +1,51 @@
import { afterEach, describe, expect, it, vi } from "vitest";
vi.mock("@/platform/ipc", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/platform/ipc")>();
return {
...actual,
call: vi.fn(async (r: Promise<unknown>) => r),
commands: { ...actual.commands, vcsLogUsers: vi.fn() },
};
});
import { commands } from "@/platform/ipc";
import { fetchLogUsers, knownLogUsers, resetLogUsers } from "../log/logUsers";
const users = vi.mocked(commands.vcsLogUsers);
afterEach(() => {
resetLogUsers();
users.mockReset();
});
describe("Log User filter authors", () => {
it("knows nothing before the first list and reuses a running fetch", async () => {
let resolve!: (v: string[]) => void;
users.mockReturnValue(new Promise((r) => (resolve = r)) as never);
expect(knownLogUsers("/repo")).toEqual([]);
const a = fetchLogUsers("/repo");
const b = fetchLogUsers("/repo");
expect(users).toHaveBeenCalledTimes(1);
resolve(["Ann <a@x>"]);
expect(await a).toEqual(["Ann <a@x>"]);
expect(await b).toEqual(["Ann <a@x>"]);
expect(knownLogUsers("/repo")).toEqual(["Ann <a@x>"]);
});
it("answers from the known list while it is recent and fetches again later", async () => {
users.mockResolvedValueOnce(["Ann <a@x>"] as never).mockResolvedValueOnce(["Ann <a@x>", "Bob <b@x>"] as never);
await fetchLogUsers("/repo");
expect(await fetchLogUsers("/repo", Date.now() + 60_000)).toEqual(["Ann <a@x>"]);
expect(users).toHaveBeenCalledTimes(1);
expect(await fetchLogUsers("/repo", Date.now() + 10 * 60_000)).toEqual(["Ann <a@x>", "Bob <b@x>"]);
expect(users).toHaveBeenCalledTimes(2);
});
it("keeps the known list when a fetch fails", async () => {
users.mockResolvedValueOnce(["Ann <a@x>"] as never).mockRejectedValueOnce(new Error("git failed") as never);
await fetchLogUsers("/repo");
expect(await fetchLogUsers("/repo", Date.now() + 10 * 60_000)).toEqual(["Ann <a@x>"]);
expect(knownLogUsers("/other")).toEqual([]);
});
});
+11 -7
Переглянути файл
@@ -1,5 +1,4 @@
import { tx, txPart } from "@/platform/i18n";
import { call, commands } from "@/platform/ipc";
import { getAction, isActionEnabled } from "@/platform/actions";
import { useContextKeys } from "@/platform/context";
import { isMacHost } from "@/platform/keymap";
@@ -32,6 +31,7 @@ import { LogPathsEditor, chooseLogPaths } from "../PathFilterDialog";
import { openExternalLog } from "../RevisionBrowser";
import { currentRepo, repoName, useVcs } from "../store";
import { useDropdownMenu, useLogRepositories, useReadOnlyLog } from "./logHooks";
import { fetchLogUsers, knownLogUsers } from "./logUsers";
import { toggle } from "./shared";
// --- filters ----------------------------------------------------------------
@@ -198,16 +198,20 @@ export function FilterBar({ fileHistory = false }: { fileHistory?: boolean }) {
if (r) setLogBranches(r);
};
const openUser = async (anchor: HTMLElement) => {
const openUser = (anchor: HTMLElement) => {
const roots = filters.repositories?.length
? filters.repositories
: repositories.length
? repositories
: [currentRepo()];
const users = await Promise.all(roots.map((repo) => call(commands.vcsLogUsers(repo)).catch(() => [] as string[])));
const authors = [...new Set(users.flat().concat(commits.map((c) => `${c.author} <${c.email}>`)))].sort((a, b) =>
a.localeCompare(b),
);
// Listing every author of a large history takes seconds: open with the authors known so far
// (loaded commits and earlier lists) and let only Select… wait for the full list.
const fetching = Promise.all(roots.map((repo) => fetchLogUsers(repo)));
const authorsOf = (users: readonly (readonly string[])[]) =>
[...new Set(users.flat().concat(commits.map((c) => `${c.author} <${c.email}>`)))].sort((a, b) =>
a.localeCompare(b),
);
const authors = authorsOf(roots.map((repo) => knownLogUsers(repo)));
menuBelow(anchor, [
...recentFilterValues("authors").map((authors) => ({
verbatim: true,
@@ -228,7 +232,7 @@ export function FilterBar({ fileHistory = false }: { fileHistory?: boolean }) {
run: async () => {
const r = await chooseMany({
title: "Filter by User",
options: [ME, ...authors],
options: [ME, ...authorsOf(await fetching)],
initial: filters.authors,
customLabel: "Other names or emails (comma-separated):",
});
+41
Переглянути файл
@@ -0,0 +1,41 @@
/**
* Every author of a repository's history (`git log --all`) for the Log's User filter. On a large
* history listing them takes seconds, so the filter opens with what is already known and the list
* is fetched in the background, at most once per {@link REFRESH_MS} for each repository.
*/
import { call, commands } from "@/platform/ipc";
const REFRESH_MS = 5 * 60_000;
const known = new Map<string, { users: readonly string[]; at: number }>();
const pending = new Map<string, Promise<readonly string[]>>();
const key = (repo: string | null) => repo ?? "";
/** The authors of `repo`: the running fetch, else a new one unless the known list is recent. */
export function fetchLogUsers(repo: string | null, now = Date.now()): Promise<readonly string[]> {
const k = key(repo);
const running = pending.get(k);
if (running) return running;
const cached = known.get(k);
if (cached && now - cached.at < REFRESH_MS) return Promise.resolve(cached.users);
const fetch = call(commands.vcsLogUsers(repo))
.then((users) => {
known.set(k, { users, at: Date.now() });
return users as readonly string[];
})
.catch(() => known.get(k)?.users ?? [])
.finally(() => pending.delete(k));
pending.set(k, fetch);
return fetch;
}
/** The authors of `repo` fetched so far, without waiting. */
export function knownLogUsers(repo: string | null): readonly string[] {
return known.get(key(repo))?.users ?? [];
}
/** Test hook: forgets every fetched list. */
export function resetLogUsers() {
known.clear();
pending.clear();
}