尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Repomix 入门指南:将整个代码库打包为 AI 友好文件,高效投喂给 Claude、ChatGPT、Gemini 等大模型

Repomix 入门指南:将整个代码库打包为 AI 友好文件,高效投喂给 Claude、ChatGPT、Gemini 等大模型 Repomix 入门指南将整个代码库打包为 AI 友好文件高效投喂给 Claude、ChatGPT、Gemini 等大模型【免费下载链接】repomix Repomix is a powerful tool that packs your entire repository into a single, AI-friendly file. Perfect for when you need to feed your codebase to Large Language Models (LLMs) or other AI tools like Claude, ChatGPT, DeepSeek, Perplexity, Gemini, Gemma, Llama, Grok, and more.项目地址: https://gitcode.com/GitHub_Trending/rep/repomixRepomix 是一款将完整代码库打包成单个 AI 友好文件的命令行工具专为把代码交给大型语言模型LLM分析而设计兼容 ChatGPT、Claude、Gemini、Grok、DeepSeek、Perplexity、Gemma、Llama 等主流模型。本文将围绕其入门使用展开从快速开始、安装方式、核心功能到配置与进阶用法逐层深入并结合仓库源码说明其底层实现帮助你在几分钟内获得一个文件喂给 AI 做全库审查、重构、排障的完整实战能力。快速开始在你的项目根目录下运行一行命令即可完成打包npx repomixlatest就这么简单命令执行后你会在当前目录中找到一个repomix-output.xml文件其中以 AI 友好的格式整理并合并了整个代码库文件摘要、目录结构、文件内容等。然后你可以把这个文件直接发送给 AI 助手并附上类似这样的提示这个文件包含了仓库中所有文件的合并内容。 我想重构代码请先帮我审查一下。AI 将基于完整的代码库上下文进行分析并提供全面见解。在讨论具体修改时AI 还可以帮助你生成代码——通过像 Claude 的 Artifacts 这样的功能你甚至可以一次性接收多个相互依赖的文件。提示默认输出文件名与格式可通过-o, --output与--style选项调整详见后文输出格式与命令行选项章节。为什么选择 RepomixRepomix 的优势在于能够搭配 ChatGPT、Claude、Gemini、Grok 等任何订阅服务使用无需额外费用。它提供完整的代码库上下文省去了逐个查看文件的麻烦让分析更快速、更准确。有了整个代码库作为上下文Repomix 可以应用于各种场景包括但不限于方案设计与实施规划AI 基于现有架构给出建议Bug 排查跨文件追踪问题根本原因第三方库安全审计分析依赖项的安全隐患文档生成自动产出 API 文档、开发者指南等核心功能一览AI 优化以 AI 易于理解的结构化格式整理代码库默认输出 XMLToken 计数统计 Token 使用量方便管理 LLM 上下文窗口Git 感知自动识别并遵循.gitignore和.git/info/exclude文件注重安全使用 Secretlint 进行敏感信息API 密钥、令牌、私钥等检测多种输出格式可选 XML、Markdown、JSON 或纯文本格式远程仓库支持直接打包 GitHub 仓库而无需本地克隆代码压缩基于 Tree-sitter 提取类、函数、接口签名显著减少 Token 数量安装方式使用 npx无需安装npx repomixlatest这是最轻量的方式npx 会临时拉取并运行 Repomix适合快速体验。全局安装# npm npm install -g repomix # yarn yarn global add repomix # pnpm pnpm add -g repomix # bun bun add -g repomix # Homebrew brew install repomixDocker 安装Docker 是最便捷的方式之一可以避免本机环境配置问题# 处理当前目录 docker run -v .:/app -it --rm ghcr.io/yamadashy/repomix # 处理指定目录 docker run -v .:/app -it --rm ghcr.io/yamadashy/repomix path/to/directory # 处理远程仓库 docker run -v ./output:/app -it --rm ghcr.io/yamadashy/repomix --remote yamadashy/repomix浏览器扩展与 VS Code 扩展此外仓库中还提供了 Chrome/Firefox 浏览器扩展browser/目录入口见 browser/entrypoints/content.ts可直接在 GitHub 仓库页面添加Repomix按钮一键打包社区也维护了 VS Code 扩展。浏览器扩展相关实现与测试可参考 browser/README.md 与 browser/tests/repomix-integration.test.ts。系统要求与验证Node.js ≥ 22.0.0Git处理远程仓库时需要安装完成后验证repomix --version repomix --help常见使用场景打包指定目录repomix path/to/directory包含与排除文件使用 glob 模式精确控制文件选择# 仅包含指定模式的文件 repomix --include src/**/*.ts,**/*.md # 排除指定模式的文件 repomix --ignore **/*.log,tmp/处理远程仓库# 使用 GitHub URL repomix --remote https://github.com/user/repo # 使用简写形式 repomix --remote user/repo # 不使用 --remote 的简写自动检测 repomix user/repo # 指定分支/标签/提交 repomix --remote user/repo --remote-branch main repomix --remote user/repo --remote-branch 935b695文件列表输入stdin通过 stdin 逐行传入文件路径实现最大的文件选择灵活性# 使用 find 命令 find src -name *.ts -type f | repomix --stdin # 使用 git 获取跟踪的文件 git ls-files *.ts | repomix --stdin # 使用 ripgrep 查找文件 rg --files --type ts | repomix --stdin # 使用 grep 查找包含特定内容的文件 grep -l TODO **/*.ts | repomix --stdin # 使用 fzf 进行交互式文件选择 find . -name *.ts -type f | fzf -m | repomix --stdin # 从包含文件路径的文件中读取 cat file-list.txt | repomix --stdin # 使用 echo 直接输入 echo -e src/index.ts\nsrc/utils.ts | repomix --stdin使用--stdin时指定的文件会被添加到包含模式中正常的包含和忽略行为仍然生效路径可以是相对路径或绝对路径Repomix 会自动处理路径解析和去重。stdin 的文件收集实现在 src/core/file/fileStdin.ts。Git 集成差异与提交日志为 AI 提供开发上下文# 包含 git 差异未提交的更改 repomix --include-diffs # 包含 git 提交日志默认最近 50 个提交 repomix --include-logs # 包含特定数量的提交 repomix --include-logs --include-logs-count 10 # 同时包含差异和日志 repomix --include-diffs --include-logs这能带来有价值的上下文信息最近更改Git 差异显示未提交的修改开发规律Git 日志揭示通常一起更改的文件提交历史最近的提交消息提供开发重点的洞察文件关系理解在同一提交中修改的文件对应实现位于 src/core/git/gitDiffHandle.ts 与 src/core/git/gitLogHandle.ts测试覆盖见 tests/core/git/gitDiffHandle.test.ts。Token 数量优化token 计数树了解代码库的 Token 分布对优化 AI 交互至关重要repomix --token-count-tree输出类似 Token Count Tree: ──────────────────── └── src/ (70,925 tokens) ├── cli/ (12,714 tokens) │ ├── actions/ (7,546 tokens) │ └── reporters/ (990 tokens) └── core/ (41,600 tokens) ├── file/ (10,098 tokens) └── output/ (5,808 tokens)还可以设置最小 Token 阈值以聚焦大文件repomix --token-count-tree 1000 # 仅显示拥有 1000 token 的文件/目录这有助于识别高 Token 文件、用--include/--ignore优化文件选择、规划压缩策略、平衡内容与上下文。Token 计数的核心实现见 src/core/metrics/TokenCounter.ts 与 src/core/tokenCount/buildTokenCountStructure.ts。输出拆分处理大型代码库时打包输出可能超过某些 AI 工具的文件大小限制例如 Google AI Studio 的 1MB 限制repomix --split-output 1mb这将生成编号文件repomix-output.1.xml、repomix-output.2.xml等。大小可用500kb、1mb、2mb、1.5mb等可读单位指定支持小数值。文件按顶级目录分组以保持上下文单个文件或目录永远不会被拆分到多个输出文件中。实现见 src/core/output/outputSplit.ts。输出格式详解Repomix 支持四种输出格式通过--style指定格式命令适用场景XML默认repomix --style xml配合 Claude 使用时推荐解析精度更高Markdownrepomix --style markdown注重可读性时使用JSONrepomix --style json程序化处理或 API 集成纯文本repomix --style plain追求简洁与通用兼容性为什么 XML 是默认格式Repomix 基于广泛的研究与测试选择 XML 作为默认格式AnthropicClaude明确推荐使用 XML 标签来构建提示声明模型在训练过程中接触过此类提示GoogleGemini推荐在复杂任务中使用包括 XML 在内的结构化格式OpenAIGPT也在复杂场景中倡导结构化提示。基于实证证据与 AI 辅助代码分析的实际考量XML 成为默认选择。XML 输出结构示例概要file_summary 元数据和 AI 指令 /file_summary directory_structure src/ index.ts utils/ helper.ts /directory_structure files file pathsrc/index.ts // 文件内容 /file /files git_logs git_log_commit date2025-08-20 00:47:19 0900/date messagefeat(cli): Add --include-logs option/message files.../files /git_log_commit /git_logsJSON 格式与 jq 处理JSON 格式输出 camelCase 属性名fileSummary、directoryStructure、files、instruction等便于程序化解析# 列出所有文件路径 cat repomix-output.json | jq -r .files | keys[] # 计算文件总数 cat repomix-output.json | jq .files | keys | length # 提取特定文件内容 cat repomix-output.json | jq -r .files[README.md] # 按扩展名查找文件 cat repomix-output.json | jq -r .files | keys[] | select(endswith(.ts)) # 查找包含特定文本的文件 cat repomix-output.json | jq -r .files | to_entries[] | select(.value | contains(function)) | .key # 提取目录结构 cat repomix-output.json | jq -r .directoryStructure # 获取自定义指令 cat repomix-output.json | jq -r .instruction // 未提供指令各种输出样式的底层实现在 src/core/output/outputStyles/ 目录markdownStyle.ts、plainStyle.ts、xmlStyle.ts对应测试见 tests/core/output/outputStyles/。配置文件从 JSON 到 TypeScriptRepomix 支持配置文件与命令行选项两种配置方式。配置文件允许你自定义代码库的处理和输出方式。初始化配置文件repomix --init这会在项目目录创建带默认设置的repomix.config.json。也可创建全局配置作为后备repomix --init --global配置文件格式与搜索优先级Repomix 按以下优先级自动搜索配置文件同类别内 TS JS JSONTypeScriptrepomix.config.ts、repomix.config.mts、repomix.config.ctsJavaScript/ES Modulerepomix.config.js、repomix.config.mjs、repomix.config.cjsJSONrepomix.config.json5、repomix.config.jsonc、repomix.config.json配置文件支持 JSON5 语法注释、尾随逗号、无引号属性名等。仓库测试夹具见 tests/fixtures/config-ts/ 与 tests/fixtures/config-js/。全局配置位置Windows%LOCALAPPDATA%\Repomix\repomix.config.*macOS/Linux~/.config/repomix/repomix.config.*命令行选项优先于配置文件设置。TypeScript 配置示例TypeScript 配置提供完整的类型检查与 IDE 支持需将 Repomix 安装为开发依赖npm install -D repomix// repomix.config.ts import { defineConfig } from repomix; export default defineConfig({ output: { filePath: output.xml, style: xml, removeComments: true, }, ignore: { customPatterns: [**/node_modules/**, **/dist/**], }, });defineConfig与配置 Schema 定义见 src/config/configSchema.tsrepomix.config.ts的动态值时间戳、环境变量等能力使其比 JSON 更灵活。完整配置示例以下是完整的repomix.config.json示例仓库根目录亦提供实际示例 repomix.config.json{ $schema: https://repomix.com/schemas/latest/schema.json, input: { maxFileSize: 50000000 }, output: { filePath: repomix-output.xml, style: xml, filePathStyle: target-relative, parsableStyle: false, compress: false, headerText: 打包文件的自定义头部信息, fileSummary: true, directoryStructure: true, files: true, removeComments: false, removeEmptyLines: false, topFilesLength: 5, showLineNumbers: false, truncateBase64: false, copyToClipboard: false, includeEmptyDirectories: false, git: { sortByChanges: true, sortByChangesMaxCommits: 100, includeDiffs: false, includeLogs: false, includeLogsCount: 50 } }, include: [**/*], ignore: { useGitignore: true, useDefaultPatterns: true, customPatterns: [ additional-folder, **/*.log ] }, security: { enableSecurityCheck: true }, tokenCount: { encoding: o200k_base } }关键配置项说明选项说明默认值input.maxFileSize最大处理文件大小字节超过则跳过用于排除大型二进制或数据文件50000000output.filePath输出文件名repomix-output.xmloutput.style输出样式xml、markdown、json、plainxmloutput.compress使用 Tree-sitter 执行智能代码提取减少 Tokenfalseoutput.parsableStyle是否按所选样式转义输出解析更佳但可能增加 Tokenfalseoutput.headerText输出文件头部的自定义文本可为 AI 提供上下文nulloutput.fileSummary是否包含文件计数、大小等摘要trueoutput.directoryStructure是否包含目录结构trueoutput.removeComments从支持的文件类型中移除注释falseoutput.removeEmptyLines删除空行减少 Tokenfalseoutput.showLineNumbers为每行添加行号falseoutput.splitOutput按大小拆分输出为多个编号文件未设置output.tokenBudget输出超限时以非零退出码失败作为 CI/Agent 防护未设置output.git.sortByChanges按 Git 更改次数排序文件trueoutput.git.sortByChangesMaxCommits分析 Git 更改的最大提交数100output.git.includeDiffs/includeLogs是否包含 Git 差异/日志falseoutput.git.includeLogsCount包含的 Git 日志提交数50include包含的文件 glob 模式[]ignore.useGitignore是否使用.gitignore模式trueignore.useDotIgnore是否使用.ignore文件模式trueignore.useDefaultPatterns是否使用内置默认忽略模式trueignore.customPatterns额外忽略模式[]security.enableSecurityCheck是否使用 Secretlint 检测敏感信息truetokenCount.encodingToken 计数编码o200k_baseGPT-4o、cl100k_baseGPT-3.5/4o200k_base模式验证通过$schema属性为配置文件启用 JSON Schema 验证在支持 JSON Schema 的编辑器中获得自动完成与校验{ $schema: https://repomix.com/schemas/latest/schema.json, output: { filePath: repomix-output.md, style: markdown } }仓库中的 Schema 生成脚本见 website/client/scripts/generateSchema.ts生成的 Schema 位于 website/client/public/schemas/。忽略机制与二进制文件处理多层级忽略规则Repomix 提供多种设置忽略模式的方式.gitignore默认使用项目.gitignore与.git/info/exclude可通过ignore.useGitignore或--no-gitignore控制.ignore项目根目录的.ignore文件与.gitignore同格式ripgrep、the silver searcher 等工具也遵守默认模式内置常见排除项node_modules、.git、二进制文件、构建产物、各类锁文件等完整列表见 src/config/defaultIgnore.ts.repomixignoreRepomix 专属忽略文件同样遵循 gitignore 格式自定义模式ignore.customPatterns或-i, --ignoreCLI 选项优先顺序从高到低自定义模式ignore.customPatterns忽略文件.repomixignore、.ignore、.gitignore、.git/info/exclude嵌套目录中更深层的文件优先级更高默认模式ignore.useDefaultPatterns为 true 时.repomixignore示例# 缓存目录 .cache/ tmp/ # 构建输出 dist/ build/ # 日志 *.log二进制文件处理二进制文件图像、PDF、编译产物、归档等采用特殊策略保持输出高效文件内容二进制文件不包含在打包输出中目录结构二进制文件路径被列出在目录结构部分提供仓库完整概览例如仓库包含logo.png和app.jar时它们出现在目录结构中但内容不会进入文件部分。大于input.maxFileSize默认 50MB的文件会被完全跳过。安全检查与远程仓库配置信任Secretlint 安全检查Repomix 使用 Secretlint 在打包前检测敏感信息API 密钥访问令牌认证凭证私钥密码与环境变量安全检查默认启用发现问题时输出类似 Security Check: ────────────────── 2 suspicious file(s) detected and excluded: 1. config/credentials.json - Found AWS access key 2. .env.local - Found database password通过命令行禁用repomix --no-security-check或配置文件{ security: { enableSecurityCheck: false } }安全检测核心实现见 src/core/security/securityCheck.tsWorker 实现见 src/core/security/workers/securityCheckWorker.ts。远程仓库配置信任使用--remote打包远程仓库时Repomix 将仓库自身的repomix.config.*视为不受信任的代码原因在于repomix.config.ts/.js/.mjs在加载时会被执行input.processors会对匹配文件运行外部命令output.instructionFilePath及包含../的 include 模式会读取仓库之外的文件默认情况下远程仓库配置永远不会被加载。选择信任需显式操作# 使用 CLI 标志 repomix --remote user/repo --remote-trust-config # 使用环境变量 REPOMIX_REMOTE_TRUST_CONFIGtrue repomix --remote user/repo交互式终端中会显示即将运行的配置并征求确认提示对控制字符、ANSI 转义、双向控制字符进行转义以防终端篡改拒绝符号链接配置并按行数与字节数限制输出。相关实现见 src/cli/prompts/remoteConfigTrustPrompt.ts 与 src/cli/prompts/remoteConfigTrustStore.ts。进阶功能代码压缩与文件处理器代码压缩Tree-sitter--compress或配置output.compress: true使用 Tree-sitter 解析代码智能提取类、函数、接口、导入导出等基本结构同时移除实现细节repomix --compress # 远程仓库同样适用 repomix --remote yamadashy/repomix --compress主要优点显著减少 Token 数量保留类和函数签名保持导入和导出保留类型定义和接口移除函数体和实现细节Tree-sitter 解析策略位于 src/core/treeSitter/parseStrategies/含 TypeScript、Python、Go、Vue、C/C、Rust、Java 等语言查询文件位于 src/core/treeSitter/queries/。按文件设置包含级别output.patterns允许按 glob 为不同文件设置详细级别覆盖全局output.compress{ output: { compress: false, // 全局默认值作为兜底 patterns: [ { pattern: docs/**/*, compress: true }, { pattern: website/**/*, directoryStructureOnly: true } ] } }每个文件解析为三个级别之一完整内容默认、压缩Tree-sitter 处理或仅目录结构内容块完全省略。模式按数组顺序求值第一个匹配优先directoryStructureOnly优先于compress。该选项仅适用于配置文件无对应 CLI 选项。文件处理器input.processorsinput.processors在文件打包之前运行外部命令转换内容用命令标准输出替换匹配文件的内容适合减少 Token 或转换格式{ input: { processors: [ { pattern: **/*.json, command: npx toon-format/cli {file} } ] } }工作原理Repomix 将匹配文件内容写入临时文件{file}占位符必需替换为文件路径命令通过 shell 运行标准输出成为文件新内容随后照常经过安全检查、Token 计数与输出生成。常见用例模式command作用**/*.jsonjq -c . {file}去除空白压缩 JSON**/*.jsonnpx toon-format/cli {file}转换为紧凑省 Token 的 TOON 格式**/*.svgnpx svgo -i {file} -o -压缩 SVG**/*.ipynbjupyter nbconvert --to script --stdout {file}notebook 转纯 Python 脚本每个处理器选项timeout默认 60000ms与onErrorfail中止打包 /skip回退原始内容。⚠️ 安全提醒文件处理器会执行配置中的任意命令仅在本地 CLI 运行、或对--remote显式传入--remote-trust-config时启用库 API、MCP 服务器与托管 Web 中均被禁用。信任边界逻辑见 src/core/file/workers/fileProcessWorker.ts。命令行选项速查# 基本使用 repomix # 自定义输出文件与格式 repomix -o my-output.xml --style xml # 输出到标准输出 repomix --stdout custom-output.txt # 输出到标准输出后管道给其他命令 repomix --stdout | llm 请解释这段代码的作用。 # Git 集成 repomix --include-logs repomix --include-logs --include-logs-count 10 repomix --include-diffs --include-logs # 模式筛选 repomix --include src/**/*.ts,*.md --ignore *.test.js,docs/** # 远程仓库分支/提交/简写 repomix --remote https://github.com/user/repo/tree/main repomix --remote https://github.com/user/repo/commit/836abcd7335137228ad77feb28655d85712680f1 repomix --remote user/repo # stdin 文件列表 find src -name *.ts -type f | repomix --stdin # Token 计数分析 repomix --token-count-tree repomix --token-count-tree 1000 # 监视模式文件变更时自动重新打包 repomix --watch repomix -w --include src/**/*.tsCLI 入口与参数解析见 src/cli/cliRun.ts各动作实现见 src/cli/actions/。常用选项速查选项说明-v, --version显示版本信息并退出--verbose/--quiet详细调试日志 / 仅输出错误--stdout输出写入标准输出而非文件--stdin从标准输入逐行读取文件路径--copy输出同时复制到系统剪贴板--token-count-tree [threshold]显示带 Token 计数的文件树-o, --output file输出文件路径-表示标准输出--style style输出格式xml / markdown / json / plain--compressTree-sitter 代码压缩--remove-comments打包前剥离代码注释--split-output size拆分输出如500kb、2mb--include/-i, --ignore包含 / 排除 glob 模式--no-gitignore/--no-dot-ignore/--no-default-patterns关闭对应忽略来源--remote url/--remote-branch name远程仓库与分支/标签/提交--remote-trust-config信任远程仓库配置默认禁用-c, --config path指定自定义配置文件--init/--global初始化配置 / 全局配置--no-security-check跳过敏感信息扫描--token-budget number输出超 N Token 时非零退出码失败--mcp/--sandbox [dir]作为 MCP 服务器运行 / 限制文件工具范围-w, --watch监视模式自动重新打包监视模式仅适用于本地目录无法与--remote、--stdout、--stdin、--split-output、--skill-generate或--copy组合使用。写给 AI 的高质量提示词模板将打包文件交给 AI 时一份结构化的提示词能显著提升结果质量分析此代码库的架构 1. 评估整体结构和模式 2. 识别潜在的架构问题 3. 提出改进可扩展性的建议 4. 标注遵循最佳实践的部分对此代码库进行安全性评审 1. 识别潜在的安全漏洞 2. 检查常见的安全反模式 3. 评审错误处理和输入验证 4. 评估依赖项的安全性这个代码库在服务器端有内存泄漏问题。应用程序运行几个小时后会崩溃。 请分析整个代码库并识别潜在原因。我想为这个应用程序添加用户认证功能。请审查当前的代码库结构 并建议最适合现有架构的方法。你正在帮助一位新开发者了解这个代码库。请提供架构概述 解释主要组件及其交互并突出显示首先应该审查的最重要文件。获取更好结果的技巧明确具体包含清晰目标和评估标准、设置上下文说明角色与专业水平、请求格式定义期望响应结构、设置优先级指出最重要方面。模型特定使用建议Claude使用 XML 输出格式将重要指令放在最后指定响应结构ChatGPT使用 Markdown 格式大型代码库分成小节包含系统角色提示Gemini适用于所有格式每次请求专注于特定领域使用逐步分析与 AI 工具链集成MCP 服务器Repomix 还可以作为 Model Context ProtocolMCP服务器运行将打包能力直接集成到 AI 助手中repomix --mcp --sandbox path/to/project--sandbox将文件工具限制在工作区目录内所有路径相对根目录解析绝对路径/主机路径被拒绝同时禁用远程打包与 Skill 生成等能力。MCP 服务器实现见 src/mcp/mcpServer.ts 与 src/mcp/tools/含packCodebaseTool.ts、packRemoteRepositoryTool.ts、readRepomixOutputTool.ts、grepRepomixOutputTool.ts等。下一步学习路径围绕本入门指南仓库提供了完整的配套文档与源码可继续深入安装指南更多安装方式与验证步骤使用指南基本与高级用法配置完整配置项参考安全功能安全检查与远程配置信任细节输出格式四种格式详细对比命令行选项完整 CLI 参考提示词示例更多提示模板使用场景实际案例与工作流MCP 服务器与 AI 助手直接集成核心源码入口为 src/index.ts导出pack、collectFiles、runSecurityCheck、TokenCounter、defineConfig等 API仓库根配置示例见 repomix.config.json行为测试覆盖于 tests/ 目录。祝你在 AI 辅助开发中用 Repomix 打通全库上下文这一步。【免费下载链接】repomix Repomix is a powerful tool that packs your entire repository into a single, AI-friendly file. Perfect for when you need to feed your codebase to Large Language Models (LLMs) or other AI tools like Claude, ChatGPT, DeepSeek, Perplexity, Gemini, Gemma, Llama, Grok, and more.项目地址: https://gitcode.com/GitHub_Trending/rep/repomix创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表