дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 09:11:10 +02:00
fix(vcs): follow a branch rename only in Log tabs of that repository (#7283)
Renaming a branch renamed the Branch filter and Log tabs of every repository that had a branch of the same name. The rename now applies only to views scoped to the renamed repository (and its linked worktrees, which share its branches). Co-authored-by: audichuang <[email protected]>
Цей коміт міститься в:
1 джерело
783daaa041
коміт
83dcdc8eca
2 files changed
+80
-12
No files matched your search
@@ -149,4 +149,51 @@ describe("renameBranchInLogFilters", () => {
|
||||
expect(useGitLog.getState().logTabs[0]!.target).toBe("main");
|
||||
expect(rememberedLogBranches("/mem-rename-keep")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("keeps another repository's tab of the same branch", () => {
|
||||
project("/B");
|
||||
useVcs.setState({ roots: ["/A", "/B"] });
|
||||
const tab = openLogTab("branch", "feature");
|
||||
selectGitTab("log");
|
||||
renameBranchInLogFilters("/A", "feature", "renamed");
|
||||
const record = useGitLog.getState().logTabs.find((t) => t.id === tab)!;
|
||||
expect([record.repo, record.target, logState(tab).filters.branches]).toEqual(["/B", "feature", ["feature"]]);
|
||||
});
|
||||
|
||||
it("renames the tab of a linked worktree sharing the repository's branches", () => {
|
||||
project("/W", "/W-wt");
|
||||
useVcs.setState({ roots: ["/W", "/W-wt"], branchRoots: { "/W-wt": "/W" } });
|
||||
const tab = openLogTab("branch", "feature");
|
||||
selectGitTab("log");
|
||||
renameBranchInLogFilters("/W", "feature", "renamed");
|
||||
expect(useGitLog.getState().logTabs.find((t) => t.id === tab)!.target).toBe("renamed");
|
||||
expect(logState(tab).filters.branches).toEqual(["renamed"]);
|
||||
useVcs.setState({ branchRoots: {} });
|
||||
});
|
||||
|
||||
it("follows the rename in a log of a repository the project does not list", () => {
|
||||
project("/A");
|
||||
useVcs.setState({ roots: ["/A", "/B"] });
|
||||
// An external log of a linked worktree outside the project.
|
||||
useGitLog.setState({ filters: { ...emptyFilters(), branches: ["feature"], repositories: ["/elsewhere"] } });
|
||||
renameBranchInLogFilters("/A", "feature", "renamed");
|
||||
expect(useGitLog.getState().filters.branches).toEqual(["renamed"]);
|
||||
});
|
||||
|
||||
it("renames as before when the renamed repository is no longer listed", () => {
|
||||
project("/B");
|
||||
useVcs.setState({ roots: ["/B"] });
|
||||
const tab = openLogTab("branch", "topic");
|
||||
selectGitTab("log");
|
||||
renameBranchInLogFilters("/gone", "topic", "renamed");
|
||||
expect(useGitLog.getState().logTabs.find((t) => t.id === tab)!.target).toBe("renamed");
|
||||
});
|
||||
|
||||
it("keeps a branch filter that queries only other repositories", () => {
|
||||
project("/A");
|
||||
useVcs.setState({ roots: ["/A", "/B"] });
|
||||
useGitLog.setState({ filters: { ...emptyFilters(), branches: ["feature"], repositories: ["/B"] } });
|
||||
renameBranchInLogFilters("/A", "feature", "renamed");
|
||||
expect(useGitLog.getState().filters.branches).toEqual(["feature"]);
|
||||
});
|
||||
});
|
||||
@@ -311,25 +311,46 @@ export function setLogBranches(branches: readonly string[]) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Follows a branch rename: the Branch filter of every open view, the choice
|
||||
* Follows a branch rename in `repo`: the Branch filter of every open view, the choice
|
||||
* remembered for the repository and "Log: branch" tabs keep tracking the branch
|
||||
* under its new name instead of filtering an unknown revision.
|
||||
* under its new name instead of filtering an unknown revision. A view or tab scoped to
|
||||
* other repositories keeps its branch of the same name.
|
||||
*/
|
||||
export function renameBranchInLogFilters(repo: string, from: string, to: string) {
|
||||
const rename = (branches: readonly string[]) => branches.map((b) => (b === from ? to : b));
|
||||
const remembered = rememberedLogBranches(repo);
|
||||
if (remembered?.includes(from)) rememberLogBranches(repo, rename(remembered));
|
||||
// Linked worktrees share their main repository's branches. A repository the project does not
|
||||
// list (an external log) may be one of them, so only a listed one counts as another repository.
|
||||
const { roots: listed, branchRoots } = useVcs.getState();
|
||||
const group = (root: string) => branchRoots[root] ?? root;
|
||||
const owns = (root: string) => group(root) === group(repo);
|
||||
const isListed = (root: string) => listed.includes(root) || root in branchRoots;
|
||||
// A repository no longer listed (its mapping removed meanwhile) can't be told apart: as before.
|
||||
const other = (root: string) => isListed(repo) && isListed(root) && !owns(root);
|
||||
const tabRoots = (tab: LogTab | undefined) =>
|
||||
tab?.repositories?.length ? tab.repositories : tab?.repo ? [tab.repo] : [];
|
||||
const renamed = (filters: LogFilters, tab: LogTab | undefined): LogFilters | null => {
|
||||
if (!filters.branches.includes(from)) return null;
|
||||
// The repositories the view queries, as logRepositoryScope picks them.
|
||||
const roots = tabRoots(tab).length ? tabRoots(tab) : (filters.repositories ?? []);
|
||||
const queried = filters.repositories?.length ? roots.filter((r) => filters.repositories!.includes(r)) : roots;
|
||||
if (queried.length && queried.every(other)) return null;
|
||||
return { ...filters, branches: rename(filters.branches) };
|
||||
};
|
||||
const s = useGitLog.getState();
|
||||
if (s.filters.branches.includes(from))
|
||||
useGitLog.setState({ filters: { ...s.filters, branches: rename(s.filters.branches) } });
|
||||
for (const [id, view] of parked)
|
||||
if (view.filters.branches.includes(from))
|
||||
setViewState(id, { filters: { ...view.filters, branches: rename(view.filters.branches) } });
|
||||
if (s.logTabs.some((t) => t.kind === "branch" && t.target === from))
|
||||
useGitLog.setState((l) => ({
|
||||
logTabs: l.logTabs.map((t) => (t.kind === "branch" && t.target === from ? { ...t, target: to } : t)),
|
||||
}));
|
||||
if (s.filters.branches.includes(from)) void reloadLog();
|
||||
const tabOf = (id: string) => s.logTabs.find((t) => t.id === id);
|
||||
const shown = renamed(s.filters, tabOf(s.shownLog));
|
||||
if (shown) useGitLog.setState({ filters: shown });
|
||||
for (const [id, view] of parked) {
|
||||
const next = renamed(view.filters, tabOf(id));
|
||||
if (next) setViewState(id, { filters: next });
|
||||
}
|
||||
const follows = (t: LogTab) =>
|
||||
t.kind === "branch" && t.target === from && !(tabRoots(t).length && tabRoots(t).every(other));
|
||||
if (s.logTabs.some(follows))
|
||||
useGitLog.setState((l) => ({ logTabs: l.logTabs.map((t) => (follows(t) ? { ...t, target: to } : t)) }));
|
||||
if (shown) void reloadLog();
|
||||
}
|
||||
|
||||
const loadPromises = new Map<string, Promise<void>>();
|
||||
|
||||
Reference in new issue
Заблокувати користувача