Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"awilix": "^12.1.0",
"chalk": "^5.3.0",
"commander": "^11.1.0",
"dotenv": "^17.3.1",
"prompts": "^2.4.2"
},
"devDependencies": {
Expand Down
27 changes: 26 additions & 1 deletion src/commands/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* git-mem hook prompt-submit
*/

import { join } from 'path';
import { execFileSync } from 'child_process';
import { config as loadEnv } from 'dotenv';
import { createContainer } from '../infrastructure/di';
import { readStdin } from '../hooks/utils/stdin';
import { setupShutdown } from '../hooks/utils/shutdown';
Expand Down Expand Up @@ -100,6 +103,22 @@ export function buildEvent(eventType: HookEventType, input: IHookInput): HookEve
}
}

/**
* Find git repository root from a working directory.
* Returns cwd if git command fails (graceful fallback).
*/
function findGitRoot(cwd: string): string {
try {
return execFileSync('git', ['rev-parse', '--show-toplevel'], {
cwd,
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'pipe'],
}).trim();
} catch {
return cwd;
}
}

/** Stderr labels per event for user-facing messages. */
const STDERR_LABELS: Record<HookEventType, { success: string; prefix: string }> = {
'session:start': { success: 'Memory loaded.', prefix: 'Memory loaded' },
Expand All @@ -120,7 +139,13 @@ export async function hookCommand(eventName: string, _logger?: ILogger): Promise

try {
const input = await readStdin<IHookInput>();
const config = loadHookConfig(input.cwd);
const cwd = input.cwd ?? process.cwd();
const repoRoot = findGitRoot(cwd);

// Load .env from repository root for API keys (e.g., ANTHROPIC_API_KEY)
loadEnv({ path: join(repoRoot, '.env'), quiet: true });

Comment on lines +145 to +147
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New behavior: hooks now load .env and mutate process.env before reading config/dispatching. There are existing unit/integration tests for hook behavior, but none assert that .env loading works (or that it doesn’t affect unrelated env vars). Adding an integration test that writes a .env in the temp repo and verifies the hook picks up a value from it would prevent regressions.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point on test coverage. The existing hook integration tests cover the commit-msg flow but don't specifically test .env loading. Will defer adding that test to a follow-up since we've manually verified the end-to-end flow works.

const config = loadHookConfig(repoRoot);

if (!isEventEnabled(config.hooks, eventName)) {
clearTimeout(timer);
Expand Down