/** Git User Name Is Not Defined: before a commit, asks for `user.name` / `user.email` when a repository has none. */ import { useState } from "react"; import { tx, useTx } from "@/platform/i18n"; import { call, commands } from "@/platform/ipc"; import { useProject } from "@/platform/project"; import { Checkbox, Dialog, Input, T } from "@/ui"; import { notifications } from "@/workbench/contributions"; import { openDialog } from "./dialogs"; import styles from "./common.module.css"; export interface CommitIdentity { name: string; email: string; /** Written to the user's global Git config instead of each repository's. */ global: boolean; } interface Configured { name: string | null; email: string | null; } async function configured(root: string | null): Promise { const get = (key: string) => call(commands.vcsConfigGet(root, key)).then( (v) => v?.trim() || null, () => null, ); const [name, email] = await Promise.all([get("user.name"), get("user.email")]); return { name, email }; } /** The account name in the project path (`/Users//…`), as the proposed user name. */ function systemUserName(): string { const root = useProject.getState().project?.root ?? ""; return /^\/(?:Users|home)\/([^/]+)/.exec(root)?.[1] ?? /^[A-Za-z]:[\\/]Users[\\/]([^\\/]+)/.exec(root)?.[1] ?? ""; } export function identityError(name: string, email: string): string | null { if (!name.trim() || !email.trim()) return tx("You need to specify username and email for Git"); if (!email.includes("@")) return tx("The email address must contain the '@' symbol"); return null; } /** * Makes sure every repository the commit writes to has a commit identity: when one lacks * `user.name` or `user.email`, asks for them and sets them. False when the user cancelled * or they could not be set. */ export async function ensureCommitIdentity(roots: readonly (string | null)[], amend: boolean): Promise { const found = await Promise.all(roots.map(async (root) => ({ root, ...(await configured(root)) }))); const missing = found.filter((r) => !r.name || !r.email); if (!missing.length) return true; // A user that every other repository agrees on is proposed (IDEA does the same). const others = found.filter((r) => r.name && r.email); const same = others.length > 0 && others.every((r) => r.name === others[0]!.name && r.email === others[0]!.email); const initial = { name: same ? others[0]!.name! : (missing.find((r) => r.name)?.name ?? systemUserName()), email: same ? others[0]!.email! : (missing.find((r) => r.email)?.email ?? ""), }; const identity = await new Promise((resolve) => openDialog((close) => ( 1 ? missing.map((r) => r.root ?? "") : []} amend={amend} done={(v) => { close(); resolve(v); }} /> )), ); if (!identity) return false; try { if (identity.global) await call(commands.vcsSetUserIdentity(missing[0]!.root, identity.name, identity.email, true)); else for (const { root } of missing) await call(commands.vcsSetUserIdentity(root, identity.name, identity.email, false)); return true; } catch (e) { notifications().notify({ group: "vcs", level: "error", title: tx("Could not set the Git user name and email"), body: e instanceof Error ? e.message : String(e), }); return false; } } function CommitIdentityDialog({ initial, roots, amend, done, }: { initial: { name: string; email: string }; /** The repositories without an identity, listed when the commit spans several. */ roots: readonly string[]; amend: boolean; done: (identity: CommitIdentity | null) => void; }) { const t = useTx(); const [name, setName] = useState(initial.name); const [email, setEmail] = useState(initial.email); const [global, setGlobal] = useState(true); const [submitted, setSubmitted] = useState(false); const error = submitted ? identityError(name, email) : null; const n = roots.length; const submit = () => { setSubmitted(true); if (identityError(name, email)) return; done({ name: name.trim(), email: email.trim(), global }); }; return ( done(null)} width={440} buttons={[ { label: amend ? "Set and Amend Commit" : "Set and Commit", role: "default", onClick: submit }, { label: "Cancel", role: "cancel" }, ]} >
{roots.length > 0 && (
{n === 1 ? t("Git user.name and user.email properties are not defined in 1 root") : t`Git user.name and user.email properties are not defined in ${n} roots`} {roots.map((r) => (
{r}
))}
)} setName(e.target.value)} /> setEmail(e.target.value)} /> {error && (
{error}
)}
); }