diff

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package diff 提供文件补丁解析与应用能力(从 opencode internal/diff 迁移)。

与 opencode 版本的差异:去掉 chroma / lipgloss / tui/theme 等终端渲染依赖, 只保留工具真正需要的部分——生成 unified diff、解析 patch 文本、构建并应用 commit。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyCommit

func ApplyCommit(commit Commit, writeFn func(string, string) error, removeFn func(string) error) error

func GenerateDiff

func GenerateDiff(beforeContent, afterContent, fileName string) (string, int, int)

GenerateDiff 生成 before → after 的 unified diff,并返回新增 / 删除行数。

fileName 仅用于 diff 头(a/<name> b/<name>);相对路径会去掉工作目录前缀。

func IdentifyFilesAdded

func IdentifyFilesAdded(text string) []string

func IdentifyFilesNeeded

func IdentifyFilesNeeded(text string) []string

func LoadFiles

func LoadFiles(paths []string, openFn func(string) (string, error)) (map[string]string, error)

func OpenFile

func OpenFile(p string) (string, error)

func ProcessPatch

func ProcessPatch(text string, openFn func(string) (string, error), writeFn func(string, string) error, removeFn func(string) error) (string, error)

func RemoveFile

func RemoveFile(p string) error

func ValidatePatch

func ValidatePatch(patchText string, files map[string]string) (bool, string, error)

func WriteFile

func WriteFile(p string, content string) error

Types

type ActionType

type ActionType string
const (
	ActionAdd    ActionType = "add"
	ActionDelete ActionType = "delete"
	ActionUpdate ActionType = "update"
)

type Chunk

type Chunk struct {
	OrigIndex int      // line index of the first line in the original file
	DelLines  []string // lines to delete
	InsLines  []string // lines to insert
}

type Commit

type Commit struct {
	Changes map[string]FileChange
}

func AssembleChanges

func AssembleChanges(orig map[string]string, updatedFiles map[string]string) Commit

func PatchToCommit

func PatchToCommit(patch Patch, orig map[string]string) (Commit, error)

type DiffError

type DiffError struct {
	// contains filtered or unexported fields
}

func NewDiffError

func NewDiffError(message string) DiffError

Helper functions for error handling

func (DiffError) Error

func (e DiffError) Error() string

type FileChange

type FileChange struct {
	Type       ActionType
	OldContent *string
	NewContent *string
	MovePath   *string
}

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

func NewParser

func NewParser(currentFiles map[string]string, lines []string) *Parser

func (*Parser) Parse

func (p *Parser) Parse() error

type Patch

type Patch struct {
	Actions map[string]PatchAction
}

func TextToPatch

func TextToPatch(text string, orig map[string]string) (Patch, int, error)

type PatchAction

type PatchAction struct {
	Type     ActionType
	NewFile  *string
	Chunks   []Chunk
	MovePath *string
}

Jump to

Keyboard shortcuts

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