applypatch

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package applypatch 提供 apply_patch 工具:codex `*** Begin Patch` 格式的 解析与应用(多文件 增/改/删/改名,事务性——任一失败全部不落盘)。 格式与匹配算法移植自 openai/codex codex-rs/apply-patch crate (parser.rs / file_update.rs / seek_sequence.rs,Apache-2.0;streaming 与 CLI 部分不移植)。行尾策略取 NormalizeToLf 模式(统一 LF + 保留尾换行; PreserveLineEndings 混合行尾保留留后续)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTools

func NewTools(cfg Config) ([]contract.Tool, error)

NewTools 构造 apply_patch(写面:进审批名单,整补丁一批)。

Types

type Chunk

type Chunk struct {
	ChangeContext string // @@ 头文本(定位锚,单行 seek)
	OldLines      []string
	NewLines      []string
	IsEOF         bool // *** End of File 标记:优先在文件尾匹配
}

Chunk 单个改动块(一个 @@ 区段)。OldLines/NewLines 含上下文行; ContextPairs 记录两侧同为上下文的行号对(PreserveLineEndings 模式用, 本模式仅调试展示)。

type Config

type Config struct {
	Root        string
	ProtectDirs []string
}

Config 构造配置。Root = 工作区根(空 = 拒绝构造,P0 纪律)。ProtectDirs = 写保护区顶层子目录名——补丁任一目标(含 Move to 改名目标)命中即整单 拒绝(判定在 Apply 前,事务性自然成立);nil/空 = 不设区零变化。

type FileOp

type FileOp struct {
	Kind     Kind
	Path     string
	MoveTo   string   // Update 可选改名
	Chunks   []Chunk  // Update
	Contents []string // Add(+ 行内容)
}

FileOp 单文件操作。

func Parse

func Parse(raw string) ([]FileOp, error)

Parse 解析补丁文本 → 文件操作序列。

type FileResult

type FileResult struct {
	Path    string `json:"path"`
	Action  string `json:"action"` // added | updated | deleted | renamed
	Added   int    `json:"added"`
	Removed int    `json:"removed"`
}

FileResult 单文件结果。

func Apply

func Apply(root string, ops []FileOp) ([]FileResult, error)

Apply 应用补丁到 root 工作区(事务性:解析与计算全部成功才落盘)。 返回逐文件摘要。

type Kind

type Kind int

Kind 文件操作类型。

const (
	KindAdd Kind = iota
	KindUpdate
	KindDelete
)

文件操作三态。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL