diff

package
v0.3.29 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package diff 把 unified git diff 文本解析成结构化数据模型, 供前端渲染和 AI 解释共用。零第三方依赖。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HunkID

func HunkID(path string, h Hunk) string

HunkID 是 hunk 的稳定标识:文件路径 + hunk 原文的内容哈希。

哈希覆盖的是 hunk.Raw(),也就是模型和界面看到的那份文本,@@ 行里的 行号也包括在内 —— 「同一处改动挪到了别的行」算两个不同的块。 界面用它当列表 key,AI 解释缓存也用它当 key 的一半。

func IsBinaryContent added in v0.3.16

func IsBinaryContent(content []byte) bool

IsBinaryContent 按 git 的判据说一份内容是不是二进制:前 8000 字节里 有没有 NUL。普通文本(哪怕不是 UTF-8)没有,图片、压缩包、可执行文件都有。

不能用 utf8.Valid 代替:NUL 是合法的 UTF-8,二进制内容能整份通过校验, 然后被当成文本画成一屏乱码。

func IsImagePath added in v0.3.16

func IsImagePath(path string) bool

IsImagePath 说这个路径是不是一张可以直接摆上屏幕的图片。

func RangePatch added in v0.3.4

func RangePatch(path, oldText, newText string, r HunkRange) string

RangePatch 生成只包含这一段改动的补丁:path 是仓库内相对路径, oldText/newText 是两个侧的**完整原文**(带不带末尾换行由它们自己说)。

为什么要有它:DiffView 的段是 mygo 自己的算法(histogram + indent 启发) 划出来的,和 git 的 -U3 hunk 不保证一一对应,所以不能像 StageHunk 那样 重跑 git diff 再按 hunk ID 切片,只能按行区间现造补丁。

补丁只描述这一段 —— 同文件里别的改动绝不能混进来,这是这个函数的全部 意义。git apply 是按行号 + 上下文在目标文件里定位的,多带一段就等于把 用户没点的改动也一起改了。

上下文取旧侧:补丁最终打的是旧侧那份文件(未暂存方向打 index、已暂存方向 打 HEAD),git 拿这些行去比对的就是它;从新侧取会在「另一侧先被改过」时 对不上。行号按旧侧真实行号(1 基)写头,同理。

区间越界(或者压根不是一段改动)返回空串,不 panic:调用方拿来的是「点 下去那一刻」的行号,文件随后变了就是这个样子,上层据此报「这一段已经变了」。

func SliceHunk

func SliceHunk(raw, path, hunkID string) (string, bool)

SliceHunk 从一份完整的 `git diff` 原文里切出「path 这个文件的 hunkID 那一段」: 文件头(diff --git 到第一个 @@ 之前)+ 那一段 hunk,字节原样。

为什么不在模型里把 Hunk.Raw() 拼回去:Parse 会丢掉 "\ No newline at end of file" 这类标记(它们只影响「行尾有没有换行」, 不进 Lines),拿 Raw() 拼出来的 patch 少了它,git apply 会按另一套 行尾约定处理甚至直接拒收。所以这里在原文上按和 Parse 完全一致的规则扫 ('\' 行不计数、'+' 计新侧、'-' 计旧侧、其余两侧都计),整段原文切出来。

找不到目标(路径不在、hunkID 不在、文件根本没有 hunk)返回 false —— 调用方借此发现「用户点下去和 git 真正执行之间文件被改过」,这是正常 竞态,不是错误。

Types

type FileDiff

type FileDiff struct {
	OldPath  string `json:"oldPath"`
	NewPath  string `json:"newPath"`
	Status   string `json:"status"`
	Binary   bool   `json:"binary,omitempty"` // 二进制文件:没有 hunk,界面显示「二进制文件已变更」
	Hunks    []Hunk `json:"hunks"`
	AddCount int    `json:"addCount"`
	DelCount int    `json:"delCount"`
}

FileDiff 是单个文件的变更。Status: added / deleted / modified / renamed。

func NewFileFromContent

func NewFileFromContent(path string, content []byte) FileDiff

NewFileFromContent 把一个全新文件的内容合成成「整文件新增」的 FileDiff。

未跟踪的文件不会出现在 `git diff` 的输出里(git 根本不认为它属于版本控制), 但 review 时恰恰最需要看到它们,所以单独合成一份。

func Parse

func Parse(raw string) []FileDiff

Parse 解析完整的 git diff 输出(也兼容没有 diff --git 头的裸 patch)。

func (*FileDiff) AssignHunkIDs

func (f *FileDiff) AssignHunkIDs()

AssignHunkIDs 给每个 hunk 算好 ID(见 HunkID)。

func (FileDiff) Path

func (f FileDiff) Path() string

Path 是这个文件的主路径。删除的文件没有新路径,退回旧的。

type Hunk

type Hunk struct {
	ID       string `json:"id"`
	Header   string `json:"header"`
	Section  string `json:"section,omitempty"` // @@ 后面的函数名等上下文
	OldStart int    `json:"oldStart"`
	OldLines int    `json:"oldLines"`
	NewStart int    `json:"newStart"`
	NewLines int    `json:"newLines"`
	Lines    []Line `json:"lines"`
}

Hunk 是一个 @@ 代码块。

func (Hunk) NewEndLine

func (h Hunk) NewEndLine() int

NewEndLine 返回 hunk 覆盖到的最后一个新侧行号(纯删除的情形见 NewStartLine)。

func (Hunk) NewStartLine

func (h Hunk) NewStartLine() int

NewStartLine 返回 hunk 覆盖到的第一个新侧行号。

新侧行数为 0 时(纯删除的 hunk),git 把头里的起始行号写成插入点之前 的那一行 —— `@@ -5,3 +4,0 @@` 的意思是「删掉旧文件 5-7 行,新文件第 4 行之后什么都不剩」,所以它覆盖的起点是第 5 行。

界面算「未改动区间」和还原旧文件都要靠这个约定,所以放在这里共用。

func (Hunk) Raw

func (h Hunk) Raw() string

Raw 把 hunk 还原成 unified diff 文本,用于喂给模型。

type HunkRange added in v0.3.4

type HunkRange struct{ OldStart, OldEnd, NewStart, NewEnd int }

HunkRange 是 DiffView 给的一段改动:两个侧各一个 0 基、末尾开的行区间。

0 基 + 末尾开是 editor.DiffView 的口径,和 git 补丁头里的 1 基闭区间差 一个数;换算只发生在 RangePatch 内部,调用方不要自己加减一。

type Line

type Line struct {
	Type    LineType `json:"type"`
	Content string   `json:"content"`
	OldNum  int      `json:"oldNum,omitempty"`
	NewNum  int      `json:"newNum,omitempty"`
}

Line 是 diff 中的一行,OldNum/NewNum 为 0 表示该行在对应侧不存在。

type LineType

type LineType string

LineType 表示 diff 中一行的类型。

const (
	LineAdd     LineType = "add"
	LineDel     LineType = "del"
	LineContext LineType = "context"
)

Jump to

Keyboard shortcuts

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