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

git

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrFileChanged = errors.New("git: file changed since it was read")
View Source
var ErrPathEscape = errors.New("git: path escapes novel directory")

Functions

func ChapterPath

func ChapterPath(id int64) string

ChapterPath 返回章节正文文件相对路径(按章节 id)。

func CoverPath

func CoverPath() string

func GoinkPath

func GoinkPath() string

func OutlinePath

func OutlinePath(id int64) string

OutlinePath 返回章节大纲文件相对路径(按章节 id)。

func ParseVolumePath added in v1.6.0

func ParseVolumePath(path string) (int64, bool)

ParseVolumePath 解析卷纲虚拟路径。卷 ID 必须是规范的正整数表示。

func PlanPath

func PlanPath(scope string) string

func ReadFile

func ReadFile(novelID int64, path string) (string, error)

func RemoveFile added in v1.6.0

func RemoveFile(novelID int64, path string) error

RemoveFile 删除小说仓库中的文件。文件不存在时视为已删除,便于清理可选的大纲文件。

func ResolvePath

func ResolvePath(path string, novelID int64) (string, error)

ResolvePath 将用户输入路径解析为真实文件系统路径。 ~/.goink/ 展开到用户目录;其他相对路径基于小说目录。

func SafePath

func SafePath(base, rel string) (string, error)

SafePath 对给定的上级目录和相对路径求最终路径,如果路径跳出上级目录则返回 error。

func VolumePath added in v1.6.0

func VolumePath(volumeID int64) string

VolumePath 返回卷纲文件相对路径(按卷 id)。

func WriteFile

func WriteFile(novelID int64, path, content string) error

func WriteFileAtPathIfUnchanged added in v1.6.0

func WriteFileAtPathIfUnchanged(fullPath, expected, content string) error

WriteFileAtPathIfUnchanged 供已验证绝对路径的调用方共用同一进程写锁。

func WriteFileIfUnchanged added in v1.6.0

func WriteFileIfUnchanged(novelID int64, path, expected, content string) error

WriteFileIfUnchanged 将比较与写入放在同一个进程锁内,避免应用内写入互相覆盖。

Types

type ChapterPathRef added in v1.6.0

type ChapterPathRef struct {
	ID        int64
	VolumeID  int64
	IsOutline bool
	IsNew     bool
}

ChapterPathRef 是章节正文或章节大纲虚拟路径的解析结果。 VolumeID 仅来自两级容错路径,不参与物理文件定位;IsNew 表示 new.md 创建通道。

func ParseChapterLikePath added in v1.6.0

func ParseChapterLikePath(path string) (ChapterPathRef, bool)

ParseChapterLikePath 解析 rw_tools 支持的章节正文或大纲虚拟路径。 支持扁平主格式和带卷 ID 的容错别名;旧的纯数字章节号路径不被接受。

type CommitInfo

type CommitInfo struct {
	Hash         string    `json:"hash"`
	ShortHash    string    `json:"shortHash"`
	Message      string    `json:"message"`
	Time         time.Time `json:"time"`
	AuthorName   string    `json:"authorName"`
	AuthorEmail  string    `json:"authorEmail"`
	FilesChanged int       `json:"filesChanged"`
	Insertions   int       `json:"insertions"`
	Deletions    int       `json:"deletions"`
}

CommitInfo 是 git log 的单条记录。

type FileDiff

type FileDiff struct {
	Path            string `json:"path"`
	ChangeType      string `json:"changeType"`
	OriginalContent string `json:"original"`
	ModifiedContent string `json:"modified"`
}

FileDiff 表示一次 commit 中单个文件的变更。

type FileEntry

type FileEntry struct {
	Path       string `json:"path"`
	ChangeType string `json:"changeType"` // "added" | "modified" | "deleted" | "renamed"
	OldPath    string `json:"oldPath"`    // 仅 renamed 时有值,表示重命名前的路径
}

FileEntry 表示 commit 中一个变更文件的简要信息(不含内容)。

type Repo

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

Repo 管理单部小说的 Git 仓库,提供文件读写和版本控制。

func New

func New(novelID int64, gitName, gitEmail string, logger *slog.Logger) (*Repo, error)

New 为指定小说打开已有仓库,不存在则 git init + 首次空 commit。 gitName 和 gitEmail 用于设置仓库级 git config。空字符串时使用默认值。 logger 为 nil 时使用 slog.Default()。

func (*Repo) Commit

func (r *Repo) Commit(msg string) (string, error)

func (*Repo) CommitFileList

func (r *Repo) CommitFileList(hash string) (*CommitInfo, []FileEntry, error)

CommitFileList 返回指定 commit 的详细信息和变更文件列表(不含文件内容)。

func (*Repo) DiffContent

func (r *Repo) DiffContent(relPath, proposed string) (string, error)

DiffContent 对比当前工作区文件与 proposed 内容,返回 unified diff。 文件不存在时以空内容为基准。用临时文件 + git diff --no-index 实现。

func (*Repo) HasUncommitted

func (r *Repo) HasUncommitted() (bool, error)

func (*Repo) Log

func (r *Repo) Log(relPath string, n int) ([]CommitInfo, error)

func (*Repo) LogDetailed

func (r *Repo) LogDetailed(n int, afterHash string) ([]CommitInfo, error)

LogDetailed 返回带详细统计的提交历史,按时间降序(最新在前)。 afterHash 非空时,返回该 commit 之后的更早提交(基于 git log <afterHash> --skip=1)。

func (*Repo) RevertAbort

func (r *Repo) RevertAbort() error

RevertAbort 取消进行中的 revert,丢弃所有暂存的 revert 内容。

func (*Repo) RevertNoCommit

func (r *Repo) RevertNoCommit(hashes []string) error

RevertNoCommit 逆序 revert 所有 hash,仅暂存不提交。冲突时自动 abort。 调用方负责最终 commit(或 abort)。

func (*Repo) SetGitConfig

func (r *Repo) SetGitConfig(gitName, gitEmail string) error

SetGitConfig 在已有仓库上更新仓库级 user.name / user.email 配置。

func (*Repo) ShowFile

func (r *Repo) ShowFile(hash, filePath string) (*FileDiff, error)

ShowFile 返回指定 commit 中某个文件的前后内容。 对于 added 文件 original 为空,对于 deleted 文件 modified 为空。

func (*Repo) StageAll

func (r *Repo) StageAll() error

Jump to

Keyboard shortcuts

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