patch2pr

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2020 License: MIT Imports: 11 Imported by: 1

README

patch2pr

PkgGoDev

Create GitHub pull requests from Git patches without cloning the repository.

Why?

As a command line tool, it's mostly a curiosity and test for the library, but might have some use for exceptionally large repositories or in environments where cloning is not feasible.

As a library, however, it enables tools to make automated code changes without giving every part of system write access or requiring extra logic for managing clones. One part of the system can generate a patch and send it to another part that uses this library to apply it and create a pull request.

Usage: CLI

Install the CLI using go get:

go get -u github.com/bluekeyes/patch2pr/cmd/patch2pr

The CLI takes a path to a patch file as the only argument or reads a patch from stdin if no file is given.

The other required arguments are:

  • The -repository flag to specify the repository in owner/name format
  • A GitHub token with repo scope, either with the -token flag or in the GITHUB_TOKEN environment variable.

For example:

$ export GITHUB_TOKEN="token"
$ patch2pr -repository bluekeyes/patch2pr /path/to/file.patch

See the CLI help (-h or -help) or below for full details.

Full Usage
Usage: patch2pr [options] [patch]

  Create a GitHub pull request from a patch file

  This command parses a patch, applies it, and creates a pull request with the
  result. It does not clone the repository to apply the patch. If no patch file
  is given, the command reads the patch from standard input.

  By default, patch2pr uses the patch header for author and committer
  information, falling back to the authenticated GitHub user if the headers are
  missing or invalid. Callers can override these values using the standard Git
  environment variables:

    GIT_AUTHOR_NAME
    GIT_AUTHOR_EMAIL
    GIT_AUTHOR_DATE
    GIT_COMMITTER_NAME
    GIT_COMMITER_EMAIL
    GIT_COMMITER_DATE

  Override the commit message by using the -message flag.

Options:

  -base-branch=branch  The branch to target with the pull request. If unset,
                       use the repository's default branch.

  -force               Update the head branch even if it exists and is not a
                       fast-forward.

  -head-branch=branch  The branch to create or update with the new commit. If
                       unset, use 'patch2pr'.

  -json                Output information about the new commit and pull request
                       in JSON format.

  -message=message     Message for the commit. Overrides the patch header.

  -no-pull-request     Do not create a pull request after creating a commit.

  -patch-base=base     Base commit to apply the patch to. Can be a SHA1, a
                       branch, or a tag. Branches and tags must start with
                       'refs/heads/' or 'refs/tags/' respectively. If unset,
                       use the repository's default branch.

  -pull-body=body      The body for the pull request. If unset, use the body of
                       the commit message.

  -pull-title=title    The title for the pull request. If unset, use the title
                       of the commit message.

  -repository=repo     Repository to apply the patch to in 'owner/name' format.
                       Required.

  -token=token         GitHub API token with 'repo' scope for authentication.
                       If unset, use the value of the GITHUB_TOKEN environment
                       variable.

  -url=url             GitHub API URL. If unset, use https://api.github.com.

Usage: Library

The CLI is built on the patch2pr library, which can be used to build other tools that apply patches directly to GitHub. See the documentation for full details.

The library uses google/go-github to interact with GitHub and exposes types from that package in the API.

Stability

Experimental. The interface for both the CLI and the library may change. While the underlying patch library (bluekeyes/go-gitdiff) has good test coverage, it hasn't been used extensively against real-world patches.

Contributing

Contributions are welcome. If reporting an issue with applying a patch, please include the patch file and the base commit or file content if possible. A link to a public repository is most helpful.

At this time, I don't intend to support services other than GitHub. If you'd like support for another service, please file an issue with a link to the relevant API documentation so I can estimate the work involved in adding the necessary abstractions.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCommitMessage = "Apply patch with patch2pr"

DefaultCommitMessage is the commit message used when no message is provided in a patch header.

Functions

This section is empty.

Types

type Applier

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

Applier applies patches to create trees and commits in a repository.

func NewApplier

func NewApplier(client *github.Client, repo Repository, c *github.Commit) *Applier

NewApplier creates a new Applier for a repository. The Applier applies changes on top of commit c.

func (*Applier) Apply

func (a *Applier) Apply(ctx context.Context, f *gitdiff.File) (*github.TreeEntry, error)

Apply applies the changes in a file, adds the result to the list of pending tree entries, and returns the entry. If the application succeeds, Apply creates a blob in the repository with the modified content.

func (*Applier) Commit

func (a *Applier) Commit(ctx context.Context, header *gitdiff.PatchHeader) (*github.Commit, error)

Commit commits the latest tree, optionally using the details in header. If there are pending tree entries, it calls CreateTree before creating the commit. If header is nil or missing fields, Commit uses a default message, the current time, and the authenticated user as needed for the commit details. Commit returns an error if there are no pending trees or tree entries.

func (*Applier) CreateTree

func (a *Applier) CreateTree(ctx context.Context) (*github.Tree, error)

CreateTree creates a tree from the pending tree entries and clears the entry list. The new tree serves as the base tree for future Apply calls. CreateTree returns an error if there are no pending tree entires.

func (*Applier) Entries

func (a *Applier) Entries() []*github.TreeEntry

Entries returns the list of pending tree entries.

func (*Applier) Reset

func (a *Applier) Reset(c *github.Commit)

Reset resets the applier so that future Apply calls start from commit c. It removes pending tree entries and clears the latest tree. Reset does not modify the remote repository.

type Reference

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

Reference is a named reference in a repository.

func NewReference

func NewReference(client *github.Client, repo Repository, ref string) *Reference

NewReference creates a new Reference for ref in repo.

func (*Reference) PullRequest

func (r *Reference) PullRequest(ctx context.Context, spec *github.NewPullRequest) (*github.PullRequest, error)

PullRequest create a new pull request for the reference. The reference must be a branch (start with "refs/heads/".) The pull request takes values from spec, except for Head, which is set to the reference.

func (*Reference) Set

func (r *Reference) Set(ctx context.Context, sha string, force bool) error

Set creates or updates the reference to point to sha. If force is true and the reference exists, Set updates it even if the update is not a fast-forward.

type Repository

type Repository struct {
	Owner string
	Name  string
}

Repository identifies a GitHub repository.

func ParseRepository

func ParseRepository(s string) (Repository, error)

ParseRepository parses a Repository from a string in "owner/name" format.

func (Repository) String

func (r Repository) String() string

Directories

Path Synopsis
cmd
patch2pr command

Jump to

Keyboard shortcuts

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