git-hooks.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * Git Sync Hooks
  3. *
  4. * When the live file watcher is disabled (e.g. on WSL2 `/mnt/*` drives,
  5. * see watch-policy.ts), the CodeGraph index would otherwise go stale until
  6. * the user runs `codegraph sync` by hand. As an opt-in alternative, we can
  7. * install git hooks that refresh the index after the operations that change
  8. * files on disk: commit, merge (covers `git pull`), and checkout.
  9. *
  10. * The hooks run `codegraph sync` in the background so they never block git,
  11. * and are guarded by `command -v codegraph` so they no-op cleanly when the
  12. * CLI isn't on PATH. Our snippet is delimited by marker comments so install
  13. * is idempotent and removal preserves any user-authored hook content.
  14. */
  15. import * as fs from 'fs';
  16. import * as path from 'path';
  17. import { execFileSync } from 'child_process';
  18. const MARKER_BEGIN = '# >>> codegraph sync hook >>>';
  19. const MARKER_END = '# <<< codegraph sync hook <<<';
  20. export type GitHookName = 'post-commit' | 'post-merge' | 'post-checkout';
  21. /** Hooks installed by default: commit, merge (git pull), and checkout. */
  22. export const DEFAULT_SYNC_HOOKS: GitHookName[] = ['post-commit', 'post-merge', 'post-checkout'];
  23. export interface GitHookResult {
  24. /** Hook names that were created or updated. */
  25. installed: GitHookName[];
  26. /** Resolved hooks directory, or null when not a git repo. */
  27. hooksDir: string | null;
  28. /** Reason nothing happened (e.g. not a git repository). */
  29. skipped?: string;
  30. }
  31. /**
  32. * Whether `projectRoot` is inside a git working tree. Returns false if git
  33. * isn't installed or the path isn't a repo.
  34. */
  35. export function isGitRepo(projectRoot: string): boolean {
  36. try {
  37. const out = execFileSync('git', ['rev-parse', '--is-inside-work-tree'], {
  38. cwd: projectRoot,
  39. encoding: 'utf8',
  40. stdio: ['ignore', 'pipe', 'ignore'],
  41. windowsHide: true,
  42. }).trim();
  43. return out === 'true';
  44. } catch {
  45. return false;
  46. }
  47. }
  48. /**
  49. * Resolve the git hooks directory for a project, honoring `core.hooksPath`
  50. * and git worktrees. Returns an absolute path, or null when not a repo.
  51. */
  52. function gitHooksDir(projectRoot: string): string | null {
  53. try {
  54. const out = execFileSync('git', ['rev-parse', '--git-path', 'hooks'], {
  55. cwd: projectRoot,
  56. encoding: 'utf8',
  57. stdio: ['ignore', 'pipe', 'ignore'],
  58. windowsHide: true,
  59. }).trim();
  60. if (!out) return null;
  61. return path.isAbsolute(out) ? out : path.resolve(projectRoot, out);
  62. } catch {
  63. return null;
  64. }
  65. }
  66. /** The shell snippet (between markers) injected into each hook. */
  67. function markerBlock(): string {
  68. return [
  69. MARKER_BEGIN,
  70. '# Keeps the CodeGraph index fresh while the live file watcher is off',
  71. '# (e.g. WSL2 /mnt drives). Runs in the background so it never blocks git.',
  72. '# Managed by codegraph; remove with `codegraph uninit` or delete this block.',
  73. 'if command -v codegraph >/dev/null 2>&1; then',
  74. ' ( codegraph sync >/dev/null 2>&1 & ) >/dev/null 2>&1',
  75. 'fi',
  76. MARKER_END,
  77. ].join('\n');
  78. }
  79. /** Remove our marker block (and the marker lines) from hook content. */
  80. function stripMarkerBlock(content: string): string {
  81. const lines = content.split('\n');
  82. const kept: string[] = [];
  83. let inBlock = false;
  84. for (const line of lines) {
  85. const trimmed = line.trim();
  86. if (trimmed === MARKER_BEGIN) { inBlock = true; continue; }
  87. if (trimmed === MARKER_END) { inBlock = false; continue; }
  88. if (!inBlock) kept.push(line);
  89. }
  90. return kept.join('\n');
  91. }
  92. /** Whether a hook body is just a shebang / blank lines (i.e. only ever ours). */
  93. function isEffectivelyEmpty(content: string): boolean {
  94. return content
  95. .split('\n')
  96. .map((l) => l.trim())
  97. .every((l) => l.length === 0 || l.startsWith('#!'));
  98. }
  99. function chmodExecutable(file: string): void {
  100. try {
  101. fs.chmodSync(file, 0o755);
  102. } catch {
  103. /* chmod is a no-op / unsupported on some platforms (e.g. Windows) */
  104. }
  105. }
  106. /**
  107. * Install (or update) the CodeGraph sync hooks in a git repository.
  108. * Idempotent: re-running replaces our marker block rather than duplicating
  109. * it, and any user-authored hook content is preserved.
  110. */
  111. export function installGitSyncHook(
  112. projectRoot: string,
  113. hooks: GitHookName[] = DEFAULT_SYNC_HOOKS,
  114. ): GitHookResult {
  115. const hooksDir = gitHooksDir(projectRoot);
  116. if (!hooksDir) {
  117. return { installed: [], hooksDir: null, skipped: 'not a git repository' };
  118. }
  119. try {
  120. fs.mkdirSync(hooksDir, { recursive: true });
  121. } catch {
  122. return { installed: [], hooksDir, skipped: 'could not access the git hooks directory' };
  123. }
  124. const block = markerBlock();
  125. const installed: GitHookName[] = [];
  126. for (const hook of hooks) {
  127. const file = path.join(hooksDir, hook);
  128. let content: string;
  129. if (fs.existsSync(file)) {
  130. // Strip any prior block, then re-append the current one.
  131. const base = stripMarkerBlock(fs.readFileSync(file, 'utf8')).replace(/\s*$/, '');
  132. content = base.length > 0
  133. ? `${base}\n\n${block}\n`
  134. : `#!/bin/sh\n${block}\n`;
  135. } else {
  136. content = `#!/bin/sh\n${block}\n`;
  137. }
  138. fs.writeFileSync(file, content);
  139. chmodExecutable(file);
  140. installed.push(hook);
  141. }
  142. return { installed, hooksDir };
  143. }
  144. /**
  145. * Remove the CodeGraph sync hooks. Strips only our marker block; deletes the
  146. * hook file entirely when nothing but a shebang remains, otherwise rewrites
  147. * the user's content untouched.
  148. */
  149. export function removeGitSyncHook(
  150. projectRoot: string,
  151. hooks: GitHookName[] = DEFAULT_SYNC_HOOKS,
  152. ): GitHookResult {
  153. const hooksDir = gitHooksDir(projectRoot);
  154. if (!hooksDir) {
  155. return { installed: [], hooksDir: null, skipped: 'not a git repository' };
  156. }
  157. const removed: GitHookName[] = [];
  158. for (const hook of hooks) {
  159. const file = path.join(hooksDir, hook);
  160. if (!fs.existsSync(file)) continue;
  161. const original = fs.readFileSync(file, 'utf8');
  162. if (!original.includes(MARKER_BEGIN)) continue;
  163. const stripped = stripMarkerBlock(original);
  164. if (isEffectivelyEmpty(stripped)) {
  165. fs.unlinkSync(file);
  166. } else {
  167. fs.writeFileSync(file, `${stripped.replace(/\s*$/, '')}\n`);
  168. chmodExecutable(file);
  169. }
  170. removed.push(hook);
  171. }
  172. return { installed: removed, hooksDir };
  173. }
  174. /** Whether any CodeGraph sync hook is currently installed. */
  175. export function isSyncHookInstalled(
  176. projectRoot: string,
  177. hooks: GitHookName[] = DEFAULT_SYNC_HOOKS,
  178. ): boolean {
  179. const hooksDir = gitHooksDir(projectRoot);
  180. if (!hooksDir) return false;
  181. return hooks.some((hook) => {
  182. const file = path.join(hooksDir, hook);
  183. return fs.existsSync(file) && fs.readFileSync(file, 'utf8').includes(MARKER_BEGIN);
  184. });
  185. }