git

package
v0.0.36 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeCommit = "commit"
	TypeTree   = "tree"
	TypeBlob   = "blob"
	TypeTag    = "tag"
)

The various types of objects in git.

View Source
const Missing = "0000000000000000000000000000000000000000"

Missing is a sentinel zero-value for object id (aka sha). Git treats this value as "this thing doesn't exist". For example, when updating a ref, if the old value is specified as EmptyOid, Git will refuse to update the ref if already exists.

Variables

View Source
var ErrRemoteNotFound = errors.Sentinel("this repository doesn't have a remote origin")

Functions

func FindClosesPullRequestComments added in v0.0.30

func FindClosesPullRequestComments(cis []*CommitInfo) map[int64]string

FindClosesPullRequestComments finds the "closes #123" instructions from the commit messages. This returns a PR number to commit hash mapping.

See https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword

func ShortSha

func ShortSha(sha string) string

Types

type CheckoutBranch

type CheckoutBranch struct {
	// The name of the branch to checkout.
	Name string
	// Specifies the "-b" flag to git.
	// The checkout will fail if the branch already exists.
	NewBranch bool
	// Specifies the ref that new branch will have HEAD at
	// Requires the "-b" flag to be specified
	NewHeadRef string
}

type CherryPick added in v0.0.16

type CherryPick struct {
	// Commits is a list of commits to apply.
	Commits []string

	// NoCommit specifies whether or not to cherry-pick without committing
	// (equivalent to the --no-commit flag on `git cherry-pick`).
	NoCommit bool

	// FastForward specifies whether or not to fast-forward the current branch
	// if possible (equivalent to the --ff flag on `git cherry-pick`).
	// If true, and the parent of the commit is the current HEAD, the HEAD
	// will be fast forwarded to the commit (instead of re-applied).
	FastForward bool

	// Resume specifies how to resume a cherry-pick operation that was
	// interrupted by a conflict (equivalent to the --continue, --skip, --quit,
	// and --abort flags on `git cherry-pick`).
	// Mutually exclusive with all other options.
	Resume CherryPickResume
}

type CherryPickResume added in v0.0.16

type CherryPickResume string
const (
	CherryPickContinue CherryPickResume = "continue"
	CherryPickSkip     CherryPickResume = "skip"
	CherryPickQuit     CherryPickResume = "quit"
	CherryPickAbort    CherryPickResume = "abort"
)

type Commit added in v0.0.17

type Commit struct {
	Tree      string
	Parents   []string
	Author    string
	Committer string
	Message   string
}

func ParseCommitContents added in v0.0.17

func ParseCommitContents(contents []byte) (Commit, error)

func (*Commit) MessageTitle added in v0.0.17

func (c *Commit) MessageTitle() string

type CommitInfo

type CommitInfo struct {
	Hash      string
	ShortHash string
	Subject   string
	Body      string
}

func (CommitInfo) BodyWithPrefix added in v0.0.13

func (c CommitInfo) BodyWithPrefix(prefix string) []string

type CommitInfoOpts

type CommitInfoOpts struct {
	Rev string
}

type Diff

type Diff struct {
	// If true, there are no differences between the working tree and the commit.
	Empty    bool
	Contents string
}

type DiffOpts

type DiffOpts struct {
	// The revisions to compare.
	// The behavior of the diff changes depending on how these are specified.
	//   - If empty, the generated diff is relative to the current staging area.
	//   - If one commit is given, the diff is calculated between the working tree
	//     and the given commit.
	//   - If two commits are given (or one string representing a commit range
	//     like `<a>..<b>`), the diff is calculated between the two commits.
	Specifiers []string
	// If true, don't actually generate the diff, just return whether or not its
	// empty. If set, Diff.Contents will always be an empty string.
	Quiet bool
	// If true, shows the colored diff.
	Color bool
	// If specified, compare only the specified paths.
	Paths []string
}

type ErrCherryPickConflict added in v0.0.16

type ErrCherryPickConflict struct {
	ConflictingCommit string
	Output            string
}

func (ErrCherryPickConflict) Error added in v0.0.16

func (e ErrCherryPickConflict) Error() string

type GetRefs

type GetRefs struct {
	// The revisions to retrieve.
	Revisions []string
}

type GetRefsItem

type GetRefsItem struct {
	// The revision that was requested (exactly as given in GetRefs.Revisions)
	Revision string
	// The git object ID that the revision resolved to
	OID string
	// The type of the git object
	Type string
	// The contents of the git object
	Contents []byte
}

type ListRefs

type ListRefs struct {
	Patterns []string
}

type LogOpts added in v0.0.30

type LogOpts struct {
	// RevisionRange is the range of the commits specified by the format described in
	// git-log(1).
	RevisionRange []string
}

type MergeBase

type MergeBase struct {
	Revs []string
}

type Origin

type Origin struct {
	URL *url.URL
	// The URL slug that corresponds to repository.
	// For example, github.com/my-org/my-repo becomes my-org/my-repo.
	RepoSlug string
}

type Output

type Output struct {
	ExitCode  int
	ExitError *exec.ExitError
	Stdout    []byte
	Stderr    []byte
}

func (Output) Lines added in v0.0.5

func (o Output) Lines() []string

type RebaseOpts added in v0.0.4

type RebaseOpts struct {
	// Required (unless Continue is true)
	// The upstream branch to rebase onto.
	Upstream string
	// Optional (mutually exclusive with all other options)
	// If set, continue a rebase (all other options are ignored).
	Continue bool
	// Optional (mutually exclusive with all other options)
	Abort bool
	// Optional (mutually exclusive with all other options)
	Skip bool
	// Optional
	// If set, use `git rebase --onto <upstream> ...`
	Onto string
	// Optional
	// If set, this is the branch that will be rebased; otherwise, the current
	// branch is rebased.
	Branch string
}

type RebaseResult added in v0.0.8

type RebaseResult struct {
	Status RebaseStatus
	Hint   string
	// The "headline" of the error message (if any)
	ErrorHeadline string
}

type RebaseStatus added in v0.0.8

type RebaseStatus int
const (
	RebaseAlreadyUpToDate RebaseStatus = iota
	RebaseUpdated         RebaseStatus = iota
	RebaseConflict        RebaseStatus = iota
	RebaseNotInProgress   RebaseStatus = iota
	// RebaseAborted indicates that an in-progress rebase was aborted.
	// Only returned if Rebase was called with Abort: true.
	RebaseAborted RebaseStatus = iota
)

type RefInfo

type RefInfo struct {
	Name string
	Type string
	Oid  string
	// The name of the upstream ref (e.g., refs/remotes/<remote>/<branch>)
	Upstream string
	// The status of the ref relative to the upstream.
	UpstreamStatus UpstreamStatus
}

type Repo

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

func OpenRepo

func OpenRepo(repoDir string, gitDir string) (*Repo, error)

func (*Repo) AvDir added in v0.0.15

func (r *Repo) AvDir() string

func (*Repo) AvTmpDir added in v0.0.15

func (r *Repo) AvTmpDir() string

func (*Repo) BranchDelete added in v0.0.15

func (r *Repo) BranchDelete(names ...string) error

BranchDelete deletes the given branches (equivalent to `git branch -D`).

func (*Repo) BranchSetConfig added in v0.0.32

func (r *Repo) BranchSetConfig(name, key, value string) error

BranchSetConfig sets a config on the given branch (equivalent to `git config branch.<branch>.<key> <value>`).

func (*Repo) CheckCleanWorkdir added in v0.0.12

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

CheckCleanWorkdir returns if the workdir is clean.

func (*Repo) CheckoutBranch

func (r *Repo) CheckoutBranch(opts *CheckoutBranch) (string, error)

CheckoutBranch performs a checkout of the given branch and returns the name of the previous branch, if any (this can be used to restore the previous branch if necessary). The returned previous branch name may be empty if the repo is currently not checked out to a branch (i.e., in detached HEAD state).

func (*Repo) CherryPick added in v0.0.16

func (r *Repo) CherryPick(opts CherryPick) error

CherryPick applies the given commits on top of the current HEAD. If there are conflicts, ErrCherryPickConflict is returned.

func (*Repo) CommitInfo

func (r *Repo) CommitInfo(opts CommitInfoOpts) (*CommitInfo, error)

func (*Repo) CurrentBranchName

func (r *Repo) CurrentBranchName() (string, error)

CurrentBranchName returns the name of the current branch. The name is return in "short" format -- i.e., without the "refs/heads/" prefix. IMPORTANT: This function will return an error if the repository is currently in a detached-head state (e.g., during a rebase conflict).

func (*Repo) DefaultBranch

func (r *Repo) DefaultBranch() (string, error)

func (*Repo) DetachedHead added in v0.0.14

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

DetachedHead returns true if the repository is in the detached HEAD.

func (*Repo) Diff

func (r *Repo) Diff(d *DiffOpts) (*Diff, error)

func (*Repo) Dir

func (r *Repo) Dir() string

func (*Repo) DoesBranchExist added in v0.0.18

func (r *Repo) DoesBranchExist(branch string) (bool, error)

func (*Repo) DoesRefExist added in v0.0.18

func (r *Repo) DoesRefExist(ref string) (bool, error)

func (*Repo) DoesRemoteBranchExist added in v0.0.15

func (r *Repo) DoesRemoteBranchExist(branch string) (bool, error)

func (*Repo) GetRefs

func (r *Repo) GetRefs(opts *GetRefs) ([]*GetRefsItem, error)

GetRefs reads the contents of the specified objects from the repository. This corresponds to the `git cat-file --batch` command.

func (*Repo) Git

func (r *Repo) Git(args ...string) (string, error)

func (*Repo) GitDir

func (r *Repo) GitDir() string

func (*Repo) GoGitRepo added in v0.0.36

func (r *Repo) GoGitRepo() *git.Repository

func (*Repo) HasChangesToBeCommitted added in v0.0.12

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

HasChangesToBeCommitted returns if there's a staged changes to be committed.

func (*Repo) IsTrunkBranch added in v0.0.36

func (r *Repo) IsTrunkBranch(name string) (bool, error)

func (*Repo) ListRefs

func (r *Repo) ListRefs(showRef *ListRefs) ([]RefInfo, error)

ListRefs lists all refs in the repository (optionally matching a specific pattern).

func (*Repo) Log added in v0.0.30

func (r *Repo) Log(opts LogOpts) ([]*CommitInfo, error)

Log returns a list of commits specified by the range.

func (*Repo) LsRemote added in v0.0.16

func (r *Repo) LsRemote(remote string) (map[string]string, error)

func (*Repo) MergeBase

func (r *Repo) MergeBase(mb *MergeBase) (string, error)

func (*Repo) Origin

func (r *Repo) Origin() (*Origin, error)

func (*Repo) ReadStateFile added in v0.0.36

func (r *Repo) ReadStateFile(kind StateFileKind, msg any) error

func (*Repo) Rebase added in v0.0.4

func (r *Repo) Rebase(opts RebaseOpts) (*Output, error)

func (*Repo) RebaseParse added in v0.0.8

func (r *Repo) RebaseParse(opts RebaseOpts) (*RebaseResult, error)

RebaseParse runs a `git rebase` and parses the output into a RebaseResult.

func (*Repo) RevList added in v0.0.5

func (r *Repo) RevList(opts RevListOpts) ([]string, error)

RevList list commits that are reachable from the given commits (excluding commits reachable from the given exclusions).

func (*Repo) RevParse

func (r *Repo) RevParse(rp *RevParse) (string, error)

func (*Repo) Run

func (r *Repo) Run(opts *RunOpts) (*Output, error)

func (*Repo) TrunkBranches added in v0.0.36

func (r *Repo) TrunkBranches() ([]string, error)

func (*Repo) UpdateRef

func (r *Repo) UpdateRef(update *UpdateRef) error

UpdateRef updates the specified ref within the Git repository.

func (*Repo) WriteStateFile added in v0.0.36

func (r *Repo) WriteStateFile(kind StateFileKind, msg any) error

type RevListOpts added in v0.0.5

type RevListOpts struct {
	// A list of commit roots, or exclusions if the commit sha starts with a
	// caret (^). As a special case, "foo..bar" is equivalent to "foo ^bar"
	// which means every commit reachable from foo but not from bar.
	// For example, to list all of the commits introduced in a pull request,
	// the specifier would be "HEAD..master".
	// See `git rev-list --help`.
	Specifiers []string

	// If true, display the commits in chronological order.
	Reverse bool
}

type RevParse

type RevParse struct {
	// The name of the branch to parse.
	// If empty, the current branch is parsed.
	Rev              string
	SymbolicFullName bool
}

type RunOpts

type RunOpts struct {
	Args []string
	Env  []string
	// If true, return a non-nil error if the command exited with a non-zero
	// exit code.
	ExitError bool
	// If true, the standard I/Os are connected to the console, allowing the git command to
	// interact with the user. Stdout and Stderr will be empty.
	Interactive bool
	// The standard input to the command (if any). Mutually exclusive with Interactive.
	Stdin io.Reader
}

type StateFileKind added in v0.0.36

type StateFileKind string
const (
	StateFileKindSync    StateFileKind = "stack-sync.state.json"
	StateFileKindReorder StateFileKind = "stack-reorder.state.json"
	StateFileKindRestack StateFileKind = "stack-restack.state.json"
	StateFileKindSyncV2  StateFileKind = "stack-sync-v2.state.json"
)

type UpdateRef

type UpdateRef struct {
	// The name of the ref (e.g., refs/heads/my-branch).
	Ref string
	// The Git object ID to set the ref to.
	New string
	// Only update the ref if the current value (before the update) is equal to
	// this object ID. Use Missing to only create the ref if it didn't
	// already exists (e.g., to avoid overwriting a branch).
	Old string

	// Create a reflog for this ref change.
	CreateReflog bool
}

type UpstreamStatus

type UpstreamStatus string

UpstreamStatus is the status of a git ref (usually a branch) relative to its upstream.

const (
	Ahead     UpstreamStatus = ">"
	Behind    UpstreamStatus = "<"
	Divergent UpstreamStatus = "<>"
	InSync    UpstreamStatus = "="
)

The possible upstream statuses. These match what is returned by Git's `%(upstream:trackshort)` format directive.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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