Help us shape the future of Go! Take the 2026 Go Developer Survey

diffx

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package diffx unified diff 解析纯函数(收编自 argus internal/context diffprep 与 runner/inject,v0.10.35):按 "diff --git" 切分多文件 diff、 提取目标路径、提取 hunk 新行区间。纯函数、零依赖、不改输入。

边界:解析面只覆盖 git 风格 unified diff 的结构性标记(+++ / @@ 行), 不做语义级 diff(那是 go-git-platform diff 侧的领域);路径以 +++ b/<path> 为准(新增/修改),删除文件回退 --- a/<path>。

v0.12.0 解析强化:quoted path(`+++ b/"a b.go"`,git 对含空格/特殊字符 路径的 C 风格转义——去引号并反转义)、--no-prefix 形态(`+++ <path>` 无 b/ 侧前缀,diff.noprefix 配置用户常见)、CRLF 残留剥除,三形态统一收口在 headerPath。DiffPath 的识别止于首个 @@(hunk 体内容行不参与——正文 `++ b/foo` 加行后即 `+++ b/foo` 的歧义形态在单块路径提取上根除); HunkLines 刻意保持宽松逐行识别(兼容无 diff --git 分隔的拼接/截断形态 ——真实消费方送来的常是这种),残留歧义(hunk 体中恰为完整 `+++ b/x` 形态的内容行会被当新文件头)作为宽松模式的有意取舍在此声明。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiffPath

func DiffPath(chunk string) string

DiffPath 从单文件 diff 块提取目标路径(+++ b/<path>,删除文件回退 --- a/<path>;行尾制表符附加信息剥除,/dev/null 不是目标)。 识别止于**首个 @@**(v0.12.0):hunk 体内容行不参与——正文 `++ b/x` 加行 后即 `+++ b/x` 的歧义形态在单块路径提取上根除。

func HunkLines

func HunkLines(diffText string) map[string][]LineRange

HunkLines 从 diff 文本提取每个文件的新行区间(闭区间 [start,end])。 纯删除块(新增行数 0)不计;@@ 行前的 +++ 头决定归属文件。 v0.12.0 状态机化:文件头只在头区识别,hunk 体的内容行不干扰归属。

func SplitChunks

func SplitChunks(raw string) []string

SplitChunks 按 "\ndiff --git " 切块,返回保序块列表(每块补回前缀;首块 容忍缺失前缀,空 preamble 跳过)。需要顺序语义(同路径块后者覆盖等确定性 行为)的调用方用它;只需 path→块 映射时用 SplitUnifiedDiff。

func SplitUnifiedDiff

func SplitUnifiedDiff(raw string) map[string]string

SplitUnifiedDiff 将拼接的 unified diff 按文件路径切分为 map[path]完整块 (块保留 "diff --git " 前缀,与常见存储/展示形态一致)。无路径的块忽略; 空输入返回空 map。同路径多块(merge/拆分提交)后者覆盖——需要保序保重复 用 SplitChunks。

Types

type LineRange

type LineRange = [2]int

LineRange 新文件侧行区间(闭区间);[2]int 别名——嵌入方既有签名零改动。

Jump to

Keyboard shortcuts

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