patch

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package patch manages patch files as stored in the Microsoft Go repository alongside a submodule.

Index

Constants

View Source
const ConfigFileName = ".git-go-patch"

ConfigFileName is the name of the config file on disk. The name is similar to ".gitignore", etc.

Variables

This section is empty.

Functions

func Apply

func Apply(config *FoundConfig, mode ApplyMode) error

Apply runs a Git command to apply the patches in the repository onto the submodule. The exact Git command used ("am" or "apply") depends on the patch mode.

func WalkGoPatches

func WalkGoPatches(config *FoundConfig, fn func(string) error) error

WalkGoPatches finds patches in the given Microsoft Go repository root directory and runs fn once per patch file path. If fn returns an error, walking terminates and the error is returned. The walk iterates in the order the patches should be applied (alphabetical filename order).

func WalkPatches

func WalkPatches(dir string, fn func(string) error) error

WalkPatches finds patches in the given directory and runs fn once per patch file path. If fn returns an error, walking terminates and the error is returned. The walk iterates in the order the patches should be applied (alphabetical filename order).

Types

type ApplyMode

type ApplyMode int
const (
	// ApplyModeCommits applies patches as commits. This is useful for developing changes to the
	// patches, because the commits can later be automatically extracted back into patch files.
	// Creating these commits creates new commit hashes, so it is not desirable if the HEAD commit
	// should correspond to a "real" commit.
	ApplyModeCommits ApplyMode = iota
	// ApplyModeIndex applies patches as changes to the Git index and working tree. Doesn't change
	// HEAD: it will continue to point to the same commit--likely upstream.
	//
	// This makes it more difficult to develop and save changes, but it is still possible. Patch
	// changes show up as staged changes, and additional changes show up as unstaged changes, so
	// they can still be differentiated and preserved.
	ApplyModeIndex
)

type Config

type Config struct {
	// MinimumToolVersion, if defined, causes "git-go-patch" commands to refuse to run if the tool's
	// version is lower than this version. This can be used to introduce features into the
	// "git-go-patch" tool that don't work in previous versions, like new patch commands.
	MinimumToolVersion string

	// SubmoduleDir is the submodule directory to patch, relative to the config file.
	SubmoduleDir string
	// PatchesDir is the directory with patch files to use, relative to the config file.
	PatchesDir string
	// StatusFileDir is a gitignored directory to put workflow-related temporary status files,
	// relative to the config file.
	StatusFileDir string

	// ExtractAsAuthor makes "git-go-patch extract" set this author in the resulting patch file.
	//
	// This can be used to avoid patch file attribution confusion: when a patch goes through the
	// "apply" "rebase" "extract" process, the patch file's "author" field stays the same, but
	// someone else changed the content. This can make it appear that an author understands (or
	// endorses) a change, although they might not. Using this setting with (e.g.) a bot account
	// allows the project to consistently assign a single author to all patch files to avoid the
	// confusion. The correct way to see the contributors to a patch file is to view the patch
	// file's history, not the author line of the patch file.
	//
	// Example: "microsoft-golang-bot <microsoft-golang-bot@users.noreply.github.com>"
	ExtractAsAuthor string `json:",omitempty"`
}

Config is the patch config file content. The default values are used as the default "init" content, so some fields are "omitempty" to avoid showing up by default.

type FoundConfig

type FoundConfig struct {
	Config
	RootDir string
}

FoundConfig is a Config file's contents plus the location the config file was found, the RootDir.

func FindAncestorConfig

func FindAncestorConfig(dir string) (*FoundConfig, error)

FindAncestorConfig finds and reads the config file governing dir. Searches dir and all ancestor directories of dir, similar to how ".git" files work.

If no config file is found in any ancestor, and an ancestor dir appears to be like microsoft/go by convention (contains a "patches" directory and a "go" directory), creates a config struct to fit the conventional repo.

func (*FoundConfig) FullPostPatchStatusFilePath

func (c *FoundConfig) FullPostPatchStatusFilePath() string

FullPostPatchStatusFilePath is the full path to the "post-patch" status file. This may be used to determine if the submodule is fresh (the dev just ran "git go-patch apply" and nothing else) or if it might contain dev work that shouldn't be discarded.

func (*FoundConfig) FullPrePatchStatusFilePath

func (c *FoundConfig) FullPrePatchStatusFilePath() string

FullPrePatchStatusFilePath is the full path to the "pre-patch" status file, which may store the commit hash before a "git go-patch apply" so "git go-patch extract" can use it later.

func (*FoundConfig) FullProjectRoots

func (c *FoundConfig) FullProjectRoots() (rootDir, submoduleDir string)

FullProjectRoots returns the full path for the project and submodule root dirs. This function is provided for convenience while migrating old code onto the config file API.

func (*FoundConfig) FullStatusFileDir

func (c *FoundConfig) FullStatusFileDir() string

FullStatusFileDir is the full status file dir path.

type Header struct {
	FromTimestamp string
	FromAuthor    string
	Date          string
	Subject       string
}

Header is the part of a Git patch file before the "---".

type MatchCheckRepo

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

MatchCheckRepo checks whether each patch in a series of patches makes actual changes, or if it is equivalent to an existing patch. The purpose is to let "git go-patch extract" ignore superficial differences like chunk line numbers and index hashes that would only distract code reviewers.

func NewMatchCheckRepo

func NewMatchCheckRepo(submodulePath, baseCommit, patchesDir string) (*MatchCheckRepo, error)

NewMatchCheckRepo clones the given submodule to a temp repo and prepares to search the patchesDir for a match to each patch passed to Apply. Returns the created context for this process. Call the context's AttemptDelete method to clean up the temp dir if desired.

func (*MatchCheckRepo) AttemptDelete

func (m *MatchCheckRepo) AttemptDelete()

AttemptDelete tries to delete the temp check repo dir. If an error occurs, log it, but this is not fatal. It is inside the user's temp dir, so it will be cleaned up later by the OS anyway.

func (*MatchCheckRepo) CheckedApply

func (m *MatchCheckRepo) CheckedApply(path string, p *Patch) (string, error)

CheckedApply applies the patch at path to the MatchCheckRepo while checking for matches. If a matching patch is found, that patch's path is returned.

It is recommended to call this in a WalkPatches or WalkGoPatches fn: the method must be called once and only once for each patch, and the calls must be in the correct patch application order.

type Patch

type Patch struct {
	Header
	Content string
}

Patch is a parsed Git patch file.

func Read

func Read(r io.Reader) (*Patch, error)

Read reads and parses a patch from r.

func ReadFile

func ReadFile(path string) (*Patch, error)

ReadFile reads and parses the named patch file.

func (*Patch) String

func (h *Patch) String() string

Jump to

Keyboard shortcuts

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