/install code-graph
代码图谱(GitNexus)
为当前项目一键完成 GitNexus 安装、MCP 配置和知识图谱构建。每一步都有跳过检测,已完成的步骤不会重复执行。
语言支持
TypeScript、JavaScript、Python、Java、C、C++、C#、Go、Rust、PHP、Swift、Dart、Kotlin、Ruby 等 14 种语言。
执行流程
按顺序执行以下步骤。每步先检测当前状态,已完成则跳过。
第 1 步:定位项目根目录
gitnexus analyze 需在项目根目录下执行。首先检查当前工作目录是否即为项目根目录:
git rev-parse --show-toplevel 2>&1 && echo "已定位到项目根目录"
如果 Git 未初始化,则通过常见项目标志文件判断:
ls package.json 2>&1 || ls pom.xml 2>&1 || ls go.mod 2>&1 || ls Cargo.toml 2>&1 || ls requirements.txt 2>&1 || echo "未找到项目根目录标志文件"
- 当前目录即为根目录 → 继续下一步
- 检测到其他目录为根目录 →
cd到该目录 - 找不到任何项目标志 → 询问用户项目根目录在哪,或使用
git init初始化
第 2 步:检查 Node.js 环境
node --version
要求 Node.js >= 22。 如果版本过低或未安装:
- 引导用户到 nodejs.org 下载 LTS 版本
- Windows 用户也可用
winget install OpenJS.NodeJS.LTS安装 - 升级完成后重新执行本技能
第 3 步:安装 GitNexus
先检测,再决定是否安装:
gitnexus --version 2>&1
判断逻辑:
- 输出类似
1.x.x的版本号 → 已安装,跳过本步骤,直接进入第 4 步 - 输出
command not found或not recognized→ 未安装,执行以下安装
安装命令(Windows,推荐全局安装):
set GITNEXUS_SKIP_OPTIONAL_GRAMMARS=1 && npm install -g gitnexus
安装命令(macOS / Linux):
GITNEXUS_SKIP_OPTIONAL_GRAMMARS=1 npm install -g gitnexus
GITNEXUS_SKIP_OPTIONAL_GRAMMARS=1跳过 Dart/Proto 原生语法解析器的编译,安装只需几秒,无需 C++ 工具链。
权限不足时的回退方案 — 如果全局安装失败(EACCES / Permission denied),后续命令统一改用 npx 前缀:
npx gitnexus analyze # 代替 gitnexus analyze
npx gitnexus setup # 代替 gitnexus setup
npx gitnexus status # 代替 gitnexus status
第 4 步:配置 MCP 服务
gitnexus setup 自动检测已安装的编辑器并写入 MCP 配置,全局只需执行一次。
先检测是否已配置:
gitnexus setup 2>&1
判断逻辑:
- 输出包含
already configured、up to date、MCP configuration exists等字样 → 已配置,跳过 - 输出包含
wrote、created、configured、Updated等字样 → 配置成功 - 若
gitnexus命令不可用(未全局安装),使用npx gitnexus setup
setup是幂等的,重复执行不会产生重复配置,可安全地直接运行。
第 5 步:构建知识图谱
先检测当前项目是否已索引:
gitnexus status 2>&1
判断逻辑:
- 输出包含
Indexed、symbols、up to date、fresh等字样 → 已索引且是最新的,跳过本步骤 - 输出包含
Not indexed、No index found、unknown等字样 → 未索引,执行以下命令 - 如果
gitnexus不可用,改用npx gitnexus status
执行索引:
gitnexus analyze
此命令解析整个代码库、解析导入关系、追踪调用链、检测代码社区,在项目根目录生成 .gitnexus/ 索引(自动加入 .gitignore)。优先使用gitnexus analyze进行索引,若出现错误则尝试增加选项。
如果需要强制重建(已索引但过期):
gitnexus analyze --force
常用选项:
| 选项 | 用途 |
|---|---|
--embeddings |
启用语义/向量搜索,速度较慢但搜索更强 |
--force |
强制重建索引(索引过期时使用) |
--skip-embeddings |
跳过嵌入生成,大幅加速(默认行为) |
--skip-git |
允许对非 git 目录构建索引 |
--skip-agents-md |
保留自定义的 CLAUDE.md / AGENTS.md |
--verbose |
输出详细日志,包括被跳过的文件 |
--worker-timeout 60 |
大型仓库增加 Worker 超时时间 |
预期输出:
Scanning files... ✓ (1,247 files)
Parsing code... ✓ (TypeScript, Python, etc.)
Resolving imports... ✓ (3,892 relationships)
Tracing calls... ✓ (104 processes)
Creating indexes... ✓
Done! Indexed 30,816 symbols in 12s
第 6 步:验证
gitnexus status
gitnexus list
status 确认当前项目的索引状态,list 展示全局所有已索引的仓库。
索引后可用的 MCP 工具
| 工具 | 用途 |
|---|---|
list_repos |
查看所有已索引的仓库 |
query |
混合搜索(关键词 + 语义),按执行流程分组 |
context |
360° 符号视图(调用者、被调用、所属流程) |
impact |
爆炸半径分析 — 修改某个符号会影响哪些地方 |
detect_changes |
Git diff 影响映射 |
cypher |
原始图谱查询语言 |
rename |
多文件协调重命名 |
MCP 资源:
| 资源 | 用途 |
|---|---|
gitnexus://repo/{name}/context |
代码库概览 + 索引新鲜度检查 |
gitnexus://repo/{name}/clusters |
所有功能聚类 |
gitnexus://repo/{name}/processes |
所有执行流程 |
gitnexus://repo/{name}/schema |
图谱 Schema(Cypher 查询用) |
常见问题排查
"Not inside a git repository"(不在 git 仓库中)
git init && git add . && git commit -m "initial"
gitnexus analyze
或使用 --skip-git 跳过 git 检查:
gitnexus analyze --skip-git
"Permission denied"(npm install -g 权限不足)
改用 npx 免安装运行:
npx gitnexus analyze
"Already indexed"(已索引)
索引已存在且是最新的,无需操作。如需强制重建:
gitnexus analyze --force
"Node version too old"(Node 版本过旧)
需要 Node.js >= 22,升级后重试:
- Windows:
winget install OpenJS.NodeJS.LTS - macOS:
brew install node - Linux: 使用 nvm
MCP 工具未出现在 Claude Code / Cursor 中
- 重新执行
gitnexus setup - 重启编辑器
- 确认 gitnexus 在 PATH 中:Windows 用
where gitnexus,Unix 用which gitnexus
索引过期(代码变更后)
gitnexus analyze # 增量更新,保留已有嵌入
gitnexus analyze --force # 完全重建
对话示例
用户:"安装GitNexus"
执行:
- 定位项目根目录
node --version→ 确认 >= 22gitnexus --version→ 未安装 →npm install -g gitnexusgitnexus setup→ MCP 已配置则跳过gitnexus status→ 未索引 →gitnexus analyzegitnexus status→ 验证成功- 回复用户:"已索引 N 个符号,生成知识图谱。可通过 context / impact / query 等 MCP 工具查询代码库。"
- Make sure OpenClaw is installed (local or Docker)
- Run the install command in chat:
/install code-graph - After installation, invoke the skill by name or use
/code-graph - Provide required inputs per the skill's parameter spec and get structured output
What is Code Graph(代码图谱)?
一键安装 GitNexus 并为当前项目构建代码知识图谱。当用户说「安装GitNexus」「设置GitNexus」「初始化GitNexus」「配置GitNexus」「用GitNexus建图」「GitNexus一键配置」时使用。 It is an AI Agent Skill for Claude Code / OpenClaw, with 45 downloads so far.
How do I install Code Graph(代码图谱)?
Run "/install code-graph" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.
Is Code Graph(代码图谱) free?
Yes, Code Graph(代码图谱) is completely free, licensed under MIT-0. You can download, install and use it at no cost.
Which platforms does Code Graph(代码图谱) support?
Code Graph(代码图谱) is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).
Who created Code Graph(代码图谱)?
It is built and maintained by manniu (@babywhale); the current version is v1.0.1.