mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
feat(vcs): improve Git workflows (#5174)
Improve OpenIDEA's Git workflows across commit and staging, branch and remote operations, merge and rebase, Git Log and signing, worktrees, stash and shelf, patch application, and clone settings. Adds a one-click stash for tracked changes across Git roots and lets users open a worktree branch's associated GitHub review from its context menu. Cloning now includes submodules by default (Settings › Version Control › Git, can be turned off). Repository entry points added here run in each repository's execution scope, so they use the project's own git executable.
This commit is contained in:
@@ -196,7 +196,7 @@ export async function openInWorktree(number: number): Promise<void> {
|
||||
failure: tx`Couldn't create the worktree for #${number}`,
|
||||
};
|
||||
const added = await withBusy(op, async () => {
|
||||
await call(commands.vcsAddWorktree(revs.root, path, pr.headRefOid, null));
|
||||
await call(commands.vcsAddWorktree(revs.root, path, pr.headRefOid, null, null, null));
|
||||
return true;
|
||||
});
|
||||
if (added) await executeAction("file.openInNewWindow", path);
|
||||
|
||||
@@ -22,6 +22,7 @@ import { changeRepository, headerMenu } from "./toolwindow/PullRequestsToolWindo
|
||||
import { contextPullRequest, resetTabs } from "./toolwindow/store";
|
||||
import { activeTimelinePullRequest, openTimeline, timelineIcon, timelineTitle } from "./toolwindow/timelineInput";
|
||||
import { numberDialog } from "./ui/dialogs";
|
||||
import { activateWorktreeReviews } from "./worktreeReviews";
|
||||
|
||||
/** The pull request an action applies to: its argument, the timeline editor, the active tab or the list selection. */
|
||||
function targetPullRequest(arg: unknown): number | null {
|
||||
@@ -118,6 +119,7 @@ export function activate(): () => void {
|
||||
}),
|
||||
activateReviewDiff(),
|
||||
activateReviewMode(),
|
||||
activateWorktreeReviews(),
|
||||
];
|
||||
const stopEditors = maybeEditorService()?.onActiveInputChange?.(() =>
|
||||
setContext("githubTimelineActive", activeTimelinePullRequest() !== null),
|
||||
|
||||
@@ -39,7 +39,7 @@ export async function applySuggestion(
|
||||
const abs = `${root}/${target.path}`;
|
||||
const text = await call(commands.fsReadText(abs));
|
||||
const { patch, start, lines } = buildSuggestionPatch(text, target, suggestion);
|
||||
const outcome = await call(commands.vcsApplyPatch(connectedRoot(), patch));
|
||||
const outcome = await call(commands.vcsApplyPatch(connectedRoot(), patch, null));
|
||||
const files = outcome.conflicts.join(", ");
|
||||
if (files) throw new Error(tx`Conflicts in ${files}`);
|
||||
if (commitMessage) {
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { useWorktreeReviewProvider } from "@/workbench/contributions";
|
||||
import { useGhRepository, type GHConnection } from "./data/repository";
|
||||
import { activateWorktreeReviews } from "./worktreeReviews";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
remotes: vi.fn(),
|
||||
config: vi.fn(),
|
||||
rest: vi.fn(),
|
||||
openPullRequest: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/platform/ipc", async (importOriginal) => {
|
||||
const real = await importOriginal<typeof import("@/platform/ipc")>();
|
||||
return {
|
||||
...real,
|
||||
commands: {
|
||||
...real.commands,
|
||||
vcsRemotes: (root: string) => Promise.resolve({ status: "ok", data: mocks.remotes(root) }),
|
||||
vcsConfigGet: (root: string, key: string) => Promise.resolve({ status: "ok", data: mocks.config(root, key) }),
|
||||
},
|
||||
};
|
||||
});
|
||||
vi.mock("./api/client", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("./api/client")>()),
|
||||
rest: mocks.rest,
|
||||
}));
|
||||
vi.mock("./toolwindow/open", () => ({ openPullRequest: mocks.openPullRequest }));
|
||||
|
||||
afterEach(() => {
|
||||
useGhRepository.setState({ connection: null });
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
it("opens only the review of the tracked branch from the connected worktree group", async () => {
|
||||
const stop = activateWorktreeReviews();
|
||||
try {
|
||||
expect(useWorktreeReviewProvider.getState().lookup).toBeNull();
|
||||
useGhRepository.setState({
|
||||
connection: {
|
||||
root: "C:/repo/review",
|
||||
coordinates: { owner: "org", name: "base" },
|
||||
} as GHConnection,
|
||||
});
|
||||
const lookup = useWorktreeReviewProvider.getState().lookup!;
|
||||
expect(await lookup({ root: "C:/unrelated", paths: [], branches: ["feature"] })).toEqual(new Map());
|
||||
expect(mocks.remotes).not.toHaveBeenCalled();
|
||||
|
||||
mocks.remotes.mockReturnValue([{ name: "origin", url: "https://github.com/org/base.git" }]);
|
||||
const config: Record<string, string> = {
|
||||
"branch.feature.remote": "origin",
|
||||
"branch.feature.merge": "refs/heads/remote-feature",
|
||||
"branch.closed.remote": "origin",
|
||||
"branch.closed.merge": "refs/pull/5/head",
|
||||
};
|
||||
mocks.config.mockImplementation((_root, key: string) => config[key] ?? null);
|
||||
mocks.rest
|
||||
.mockResolvedValueOnce([
|
||||
{ number: 3, title: "Different fork", state: "open", head: { repo: { full_name: "org/other" } } },
|
||||
{ number: 7, title: "Matching review", state: "open", head: { repo: { full_name: "org/base" } } },
|
||||
])
|
||||
.mockResolvedValueOnce({ number: 5, title: "Closed review", state: "closed" });
|
||||
|
||||
const reviews = await lookup({
|
||||
root: "C:/repo/main",
|
||||
paths: ["c:\\repo\\review"],
|
||||
branches: ["feature", "closed", "no-upstream"],
|
||||
});
|
||||
expect(mocks.remotes).toHaveBeenCalledExactlyOnceWith("C:/repo/review");
|
||||
expect(mocks.rest).toHaveBeenCalledWith(
|
||||
"GET",
|
||||
"repos/org/base/pulls?state=open&head=org%3Aremote-feature&per_page=1",
|
||||
);
|
||||
expect(mocks.rest).toHaveBeenCalledWith("GET", "repos/org/base/pulls/5");
|
||||
expect([...reviews.keys()]).toEqual(["feature"]);
|
||||
expect(reviews.get("feature")?.title).toBe("Matching review");
|
||||
await reviews.get("feature")?.open();
|
||||
expect(mocks.openPullRequest).toHaveBeenCalledExactlyOnceWith(7);
|
||||
useGhRepository.setState({ connection: null });
|
||||
await reviews.get("feature")?.open();
|
||||
expect(mocks.openPullRequest).toHaveBeenCalledTimes(1);
|
||||
expect(useWorktreeReviewProvider.getState().lookup).toBeNull();
|
||||
} finally {
|
||||
stop();
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,89 @@
|
||||
import { call, commands } from "@/platform/ipc";
|
||||
import { ipcPath } from "@/platform/paths";
|
||||
import { useProject } from "@/platform/project";
|
||||
import {
|
||||
registerWorktreeReviewProvider,
|
||||
type WorktreeReview,
|
||||
type WorktreeReviewQuery,
|
||||
} from "@/workbench/contributions";
|
||||
import { rest } from "./api/client";
|
||||
import { sameRepository, type GHConnection, useGhRepository } from "./data/repository";
|
||||
import { openPullRequestQuery, trackedBranch } from "./reviewMode/model";
|
||||
import { openPullRequest } from "./toolwindow/open";
|
||||
|
||||
interface PullRequestMatch {
|
||||
number: number;
|
||||
title: string;
|
||||
state: string;
|
||||
head: { repo: { full_name: string } | null };
|
||||
}
|
||||
|
||||
function normalizedPath(path: string) {
|
||||
const normalized = ipcPath(path).replace(/\/+$/, "");
|
||||
return /^[A-Za-z]:\//.test(normalized) ? normalized.toLowerCase() : normalized;
|
||||
}
|
||||
|
||||
async function findReviews(
|
||||
{ root, paths, branches }: WorktreeReviewQuery,
|
||||
connection: GHConnection,
|
||||
): Promise<ReadonlyMap<string, WorktreeReview>> {
|
||||
const reviews = new Map<string, WorktreeReview>();
|
||||
const project = useProject.getState().project?.root;
|
||||
const repo = connection.root ?? project;
|
||||
if (!repo || ![root, ...paths].some((path) => normalizedPath(path) === normalizedPath(repo))) return reviews;
|
||||
const isCurrent = () =>
|
||||
useGhRepository.getState().connection === connection && useProject.getState().project?.root === project;
|
||||
const base = connection.coordinates;
|
||||
const remotes = await call(commands.vcsRemotes(repo));
|
||||
if (!isCurrent()) return reviews;
|
||||
await Promise.allSettled(
|
||||
branches.map(async (branch) => {
|
||||
const [remote, merge] = await Promise.all([
|
||||
call(commands.vcsConfigGet(repo, `branch.${branch}.remote`)),
|
||||
call(commands.vcsConfigGet(repo, `branch.${branch}.merge`)),
|
||||
]);
|
||||
const tracked = trackedBranch(remote, merge, remotes, base);
|
||||
if (!tracked || !isCurrent()) return;
|
||||
let match: PullRequestMatch | undefined;
|
||||
if (tracked.pullRequest !== null && sameRepository(tracked.repository, base)) {
|
||||
match =
|
||||
(await rest<PullRequestMatch | null>(
|
||||
"GET",
|
||||
`repos/${base.owner}/${base.name}/pulls/${tracked.pullRequest}`,
|
||||
)) ?? undefined;
|
||||
} else {
|
||||
const query = openPullRequestQuery(base, tracked);
|
||||
if (!query) return;
|
||||
const head = `${tracked.repository.owner}/${tracked.repository.name}`.toLowerCase();
|
||||
match = (await rest<PullRequestMatch[] | null>("GET", query))?.find(
|
||||
(pr) => pr.head.repo?.full_name.toLowerCase() === head,
|
||||
);
|
||||
}
|
||||
if (match?.state !== "open" || !isCurrent()) return;
|
||||
const { title, number } = match;
|
||||
reviews.set(branch, {
|
||||
title,
|
||||
open: async () => {
|
||||
if (isCurrent()) await openPullRequest(number);
|
||||
},
|
||||
});
|
||||
}),
|
||||
);
|
||||
return reviews;
|
||||
}
|
||||
|
||||
export function activateWorktreeReviews() {
|
||||
let dispose: (() => void) | undefined;
|
||||
const sync = (connection: GHConnection | null) => {
|
||||
dispose?.();
|
||||
dispose = connection ? registerWorktreeReviewProvider((query) => findReviews(query, connection)) : undefined;
|
||||
};
|
||||
const stop = useGhRepository.subscribe((state, previous) => {
|
||||
if (state.connection !== previous.connection) sync(state.connection);
|
||||
});
|
||||
sync(useGhRepository.getState().connection);
|
||||
return () => {
|
||||
stop();
|
||||
dispose?.();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user