git

package
v2.100.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 19 Imported by: 5

Documentation

Index

Constants

View Source
const MergeBaseConfig = "gh-merge-base"

MergeBaseConfig is the configuration setting to keep track of the PR target branch.

Variables

View Source
var AllMatchingCredentialsPattern = CredentialPattern{/* contains filtered or unexported fields */}

AllMatchingCredentialsPattern allows for setting gh as credential helper for all hosts. However, we should endeavour to remove it as it's less secure.

View Source
var ErrNotOnAnyBranch = errors.New("git: not on any branch")

ErrNotOnAnyBranch indicates that the user is in detached HEAD state.

Functions

func IsURL

func IsURL(u string) bool

func IsolateConfig added in v2.98.0

func IsolateConfig(t *testing.T)

IsolateConfig prevents the ambient git configuration from reaching tests that shell out to real git.

https://git-scm.com/docs/git-config#ENVIRONMENT

func ParseURL

func ParseURL(rawURL string) (*url.URL, error)

ParseURL normalizes git remote urls

func ShortSHA added in v2.90.0

func ShortSHA(sha string) string

ShortSHA returns the first 8 characters of a SHA hash for display purposes.

Types

type BranchConfig

type BranchConfig struct {
	RemoteName     string   // .remote if string
	RemoteURL      *url.URL // .remote if url
	MergeRef       string   // .merge
	PushRemoteName string   // .pushremote if string
	PushRemoteURL  *url.URL // .pushremote if url

	// MergeBase is the optional base branch to target in a new PR if `--base` is not specified.
	MergeBase string
}

These are the keys we read from the git branch.<name> config.

type Client added in v2.18.0

type Client struct {
	GhPath  string
	RepoDir string
	GitPath string
	Stderr  io.Writer
	Stdin   io.Reader
	Stdout  io.Writer
	// contains filtered or unexported fields
}

func (*Client) AddRemote added in v2.18.0

func (c *Client) AddRemote(ctx context.Context, name, urlStr string, trackingBranches []string) (*Remote, error)

func (*Client) AuthenticatedCommand added in v2.18.0

func (c *Client) AuthenticatedCommand(ctx context.Context, credentialPattern CredentialPattern, args ...string) (*Command, error)

AuthenticatedCommand is a wrapper around Command that included configuration to use gh as the credential helper for git.

func (*Client) CheckoutBranch added in v2.18.0

func (c *Client) CheckoutBranch(ctx context.Context, branch string) error

func (*Client) CheckoutNewBranch added in v2.18.0

func (c *Client) CheckoutNewBranch(ctx context.Context, remoteName, branch string) error

func (*Client) Clone added in v2.18.0

func (c *Client) Clone(ctx context.Context, cloneURL string, args []string, mods ...CommandModifier) (string, error)

func (*Client) Command added in v2.18.0

func (c *Client) Command(ctx context.Context, args ...string) (*Command, error)

func (*Client) CommitBody added in v2.18.0

func (c *Client) CommitBody(ctx context.Context, sha string) (string, error)

func (*Client) Commits added in v2.18.0

func (c *Client) Commits(ctx context.Context, baseRef, headRef string) ([]*Commit, error)

func (*Client) Config added in v2.18.0

func (c *Client) Config(ctx context.Context, name string) (string, error)

func (*Client) Copy added in v2.31.0

func (c *Client) Copy() *Client

func (*Client) CurrentBranch added in v2.18.0

func (c *Client) CurrentBranch(ctx context.Context) (string, error)

CurrentBranch reads the checked-out branch for the git repository.

func (*Client) DeleteLocalBranch added in v2.18.0

func (c *Client) DeleteLocalBranch(ctx context.Context, branch string) error

func (*Client) DeleteLocalTag added in v2.35.0

func (c *Client) DeleteLocalTag(ctx context.Context, tag string) error

func (*Client) Fetch added in v2.19.0

func (c *Client) Fetch(ctx context.Context, remote string, refspec string, mods ...CommandModifier) error

func (*Client) GitDir added in v2.18.0

func (c *Client) GitDir(ctx context.Context) (string, error)

func (*Client) HasLocalBranch added in v2.18.0

func (c *Client) HasLocalBranch(ctx context.Context, branch string) bool

func (*Client) IsIgnored added in v2.90.0

func (c *Client) IsIgnored(ctx context.Context, path string) (bool, error)

IsIgnored reports whether the given path is ignored by .gitignore rules. Returns an error for fatal git failures (e.g. path outside repository).

func (*Client) IsLocalGitRepo added in v2.22.0

func (c *Client) IsLocalGitRepo(ctx context.Context) (bool, error)

func (*Client) LastCommit added in v2.18.0

func (c *Client) LastCommit(ctx context.Context) (*Commit, error)

func (*Client) PathFromRoot added in v2.18.0

func (c *Client) PathFromRoot(ctx context.Context) string

Show current directory relative to the top-level directory of repository.

func (*Client) Pull added in v2.18.0

func (c *Client) Pull(ctx context.Context, remote, branch string, mods ...CommandModifier) error

func (*Client) Push added in v2.18.0

func (c *Client) Push(ctx context.Context, remote string, ref string, mods ...CommandModifier) error

func (*Client) PushDefault added in v2.66.0

func (c *Client) PushDefault(ctx context.Context) (PushDefault, error)

PushDefault returns the value of push.default in the config. If the value is not set, it returns "simple" (the default git value). See https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushdefault

func (*Client) PushRevision added in v2.71.0

func (c *Client) PushRevision(ctx context.Context, branch string) (RemoteTrackingRef, error)

PushRevision gets the value of the @{push} revision syntax An error here doesn't necessarily mean something is broken, but may mean that the @{push} revision syntax couldn't be resolved, such as in non-centralized workflows with push.default = simple. Downstream consumers should consider how to handle this error.

func (*Client) ReadBranchConfig added in v2.18.0

func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (BranchConfig, error)

ReadBranchConfig parses the `branch.BRANCH.(remote|merge|pushremote|gh-merge-base)` part of git config. If no branch config is found or there is an error in the command, it returns an empty BranchConfig. Downstream consumers of ReadBranchConfig should consider the behavior they desire if this errors, as an empty config is not necessarily breaking.

func (*Client) RemotePushDefault added in v2.66.0

func (c *Client) RemotePushDefault(ctx context.Context) (string, error)

RemotePushDefault returns the value of remote.pushDefault in the config. If the value is not set, it returns an empty string.

func (*Client) RemoteURL added in v2.90.0

func (c *Client) RemoteURL(ctx context.Context, name string) (string, error)

RemoteURL returns the fetch URL configured for the named remote.

func (*Client) Remotes added in v2.18.0

func (c *Client) Remotes(ctx context.Context) (RemoteSet, error)

func (*Client) SetBranchConfig added in v2.64.0

func (c *Client) SetBranchConfig(ctx context.Context, branch, name, value string) error

SetBranchConfig sets the named value on the given branch.

func (*Client) SetRemoteBranches added in v2.31.0

func (c *Client) SetRemoteBranches(ctx context.Context, remote string, refspec string) error

func (*Client) SetRemoteResolution added in v2.18.0

func (c *Client) SetRemoteResolution(ctx context.Context, name, resolution string) error

func (*Client) ShowRefs added in v2.18.0

func (c *Client) ShowRefs(ctx context.Context, refs []string) ([]Ref, error)

ShowRefs resolves fully-qualified refs to commit hashes.

func (*Client) ToplevelDir added in v2.18.0

func (c *Client) ToplevelDir(ctx context.Context) (string, error)

ToplevelDir returns the top-level directory path of the current repository.

func (*Client) TrackingBranchNames added in v2.26.0

func (c *Client) TrackingBranchNames(ctx context.Context, prefix string) []string

func (*Client) UncommittedChangeCount added in v2.18.0

func (c *Client) UncommittedChangeCount(ctx context.Context) (int, error)

func (*Client) UnsetRemoteResolution added in v2.21.0

func (c *Client) UnsetRemoteResolution(ctx context.Context, name string) error

func (*Client) UpdateRemoteURL added in v2.18.0

func (c *Client) UpdateRemoteURL(ctx context.Context, name, url string) error

func (*Client) WorktreePrune added in v2.99.0

func (c *Client) WorktreePrune(ctx context.Context) error

WorktreePrune removes administrative files for worktrees that no longer exist on disk.

func (*Client) WorktreeRemove added in v2.99.0

func (c *Client) WorktreeRemove(ctx context.Context, path string) error

WorktreeRemove removes the worktree at the given path via `git worktree remove <path>`.

func (*Client) Worktrees added in v2.99.0

func (c *Client) Worktrees(ctx context.Context) ([]Worktree, error)

Worktrees lists the repository's worktrees by parsing the output of `git worktree list --porcelain`.

type Command added in v2.20.1

type Command struct {
	*exec.Cmd
}

func (*Command) Output added in v2.20.1

func (gc *Command) Output() ([]byte, error)

func (*Command) Run added in v2.20.1

func (gc *Command) Run() error

type CommandModifier added in v2.19.0

type CommandModifier func(*Command)

Allow individual commands to be modified from the default client options.

func WithRepoDir added in v2.19.0

func WithRepoDir(repoDir string) CommandModifier

func WithStderr added in v2.19.0

func WithStderr(stderr io.Writer) CommandModifier

func WithStdin added in v2.19.0

func WithStdin(stdin io.Reader) CommandModifier

func WithStdout added in v2.19.0

func WithStdout(stdout io.Writer) CommandModifier

type Commit

type Commit struct {
	Sha   string
	Title string
	Body  string
}

type CredentialPattern added in v2.63.0

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

CredentialPattern is used to inform AuthenticatedCommand which patterns Git should match against when trying to find credentials. It is a little over-engineered as a type because we want AuthenticatedCommand to have a clear compilation error when this is not provided, as opposed to using a string which might compile with `client.AuthenticatedCommand(ctx, "fetch")`.

It is only usable when constructed by another function in the package because the empty pattern, without allMatching set to true, will result in an error in AuthenticatedCommand.

Callers can currently opt-in to a slightly less secure mode for backwards compatibility by using AllMatchingCredentialsPattern.

func CredentialPatternFromGitURL added in v2.63.0

func CredentialPatternFromGitURL(gitURL string) (CredentialPattern, error)

CredentialPatternFromGitURL takes a git remote URL e.g. "https://github.com/cli/cli.git" or "git@github.com:cli/cli.git" and returns the credential pattern that should be used for it.

func CredentialPatternFromHost added in v2.63.1

func CredentialPatternFromHost(host string) CredentialPattern

CredentialPatternFromHost expects host to be in the form "github.com" and returns the credential pattern that should be used for it. It does not perform any canonicalisation e.g. "api.github.com" will not work as expected.

type GitError added in v2.18.0

type GitError struct {
	ExitCode int
	Stderr   string
	// contains filtered or unexported fields
}

func (*GitError) Error added in v2.18.0

func (ge *GitError) Error() string

func (*GitError) Unwrap added in v2.18.0

func (ge *GitError) Unwrap() error

type NotInstalled

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

func (*NotInstalled) Error

func (e *NotInstalled) Error() string

func (*NotInstalled) Unwrap added in v2.18.0

func (e *NotInstalled) Unwrap() error

type PushDefault added in v2.71.0

type PushDefault string

PushDefault defines the action git push should take if no refspec is given. See: https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushdefault

const (
	PushDefaultNothing  PushDefault = "nothing"
	PushDefaultCurrent  PushDefault = "current"
	PushDefaultUpstream PushDefault = "upstream"
	PushDefaultTracking PushDefault = "tracking"
	PushDefaultSimple   PushDefault = "simple"
	PushDefaultMatching PushDefault = "matching"
)

func ParsePushDefault added in v2.71.0

func ParsePushDefault(s string) (PushDefault, error)

type Ref

type Ref struct {
	Hash string
	Name string
}

Ref represents a git commit reference.

type Remote

type Remote struct {
	Name     string
	Resolved string
	FetchURL *url.URL
	PushURL  *url.URL
}

Remote is a parsed git remote.

func NewRemote

func NewRemote(name string, u string) *Remote

func (*Remote) String

func (r *Remote) String() string

type RemoteSet

type RemoteSet []*Remote

RemoteSet is a slice of git remotes.

func (RemoteSet) Len added in v2.18.0

func (r RemoteSet) Len() int

func (RemoteSet) Less added in v2.18.0

func (r RemoteSet) Less(i, j int) bool

func (RemoteSet) Swap added in v2.18.0

func (r RemoteSet) Swap(i, j int)

type RemoteTrackingRef added in v2.71.0

type RemoteTrackingRef struct {
	Remote string
	Branch string
}

RemoteTrackingRef is the structured form of the string "refs/remotes/<remote>/<branch>". For example, the @{push} revision syntax could report "refs/remotes/origin/main" which would be parsed into RemoteTrackingRef{Remote: "origin", Branch: "main"}.

func ParseRemoteTrackingRef added in v2.71.0

func ParseRemoteTrackingRef(s string) (RemoteTrackingRef, error)

➜ git rev-parse --symbolic-full-name bar/baz@{push} refs/remotes/foo/bar/baz ```

When using this ref, git assumes it means `remote: foo` `branch: bar/baz`.

func (RemoteTrackingRef) String added in v2.71.0

func (r RemoteTrackingRef) String() string

type Worktree added in v2.99.0

type Worktree struct {
	// Path is the absolute path to the worktree's working directory.
	Path string
	// Ref is the fully qualified ref checked out in the worktree
	// (e.g. "refs/heads/main"). It is empty when the worktree has a detached
	// HEAD or is the bare main worktree.
	Ref string
	// Prunable indicates that the worktree's administrative files reference
	// a working directory that no longer exists.
	Prunable bool
}

Worktree represents a single entry from `git worktree list --porcelain`.

func WorktreeForBranch added in v2.99.0

func WorktreeForBranch(worktrees []Worktree, branch string) *Worktree

WorktreeForBranch returns the worktree that has branch checked out, or nil when the branch is not associated with any worktree.

Jump to

Keyboard shortcuts

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