跳到正文
Skillpedia
通用编程6.2k · #14386

apply-patch

它能做什么

这项技能使用 Git 把一组代码改动作为一个补丁应用。适合需要修改多个文件的智能体,或者简单编辑总是匹配失败的情况。

适用场景

  • ✓当一项改动需要编辑三个或更多文件时。
  • ✓当简单的查找替换编辑因文本不匹配而不断失败的时候。
  • ✓当你想应用一个来自拉取请求或 git diff 的补丁时。

技能链接

https://github.com/agentscope-ai/agentscope-java/blob/main/agentscope-examples/agents/agentscope-codingagent/src/main/resources/workspace-templates/skills/apply-patch/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/agentscope-ai/agentscope-java/blob/main/agentscope-examples/agents/agentscope-codingagent/src/main/resources/workspace-templates/skills/apply-patch/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请把这个模块中四个文件的同一个重命名作为一个补丁应用,并先检查它能否干净地应用。”

每周 5 个新技能,1 分钟读完。

工作原理

这项技能会写出描述改动的统一差异格式(unified diff),检查它能否应用,然后用 Git 一次性应用。如果补丁被拒绝,智能体会重新读取当前的行,并重新生成补丁。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Apply Patch

The built-in edit_file does an exact string match on a single file and has no fuzzy tolerance. For multi-file changes, or when edit_file keeps failing to find the string, construct a unified diff and apply it atomically with git.

Procedure

  1. Write the unified diff to a file in the sandbox (use write_file or a heredoc via execute):

    diff --git a/path/to/file b/path/to/file
    --- a/path/to/file
    +++ b/path/to/file
    @@ -10,7 +10,7 @@ context line
    -old line
    +new line
    
  2. Validate before applying: git apply --check changes.patch

  3. Apply: git apply changes.patch (use -p0 if paths are not a/...b/...).

  4. If git apply rejects the hunk, the surrounding context is stale — read_file the exact lines again, regenerate the diff with correct context, and retry.

When to prefer this

  • Editing 3+ files for one logical change (atomic, all-or-nothing).
  • Repetitive or whitespace-sensitive edits where edit_file exact-match struggles.
  • Re-applying a patch produced elsewhere (e.g. from a PR or git diff).

Always run the verify-changes skill afterwards.

常见问题

它会先检查补丁吗?

会。技能会在应用补丁之前进行检查。

如果补丁失败会怎样?

智能体会重新读取文件的当前内容,并生成新的补丁。

这是全部成功或全部失败吗?

是的。补丁作为一次改动应用,因此设计上是全部成功或全部失败。

详情

作者的原始描述

Apply multi-file or tricky edits atomically with git apply instead of many fragile edit_file calls. Use when changing several files at once or when edit_file fails to match.

repo: agentscope-ai/agentscope-java

热度

GitHub 星标
6.2k
排名
#14386
许可证
Apache-2.0
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能