engine

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 21 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 engine holds the operations that combine snapshots: three-way merges of trees and the rebase step used when landing changes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsGenerated added in v0.2.0

func IsGenerated(data []byte) bool

IsGenerated reports whether a file says it was generated: one of its first lines is "// Code generated ... DO NOT EDIT.". A conflict in such a file is fixed by regenerating it, not by editing conflict markers.

Types

type Conflict

type Conflict struct {
	Path   string
	Kind   ConflictKind
	Base   object.File // zero if the file did not exist in base
	Ours   object.File // zero if ours deleted it
	Theirs object.File // zero if theirs deleted it
	// Markers is the file with diff3-style conflict markers, for content
	// and add/add conflicts on text files.
	Markers []byte
	Detail  string
}

Conflict is one path that needs a decision.

type ConflictKind

type ConflictKind string

ConflictKind names why a path could not be merged.

const (
	ConflictContent      ConflictKind = "content"       // both sides edited the same lines
	ConflictAddAdd       ConflictKind = "add/add"       // both sides created the file differently
	ConflictModifyDelete ConflictKind = "modify/delete" // ours edited, theirs deleted
	ConflictDeleteModify ConflictKind = "delete/modify" // ours deleted, theirs edited
	ConflictMode         ConflictKind = "mode"          // both sides changed the mode differently
	ConflictBinary       ConflictKind = "binary"        // both sides changed a binary file
	ConflictPath         ConflictKind = "path"          // a file and a directory collide
)

type GeneratedFile added in v0.3.0

type GeneratedFile struct {
	Path string
	// Command regenerates it, from a [[generated]] rule in vx.toml; empty
	// when the rule has none or the file was recognised by its
	// "Code generated ... DO NOT EDIT." line.
	Command string
}

GeneratedFile is a generated file both sides changed, kept from ours.

type GoSides added in v0.4.0

type GoSides struct {
	Base, Ours, Theirs, Merged hash.Hash
	OursName, TheirsName       string
}

GoSides are the trees of a three-way merge and its result, for CheckGoSides. The names finish "changed in ..." and "added by ...", e.g. "this land" and "the effort".

type MergeOption added in v0.3.0

type MergeOption func(*mergeConfig)

MergeOption changes how MergeTrees treats some paths.

func WithGenerated added in v0.3.0

func WithGenerated(f func(path string) (command string, ok bool)) MergeOption

WithGenerated says which paths are generated (vx.toml [[generated]] rules, see rules.Rules.GeneratedMatcher). Files that say "Code generated ... DO NOT EDIT." are generated with or without this option.

type MergeResult

type MergeResult struct {
	// Tree is the merged root tree. It is Zero when there are conflicts.
	Tree hash.Hash
	// Conflicts lists paths that could not be merged.
	Conflicts []Conflict
	// AutoMerged lists paths both sides changed that merged line by line.
	AutoMerged []string
	// KeptBoth lists the paths in AutoMerged where both sides inserted
	// different lines at the same place and the merge kept both (the side
	// passed as theirs first). Worth a look: the order may matter.
	KeptBoth []string
	// MergedByDecl lists the .go paths in AutoMerged whose lines conflicted
	// but whose top-level declarations did not: each side changed different
	// functions, methods, types, vars or consts, so the file was rebuilt
	// from both sides' declarations, imports united, and gofmt'ed. Worth a
	// look, like KeptBoth.
	MergedByDecl []string
	// Generated lists generated files both sides changed. They are never
	// text-merged: the result has ours (the side being landed, synced,
	// shipped or replayed), and the file has to be regenerated.
	Generated []GeneratedFile
}

MergeResult is the outcome of MergeTrees.

func MergeTrees

func MergeTrees(ctx context.Context, s blob.Store, base, ours, theirs hash.Hash, labels merge.Labels, opts ...MergeOption) (MergeResult, error)

MergeTrees merges ours and theirs, which both descend from base. Paths changed on one side take that side. Paths changed on both sides are merged line by line when possible. With conflicts, the result has no tree.

Generated files (see WithGenerated) changed on both sides are not merged: ours wins and the path is listed in Generated.

func MergeTreesKeepConflicts

func MergeTreesKeepConflicts(ctx context.Context, s blob.Store, base, ours, theirs hash.Hash, labels merge.Labels, opts ...MergeOption) (MergeResult, error)

MergeTreesKeepConflicts is MergeTrees for a working copy: it always returns a tree. Text conflicts are written into the file with conflict markers; other conflicts keep our side. The caller shows the conflicts and the person or agent resolves them in place.

func (MergeResult) Clean

func (r MergeResult) Clean() bool

Clean reports whether the merge has no conflicts.

type Warning added in v0.2.0

type Warning struct {
	Path   string      // package directory for duplicates ("." for the root), file path for parse errors
	Kind   WarningKind //
	Name   string      // duplicate: the name, "Recv.Name" for methods
	Decl   string      // duplicate: func, method, type, var, const, or "declaration" when kinds differ
	Files  []string    // duplicate: base names of the declaring files, sorted and unique
	Detail string      // human-readable summary
}

Warning is a semantic problem in a tree: the text merged, but the result will probably not build.

func CheckGo added in v0.2.0

func CheckGo(ctx context.Context, s blob.Store, root hash.Hash, changed []string) ([]Warning, error)

CheckGo looks for duplicate top-level declarations in the Go packages of root that contain one of the changed paths. Every .go file of such a directory is parsed, not only the changed ones, and packages are told apart by name, so package x and its external test package x_test in the same directory are checked separately. A duplicate is reported only when at least one of its declarations is in a changed file, so problems that predate the change are not repeated on every land.

Changed .go files that do not parse get a parse warning; other files that do not parse are skipped.

Limitations: build constraints (//go:build lines and _GOOS/_GOARCH file name suffixes) are evaluated for linux/amd64 with cgo and no custom tags. Files excluded on that platform are not checked, so a duplicate between two files for another platform, or one behind a custom tag, is missed. Only redeclarations are detected: an import name that collides with a package-level name, or a method that collides with a field, is not.

func CheckGoSides added in v0.4.0

func CheckGoSides(ctx context.Context, s blob.Store, sd GoSides) ([]Warning, error)

CheckGoSides looks for semantic collisions between the two sides of a merge in Go code, where one side's change and the other side's new code meet:

  • signature: a function or method whose parameters or results one side changed, while the other side added or changed calls that still have the old shape, in the same package or in a package that imports it within the tree;
  • removed: a top-level name one side removed or renamed (it is gone from the merged package) that code the other side added or changed still uses.

It is textual, without a type checker, and tuned to stay quiet when in doubt (docs/agents.md lists the heuristics):

  • Only packages one side changed are searched for changed declarations, and only files the other side changed for uses; a use counts when it is on a line the other side contributed to the merged file (a line of the merged file that the first side's version of the file does not have).
  • Signatures are compared by their type parameters and parameter and result types as written (names do not matter), and the change must have survived the merge.
  • A call to a changed function is reported when its argument count fits the old signature and not the new one, when the result count changed and the call's value is used, or, when only parameter types changed, when an argument at a changed position is not a literal.
  • Methods are matched by name on any receiver, only for calls in the method's own package or a package that imports it, and only when the argument count stops fitting; removed methods are not reported.
  • A name in the same package counts only where the parser leaves it unresolved (not a local variable, parameter or field); one in another package only as PKG.Name with PKG naming the import.

Files that do not parse (conflict markers) are skipped; build constraints are not evaluated.

func GeneratedWarnings added in v0.3.0

func GeneratedWarnings(gs []GeneratedFile) []Warning

GeneratedWarnings turns generated files into warnings, sorted by path:

generated file gen/x.go: regenerate with `buf generate`

func MergedByDeclWarnings added in v0.4.0

func MergedByDeclWarnings(paths []string) []Warning

MergedByDeclWarnings turns files merged by declaration into warnings, sorted by path, for the surfaces that have no list of merged files.

type WarningKind added in v0.2.0

type WarningKind string

WarningKind names a problem found in a merged tree that does not stop a land.

const (
	// WarningDuplicate: one Go package declares the same top-level name twice,
	// typically because two changes each added it in a different file and the
	// text merged cleanly.
	WarningDuplicate WarningKind = "duplicate"
	// WarningParse: a changed Go file does not parse.
	WarningParse WarningKind = "parse"
	// WarningCheck: a check could not run; Detail says why.
	WarningCheck WarningKind = "check"
)
const (
	// WarningSignature: one side changed the parameters or results of a
	// function or method, and the other side added or changed calls that
	// still use the old shape.
	WarningSignature WarningKind = "signature"
	// WarningRemoved: one side removed or renamed a top-level name that
	// code the other side added or changed still uses.
	WarningRemoved WarningKind = "removed"
	// WarningMergedByDecl: a Go file whose lines conflicted was merged by
	// declaration (ship plans, rebases and forecasts report it this way;
	// lands and syncs list the files).
	WarningMergedByDecl WarningKind = "merged-by-declaration"
)
const WarningGenerated WarningKind = "generated"

WarningGenerated: both sides changed a generated file; the merge kept ours and the file needs regenerating.

Jump to

Keyboard shortcuts

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