Skip to content

feat: add codegraph auto-init-repos command#321

Open
CagesThrottleUs wants to merge 17 commits into
colbymchenry:mainfrom
CagesThrottleUs:feature/add-support-for-auto-attach
Open

feat: add codegraph auto-init-repos command#321
CagesThrottleUs wants to merge 17 commits into
colbymchenry:mainfrom
CagesThrottleUs:feature/add-support-for-auto-attach

Conversation

@CagesThrottleUs
Copy link
Copy Markdown

What this adds

A new codegraph auto-init-repos command that installs a global git template hook so every subsequent git clone or git init automatically runs codegraph init and codegraph index — no manual step required per repo.

# Install once — all future clones auto-initialize
codegraph auto-init-repos

# Undo
codegraph auto-init-repos --remove

How it works

Git copies the contents of ~/.git-templates/ into every new repo's .git/ at clone/init time. The command writes a post-checkout hook into that directory. On first checkout after a clone, the hook:

  1. Checks if .codegraph/ exists
  2. If not → runs codegraph init . + codegraph index, and appends .codegraph/ to .gitignore (idempotent via grep -qxF)
  3. If yes → runs codegraph sync in the background (non-blocking)

The hook is guarded by command -v codegraph so it no-ops silently on machines where CodeGraph is not installed. It also checks $3 = 1 to skip file-level git checkout -- file calls and only fire on actual branch switches/clones.

Platform scope: macOS, Linux, Git for Windows (MINGW). Native Windows cmd/PowerShell is out of scope.

Code structure

src/sync/hook-utils.ts          # NEW — shared primitives (stripMarkerBlock, isEffectivelyEmpty, chmodExecutable)
src/sync/git-hooks.ts           # REFACTORED — now imports from hook-utils (no behavior change)
src/sync/global-hooks.ts        # NEW — resolveTemplateDir, installGlobalAutoInitHook, removeGlobalAutoInitHook
src/bin/auto-init-repos-action.ts # NEW — extracted CLI action handler (clack injected for testability)
src/bin/codegraph.ts            # MODIFIED — registers the auto-init-repos command

Tests

53 new tests across 3 test files, all passing. Full suite: 770 tests, 0 failures.

File Tests Coverage
__tests__/hook-utils.test.ts 14 stripMarkerBlock (6), isEffectivelyEmpty (6), chmodExecutable (2)
__tests__/global-hooks.test.ts 22 resolveTemplateDir, install/remove/status, idempotency, git config isolation
__tests__/auto-init-repos-cli.test.ts 12 install path, remove path, error handling
__tests__/git-hooks.test.ts 7 existing tests — all still pass after refactor

Tests use GIT_CONFIG_GLOBAL env var isolation so they never touch the real ~/.gitconfig.

Design spec

Full requirements, test traceability matrix, and design decisions are in:
docs/superpowers/specs/2026-05-22-auto-init-repos-design.md

Every line of code maps back to a named requirement (REQ-HU-xx, REQ-GL-xx, REQ-CLI-xx). Every requirement has at least one test ID.

Install / remove behavior

Scenario Result
First run Creates ~/.git-templates/hooks/post-checkout, sets git config --global init.templateDir if not already set
Re-run (idempotent) Detects byte-identical block → reports "Already installed", no file write
Existing user hook Appends our block after user content, preserves everything else
--remove with shared hook Strips only our marker block, preserves user content
--remove when only ours Deletes the file entirely
--remove never Touches git config init.templateDir

stripMarkerBlock, isEffectivelyEmpty, and chmodExecutable were
duplicated in git-hooks.ts. Extracting them here lets global-hooks.ts
reuse the same logic without copy-pasting, and pins their contracts
with 11 explicit unit tests.
Installs a post-checkout snippet into the git template directory so
every new git clone automatically runs codegraph init + index. Uses
the same marker-block pattern as the per-repo sync hooks, with full
idempotency and surgical remove that preserves user hook content.
Extracts the auto-init-repos command handler into its own module so
tests can import it directly and mock global-hooks without spawning a
subprocess. Clack is injected via parameter to avoid ESM dynamic
import issues in the test environment.
…guard

Three fixes: normalize trailing whitespace before the unchanged equality
check so re-installs are not spuriously written; gate mkdirSync in
resolveTemplateDir to only run when writeConfig is true, preventing
~/.git-templates/hooks/ from being silently created by remove/status
calls; add a \$3=1 guard to the hook script so it skips file-level
git checkout calls and only fires on actual branch switches.
Documents the new auto-init-repos command in the Initialize Projects
section and adds it to the CLI reference table so users can discover
the zero-config cloning workflow.
Resolves conflict in codegraph.ts: upstream added codegraph uninstall,
this branch added auto-init-repos — both commands kept in order.
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.

1 participant