Skip to content

Git 操作

git_operations 工具为 PRX Agent 提供结构化的 Git 版本控制操作能力。与通过 shell 工具直接执行 git 命令不同,git_operations 提供了类型安全的参数接口和标准化的输出格式,使 Agent 能够更可靠地进行版本控制操作。

该工具支持 Git 的常用操作:查看仓库状态、比较差异、创建提交、推送和拉取远程变更、查看提交日志以及分支管理。所有操作都在工作区仓库的上下文中执行,Agent 无需关心仓库路径等底层细节。

git_operationsall_tools() 模式下始终可用,无需额外配置。但可以通过工具策略限制 Agent 的 Git 操作权限。

配置

git_operations 工具默认无需特殊配置。通过工具策略控制访问权限:

toml
[security.tool_policy.tools]
git_operations = "allow"       # "allow" | "deny" | "supervised"

建议在不同场景下使用不同策略:

toml
# 代码审查 Agent — 仅读操作,建议 allow
[agents.reviewer]
allowed_tools = ["file_read", "git_operations"]

# 开发 Agent — 需要写操作,建议 supervised
[agents.developer]
allowed_tools = ["file_read", "file_write", "shell", "git_operations"]

在工作区级别配置 Git 行为:

toml
[workspace]
path = "/home/user/projects/my-repo"  # 工作区路径
git_auto_commit = false                # 是否自动提交变更
git_branch_prefix = "prx/"            # Agent 创建分支时的前缀

使用方法

查看仓库状态

json
{
  "tool": "git_operations",
  "arguments": {
    "action": "status"
  }
}

返回:

json
{
  "branch": "main",
  "clean": false,
  "staged": ["src/main.rs"],
  "modified": ["src/config.rs", "Cargo.toml"],
  "untracked": ["src/new_module.rs"],
  "ahead": 2,
  "behind": 0
}

查看差异

json
{
  "tool": "git_operations",
  "arguments": {
    "action": "diff",
    "target": "src/main.rs"
  }
}

暂存文件

json
{
  "tool": "git_operations",
  "arguments": {
    "action": "add",
    "files": ["src/main.rs", "src/config.rs"]
  }
}

创建提交

json
{
  "tool": "git_operations",
  "arguments": {
    "action": "commit",
    "message": "fix: 修复配置文件解析错误\n\n修正了 TOML 解析器在处理嵌套表时的类型转换问题。"
  }
}

推送到远程

json
{
  "tool": "git_operations",
  "arguments": {
    "action": "push",
    "remote": "origin",
    "branch": "main"
  }
}

拉取远程变更

json
{
  "tool": "git_operations",
  "arguments": {
    "action": "pull",
    "remote": "origin",
    "branch": "main"
  }
}

查看提交日志

json
{
  "tool": "git_operations",
  "arguments": {
    "action": "log",
    "limit": 10
  }
}

分支管理

json
// 列出分支
{
  "tool": "git_operations",
  "arguments": {
    "action": "branch",
    "sub_action": "list"
  }
}

// 创建新分支
{
  "tool": "git_operations",
  "arguments": {
    "action": "branch",
    "sub_action": "create",
    "name": "feature/new-tool"
  }
}

// 切换分支
{
  "tool": "git_operations",
  "arguments": {
    "action": "branch",
    "sub_action": "checkout",
    "name": "feature/new-tool"
  }
}

参数

通用参数

参数类型必填说明
actionstring操作类型(见下表)

操作类型及参数

操作说明额外参数
status查看仓库状态
diff查看差异target(文件路径或分支名)、staged(是否仅显示暂存区差异)
add暂存文件files(文件路径列表)
commit创建提交message(提交消息,必填)
push推送remote(远程名)、branch(分支名)
pull拉取remote(远程名)、branch(分支名)
log提交日志limit(数量上限)、since(起始日期)、author(作者过滤)
branch分支管理sub_actionlist/create/checkout/delete)、name(分支名)

diff 参数详情

参数类型必填默认值说明
targetstring比较目标(文件路径或分支名),省略时显示所有变更
stagedbooleanfalse是否仅显示暂存区的差异

log 参数详情

参数类型必填默认值说明
limitinteger20返回的最大提交数
sincestring起始日期(ISO 8601 格式)
authorstring按作者名过滤

branch 参数详情

参数类型必填说明
sub_actionstringlist(列出)、create(创建)、checkout(切换)、delete(删除)
namestring条件分支名(create/checkout/delete 时必填)

典型工作流

Agent 辅助代码提交

用户: 提交当前的修改

Agent:
1. [git_operations: status]  → 查看哪些文件被修改
2. [git_operations: diff]    → 查看具体变更内容
3. 分析变更,生成提交消息
4. [git_operations: add]     → 暂存相关文件
5. [git_operations: commit]  → 创建提交

Agent 辅助代码审查

用户: 审查 feature/auth 分支的变更

Agent:
1. [git_operations: diff, target="main..feature/auth"]  → 查看分支差异
2. [git_operations: log, branch="feature/auth"]          → 查看提交历史
3. [file_read: 修改的文件]                                → 详细审查代码
4. 输出审查意见

安全性

写操作风险

Git 的写操作(commit、push、branch delete)可能产生不可逆的影响。建议:

操作风险级别建议策略
status / diff / logallow
addallow
commitsupervised
pushsupervised
branch createallow
branch deletesupervised
pullallow(可能产生冲突)

Force Push 防护

git_operations 工具不支持 --force 参数,从根本上防止 force push 导致的代码丢失。如果确实需要 force push,用户必须通过 shell 工具手动执行。

凭据安全

Git 操作可能涉及远程仓库认证。PRX 使用系统的 Git 凭据管理(credential helper),不在命令行中传递密码或 token。环境变量净化确保 GIT_TOKEN 等变量不会泄露。

审计追踪

所有 Git 操作都记录在 PRX 审计日志中:

[2024-01-15T10:30:00Z] tool=git_operations action=status branch=main
[2024-01-15T10:30:05Z] tool=git_operations action=add files=["src/main.rs"]
[2024-01-15T10:30:10Z] tool=git_operations action=commit message="fix: ..."
[2024-01-15T10:30:15Z] tool=git_operations action=push remote=origin branch=main

相关文档

Released under the Apache-2.0 License.