Skip to content

feat: use git root for consistent project name detection#1298

Open
Boltthecat19 wants to merge 1 commit intothedotmack:mainfrom
Boltthecat19:feat/project-directory-isolation
Open

feat: use git root for consistent project name detection#1298
Boltthecat19 wants to merge 1 commit intothedotmack:mainfrom
Boltthecat19:feat/project-directory-isolation

Conversation

@Boltthecat19
Copy link

Problem

getProjectName() uses path.basename(cwd) to identify the current project. This produces inconsistent names when Claude is invoked from a subdirectory — e.g. working in my-project/src/utils yields a project name of utils, not my-project. Running from the home directory yields the username.

This causes observations and memory to be siloed under incorrect project names, breaking cross-session context.

Solution

Add a findGitRoot() utility (src/utils/git-root.ts) that walks up the directory tree from cwd looking for a .git entry (directory or file, to support worktrees). The git root directory name is used as the project identifier. Non-git directories fall back to the existing path.basename(cwd) behavior.

Changes

  • src/utils/git-root.ts — new utility, walks cwd upward to find .git
  • src/utils/project-name.ts — prefer git root name, fall back to basename
  • tests/utils/git-root.test.ts — 6 unit tests: null input, no git, cwd root, subdirectory, worktree (.git file)
  • tests/utils/project-name.test.ts — 5 unit tests covering all branches

Test Results

All 11 new tests pass in isolation and with --no-parallel.

Note: The full bun test suite shows 5 spurious failures for getProjectName caused by a pre-existing mock.module() leak in tests/hooks/context-reinjection-guard.test.ts:32 that pollutes parallel test workers. This is unrelated to this PR and will be addressed separately.

getProjectName() previously used path.basename(cwd), which produces
inconsistent project names when Claude is invoked from a subdirectory
or from the home directory (e.g. project name becomes 'src' or 'derick').

Add findGitRoot() utility that walks up the directory tree to find the
nearest .git entry (directory or file for worktrees), then use that
directory name as the project identifier. Falls back to existing
basename behavior for non-git directories.

- src/utils/git-root.ts: new utility, walks cwd upward looking for .git
- src/utils/project-name.ts: prefer git root name, fall back to basename
- tests/utils/git-root.test.ts: 6 unit tests covering null, no-git, subdir, worktree
- tests/utils/project-name.test.ts: 5 unit tests covering all branches

All 11 new tests pass in isolation and with --no-parallel.
Note: full suite shows 5 failures for getProjectName due to a pre-existing
mock.module() leak in tests/hooks/context-reinjection-guard.test.ts:32
that affects parallel test workers. Tracked separately.
Copy link

@xkonjin xkonjin left a comment

Choose a reason for hiding this comment

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

Quick review pass:

  • Main risk area here is input validation, path handling, and malformed payload behavior.
  • Good to see test coverage move with the code; I’d still make sure it exercises the unhappy path around input validation, path handling, and malformed payload behavior rather than only the happy path.
  • Before merge, I’d smoke-test the behavior touched by git-root.ts, project-name.ts, git-root.test.ts (+1 more) with malformed input / retry / rollback cases, since that’s where this class of change usually breaks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants