/** * Settings › Version Control › Git › Enable Git integration. Off, the backend finds no * repositories and refuses the commands that start git on their own (clone, init, GitHub); * the frontend leaves out every Git action, tool window and menu. */ import { create } from "zustand"; import { setContext } from "./context"; import { onWorkflowSettingsChange, useWorkflowSettings, workflowSettings } from "./workflowSettings"; export function gitEnabled(): boolean { return workflowSettings().vcs.git.enabled; } export function useGitEnabled(): boolean { return useWorkflowSettings((w) => w.vcs.git.enabled); } /** * Keeps the `gitDisabled` context key (for `when` clauses) in step with the setting. * Unset it reads as enabled, so a window that never installs it keeps its Git actions. */ export function installGitEnabledContext(): () => void { setContext("gitDisabled", !gitEnabled()); const off = onWorkflowSettingsChange( (w) => w.vcs.git.enabled, (on) => setContext("gitDisabled", !on), ); return () => { off(); setContext("gitDisabled", false); }; } /** Runs `register` while Git integration is on and disposes what it registered while it is off. */ export function whileGitEnabled(register: () => () => void): () => void { let dispose: (() => void) | null = null; const sync = (on: boolean) => { if (on && !dispose) dispose = register(); else if (!on && dispose) { dispose(); dispose = null; } }; sync(gitEnabled()); const off = onWorkflowSettingsChange((w) => w.vcs.git.enabled, sync); return () => { off(); dispose?.(); dispose = null; }; } /** * The project has a working copy of another system (Subversion), which Enable Git integration * does not turn off: the shared version control UI (Commit, log, annotate) stays for it, and * what only Git has is hidden by the root's system (`vcsKind`). */ /** * `menu`: the version control menu's name, as IDEA names it: the project's one system (`git`, * `svn`), else `vcs`. */ export type VcsMenuName = "git" | "svn" | "vcs"; export const useOtherVcsRoots = create<{ present: boolean; menu: VcsMenuName }>(() => ({ present: false, menu: "git", })); export function setOtherVcsRoots(present: boolean, menu: VcsMenuName = "git"): void { const s = useOtherVcsRoots.getState(); if (s.present !== present || s.menu !== menu) useOtherVcsRoots.setState({ present, menu }); } /** Git integration is on, or the project has a working copy of another system. */ export function vcsEnabled(): boolean { return gitEnabled() || useOtherVcsRoots.getState().present; } /** [`whileGitEnabled`] for what other systems share with Git: on while [`vcsEnabled`]. */ export function whileVcsEnabled(register: () => () => void): () => void { let dispose: (() => void) | null = null; const sync = () => { const on = vcsEnabled(); if (on && !dispose) dispose = register(); else if (!on && dispose) { dispose(); dispose = null; } }; sync(); const offs = [onWorkflowSettingsChange((w) => w.vcs.git.enabled, sync), useOtherVcsRoots.subscribe(sync)]; return () => { offs.forEach((off) => off()); dispose?.(); dispose = null; }; }