patch

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCode

func AddCode(edit *goedit.Edit, pos token.Pos, code string)

func AddCodeAfterImportsLegacy deprecated added in v1.1.4

func AddCodeAfterImportsLegacy(code string, beginMark string, endMark string, contents []string) string

Deprecated: use AddImportAfterName instead

func AddContentAfter deprecated

func AddContentAfter(content string, beginMark string, endMark string, seq []string, addContent string) string

Deprecated: use AddContentAtIndex instead

func AddContentAfterName added in v1.1.4

func AddContentAfterName(code string, beginMark string, endMark string, insertContent string) string

func AddContentBefore deprecated

func AddContentBefore(content string, beginMark string, endMark string, seq []string, addContent string) string

Deprecated: use AddContentAtIndex instead

func AddImport

func AddImport(edit *goedit.Edit, file *ast.File, name string, pkgPath string)

func AddImportAfterName added in v1.1.4

func AddImportAfterName(code string, beginMark string, endMark string, name string, pkgPath string) string

package something -> package something;import __my_fmt "fmt"

func Append added in v1.1.2

func Append(edit *goedit.Edit, file *ast.File, s string)

Append inserts s at the end of the buffer using its length, NOTE: when the source file has \r\n the file.End() isn't correct

func ApplyPatches added in v1.2.0

func ApplyPatches(patchDir, goroot, xgoRepoRoot string, extraEnv map[string]string, skipKinds []string, generateHandler GenerateHandler) error

ApplyPatches walks a patch directory and applies all operations to a GOROOT. Auto-discovers:

  • .xgo.patch files → applied to corresponding GOROOT files
  • Other files (except __config__.json) → copied one-to-one to GOROOT
  • __config__.json → copy-dir instructions + generate commands

extraEnv provides variable substitution for ${VAR} in generate commands. skipKinds, if non-empty, skips generate entries whose Kind field matches any value. generateHandler is called for generate entries with non-empty Kind; it should return nil for known kinds and an error for unrecognized ones.

func ApplyXgoPatchContent added in v1.2.0

func ApplyXgoPatchContent(source string, patchContent string) (string, error)

ApplyXgoPatchContent applies parsed patch content to a source string. Each <patch> block is applied independently; blocks re-parse after prior block edits. Before applying a named patch, any existing markers with that name are cleared.

func ApplyXgoPatchFile added in v1.2.0

func ApplyXgoPatchFile(patchFile, targetFile string) error

ApplyXgoPatchFile reads a .xgo.patch file and applies it to the target file. The target file is modified in place; existing edits with the same patch name are cleared first (idempotent).

func CleanPatch

func CleanPatch(content string) string

CleanPatch removes all content between /*<begin and /*<end markers, and also handles <next-line-original> annotations.

func CleanPatchMarkers

func CleanPatchMarkers(content string, start PatchMarker, end PatchMarker) string

func EditFile

func EditFile(file string, callback func(content string) (string, error)) (err error)

func FmtLineDirective

func FmtLineDirective(srcFile string, line int) string

for line directive, check https://github.com/golang/go/blob/24b395119b4df7f16915b9f01a6aded647b79bbd/src/cmd/compile/doc.go#L171 this tells the compiler, next line's line number is `line`

func GetFuncInsertPosition

func GetFuncInsertPosition(file *ast.File) token.Pos

NOTE: new func is always inserted after a position because a function or variable may have some special directive like go:embed, go:linkname... we cannot add anything before them.

func RemoveBuildIgnore

func RemoveBuildIgnore(content string) (string, error)

func RemoveMarker

func RemoveMarker(content string, marker string) (string, error)

func ReplaceContentAfter

func ReplaceContentAfter(content string, beginMark string, endMark string, seq []string, target string, replaceContent string) string

func UpdateContent

func UpdateContent(content string, beginMark string, endMark string, seq []string, i int, position UpdatePosition, addContent string) string

func UpdateContentLines

func UpdateContentLines(content string, beginMark string, endMark string, seq []string, i int, position UpdatePosition, addContent string) string

UpdateContentLines add content before or after the `i`'s anchor in `seq` two lines will be automatically added after `beginMark`, and before `endMark`, so you don't need to include a line in `addContent`

Types

type Command added in v1.2.0

type Command struct {
	Type CommandType

	// For goto:
	GotoTarget string // e.g. "struct Foo", "func Bar", "func (t *T) Baz", "opening {", "closing }", "field Name"

	// For match / find_for_replace / replace_directive (old text):
	SearchText string

	// For insert_before / insert_after / replace / replace_directive (new text):
	EditText string

	// For copy_func / replace_directive:
	CopySource string // source function name
	CopyTarget string // target function name or replace_directive new text
}

Command represents a single instruction within a <patch> block.

func (Command) String added in v1.2.0

func (c Command) String() string

String returns a human-readable representation of the command.

type CommandType added in v1.2.0

type CommandType int

CommandType identifies the type of a command within a patch block.

const (
	CmdGoto             CommandType = iota // goto struct/func/interface/opening/closing/field
	CmdMatch                               // match <text>
	CmdFindForReplace                      // find_for_replace <text>
	CmdInsertBefore                        // insert_before <text>
	CmdInsertAfter                         // insert_after <text>
	CmdReplace                             // replace <text> (requires prior find_for_replace)
	CmdNewline                             // newline
	CmdCopyFunc                            // copy_func <source> as <target> [append to file end]
	CmdReplaceDirective                    // replace_directive <old> with <new>
	CmdInsertAfterLine                     // insert_after_line <text> (includes trailing \n in insert point)
)

type Config added in v1.2.0

type Config struct {
	Version  string          `json:"version"`
	Copy     []CopyEntry     `json:"copy,omitempty"`
	Generate []GenerateEntry `json:"generate,omitempty"`
}

Config represents the __config__.json file in a patch directory.

func LoadConfig added in v1.2.0

func LoadConfig(patchDir string) (*Config, error)

LoadConfig reads and parses the __config__.json file from a patch directory. Returns an empty Config if the file does not exist.

type CopyEntry added in v1.2.0

type CopyEntry struct {
	From        string   `json:"from"` // source relative to xgo repo root
	To          string   `json:"to"`   // destination relative to GOROOT; empty = patchDir
	IgnoreFiles []string `json:"ignore_files,omitempty"`
}

CopyEntry represents a directory copy instruction in __config__.json.

type FilePath

type FilePath []string

func (FilePath) Append

func (c FilePath) Append(s ...string) FilePath

func (FilePath) JoinPrefix

func (c FilePath) JoinPrefix(s ...string) string

type GenerateEntry added in v1.2.0

type GenerateEntry struct {
	Kind       string             `json:"kind,omitempty"`
	Cmd        StringSliceOrSlice `json:"cmd,omitempty"`
	CmdWindows StringSliceOrSlice `json:"cmd_windows,omitempty"`
	Cwd        string             `json:"cwd,omitempty"` // working dir relative to goroot
	Comments   []string           `json:"comments,omitempty"`
	Env        map[string]string  `json:"env,omitempty"`
	Outputs    []string           `json:"outputs,omitempty"`
}

GenerateEntry represents a command to run during patching, or a kind-based operation handled by a GenerateHandler callback.

func (*GenerateEntry) EffectiveCmd added in v1.2.0

func (g *GenerateEntry) EffectiveCmd() []string

type GenerateHandler added in v1.2.0

type GenerateHandler func(kind string, extraEnv map[string]string) error

GenerateHandler is called for generate entries with non-empty Kind. extraEnv provides the same variable map used for ${VAR} substitution. Return a nil error if the handler handled the kind; a non-nil error aborts. The handler should return an error if the kind is unrecognized.

type PatchBlock added in v1.2.0

type PatchBlock struct {
	Name     string
	Commands []Command
}

PatchBlock represents a single <patch name>...</patch> block.

type PatchFile added in v1.2.0

type PatchFile struct {
	Blocks []PatchBlock
}

PatchFile represents a parsed .xgo.patch file containing one or more <patch> blocks.

func ParseXgoPatch added in v1.2.0

func ParseXgoPatch(content string) (*PatchFile, error)

ParseXgoPatch parses a .xgo.patch file content into structured blocks and commands.

type PatchMarker

type PatchMarker struct {
	Begin string
	End   string
}

type StringSliceOrSlice added in v1.2.0

type StringSliceOrSlice []string

func (*StringSliceOrSlice) UnmarshalJSON added in v1.2.0

func (s *StringSliceOrSlice) UnmarshalJSON(data []byte) error

type UpdatePosition

type UpdatePosition int
const (
	UpdatePosition_After   UpdatePosition = 0
	UpdatePosition_Before  UpdatePosition = 1
	UpdatePosition_Replace UpdatePosition = 2
)

Jump to

Keyboard shortcuts

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