Files
llmx/codex-cli/src/utils/check-in-git.ts
Ilan Bigio 59a180ddec Initial commit
Signed-off-by: Ilan Bigio <ilan@openai.com>
2025-04-16 12:56:08 -04:00

32 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { execSync } from "child_process";
/**
* Returns true if the given directory is part of a Git repository.
*
* This uses the canonical Git command `git rev-parse --is-inside-work-tree`
* which exits with status 0 when executed anywhere inside a working tree
* (including the repo root) and exits with a nonzero status otherwise. We
* intentionally ignore stdout/stderr and only rely on the exit code so that
* this works consistently across Git versions and configurations.
*
* The function is fully synchronous because it is typically used during CLI
* startup (e.g. to decide whether to enable certain Gitspecific features) and
* a synchronous check keeps such callsites simple. The command is extremely
* fast (~1ms) so blocking the eventloop briefly is acceptable.
*/
export function checkInGit(workdir: string): boolean {
try {
// "git rev-parse --is-inside-work-tree" prints either "true" or "false" to
// stdout. We don't care about the output — only the exit status — so we
// discard stdio for maximum performance and to avoid leaking noise if the
// caller happens to inherit stdio.
execSync("git rev-parse --is-inside-work-tree", {
cwd: workdir,
stdio: "ignore",
});
return true;
} catch {
return false;
}
}